blob: 36593ec13c4b863020b0addc86b1087a9a48cdf1 [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 Moolenaar071d4272004-06-13 20:20:40 +00004303#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4304 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4305 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004306 p = skiptowhite(arg);
4307 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
4309 xp->xp_context = EXPAND_LANGUAGE;
4310 xp->xp_pattern = arg;
4311 }
4312 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004313 {
4314 if ( STRNCMP(arg, "messages", p - arg) == 0
4315 || STRNCMP(arg, "ctype", p - arg) == 0
4316 || STRNCMP(arg, "time", p - arg) == 0)
4317 {
4318 xp->xp_context = EXPAND_LOCALES;
4319 xp->xp_pattern = skipwhite(p);
4320 }
4321 else
4322 xp->xp_context = EXPAND_NOTHING;
4323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 break;
4325#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004326#if defined(FEAT_PROFILE)
4327 case CMD_profile:
4328 set_context_in_profile_cmd(xp, arg);
4329 break;
4330#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004331 case CMD_behave:
4332 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004333 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004334 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004336 case CMD_messages:
4337 xp->xp_context = EXPAND_MESSAGES;
4338 xp->xp_pattern = arg;
4339 break;
4340
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004341#if defined(FEAT_CMDHIST)
4342 case CMD_history:
4343 xp->xp_context = EXPAND_HISTORY;
4344 xp->xp_pattern = arg;
4345 break;
4346#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004347#if defined(FEAT_PROFILE)
4348 case CMD_syntime:
4349 xp->xp_context = EXPAND_SYNTIME;
4350 xp->xp_pattern = arg;
4351 break;
4352#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004353
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004354 case CMD_argdelete:
4355 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4356 arg = xp->xp_pattern + 1;
4357 xp->xp_context = EXPAND_ARGLIST;
4358 xp->xp_pattern = arg;
4359 break;
4360
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361#endif /* FEAT_CMDL_COMPL */
4362
4363 default:
4364 break;
4365 }
4366 return NULL;
4367}
4368
4369/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004370 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *
4372 * Backslashed delimiters after / or ? will be skipped, and commands will
4373 * not be expanded between /'s and ?'s or after "'".
4374 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004375 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 * Returns the "cmd" pointer advanced to beyond the range.
4377 */
4378 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004379skip_range(
4380 char_u *cmd,
4381 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004383 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004385 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004387 if (*cmd == '\\')
4388 {
4389 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4390 ++cmd;
4391 else
4392 break;
4393 }
4394 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
4396 if (*++cmd == NUL && ctx != NULL)
4397 *ctx = EXPAND_NOTHING;
4398 }
4399 else if (*cmd == '/' || *cmd == '?')
4400 {
4401 delim = *cmd++;
4402 while (*cmd != NUL && *cmd != delim)
4403 if (*cmd++ == '\\' && *cmd != NUL)
4404 ++cmd;
4405 if (*cmd == NUL && ctx != NULL)
4406 *ctx = EXPAND_NOTHING;
4407 }
4408 if (*cmd != NUL)
4409 ++cmd;
4410 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004411
4412 /* Skip ":" and white space. */
4413 while (*cmd == ':')
4414 cmd = skipwhite(cmd + 1);
4415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 return cmd;
4417}
4418
4419/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004420 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 *
4422 * Set ptr to the next character after the part that was interpreted.
4423 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004424 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 *
4426 * Return MAXLNUM when no Ex address was found.
4427 */
4428 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004429get_address(
4430 exarg_T *eap UNUSED,
4431 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004432 int addr_type, // flag: one of ADDR_LINES, ...
4433 int skip, // only skip the address, don't use it
4434 int silent, // no errors or side effects
4435 int to_other_file, // flag: may jump to other file
4436 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438 int c;
4439 int i;
4440 long n;
4441 char_u *cmd;
4442 pos_T pos;
4443 pos_T *fp;
4444 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004445 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 cmd = skipwhite(*ptr);
4448 lnum = MAXLNUM;
4449 do
4450 {
4451 switch (*cmd)
4452 {
4453 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 ++cmd;
4455 switch (addr_type)
4456 {
4457 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 lnum = curwin->w_cursor.lnum;
4459 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004461 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004462 break;
4463 case ADDR_ARGUMENTS:
4464 lnum = curwin->w_arg_idx + 1;
4465 break;
4466 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004467 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 lnum = curbuf->b_fnum;
4469 break;
4470 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004471 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004473 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004474 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004475 cmd = NULL;
4476 goto error;
4477 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004478#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004479 case ADDR_QUICKFIX:
4480 lnum = qf_get_cur_valid_idx(eap);
4481 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004482#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 }
4484 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 ++cmd;
4488 switch (addr_type)
4489 {
4490 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 lnum = curbuf->b_ml.ml_line_count;
4492 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004494 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 break;
4496 case ADDR_ARGUMENTS:
4497 lnum = ARGCOUNT;
4498 break;
4499 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004500 buf = lastbuf;
4501 while (buf->b_ml.ml_mfp == NULL)
4502 {
4503 if (buf->b_prev == NULL)
4504 break;
4505 buf = buf->b_prev;
4506 }
4507 lnum = buf->b_fnum;
4508 break;
4509 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 lnum = lastbuf->b_fnum;
4511 break;
4512 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004513 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004515 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004516 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004517 cmd = NULL;
4518 goto error;
4519 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004520#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004521 case ADDR_QUICKFIX:
4522 lnum = qf_get_size(eap);
4523 if (lnum == 0)
4524 lnum = 1;
4525 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004526#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004527 }
4528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
4530 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 if (*++cmd == NUL)
4532 {
4533 cmd = NULL;
4534 goto error;
4535 }
4536 if (addr_type != ADDR_LINES)
4537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004538 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004539 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 goto error;
4541 }
4542 if (skip)
4543 ++cmd;
4544 else
4545 {
4546 /* Only accept a mark in another file when it is
4547 * used by itself: ":'M". */
4548 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4549 ++cmd;
4550 if (fp == (pos_T *)-1)
4551 /* Jumped to another file. */
4552 lnum = curwin->w_cursor.lnum;
4553 else
4554 {
4555 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 cmd = NULL;
4558 goto error;
4559 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004560 lnum = fp->lnum;
4561 }
4562 }
4563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565 case '/':
4566 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 c = *cmd++;
4568 if (addr_type != ADDR_LINES)
4569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004570 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004571 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004572 goto error;
4573 }
4574 if (skip) /* skip "/pat/" */
4575 {
4576 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4577 if (*cmd == c)
4578 ++cmd;
4579 }
4580 else
4581 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004582 int flags;
4583
4584 pos = curwin->w_cursor; // save curwin->w_cursor
4585
4586 // When '/' or '?' follows another address, start from
4587 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004588 if (lnum != MAXLNUM)
4589 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004590
4591 // Start a forward search at the end of the line (unless
4592 // before the first line).
4593 // Start a backward search at the start of the line.
4594 // This makes sure we never match in the current
4595 // line, and can match anywhere in the
4596 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004597 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004598 curwin->w_cursor.col = MAXCOL;
4599 else
4600 curwin->w_cursor.col = 0;
4601 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004602 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4603 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 {
4605 curwin->w_cursor = pos;
4606 cmd = NULL;
4607 goto error;
4608 }
4609 lnum = curwin->w_cursor.lnum;
4610 curwin->w_cursor = pos;
4611 /* adjust command string pointer */
4612 cmd += searchcmdlen;
4613 }
4614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 ++cmd;
4618 if (addr_type != ADDR_LINES)
4619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004620 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004621 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 goto error;
4623 }
4624 if (*cmd == '&')
4625 i = RE_SUBST;
4626 else if (*cmd == '?' || *cmd == '/')
4627 i = RE_SEARCH;
4628 else
4629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004630 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004631 cmd = NULL;
4632 goto error;
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 if (!skip)
4636 {
4637 /*
4638 * When search follows another address, start from
4639 * there.
4640 */
4641 if (lnum != MAXLNUM)
4642 pos.lnum = lnum;
4643 else
4644 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004646 /*
4647 * Start the search just like for the above
4648 * do_search().
4649 */
4650 if (*cmd != '?')
4651 pos.col = MAXCOL;
4652 else
4653 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004654#ifdef FEAT_VIRTUALEDIT
4655 pos.coladd = 0;
4656#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004657 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004658 *cmd == '?' ? BACKWARD : FORWARD,
4659 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004660 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004661 lnum = pos.lnum;
4662 else
4663 {
4664 cmd = NULL;
4665 goto error;
4666 }
4667 }
4668 ++cmd;
4669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004672 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4673 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675
4676 for (;;)
4677 {
4678 cmd = skipwhite(cmd);
4679 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4680 break;
4681
4682 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004683 {
4684 switch (addr_type)
4685 {
4686 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004687 /* "+1" is same as ".+1" */
4688 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004689 break;
4690 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004691 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004692 break;
4693 case ADDR_ARGUMENTS:
4694 lnum = curwin->w_arg_idx + 1;
4695 break;
4696 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004697 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004698 lnum = curbuf->b_fnum;
4699 break;
4700 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004701 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004702 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004703 case ADDR_TABS_RELATIVE:
4704 lnum = 1;
4705 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004706#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004707 case ADDR_QUICKFIX:
4708 lnum = qf_get_cur_valid_idx(eap);
4709 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004710#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004711 }
4712 }
4713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (VIM_ISDIGIT(*cmd))
4715 i = '+'; /* "number" is same as "+number" */
4716 else
4717 i = *cmd++;
4718 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4719 n = 1;
4720 else
4721 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004722
4723 if (addr_type == ADDR_TABS_RELATIVE)
4724 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004725 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004726 cmd = NULL;
4727 goto error;
4728 }
4729 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004730 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004731 lnum = compute_buffer_local_count(
4732 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004734 {
4735#ifdef FEAT_FOLDING
4736 /* Relative line addressing, need to adjust for folded lines
4737 * now, but only do it after the first address. */
4738 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4739 && address_count >= 2)
4740 (void)hasFolding(lnum, NULL, &lnum);
4741#endif
4742 if (i == '-')
4743 lnum -= n;
4744 else
4745 lnum += n;
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
4748 } while (*cmd == '/' || *cmd == '?');
4749
4750error:
4751 *ptr = cmd;
4752 return lnum;
4753}
4754
4755/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004756 * Get flags from an Ex command argument.
4757 */
4758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004759get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004760{
4761 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4762 {
4763 if (*eap->arg == 'l')
4764 eap->flags |= EXFLAG_LIST;
4765 else if (*eap->arg == 'p')
4766 eap->flags |= EXFLAG_PRINT;
4767 else
4768 eap->flags |= EXFLAG_NR;
4769 eap->arg = skipwhite(eap->arg + 1);
4770 }
4771}
4772
4773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 * Function called for command which is Not Implemented. NI!
4775 */
4776 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004777ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
4779 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004780 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781}
4782
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004783#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784/*
4785 * Function called for script command which is Not Implemented. NI!
4786 * Skips over ":perl <<EOF" constructs.
4787 */
4788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004789ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 if (!eap->skip)
4792 ex_ni(eap);
4793 else
4794 vim_free(script_get(eap, eap->arg));
4795}
4796#endif
4797
4798/*
4799 * Check range in Ex command for validity.
4800 * Return NULL when valid, error message when invalid.
4801 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004802 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004803invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004805 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 if ( eap->line1 < 0
4807 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004808 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004809 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004810
4811 if (eap->argt & RANGE)
4812 {
4813 switch(eap->addr_type)
4814 {
4815 case ADDR_LINES:
4816 if (!(eap->argt & NOTADR)
4817 && eap->line2 > curbuf->b_ml.ml_line_count
4818#ifdef FEAT_DIFF
4819 + (eap->cmdidx == CMD_diffget)
4820#endif
4821 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004822 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004823 break;
4824 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004825 /* add 1 if ARGCOUNT is 0 */
4826 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004827 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828 break;
4829 case ADDR_BUFFERS:
4830 if (eap->line1 < firstbuf->b_fnum
4831 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004832 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833 break;
4834 case ADDR_LOADED_BUFFERS:
4835 buf = firstbuf;
4836 while (buf->b_ml.ml_mfp == NULL)
4837 {
4838 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004840 buf = buf->b_next;
4841 }
4842 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004843 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004844 buf = lastbuf;
4845 while (buf->b_ml.ml_mfp == NULL)
4846 {
4847 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004848 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004849 buf = buf->b_prev;
4850 }
4851 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004853 break;
4854 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004855 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004857 break;
4858 case ADDR_TABS:
4859 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004860 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004862 case ADDR_TABS_RELATIVE:
4863 /* Do nothing */
4864 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004865#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004866 case ADDR_QUICKFIX:
4867 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004868 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004869 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004870#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004871 }
4872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 return NULL;
4874}
4875
4876/*
4877 * Correct the range for zero line number, if required.
4878 */
4879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004880correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
4882 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4883 {
4884 if (eap->line1 == 0)
4885 eap->line1 = 1;
4886 if (eap->line2 == 0)
4887 eap->line2 = 1;
4888 }
4889}
4890
Bram Moolenaar748bf032005-02-02 23:04:36 +00004891#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004892/*
4893 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4894 * pattern. Otherwise return eap->arg.
4895 */
4896 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004898{
4899 char_u *p = eap->arg;
4900
Bram Moolenaara37420f2006-02-04 22:37:47 +00004901 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4902 || eap->cmdidx == CMD_vimgrepadd
4903 || eap->cmdidx == CMD_lvimgrepadd
4904 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004905 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004906 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004907 if (p == NULL)
4908 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004909 }
4910 return p;
4911}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004912
4913/*
4914 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4915 * in the command line, so that things like % get expanded.
4916 */
4917 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004918replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004919{
4920 char_u *new_cmdline;
4921 char_u *program;
4922 char_u *pos;
4923 char_u *ptr;
4924 int len;
4925 int i;
4926
4927 /*
4928 * Don't do it when ":vimgrep" is used for ":grep".
4929 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004930 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4931 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4932 || eap->cmdidx == CMD_grepadd
4933 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004934 && !grep_internal(eap->cmdidx))
4935 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004936 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4937 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004938 {
4939 if (*curbuf->b_p_gp == NUL)
4940 program = p_gp;
4941 else
4942 program = curbuf->b_p_gp;
4943 }
4944 else
4945 {
4946 if (*curbuf->b_p_mp == NUL)
4947 program = p_mp;
4948 else
4949 program = curbuf->b_p_mp;
4950 }
4951
4952 p = skipwhite(p);
4953
4954 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4955 {
4956 /* replace $* by given arguments */
4957 i = 1;
4958 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4959 ++i;
4960 len = (int)STRLEN(p);
4961 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4962 if (new_cmdline == NULL)
4963 return NULL; /* out of memory */
4964 ptr = new_cmdline;
4965 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4966 {
4967 i = (int)(pos - program);
4968 STRNCPY(ptr, program, i);
4969 STRCPY(ptr += i, p);
4970 ptr += len;
4971 program = pos + 2;
4972 }
4973 STRCPY(ptr, program);
4974 }
4975 else
4976 {
4977 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4978 if (new_cmdline == NULL)
4979 return NULL; /* out of memory */
4980 STRCPY(new_cmdline, program);
4981 STRCAT(new_cmdline, " ");
4982 STRCAT(new_cmdline, p);
4983 }
4984 msg_make(p);
4985
4986 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4987 vim_free(*cmdlinep);
4988 *cmdlinep = new_cmdline;
4989 p = new_cmdline;
4990 }
4991 return p;
4992}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004993#endif
4994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995/*
4996 * Expand file name in Ex command argument.
4997 * Return FAIL for failure, OK otherwise.
4998 */
4999 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005000expand_filename(
5001 exarg_T *eap,
5002 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005003 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
5005 int has_wildcards; /* need to expand wildcards */
5006 char_u *repl;
5007 int srclen;
5008 char_u *p;
5009 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005010 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
Bram Moolenaar748bf032005-02-02 23:04:36 +00005012#ifdef FEAT_QUICKFIX
5013 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5014 p = skip_grep_pat(eap);
5015#else
5016 p = eap->arg;
5017#endif
5018
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 /*
5020 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5021 * the file name contains a wildcard it should not cause expanding.
5022 * (it will be expanded anyway if there is a wildcard before replacing).
5023 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005024 has_wildcards = mch_has_wildcard(p);
5025 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005027#ifdef FEAT_EVAL
5028 /* Skip over `=expr`, wildcards in it are not expanded. */
5029 if (p[0] == '`' && p[1] == '=')
5030 {
5031 p += 2;
5032 (void)skip_expr(&p);
5033 if (*p == '`')
5034 ++p;
5035 continue;
5036 }
5037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 /*
5039 * Quick check if this cannot be the start of a special string.
5040 * Also removes backslash before '%', '#' and '<'.
5041 */
5042 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5043 {
5044 ++p;
5045 continue;
5046 }
5047
5048 /*
5049 * Try to find a match at this position.
5050 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005051 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5052 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (*errormsgp != NULL) /* error detected */
5054 return FAIL;
5055 if (repl == NULL) /* no match found */
5056 {
5057 p += srclen;
5058 continue;
5059 }
5060
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005061 /* Wildcards won't be expanded below, the replacement is taken
5062 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5063 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5064 {
5065 char_u *l = repl;
5066
5067 repl = expand_env_save(repl);
5068 vim_free(l);
5069 }
5070
Bram Moolenaar63b92542007-03-27 14:57:09 +00005071 /* Need to escape white space et al. with a backslash.
5072 * Don't do this for:
5073 * - replacement that already has been escaped: "##"
5074 * - shell commands (may have to use quotes instead).
5075 * - non-unix systems when there is a single argument (spaces don't
5076 * separate arguments then).
5077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005079 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 && eap->cmdidx != CMD_grep
5082 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005083 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005084 && eap->cmdidx != CMD_lgrep
5085 && eap->cmdidx != CMD_lgrepadd
5086 && eap->cmdidx != CMD_lmake
5087 && eap->cmdidx != CMD_make
5088 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#ifndef UNIX
5090 && !(eap->argt & NOSPC)
5091#endif
5092 )
5093 {
5094 char_u *l;
5095#ifdef BACKSLASH_IN_FILENAME
5096 /* Don't escape a backslash here, because rem_backslash() doesn't
5097 * remove it later. */
5098 static char_u *nobslash = (char_u *)" \t\"|";
5099# define ESCAPE_CHARS nobslash
5100#else
5101# define ESCAPE_CHARS escape_chars
5102#endif
5103
5104 for (l = repl; *l; ++l)
5105 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5106 {
5107 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5108 if (l != NULL)
5109 {
5110 vim_free(repl);
5111 repl = l;
5112 }
5113 break;
5114 }
5115 }
5116
5117 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005118 if ((eap->usefilter || eap->cmdidx == CMD_bang
5119 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005120 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 char_u *l;
5123
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005124 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (l != NULL)
5126 {
5127 vim_free(repl);
5128 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 }
5131
5132 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5133 vim_free(repl);
5134 if (p == NULL)
5135 return FAIL;
5136 }
5137
5138 /*
5139 * One file argument: Expand wildcards.
5140 * Don't do this with ":r !command" or ":w !command".
5141 */
5142 if ((eap->argt & NOSPC) && !eap->usefilter)
5143 {
5144 /*
5145 * May do this twice:
5146 * 1. Replace environment variables.
5147 * 2. Replace any other wildcards, remove backslashes.
5148 */
5149 for (n = 1; n <= 2; ++n)
5150 {
5151 if (n == 2)
5152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 /*
5154 * Halve the number of backslashes (this is Vi compatible).
5155 * For Unix and OS/2, when wildcards are expanded, this is
5156 * done by ExpandOne() below.
5157 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005158#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (!has_wildcards)
5160#endif
5161 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163
5164 if (has_wildcards)
5165 {
5166 if (n == 1)
5167 {
5168 /*
5169 * First loop: May expand environment variables. This
5170 * can be done much faster with expand_env() than with
5171 * something else (e.g., calling a shell).
5172 * After expanding environment variables, check again
5173 * if there are still wildcards present.
5174 */
5175 if (vim_strchr(eap->arg, '$') != NULL
5176 || vim_strchr(eap->arg, '~') != NULL)
5177 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005178 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005179 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 has_wildcards = mch_has_wildcard(NameBuff);
5181 p = NameBuff;
5182 }
5183 else
5184 p = NULL;
5185 }
5186 else /* n == 2 */
5187 {
5188 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005189 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 ExpandInit(&xpc);
5192 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005193 if (p_wic)
5194 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005196 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (p == NULL)
5198 return FAIL;
5199 }
5200 if (p != NULL)
5201 {
5202 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5203 p, cmdlinep);
5204 if (n == 2) /* p came from ExpandOne() */
5205 vim_free(p);
5206 }
5207 }
5208 }
5209 }
5210 return OK;
5211}
5212
5213/*
5214 * Replace part of the command line, keeping eap->cmd, eap->arg and
5215 * eap->nextcmd correct.
5216 * "src" points to the part that is to be replaced, of length "srclen".
5217 * "repl" is the replacement string.
5218 * Returns a pointer to the character after the replaced string.
5219 * Returns NULL for failure.
5220 */
5221 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005222repl_cmdline(
5223 exarg_T *eap,
5224 char_u *src,
5225 int srclen,
5226 char_u *repl,
5227 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
5229 int len;
5230 int i;
5231 char_u *new_cmdline;
5232
5233 /*
5234 * The new command line is build in new_cmdline[].
5235 * First allocate it.
5236 * Careful: a "+cmd" argument may have been NUL terminated.
5237 */
5238 len = (int)STRLEN(repl);
5239 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005240 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5242 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5243 return NULL; /* out of memory! */
5244
5245 /*
5246 * Copy the stuff before the expanded part.
5247 * Copy the expanded stuff.
5248 * Copy what came after the expanded part.
5249 * Copy the next commands, if there are any.
5250 */
5251 i = (int)(src - *cmdlinep); /* length of part before match */
5252 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 mch_memmove(new_cmdline + i, repl, (size_t)len);
5255 i += len; /* remember the end of the string */
5256 STRCPY(new_cmdline + i, src + srclen);
5257 src = new_cmdline + i; /* remember where to continue */
5258
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005259 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 i = (int)STRLEN(new_cmdline) + 1;
5262 STRCPY(new_cmdline + i, eap->nextcmd);
5263 eap->nextcmd = new_cmdline + i;
5264 }
5265 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5266 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5267 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5268 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5269 vim_free(*cmdlinep);
5270 *cmdlinep = new_cmdline;
5271
5272 return src;
5273}
5274
5275/*
5276 * Check for '|' to separate commands and '"' to start comments.
5277 */
5278 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005279separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 char_u *p;
5282
Bram Moolenaar86b68352004-12-27 21:59:20 +00005283#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005284 p = skip_grep_pat(eap);
5285#else
5286 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005287#endif
5288
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005289 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
5291 if (*p == Ctrl_V)
5292 {
5293 if (eap->argt & (USECTRLV | XFILE))
5294 ++p; /* skip CTRL-V and next char */
5295 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005296 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005297 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (*p == NUL) /* stop at NUL after CTRL-V */
5299 break;
5300 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005301
5302#ifdef FEAT_EVAL
5303 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005304 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005305 {
5306 p += 2;
5307 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005308 }
5309#endif
5310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 /* Check for '"': start of comment or '|': next command */
5312 /* :@" and :*" do not start a comment!
5313 * :redir @" doesn't either. */
5314 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5315 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5316 || p != eap->arg)
5317 && (eap->cmdidx != CMD_redir
5318 || p != eap->arg + 1 || p[-1] != '@'))
5319 || *p == '|' || *p == '\n')
5320 {
5321 /*
5322 * We remove the '\' before the '|', unless USECTRLV is used
5323 * AND 'b' is present in 'cpoptions'.
5324 */
5325 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5326 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5327 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005328 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 --p;
5330 }
5331 else
5332 {
5333 eap->nextcmd = check_nextcmd(p);
5334 *p = NUL;
5335 break;
5336 }
5337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005339
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5341 del_trailing_spaces(eap->arg);
5342}
5343
5344/*
5345 * get + command from ex argument
5346 */
5347 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005348getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 char_u *arg = *argp;
5351 char_u *command = NULL;
5352
5353 if (*arg == '+') /* +[command] */
5354 {
5355 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005356 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 command = dollar_command;
5358 else
5359 {
5360 command = arg;
5361 arg = skip_cmd_arg(command, TRUE);
5362 if (*arg != NUL)
5363 *arg++ = NUL; /* terminate command with NUL */
5364 }
5365
5366 arg = skipwhite(arg); /* skip over spaces */
5367 *argp = arg;
5368 }
5369 return command;
5370}
5371
5372/*
5373 * Find end of "+command" argument. Skip over "\ " and "\\".
5374 */
5375 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005376skip_cmd_arg(
5377 char_u *p,
5378 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 while (*p && !vim_isspace(*p))
5381 {
5382 if (*p == '\\' && p[1] != NUL)
5383 {
5384 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005385 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else
5387 ++p;
5388 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005389 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 return p;
5392}
5393
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005394 int
5395get_bad_opt(char_u *p, exarg_T *eap)
5396{
5397 if (STRICMP(p, "keep") == 0)
5398 eap->bad_char = BAD_KEEP;
5399 else if (STRICMP(p, "drop") == 0)
5400 eap->bad_char = BAD_DROP;
5401 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5402 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005403 else
5404 return FAIL;
5405 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005406}
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408/*
5409 * Get "++opt=arg" argument.
5410 * Return FAIL or OK.
5411 */
5412 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *arg = eap->arg + 2;
5416 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005417 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 /* ":edit ++[no]bin[ary] file" */
5421 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5422 {
5423 if (*arg == 'n')
5424 {
5425 arg += 2;
5426 eap->force_bin = FORCE_NOBIN;
5427 }
5428 else
5429 eap->force_bin = FORCE_BIN;
5430 if (!checkforcmd(&arg, "binary", 3))
5431 return FAIL;
5432 eap->arg = skipwhite(arg);
5433 return OK;
5434 }
5435
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005436 /* ":read ++edit file" */
5437 if (STRNCMP(arg, "edit", 4) == 0)
5438 {
5439 eap->read_edit = TRUE;
5440 eap->arg = skipwhite(arg + 4);
5441 return OK;
5442 }
5443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (STRNCMP(arg, "ff", 2) == 0)
5445 {
5446 arg += 2;
5447 pp = &eap->force_ff;
5448 }
5449 else if (STRNCMP(arg, "fileformat", 10) == 0)
5450 {
5451 arg += 10;
5452 pp = &eap->force_ff;
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 else if (STRNCMP(arg, "enc", 3) == 0)
5455 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005456 if (STRNCMP(arg, "encoding", 8) == 0)
5457 arg += 8;
5458 else
5459 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 pp = &eap->force_enc;
5461 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005462 else if (STRNCMP(arg, "bad", 3) == 0)
5463 {
5464 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005465 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
5468 if (pp == NULL || *arg != '=')
5469 return FAIL;
5470
5471 ++arg;
5472 *pp = (int)(arg - eap->cmd);
5473 arg = skip_cmd_arg(arg, FALSE);
5474 eap->arg = skipwhite(arg);
5475 *arg = NUL;
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (pp == &eap->force_ff)
5478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5480 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005481 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005483 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 /* Make 'fileencoding' lower case. */
5486 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5487 *p = TOLOWER_ASC(*p);
5488 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005489 else
5490 {
5491 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5492 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005493 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005494 return FAIL;
5495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 return OK;
5498}
5499
5500/*
5501 * ":abbreviate" and friends.
5502 */
5503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005504ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 do_exmap(eap, TRUE); /* almost the same as mapping */
5507}
5508
5509/*
5510 * ":map" and friends.
5511 */
5512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005513ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 /*
5516 * If we are sourcing .exrc or .vimrc in current directory we
5517 * print the mappings for security reasons.
5518 */
5519 if (secure)
5520 {
5521 secure = 2;
5522 msg_outtrans(eap->cmd);
5523 msg_putchar('\n');
5524 }
5525 do_exmap(eap, FALSE);
5526}
5527
5528/*
5529 * ":unmap" and friends.
5530 */
5531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005532ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 do_exmap(eap, FALSE);
5535}
5536
5537/*
5538 * ":mapclear" and friends.
5539 */
5540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005541ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5544}
5545
5546/*
5547 * ":abclear" and friends.
5548 */
5549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005550ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5553}
5554
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005559 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 * directory for security reasons.
5561 */
5562 if (secure)
5563 {
5564 secure = 2;
5565 eap->errmsg = e_curdir;
5566 }
5567 else if (eap->cmdidx == CMD_autocmd)
5568 do_autocmd(eap->arg, eap->forceit);
5569 else
5570 do_augroup(eap->arg, eap->forceit);
5571}
5572
5573/*
5574 * ":doautocmd": Apply the automatic commands to the current buffer.
5575 */
5576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005579 char_u *arg = eap->arg;
5580 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005581 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005582
Bram Moolenaar1610d052016-06-09 22:53:01 +02005583 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5584 /* Only when there is no <nomodeline>. */
5585 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005586 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589/*
5590 * :[N]bunload[!] [N] [bufname] unload buffer
5591 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5592 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5593 */
5594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597 eap->errmsg = do_bufdel(
5598 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5599 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5600 : DOBUF_UNLOAD, eap->arg,
5601 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5602}
5603
5604/*
5605 * :[N]buffer [N] to buffer N
5606 * :[N]sbuffer [N] to buffer N
5607 */
5608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005609ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
5611 if (*eap->arg)
5612 eap->errmsg = e_trailing;
5613 else
5614 {
5615 if (eap->addr_count == 0) /* default is current buffer */
5616 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5617 else
5618 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005619 if (eap->do_ecmd_cmd != NULL)
5620 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
5622}
5623
5624/*
5625 * :[N]bmodified [N] to next mod. buffer
5626 * :[N]sbmodified [N] to next mod. buffer
5627 */
5628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005632 if (eap->do_ecmd_cmd != NULL)
5633 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
5635
5636/*
5637 * :[N]bnext [N] to next buffer
5638 * :[N]sbnext [N] split and to next buffer
5639 */
5640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005641ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005644 if (eap->do_ecmd_cmd != NULL)
5645 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646}
5647
5648/*
5649 * :[N]bNext [N] to previous buffer
5650 * :[N]bprevious [N] to previous buffer
5651 * :[N]sbNext [N] split and to previous buffer
5652 * :[N]sbprevious [N] split and to previous buffer
5653 */
5654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005658 if (eap->do_ecmd_cmd != NULL)
5659 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660}
5661
5662/*
5663 * :brewind to first buffer
5664 * :bfirst to first buffer
5665 * :sbrewind split and to first buffer
5666 * :sbfirst split and to first buffer
5667 */
5668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005669ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005672 if (eap->do_ecmd_cmd != NULL)
5673 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674}
5675
5676/*
5677 * :blast to last buffer
5678 * :sblast split and to last buffer
5679 */
5680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005684 if (eap->do_ecmd_cmd != NULL)
5685 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
5691 return (c == NUL || c == '|' || c == '"' || c == '\n');
5692}
5693
5694#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5695 || defined(PROTO)
5696/*
5697 * Return the next command, after the first '|' or '\n'.
5698 * Return NULL if not found.
5699 */
5700 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005701find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 while (*p != '|' && *p != '\n')
5704 {
5705 if (*p == NUL)
5706 return NULL;
5707 ++p;
5708 }
5709 return (p + 1);
5710}
5711#endif
5712
5713/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005714 * Check if *p is a separator between Ex commands, skipping over white space.
5715 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 */
5717 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005720 char_u *s = skipwhite(p);
5721
5722 if (*s == '|' || *s == '\n')
5723 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 else
5725 return NULL;
5726}
5727
5728/*
5729 * - if there are more files to edit
5730 * - and this is the last window
5731 * - and forceit not used
5732 * - and not repeated twice on a row
5733 * return FAIL and give error message if 'message' TRUE
5734 * return OK otherwise
5735 */
5736 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005737check_more(
5738 int message, /* when FALSE check only, no messages */
5739 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5742
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005743 if (!forceit && only_one_window()
5744 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746 if (message)
5747 {
5748#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5749 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5750 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005751 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005753 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5754 NGETTEXT("%d more file to edit. Quit anyway?",
5755 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5757 return OK;
5758 return FAIL;
5759 }
5760#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005761 semsg(NGETTEXT("E173: %d more file to edit",
5762 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 quitmore = 2; /* next try to quit is allowed */
5764 }
5765 return FAIL;
5766 }
5767 return OK;
5768}
5769
5770#ifdef FEAT_CMDL_COMPL
5771/*
5772 * Function given to ExpandGeneric() to obtain the list of command names.
5773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005775get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 if (idx >= (int)CMD_SIZE)
5778# ifdef FEAT_USR_CMDS
5779 return get_user_command_name(idx);
5780# else
5781 return NULL;
5782# endif
5783 return cmdnames[idx].cmd_name;
5784}
5785#endif
5786
5787#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789uc_add_command(
5790 char_u *name,
5791 size_t name_len,
5792 char_u *rep,
5793 long argt,
5794 long def,
5795 int flags,
5796 int compl,
5797 char_u *compl_arg,
5798 int addr_type,
5799 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
5801 ucmd_T *cmd = NULL;
5802 char_u *p;
5803 int i;
5804 int cmp = 1;
5805 char_u *rep_buf = NULL;
5806 garray_T *gap;
5807
Bram Moolenaar9c102382006-05-03 21:26:49 +00005808 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 if (rep_buf == NULL)
5810 {
5811 /* Can't replace termcodes - try using the string as is */
5812 rep_buf = vim_strsave(rep);
5813
5814 /* Give up if out of memory */
5815 if (rep_buf == NULL)
5816 return FAIL;
5817 }
5818
5819 /* get address of growarray: global or in curbuf */
5820 if (flags & UC_BUFFER)
5821 {
5822 gap = &curbuf->b_ucmds;
5823 if (gap->ga_itemsize == 0)
5824 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5825 }
5826 else
5827 gap = &ucmds;
5828
5829 /* Search for the command in the already defined commands. */
5830 for (i = 0; i < gap->ga_len; ++i)
5831 {
5832 size_t len;
5833
5834 cmd = USER_CMD_GA(gap, i);
5835 len = STRLEN(cmd->uc_name);
5836 cmp = STRNCMP(name, cmd->uc_name, name_len);
5837 if (cmp == 0)
5838 {
5839 if (name_len < len)
5840 cmp = -1;
5841 else if (name_len > len)
5842 cmp = 1;
5843 }
5844
5845 if (cmp == 0)
5846 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005847 // Command can be replaced with "command!" and when sourcing the
5848 // same script again, but only once.
5849 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5850 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005852 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005853 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 goto fail;
5855 }
5856
Bram Moolenaard23a8232018-02-10 18:45:26 +01005857 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005858#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005859 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 break;
5862 }
5863
5864 /* Stop as soon as we pass the name to add */
5865 if (cmp < 0)
5866 break;
5867 }
5868
5869 /* Extend the array unless we're replacing an existing command */
5870 if (cmp != 0)
5871 {
5872 if (ga_grow(gap, 1) != OK)
5873 goto fail;
5874 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5875 goto fail;
5876
5877 cmd = USER_CMD_GA(gap, i);
5878 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5879
5880 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
5882 cmd->uc_name = p;
5883 }
5884
5885 cmd->uc_rep = rep_buf;
5886 cmd->uc_argt = argt;
5887 cmd->uc_def = def;
5888 cmd->uc_compl = compl;
5889#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005890 cmd->uc_script_ctx = current_sctx;
5891 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892# ifdef FEAT_CMDL_COMPL
5893 cmd->uc_compl_arg = compl_arg;
5894# endif
5895#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005896 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 return OK;
5899
5900fail:
5901 vim_free(rep_buf);
5902#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5903 vim_free(compl_arg);
5904#endif
5905 return FAIL;
5906}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005909#if defined(FEAT_USR_CMDS)
5910static struct
5911{
5912 int expand;
5913 char *name;
5914} addr_type_complete[] =
5915{
5916 {ADDR_ARGUMENTS, "arguments"},
5917 {ADDR_LINES, "lines"},
5918 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5919 {ADDR_TABS, "tabs"},
5920 {ADDR_BUFFERS, "buffers"},
5921 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005922 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005923 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005924 {-1, NULL}
5925};
5926#endif
5927
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005928#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929/*
5930 * List of names for completion for ":command" with the EXPAND_ flag.
5931 * Must be alphabetical for completion.
5932 */
5933static struct
5934{
5935 int expand;
5936 char *name;
5937} command_complete[] =
5938{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005939 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005941 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005943 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005945 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005946#if defined(FEAT_CSCOPE)
5947 {EXPAND_CSCOPE, "cscope"},
5948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5950 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005951 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952#endif
5953 {EXPAND_DIRECTORIES, "dir"},
5954 {EXPAND_ENV_VARS, "environment"},
5955 {EXPAND_EVENTS, "event"},
5956 {EXPAND_EXPRESSION, "expression"},
5957 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005958 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005959 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {EXPAND_FUNCTIONS, "function"},
5961 {EXPAND_HELP, "help"},
5962 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005963#if defined(FEAT_CMDHIST)
5964 {EXPAND_HISTORY, "history"},
5965#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005966#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005967 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005968 {EXPAND_LOCALES, "locale"},
5969#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005970 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {EXPAND_MAPPINGS, "mapping"},
5972 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005973 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005974 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005975#if defined(FEAT_PROFILE)
5976 {EXPAND_SYNTIME, "syntime"},
5977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005979 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005980 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005981#if defined(FEAT_SIGNS)
5982 {EXPAND_SIGN, "sign"},
5983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 {EXPAND_TAGS, "tag"},
5985 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005986 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {EXPAND_USER_VARS, "var"},
5988 {0, NULL}
5989};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005992#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005994uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
5996 int i, j;
5997 int found = FALSE;
5998 ucmd_T *cmd;
5999 int len;
6000 long a;
6001 garray_T *gap;
6002
6003 gap = &curbuf->b_ucmds;
6004 for (;;)
6005 {
6006 for (i = 0; i < gap->ga_len; ++i)
6007 {
6008 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006009 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
Bram Moolenaard29459b2016-08-26 22:29:11 +02006011 /* Skip commands which don't match the requested prefix and
6012 * commands filtered out. */
6013 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6014 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 continue;
6016
6017 /* Put out the title first time */
6018 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006019 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 found = TRUE;
6021 msg_putchar('\n');
6022 if (got_int)
6023 break;
6024
6025 /* Special cases */
6026 msg_putchar(a & BANG ? '!' : ' ');
6027 msg_putchar(a & REGSTR ? '"' : ' ');
6028 msg_putchar(gap != &ucmds ? 'b' : ' ');
6029 msg_putchar(' ');
6030
Bram Moolenaar8820b482017-03-16 17:23:31 +01006031 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 len = (int)STRLEN(cmd->uc_name) + 4;
6033
6034 do {
6035 msg_putchar(' ');
6036 ++len;
6037 } while (len < 16);
6038
6039 len = 0;
6040
6041 /* Arguments */
6042 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6043 {
6044 case 0: IObuff[len++] = '0'; break;
6045 case (EXTRA): IObuff[len++] = '*'; break;
6046 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6047 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6048 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6049 }
6050
6051 do {
6052 IObuff[len++] = ' ';
6053 } while (len < 5);
6054
6055 /* Range */
6056 if (a & (RANGE|COUNT))
6057 {
6058 if (a & COUNT)
6059 {
6060 /* -count=N */
6061 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6062 len += (int)STRLEN(IObuff + len);
6063 }
6064 else if (a & DFLALL)
6065 IObuff[len++] = '%';
6066 else if (cmd->uc_def >= 0)
6067 {
6068 /* -range=N */
6069 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6070 len += (int)STRLEN(IObuff + len);
6071 }
6072 else
6073 IObuff[len++] = '.';
6074 }
6075
6076 do {
6077 IObuff[len++] = ' ';
6078 } while (len < 11);
6079
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006080 /* Address Type */
6081 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6082 if (addr_type_complete[j].expand != ADDR_LINES
6083 && addr_type_complete[j].expand == cmd->uc_addr_type)
6084 {
6085 STRCPY(IObuff + len, addr_type_complete[j].name);
6086 len += (int)STRLEN(IObuff + len);
6087 break;
6088 }
6089
6090 do {
6091 IObuff[len++] = ' ';
6092 } while (len < 21);
6093
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 /* Completion */
6095 for (j = 0; command_complete[j].expand != 0; ++j)
6096 if (command_complete[j].expand == cmd->uc_compl)
6097 {
6098 STRCPY(IObuff + len, command_complete[j].name);
6099 len += (int)STRLEN(IObuff + len);
6100 break;
6101 }
6102
6103 do {
6104 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006105 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 IObuff[len] = '\0';
6108 msg_outtrans(IObuff);
6109
6110 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006111#ifdef FEAT_EVAL
6112 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006113 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 out_flush();
6116 ui_breakcheck();
6117 if (got_int)
6118 break;
6119 }
6120 if (gap == &ucmds || i < gap->ga_len)
6121 break;
6122 gap = &ucmds;
6123 }
6124
6125 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006126 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127}
6128
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006129 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006130uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6133 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6134 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6135 0xb9, 0x7f, 0};
6136 int i;
6137
6138 for (i = 0; fcmd[i]; ++i)
6139 IObuff[i] = fcmd[i] - 0x40;
6140 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006141 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142}
6143
6144 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006145uc_scan_attr(
6146 char_u *attr,
6147 size_t len,
6148 long *argt,
6149 long *def,
6150 int *flags,
6151 int *compl,
6152 char_u **compl_arg,
6153 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 char_u *p;
6156
6157 if (len == 0)
6158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006159 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return FAIL;
6161 }
6162
6163 /* First, try the simple attributes (no arguments) */
6164 if (STRNICMP(attr, "bang", len) == 0)
6165 *argt |= BANG;
6166 else if (STRNICMP(attr, "buffer", len) == 0)
6167 *flags |= UC_BUFFER;
6168 else if (STRNICMP(attr, "register", len) == 0)
6169 *argt |= REGSTR;
6170 else if (STRNICMP(attr, "bar", len) == 0)
6171 *argt |= TRLBAR;
6172 else
6173 {
6174 int i;
6175 char_u *val = NULL;
6176 size_t vallen = 0;
6177 size_t attrlen = len;
6178
6179 /* Look for the attribute name - which is the part before any '=' */
6180 for (i = 0; i < (int)len; ++i)
6181 {
6182 if (attr[i] == '=')
6183 {
6184 val = &attr[i + 1];
6185 vallen = len - i - 1;
6186 attrlen = i;
6187 break;
6188 }
6189 }
6190
6191 if (STRNICMP(attr, "nargs", attrlen) == 0)
6192 {
6193 if (vallen == 1)
6194 {
6195 if (*val == '0')
6196 /* Do nothing - this is the default */;
6197 else if (*val == '1')
6198 *argt |= (EXTRA | NOSPC | NEEDARG);
6199 else if (*val == '*')
6200 *argt |= EXTRA;
6201 else if (*val == '?')
6202 *argt |= (EXTRA | NOSPC);
6203 else if (*val == '+')
6204 *argt |= (EXTRA | NEEDARG);
6205 else
6206 goto wrong_nargs;
6207 }
6208 else
6209 {
6210wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006211 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 return FAIL;
6213 }
6214 }
6215 else if (STRNICMP(attr, "range", attrlen) == 0)
6216 {
6217 *argt |= RANGE;
6218 if (vallen == 1 && *val == '%')
6219 *argt |= DFLALL;
6220 else if (val != NULL)
6221 {
6222 p = val;
6223 if (*def >= 0)
6224 {
6225two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006226 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 return FAIL;
6228 }
6229
6230 *def = getdigits(&p);
6231 *argt |= (ZEROR | NOTADR);
6232
6233 if (p != val + vallen || vallen == 0)
6234 {
6235invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006236 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 return FAIL;
6238 }
6239 }
6240 }
6241 else if (STRNICMP(attr, "count", attrlen) == 0)
6242 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006243 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245 if (val != NULL)
6246 {
6247 p = val;
6248 if (*def >= 0)
6249 goto two_count;
6250
6251 *def = getdigits(&p);
6252
6253 if (p != val + vallen)
6254 goto invalid_count;
6255 }
6256
6257 if (*def < 0)
6258 *def = 0;
6259 }
6260 else if (STRNICMP(attr, "complete", attrlen) == 0)
6261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (val == NULL)
6263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006264 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 return FAIL;
6266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006268 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6269 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006272 else if (STRNICMP(attr, "addr", attrlen) == 0)
6273 {
6274 *argt |= RANGE;
6275 if (val == NULL)
6276 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006277 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006278 return FAIL;
6279 }
6280 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6281 == FAIL)
6282 return FAIL;
6283 if (addr_type_arg != ADDR_LINES)
6284 *argt |= (ZEROR | NOTADR) ;
6285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 else
6287 {
6288 char_u ch = attr[len];
6289 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006290 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 attr[len] = ch;
6292 return FAIL;
6293 }
6294 }
6295
6296 return OK;
6297}
6298
Bram Moolenaara850a712009-01-28 14:42:59 +00006299/*
6300 * ":command ..."
6301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006303ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
6305 char_u *name;
6306 char_u *end;
6307 char_u *p;
6308 long argt = 0;
6309 long def = -1;
6310 int flags = 0;
6311 int compl = EXPAND_NOTHING;
6312 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006313 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006315 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317 p = eap->arg;
6318
6319 /* Check for attributes */
6320 while (*p == '-')
6321 {
6322 ++p;
6323 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006324 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6325 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 == FAIL)
6327 return;
6328 p = skipwhite(end);
6329 }
6330
6331 /* Get the name (if any) and skip to the following argument */
6332 name = p;
6333 if (ASCII_ISALPHA(*p))
6334 while (ASCII_ISALNUM(*p))
6335 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006336 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006338 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 return;
6340 }
6341 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006342 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344 /* If there is nothing after the name, and no attributes were specified,
6345 * we are listing commands
6346 */
6347 p = skipwhite(end);
6348 if (!has_attr && ends_excmd(*p))
6349 {
6350 uc_list(name, end - name);
6351 }
6352 else if (!ASCII_ISUPPER(*name))
6353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006354 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 return;
6356 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006357 else if ((name_len == 1 && *name == 'X')
6358 || (name_len <= 4
6359 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006361 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006362 return;
6363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 else
6365 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006366 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367}
6368
6369/*
6370 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 * Clear all user commands, global and for current buffer.
6372 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006373 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006374ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
6376 uc_clear(&ucmds);
6377 uc_clear(&curbuf->b_ucmds);
6378}
6379
6380/*
6381 * Clear all user commands for "gap".
6382 */
6383 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006384uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 int i;
6387 ucmd_T *cmd;
6388
6389 for (i = 0; i < gap->ga_len; ++i)
6390 {
6391 cmd = USER_CMD_GA(gap, i);
6392 vim_free(cmd->uc_name);
6393 vim_free(cmd->uc_rep);
6394# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6395 vim_free(cmd->uc_compl_arg);
6396# endif
6397 }
6398 ga_clear(gap);
6399}
6400
6401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006402ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 int i = 0;
6405 ucmd_T *cmd = NULL;
6406 int cmp = -1;
6407 garray_T *gap;
6408
6409 gap = &curbuf->b_ucmds;
6410 for (;;)
6411 {
6412 for (i = 0; i < gap->ga_len; ++i)
6413 {
6414 cmd = USER_CMD_GA(gap, i);
6415 cmp = STRCMP(eap->arg, cmd->uc_name);
6416 if (cmp <= 0)
6417 break;
6418 }
6419 if (gap == &ucmds || cmp == 0)
6420 break;
6421 gap = &ucmds;
6422 }
6423
6424 if (cmp != 0)
6425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006426 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 return;
6428 }
6429
6430 vim_free(cmd->uc_name);
6431 vim_free(cmd->uc_rep);
6432# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6433 vim_free(cmd->uc_compl_arg);
6434# endif
6435
6436 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438 if (i < gap->ga_len)
6439 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6440}
6441
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006442/*
6443 * split and quote args for <f-args>
6444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006446uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 char_u *buf;
6449 char_u *p;
6450 char_u *q;
6451 int len;
6452
6453 /* Precalculate length */
6454 p = arg;
6455 len = 2; /* Initial and final quotes */
6456
6457 while (*p)
6458 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006459 if (p[0] == '\\' && p[1] == '\\')
6460 {
6461 len += 2;
6462 p += 2;
6463 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006464 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 {
6466 len += 1;
6467 p += 2;
6468 }
6469 else if (*p == '\\' || *p == '"')
6470 {
6471 len += 2;
6472 p += 1;
6473 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006474 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 {
6476 p = skipwhite(p);
6477 if (*p == NUL)
6478 break;
6479 len += 3; /* "," */
6480 }
6481 else
6482 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006483 int charlen = (*mb_ptr2len)(p);
Bram Moolenaar13505972019-01-24 15:04:48 +01006484
Bram Moolenaardfef1542012-07-10 19:25:10 +02006485 len += charlen;
6486 p += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488 }
6489
6490 buf = alloc(len + 1);
6491 if (buf == NULL)
6492 {
6493 *lenp = 0;
6494 return buf;
6495 }
6496
6497 p = arg;
6498 q = buf;
6499 *q++ = '"';
6500 while (*p)
6501 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006502 if (p[0] == '\\' && p[1] == '\\')
6503 {
6504 *q++ = '\\';
6505 *q++ = '\\';
6506 p += 2;
6507 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006508 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
6510 *q++ = p[1];
6511 p += 2;
6512 }
6513 else if (*p == '\\' || *p == '"')
6514 {
6515 *q++ = '\\';
6516 *q++ = *p++;
6517 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006518 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
6520 p = skipwhite(p);
6521 if (*p == NUL)
6522 break;
6523 *q++ = '"';
6524 *q++ = ',';
6525 *q++ = '"';
6526 }
6527 else
6528 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006529 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 }
6531 }
6532 *q++ = '"';
6533 *q = 0;
6534
6535 *lenp = len;
6536 return buf;
6537}
6538
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006539 static size_t
6540add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6541{
6542 size_t result;
6543
6544 result = STRLEN(mod_str);
6545 if (*multi_mods)
6546 result += 1;
6547 if (buf != NULL)
6548 {
6549 if (*multi_mods)
6550 STRCAT(buf, " ");
6551 STRCAT(buf, mod_str);
6552 }
6553
6554 *multi_mods = 1;
6555
6556 return result;
6557}
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559/*
6560 * Check for a <> code in a user command.
6561 * "code" points to the '<'. "len" the length of the <> (inclusive).
6562 * "buf" is where the result is to be added.
6563 * "split_buf" points to a buffer used for splitting, caller should free it.
6564 * "split_len" is the length of what "split_buf" contains.
6565 * Returns the length of the replacement, which has been added to "buf".
6566 * Returns -1 if there was no match, and only the "<" has been copied.
6567 */
6568 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006569uc_check_code(
6570 char_u *code,
6571 size_t len,
6572 char_u *buf,
6573 ucmd_T *cmd, /* the user command we're expanding */
6574 exarg_T *eap, /* ex arguments */
6575 char_u **split_buf,
6576 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 size_t result = 0;
6579 char_u *p = code + 1;
6580 size_t l = len - 2;
6581 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006582 enum {
6583 ct_ARGS,
6584 ct_BANG,
6585 ct_COUNT,
6586 ct_LINE1,
6587 ct_LINE2,
6588 ct_RANGE,
6589 ct_MODS,
6590 ct_REGISTER,
6591 ct_LT,
6592 ct_NONE
6593 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
6595 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6596 {
6597 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6598 p += 2;
6599 l -= 2;
6600 }
6601
Bram Moolenaar371d5402006-03-20 21:47:49 +00006602 ++l;
6603 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006605 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006607 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006609 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006611 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006613 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006615 else if (STRNICMP(p, "range>", l) == 0)
6616 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006617 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006619 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006621 else if (STRNICMP(p, "mods>", l) == 0)
6622 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624 switch (type)
6625 {
6626 case ct_ARGS:
6627 /* Simple case first */
6628 if (*eap->arg == NUL)
6629 {
6630 if (quote == 1)
6631 {
6632 result = 2;
6633 if (buf != NULL)
6634 STRCPY(buf, "''");
6635 }
6636 else
6637 result = 0;
6638 break;
6639 }
6640
6641 /* When specified there is a single argument don't split it.
6642 * Works for ":Cmd %" when % is "a b c". */
6643 if ((eap->argt & NOSPC) && quote == 2)
6644 quote = 1;
6645
6646 switch (quote)
6647 {
6648 case 0: /* No quoting, no splitting */
6649 result = STRLEN(eap->arg);
6650 if (buf != NULL)
6651 STRCPY(buf, eap->arg);
6652 break;
6653 case 1: /* Quote, but don't split */
6654 result = STRLEN(eap->arg) + 2;
6655 for (p = eap->arg; *p; ++p)
6656 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006657 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6658 /* DBCS can contain \ in a trail byte, skip the
6659 * double-byte character. */
6660 ++p;
6661 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006662 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 ++result;
6664 }
6665
6666 if (buf != NULL)
6667 {
6668 *buf++ = '"';
6669 for (p = eap->arg; *p; ++p)
6670 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006671 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6672 /* DBCS can contain \ in a trail byte, copy the
6673 * double-byte character to avoid escaping. */
6674 *buf++ = *p++;
6675 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006676 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 *buf++ = '\\';
6678 *buf++ = *p;
6679 }
6680 *buf = '"';
6681 }
6682
6683 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006684 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 /* This is hard, so only do it once, and cache the result */
6686 if (*split_buf == NULL)
6687 *split_buf = uc_split_args(eap->arg, split_len);
6688
6689 result = *split_len;
6690 if (buf != NULL && result != 0)
6691 STRCPY(buf, *split_buf);
6692
6693 break;
6694 }
6695 break;
6696
6697 case ct_BANG:
6698 result = eap->forceit ? 1 : 0;
6699 if (quote)
6700 result += 2;
6701 if (buf != NULL)
6702 {
6703 if (quote)
6704 *buf++ = '"';
6705 if (eap->forceit)
6706 *buf++ = '!';
6707 if (quote)
6708 *buf = '"';
6709 }
6710 break;
6711
6712 case ct_LINE1:
6713 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006714 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 case ct_COUNT:
6716 {
6717 char num_buf[20];
6718 long num = (type == ct_LINE1) ? eap->line1 :
6719 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006720 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6722 size_t num_len;
6723
6724 sprintf(num_buf, "%ld", num);
6725 num_len = STRLEN(num_buf);
6726 result = num_len;
6727
6728 if (quote)
6729 result += 2;
6730
6731 if (buf != NULL)
6732 {
6733 if (quote)
6734 *buf++ = '"';
6735 STRCPY(buf, num_buf);
6736 buf += num_len;
6737 if (quote)
6738 *buf = '"';
6739 }
6740
6741 break;
6742 }
6743
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006744 case ct_MODS:
6745 {
6746 int multi_mods = 0;
6747 typedef struct {
6748 int *varp;
6749 char *name;
6750 } mod_entry_T;
6751 static mod_entry_T mod_entries[] = {
6752#ifdef FEAT_BROWSE_CMD
6753 {&cmdmod.browse, "browse"},
6754#endif
6755#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6756 {&cmdmod.confirm, "confirm"},
6757#endif
6758 {&cmdmod.hide, "hide"},
6759 {&cmdmod.keepalt, "keepalt"},
6760 {&cmdmod.keepjumps, "keepjumps"},
6761 {&cmdmod.keepmarks, "keepmarks"},
6762 {&cmdmod.keeppatterns, "keeppatterns"},
6763 {&cmdmod.lockmarks, "lockmarks"},
6764 {&cmdmod.noswapfile, "noswapfile"},
6765 {NULL, NULL}
6766 };
6767 int i;
6768
6769 result = quote ? 2 : 0;
6770 if (buf != NULL)
6771 {
6772 if (quote)
6773 *buf++ = '"';
6774 *buf = '\0';
6775 }
6776
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006777 /* :aboveleft and :leftabove */
6778 if (cmdmod.split & WSP_ABOVE)
6779 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6780 /* :belowright and :rightbelow */
6781 if (cmdmod.split & WSP_BELOW)
6782 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6783 /* :botright */
6784 if (cmdmod.split & WSP_BOT)
6785 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006786
6787 /* the modifiers that are simple flags */
6788 for (i = 0; mod_entries[i].varp != NULL; ++i)
6789 if (*mod_entries[i].varp)
6790 result += add_cmd_modifier(buf, mod_entries[i].name,
6791 &multi_mods);
6792
6793 /* TODO: How to support :noautocmd? */
6794#ifdef HAVE_SANDBOX
6795 /* TODO: How to support :sandbox?*/
6796#endif
6797 /* :silent */
6798 if (msg_silent > 0)
6799 result += add_cmd_modifier(buf,
6800 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006801 /* :tab */
6802 if (cmdmod.tab > 0)
6803 result += add_cmd_modifier(buf, "tab", &multi_mods);
6804 /* :topleft */
6805 if (cmdmod.split & WSP_TOP)
6806 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006807 /* TODO: How to support :unsilent?*/
6808 /* :verbose */
6809 if (p_verbose > 0)
6810 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006811 /* :vertical */
6812 if (cmdmod.split & WSP_VERT)
6813 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006814 if (quote && buf != NULL)
6815 {
6816 buf += result - 2;
6817 *buf = '"';
6818 }
6819 break;
6820 }
6821
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 case ct_REGISTER:
6823 result = eap->regname ? 1 : 0;
6824 if (quote)
6825 result += 2;
6826 if (buf != NULL)
6827 {
6828 if (quote)
6829 *buf++ = '\'';
6830 if (eap->regname)
6831 *buf++ = eap->regname;
6832 if (quote)
6833 *buf = '\'';
6834 }
6835 break;
6836
6837 case ct_LT:
6838 result = 1;
6839 if (buf != NULL)
6840 *buf = '<';
6841 break;
6842
6843 default:
6844 /* Not recognized: just copy the '<' and return -1. */
6845 result = (size_t)-1;
6846 if (buf != NULL)
6847 *buf = '<';
6848 break;
6849 }
6850
6851 return result;
6852}
6853
6854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006855do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
6857 char_u *buf;
6858 char_u *p;
6859 char_u *q;
6860
6861 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006862 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006863 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 size_t len, totlen;
6865
6866 size_t split_len = 0;
6867 char_u *split_buf = NULL;
6868 ucmd_T *cmd;
6869#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006870 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871#endif
6872
6873 if (eap->cmdidx == CMD_USER)
6874 cmd = USER_CMD(eap->useridx);
6875 else
6876 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6877
6878 /*
6879 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006880 * First round: "buf" is NULL, compute length, allocate "buf".
6881 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 */
6883 buf = NULL;
6884 for (;;)
6885 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006886 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006887 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006889
6890 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006892 start = vim_strchr(p, '<');
6893 if (start != NULL)
6894 end = vim_strchr(start + 1, '>');
6895 if (buf != NULL)
6896 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006897 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6898 ;
6899 if (*ksp == K_SPECIAL
6900 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006901 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6902# ifdef FEAT_GUI
6903 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6904# endif
6905 ))
6906 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006907 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006908 * KS_SPECIAL KE_FILLER, like for mappings, but
6909 * do_cmdline() doesn't handle that, so convert it back.
6910 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6911 len = ksp - p;
6912 if (len > 0)
6913 {
6914 mch_memmove(q, p, len);
6915 q += len;
6916 }
6917 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6918 p = ksp + 3;
6919 continue;
6920 }
6921 }
6922
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006923 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006924 if (start == NULL || end == NULL)
6925 break;
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 /* Include the '>' */
6928 ++end;
6929
6930 /* Take everything up to the '<' */
6931 len = start - p;
6932 if (buf == NULL)
6933 totlen += len;
6934 else
6935 {
6936 mch_memmove(q, p, len);
6937 q += len;
6938 }
6939
6940 len = uc_check_code(start, end - start, q, cmd, eap,
6941 &split_buf, &split_len);
6942 if (len == (size_t)-1)
6943 {
6944 /* no match, continue after '<' */
6945 p = start + 1;
6946 len = 1;
6947 }
6948 else
6949 p = end;
6950 if (buf == NULL)
6951 totlen += len;
6952 else
6953 q += len;
6954 }
6955 if (buf != NULL) /* second time here, finished */
6956 {
6957 STRCPY(q, p);
6958 break;
6959 }
6960
6961 totlen += STRLEN(p); /* Add on the trailing characters */
6962 buf = alloc((unsigned)(totlen + 1));
6963 if (buf == NULL)
6964 {
6965 vim_free(split_buf);
6966 return;
6967 }
6968 }
6969
6970#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006971 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972#endif
6973 (void)do_cmdline(buf, eap->getline, eap->cookie,
6974 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6975#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006976 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977#endif
6978 vim_free(buf);
6979 vim_free(split_buf);
6980}
6981
6982# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6983 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006984get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6987}
6988
6989/*
6990 * Function given to ExpandGeneric() to obtain the list of user command names.
6991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006993get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 if (idx < curbuf->b_ucmds.ga_len)
6996 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6997 idx -= curbuf->b_ucmds.ga_len;
6998 if (idx < ucmds.ga_len)
6999 return USER_CMD(idx)->uc_name;
7000 return NULL;
7001}
7002
7003/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007004 * Function given to ExpandGeneric() to obtain the list of user address type names.
7005 */
7006 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007007get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007008{
7009 return (char_u *)addr_type_complete[idx].name;
7010}
7011
7012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 * Function given to ExpandGeneric() to obtain the list of user command
7014 * attributes.
7015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007017get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007020 {"addr", "bang", "bar", "buffer", "complete",
7021 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007023 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 return NULL;
7025 return (char_u *)user_cmd_flags[idx];
7026}
7027
7028/*
7029 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7035
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007036 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 return NULL;
7038 return (char_u *)user_cmd_nargs[idx];
7039}
7040
7041/*
7042 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007045get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 return (char_u *)command_complete[idx].name;
7048}
7049# endif /* FEAT_CMDL_COMPL */
7050
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007051/*
7052 * Parse address type argument
7053 */
7054 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007055parse_addr_type_arg(
7056 char_u *value,
7057 int vallen,
7058 long *argt,
7059 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007060{
7061 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007062
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007063 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7064 {
7065 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7066 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7067 if (a && b)
7068 {
7069 *addr_type_arg = addr_type_complete[i].expand;
7070 break;
7071 }
7072 }
7073
7074 if (addr_type_complete[i].expand == -1)
7075 {
7076 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007077
Bram Moolenaar1c465442017-03-12 20:10:05 +01007078 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007079 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007080 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007081 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007082 return FAIL;
7083 }
7084
7085 if (*addr_type_arg != ADDR_LINES)
7086 *argt |= NOTADR;
7087
7088 return OK;
7089}
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091#endif /* FEAT_USR_CMDS */
7092
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007093#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7094/*
7095 * Parse a completion argument "value[vallen]".
7096 * The detected completion goes in "*complp", argument type in "*argt".
7097 * When there is an argument, for function and user defined completion, it's
7098 * copied to allocated memory and stored in "*compl_arg".
7099 * Returns FAIL if something is wrong.
7100 */
7101 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007102parse_compl_arg(
7103 char_u *value,
7104 int vallen,
7105 int *complp,
7106 long *argt,
7107 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007108{
7109 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007110# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007111 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007112# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007113 int i;
7114 int valend = vallen;
7115
7116 /* Look for any argument part - which is the part after any ',' */
7117 for (i = 0; i < vallen; ++i)
7118 {
7119 if (value[i] == ',')
7120 {
7121 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007122# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007123 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007124# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007125 valend = i;
7126 break;
7127 }
7128 }
7129
7130 for (i = 0; command_complete[i].expand != 0; ++i)
7131 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007132 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007133 && STRNCMP(value, command_complete[i].name, valend) == 0)
7134 {
7135 *complp = command_complete[i].expand;
7136 if (command_complete[i].expand == EXPAND_BUFFERS)
7137 *argt |= BUFNAME;
7138 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7139 || command_complete[i].expand == EXPAND_FILES)
7140 *argt |= XFILE;
7141 break;
7142 }
7143 }
7144
7145 if (command_complete[i].expand == 0)
7146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007147 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007148 return FAIL;
7149 }
7150
7151# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7152 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7153 && arg != NULL)
7154# else
7155 if (arg != NULL)
7156# endif
7157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007158 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007159 return FAIL;
7160 }
7161
7162# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7163 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7164 && arg == NULL)
7165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007166 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007167 return FAIL;
7168 }
7169
7170 if (arg != NULL)
7171 *compl_arg = vim_strnsave(arg, (int)arglen);
7172# endif
7173 return OK;
7174}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007175
7176 int
7177cmdcomplete_str_to_type(char_u *complete_str)
7178{
7179 int i;
7180
7181 for (i = 0; command_complete[i].expand != 0; ++i)
7182 if (STRCMP(complete_str, command_complete[i].name) == 0)
7183 return command_complete[i].expand;
7184
7185 return EXPAND_NOTHING;
7186}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007187#endif
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007190ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
Bram Moolenaare6850792010-05-14 15:28:44 +02007192 if (*eap->arg == NUL)
7193 {
7194#ifdef FEAT_EVAL
7195 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7196 char_u *p = NULL;
7197
7198 if (expr != NULL)
7199 {
7200 ++emsg_off;
7201 p = eval_to_string(expr, NULL, FALSE);
7202 --emsg_off;
7203 vim_free(expr);
7204 }
7205 if (p != NULL)
7206 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007207 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007208 vim_free(p);
7209 }
7210 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007211 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007212#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007213 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007214#endif
7215 }
7216 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007217 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
7219
7220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007221ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007224 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 do_highlight(eap->arg, eap->forceit, FALSE);
7226}
7227
7228
7229/*
7230 * Call this function if we thought we were going to exit, but we won't
7231 * (because of an error). May need to restore the terminal mode.
7232 */
7233 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007234not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
7236 exiting = FALSE;
7237 settmode(TMODE_RAW);
7238}
7239
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007240 static int
7241before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7242{
7243 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7244
7245 /* Bail out when autocommands closed the window.
7246 * Refuse to quit when the buffer in the last window is being closed (can
7247 * only happen in autocommands). */
7248 if (!win_valid(wp)
7249 || curbuf_locked()
7250 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7251 return TRUE;
7252
7253 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7254 {
7255 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7256 /* Refuse to quit when locked or when the buffer in the last window is
7257 * being closed (can only happen in autocommands). */
7258 if (curbuf_locked()
7259 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7260 return TRUE;
7261 }
7262
7263 return FALSE;
7264}
7265
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007267 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007268 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 */
7270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007271ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007273 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#ifdef FEAT_CMDWIN
7276 if (cmdwin_type != 0)
7277 {
7278 cmdwin_result = Ctrl_C;
7279 return;
7280 }
7281#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007282 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007283 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007284 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007285 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007286 return;
7287 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007288 if (eap->addr_count > 0)
7289 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007290 int wnr = eap->line2;
7291
7292 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7293 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007294 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007295 }
7296 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007297 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007298
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007299 /* Refuse to quit when locked. */
7300 if (curbuf_locked())
7301 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007302
7303 /* Trigger QuitPre and maybe ExitPre */
7304 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007305 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306
7307#ifdef FEAT_NETBEANS_INTG
7308 netbeansForcedQuit = eap->forceit;
7309#endif
7310
7311 /*
7312 * If there are more files or windows we won't exit.
7313 */
7314 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7315 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007316 if ((!buf_hide(wp->w_buffer)
7317 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007318 | (eap->forceit ? CCGD_FORCEIT : 0)
7319 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007321 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
7323 not_exiting();
7324 }
7325 else
7326 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007327 /* quit last window
7328 * Note: only_one_window() returns true, even so a help window is
7329 * still open. In that case only quit, if no address has been
7330 * specified. Example:
7331 * :h|wincmd w|1q - don't quit
7332 * :h|wincmd w|q - quit
7333 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007334 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007336 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007337#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007341 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
7343}
7344
7345/*
7346 * ":cquit".
7347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007349ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351 getout(1); /* this does not always pass on the exit code to the Manx
7352 compiler. why? */
7353}
7354
7355/*
7356 * ":qall": try to quit all windows
7357 */
7358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007359ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
7361# ifdef FEAT_CMDWIN
7362 if (cmdwin_type != 0)
7363 {
7364 if (eap->forceit)
7365 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7366 else
7367 cmdwin_result = K_XF2;
7368 return;
7369 }
7370# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007371
7372 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007373 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007374 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007375 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007376 return;
7377 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007378
7379 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007380 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007383 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 getout(0);
7385 not_exiting();
7386}
7387
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388/*
7389 * ":close": close current window, unless it is the last one
7390 */
7391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007392ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007394 win_T *win;
7395 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007396#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007398 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007400#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007401 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007402 {
7403 if (eap->addr_count == 0)
7404 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007405 else
7406 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007407 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007408 {
7409 winnr++;
7410 if (winnr == eap->line2)
7411 break;
7412 }
7413 if (win == NULL)
7414 win = lastwin;
7415 ex_win_close(eap->forceit, win, NULL);
7416 }
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419
Bram Moolenaar4033c552017-09-16 20:54:51 +02007420#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421/*
7422 * ":pclose": Close any preview window.
7423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007425ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007426{
7427 win_T *win;
7428
Bram Moolenaar29323592016-07-24 22:04:11 +02007429 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430 if (win->w_p_pvw)
7431 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007432 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433 break;
7434 }
7435}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007436#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007437
Bram Moolenaarf740b292006-02-16 22:11:02 +00007438/*
7439 * Close window "win" and take care of handling closing the last window for a
7440 * modified buffer.
7441 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007443ex_win_close(
7444 int forceit,
7445 win_T *win,
7446 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447{
7448 int need_hide;
7449 buf_T *buf = win->w_buffer;
7450
7451 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007452 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007454#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if ((p_confirm || cmdmod.confirm) && p_write)
7456 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007457 bufref_T bufref;
7458
7459 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007461 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 return;
7463 need_hide = FALSE;
7464 }
7465 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007468 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 return;
7470 }
7471 }
7472
Bram Moolenaar4033c552017-09-16 20:54:51 +02007473#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007475#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007478 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007479 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007480 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007481 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482}
7483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007485 * Handle the argument for a tabpage related ex command.
7486 * Returns a tabpage number.
7487 * When an error is encountered then eap->errmsg is set.
7488 */
7489 static int
7490get_tabpage_arg(exarg_T *eap)
7491{
7492 int tab_number;
7493 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7494
7495 if (eap->arg && *eap->arg != NUL)
7496 {
7497 char_u *p = eap->arg;
7498 char_u *p_save;
7499 int relative = 0; /* argument +N/-N means: go to N places to the
7500 * right/left relative to the current position. */
7501
7502 if (*p == '-')
7503 {
7504 relative = -1;
7505 p++;
7506 }
7507 else if (*p == '+')
7508 {
7509 relative = 1;
7510 p++;
7511 }
7512
7513 p_save = p;
7514 tab_number = getdigits(&p);
7515
7516 if (relative == 0)
7517 {
7518 if (STRCMP(p, "$") == 0)
7519 tab_number = LAST_TAB_NR;
7520 else if (p == p_save || *p_save == '-' || *p != NUL
7521 || tab_number > LAST_TAB_NR)
7522 {
7523 /* No numbers as argument. */
7524 eap->errmsg = e_invarg;
7525 goto theend;
7526 }
7527 }
7528 else
7529 {
7530 if (*p_save == NUL)
7531 tab_number = 1;
7532 else if (p == p_save || *p_save == '-' || *p != NUL
7533 || tab_number == 0)
7534 {
7535 /* No numbers as argument. */
7536 eap->errmsg = e_invarg;
7537 goto theend;
7538 }
7539 tab_number = tab_number * relative + tabpage_index(curtab);
7540 if (!unaccept_arg0 && relative == -1)
7541 --tab_number;
7542 }
7543 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7544 eap->errmsg = e_invarg;
7545 }
7546 else if (eap->addr_count > 0)
7547 {
7548 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007549 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007550 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007551 tab_number = 0;
7552 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007553 else
7554 {
7555 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01007556 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01007557 {
7558 --tab_number;
7559 if (tab_number < unaccept_arg0)
7560 eap->errmsg = e_invarg;
7561 }
7562 }
7563 }
7564 else
7565 {
7566 switch (eap->cmdidx)
7567 {
7568 case CMD_tabnext:
7569 tab_number = tabpage_index(curtab) + 1;
7570 if (tab_number > LAST_TAB_NR)
7571 tab_number = 1;
7572 break;
7573 case CMD_tabmove:
7574 tab_number = LAST_TAB_NR;
7575 break;
7576 default:
7577 tab_number = tabpage_index(curtab);
7578 }
7579 }
7580
7581theend:
7582 return tab_number;
7583}
7584
7585/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007586 * ":tabclose": close current tab page, unless it is the last one.
7587 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 */
7589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007590ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007592 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007593 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007594
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007595# ifdef FEAT_CMDWIN
7596 if (cmdwin_type != 0)
7597 cmdwin_result = K_IGNORE;
7598 else
7599# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007600 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007601 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007604 tab_number = get_tabpage_arg(eap);
7605 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007606 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007607 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007608 if (tp == NULL)
7609 {
7610 beep_flush();
7611 return;
7612 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007613 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007614 {
7615 tabpage_close_other(tp, eap->forceit);
7616 return;
7617 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007618 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007619 tabpage_close(eap->forceit);
7620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007622}
7623
7624/*
7625 * ":tabonly": close all tab pages except the current one
7626 */
7627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007628ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007629{
7630 tabpage_T *tp;
7631 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007632 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007633
7634# ifdef FEAT_CMDWIN
7635 if (cmdwin_type != 0)
7636 cmdwin_result = K_IGNORE;
7637 else
7638# endif
7639 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007640 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007641 else
7642 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007643 tab_number = get_tabpage_arg(eap);
7644 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007645 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007646 goto_tabpage(tab_number);
7647 /* Repeat this up to a 1000 times, because autocommands may
7648 * mess up the lists. */
7649 for (done = 0; done < 1000; ++done)
7650 {
7651 FOR_ALL_TABPAGES(tp)
7652 if (tp->tp_topframe != topframe)
7653 {
7654 tabpage_close_other(tp, eap->forceit);
7655 /* if we failed to close it quit */
7656 if (valid_tabpage(tp))
7657 done = 1000;
7658 /* start over, "tp" is now invalid */
7659 break;
7660 }
7661 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007663 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007664 }
7665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
7668/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007669 * Close the current tab page.
7670 */
7671 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007672tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007673{
7674 /* First close all the windows but the current one. If that worked then
7675 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007676 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007677 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007678 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007679 ex_win_close(forceit, curwin, NULL);
7680# ifdef FEAT_GUI
7681 need_mouse_correct = TRUE;
7682# endif
7683}
7684
7685/*
7686 * Close tab page "tp", which is not the current tab page.
7687 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007688 * Also takes care of the tab pages line disappearing when closing the
7689 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007690 */
7691 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007692tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007693{
7694 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007695 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007696 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007697
7698 /* Limit to 1000 windows, autocommands may add a window while we close
7699 * one. OK, so I'm paranoid... */
7700 while (++done < 1000)
7701 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007702 wp = tp->tp_firstwin;
7703 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007704
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007705 /* Autocommands may delete the tab page under our fingers and we may
7706 * fail to close a window with a modified buffer. */
7707 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007708 break;
7709 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007710
Bram Moolenaar29323592016-07-24 22:04:11 +02007711 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007712
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007713 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007714 if (h != tabline_height())
7715 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007716}
7717
7718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 * ":only".
7720 */
7721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007722ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007724 win_T *wp;
7725 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726# ifdef FEAT_GUI
7727 need_mouse_correct = TRUE;
7728# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007729 if (eap->addr_count > 0)
7730 {
7731 wnr = eap->line2;
7732 for (wp = firstwin; --wnr > 0; )
7733 {
7734 if (wp->w_next == NULL)
7735 break;
7736 else
7737 wp = wp->w_next;
7738 }
7739 win_goto(wp);
7740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 close_others(TRUE, eap->forceit);
7742}
7743
7744/*
7745 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007746 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007749ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
7751 if (eap->addr_count == 0)
7752 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007753 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007757ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007759 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007760 if (!eap->skip)
7761 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007762#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007763 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007764#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007765 if (eap->addr_count == 0)
7766 win_close(curwin, FALSE); /* don't free buffer */
7767 else
7768 {
7769 int winnr = 0;
7770 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007771
Bram Moolenaar2256c992016-11-15 21:17:07 +01007772 FOR_ALL_WINDOWS(win)
7773 {
7774 winnr++;
7775 if (winnr == eap->line2)
7776 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007777 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007778 if (win == NULL)
7779 win = lastwin;
7780 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783}
7784
7785/*
7786 * ":stop" and ":suspend": Suspend Vim.
7787 */
7788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007789ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 /*
7792 * Disallow suspending for "rvim".
7793 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007794 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {
7796 if (!eap->forceit)
7797 autowrite_all();
7798 windgoto((int)Rows - 1, 0);
7799 out_char('\n');
7800 out_flush();
7801 stoptermcap();
7802 out_flush(); /* needed for SUN to restore xterm buffer */
7803#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007804 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805#endif
7806 ui_suspend(); /* call machine specific function */
7807#ifdef FEAT_TITLE
7808 maketitle();
7809 resettitle(); /* force updating the title */
7810#endif
7811 starttermcap();
7812 scroll_start(); /* scroll screen before redrawing */
7813 redraw_later_clear();
7814 shell_resized(); /* may have resized window */
7815 }
7816}
7817
7818/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007819 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 */
7821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007822ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824#ifdef FEAT_CMDWIN
7825 if (cmdwin_type != 0)
7826 {
7827 cmdwin_result = Ctrl_C;
7828 return;
7829 }
7830#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007831 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007832 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007833 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007834 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007835 return;
7836 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007837
7838 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007839 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
7841 /*
7842 * if more files or windows we won't exit
7843 */
7844 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7845 exiting = TRUE;
7846 if ( ((eap->cmdidx == CMD_wq
7847 || curbufIsChanged())
7848 && do_write(eap) == FAIL)
7849 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007850 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {
7852 not_exiting();
7853 }
7854 else
7855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007858 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859# ifdef FEAT_GUI
7860 need_mouse_correct = TRUE;
7861# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007862 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007863 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865}
7866
7867/*
7868 * ":print", ":list", ":number".
7869 */
7870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007873 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007874 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007877 for ( ;!got_int; ui_breakcheck())
7878 {
7879 print_line(eap->line1,
7880 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7881 || (eap->flags & EXFLAG_NR)),
7882 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7883 if (++eap->line1 > eap->line2)
7884 break;
7885 out_flush(); /* show one line at a time */
7886 }
7887 setpcmark();
7888 /* put cursor at last line */
7889 curwin->w_cursor.lnum = eap->line2;
7890 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894}
7895
7896#ifdef FEAT_BYTEOFF
7897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007898ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 goto_byte(eap->line2);
7901}
7902#endif
7903
7904/*
7905 * ":shell".
7906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007908ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 do_shell(NULL, 0);
7911}
7912
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007913#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007915static int drop_busy = FALSE;
7916static int drop_filec;
7917static char_u **drop_filev = NULL;
7918static int drop_split;
7919static void (*drop_callback)(void *);
7920static void *drop_cookie;
7921
7922 static void
7923handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925 exarg_T ea;
7926 int save_msg_scroll = msg_scroll;
7927
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007928 // Setting the argument list may cause screen updates and being called
7929 // recursively. Avoid that by setting drop_busy.
7930 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932 /* Check whether the current buffer is changed. If so, we will need
7933 * to split the current window or data could be lost.
7934 * We don't need to check if the 'hidden' option is set, as in this
7935 * case the buffer won't be lost.
7936 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007937 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {
7939 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007940 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 --emsg_off;
7942 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007943 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 if (win_split(0, 0) == FAIL)
7946 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007947 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
7949 /* When splitting the window, create a new alist. Otherwise the
7950 * existing one is overwritten. */
7951 alist_unlink(curwin->w_alist);
7952 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 }
7954
7955 /*
7956 * Set up the new argument list.
7957 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007958 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959
7960 /*
7961 * Move to the first file.
7962 */
7963 /* Fake up a minimal "next" command for do_argfile() */
7964 vim_memset(&ea, 0, sizeof(ea));
7965 ea.cmd = (char_u *)"next";
7966 do_argfile(&ea, 0);
7967
7968 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7969 * mode that is not needed here. */
7970 need_start_insertmode = FALSE;
7971
7972 /* Restore msg_scroll, otherwise a following command may cause scrolling
7973 * unexpectedly. The screen will be redrawn by the caller, thus
7974 * msg_scroll being set by displaying a message is irrelevant. */
7975 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007976
7977 if (drop_callback != NULL)
7978 drop_callback(drop_cookie);
7979
7980 drop_filev = NULL;
7981 drop_busy = FALSE;
7982}
7983
7984/*
7985 * Handle a file drop. The code is here because a drop is *nearly* like an
7986 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01007987 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007988 * code is very similar to :args and hence needs access to a lot of the static
7989 * functions in this file.
7990 *
7991 * The "filev" list must have been allocated using alloc(), as should each item
7992 * in the list. This function takes over responsibility for freeing the "filev"
7993 * list.
7994 */
7995 void
7996handle_drop(
7997 int filec, // the number of files dropped
7998 char_u **filev, // the list of files dropped
7999 int split, // force splitting the window
8000 void (*callback)(void *), // to be called after setting the argument
8001 // list
8002 void *cookie) // argument for "callback" (allocated)
8003{
8004 // Cannot handle recursive drops, finish the pending one.
8005 if (drop_busy)
8006 {
8007 FreeWild(filec, filev);
8008 vim_free(cookie);
8009 return;
8010 }
8011
8012 // When calling handle_drop() more than once in a row we only use the last
8013 // one.
8014 if (drop_filev != NULL)
8015 {
8016 FreeWild(drop_filec, drop_filev);
8017 vim_free(drop_cookie);
8018 }
8019
8020 drop_filec = filec;
8021 drop_filev = filev;
8022 drop_split = split;
8023 drop_callback = callback;
8024 drop_cookie = cookie;
8025
8026 // Postpone this when:
8027 // - editing the command line
8028 // - not possible to change the current buffer
8029 // - updating the screen
8030 // As it may change buffers and window structures that are in use and cause
8031 // freed memory to be used.
8032 if (text_locked() || curbuf_locked() || updating_screen)
8033 return;
8034
8035 handle_drop_internal();
8036}
8037
8038/*
8039 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8040 * reset: Handle a postponed drop.
8041 */
8042 void
8043handle_any_postponed_drop(void)
8044{
8045 if (!drop_busy && drop_filev != NULL
8046 && !text_locked() && !curbuf_locked() && !updating_screen)
8047 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048}
8049#endif
8050
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051/*
8052 * Clear an argument list: free all file names and reset it to zero entries.
8053 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056{
8057 while (--al->al_ga.ga_len >= 0)
8058 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8059 ga_clear(&al->al_ga);
8060}
8061
8062/*
8063 * Init an argument list.
8064 */
8065 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008066alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067{
8068 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8069}
8070
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071/*
8072 * Remove a reference from an argument list.
8073 * Ignored when the argument list is the global one.
8074 * If the argument list is no longer used by any window, free it.
8075 */
8076 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008077alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079 if (al != &global_alist && --al->al_refcount <= 0)
8080 {
8081 alist_clear(al);
8082 vim_free(al);
8083 }
8084}
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086/*
8087 * Create a new argument list and use it for the current window.
8088 */
8089 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8093 if (curwin->w_alist == NULL)
8094 {
8095 curwin->w_alist = &global_alist;
8096 ++global_alist.al_refcount;
8097 }
8098 else
8099 {
8100 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008101 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 alist_init(curwin->w_alist);
8103 }
8104}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008106#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107/*
8108 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008109 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8110 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 */
8112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008113alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008116 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 char_u **new_arg_files;
8118 int new_arg_file_count;
8119 char_u *save_p_su = p_su;
8120 int i;
8121
8122 /* Don't use 'suffixes' here. This should work like the shell did the
8123 * expansion. Also, the vimrc file isn't read yet, thus the user
8124 * can't set the options. */
8125 p_su = empty_option;
8126 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8127 if (old_arg_files != NULL)
8128 {
8129 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008130 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8131 old_arg_count = GARGCOUNT;
8132 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008134 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 && new_arg_file_count > 0)
8136 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008137 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8138 TRUE, fnum_list, fnum_len);
8139 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142 p_su = save_p_su;
8143}
8144#endif
8145
8146/*
8147 * Set the argument list for the current window.
8148 * Takes over the allocated files[] and the allocated fnames in it.
8149 */
8150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008151alist_set(
8152 alist_T *al,
8153 int count,
8154 char_u **files,
8155 int use_curbuf,
8156 int *fnum_list,
8157 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008160 static int recursive = 0;
8161
8162 if (recursive)
8163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008164 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008165 return;
8166 }
8167 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169 alist_clear(al);
8170 if (ga_grow(&al->al_ga, count) == OK)
8171 {
8172 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008173 {
8174 if (got_int)
8175 {
8176 /* When adding many buffers this can take a long time. Allow
8177 * interrupting here. */
8178 while (i < count)
8179 vim_free(files[i++]);
8180 break;
8181 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008182
8183 /* May set buffer name of a buffer previously used for the
8184 * argument list, so that it's re-used by alist_add. */
8185 if (fnum_list != NULL && i < fnum_len)
8186 buf_set_name(fnum_list[i], files[i]);
8187
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008189 ui_breakcheck();
8190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 vim_free(files);
8192 }
8193 else
8194 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008197
8198 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199}
8200
8201/*
8202 * Add file "fname" to argument list "al".
8203 * "fname" must have been allocated and "al" must have been checked for room.
8204 */
8205 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008206alist_add(
8207 alist_T *al,
8208 char_u *fname,
8209 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211 if (fname == NULL) /* don't add NULL file names */
8212 return;
8213#ifdef BACKSLASH_IN_FILENAME
8214 slash_adjust(fname);
8215#endif
8216 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8217 if (set_fnum > 0)
8218 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8219 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8220 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221}
8222
8223#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8224/*
8225 * Adjust slashes in file names. Called after 'shellslash' was set.
8226 */
8227 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008232 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
8234 for (i = 0; i < GARGCOUNT; ++i)
8235 if (GARGLIST[i].ae_fname != NULL)
8236 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008237 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 if (wp->w_alist != &global_alist)
8239 for (i = 0; i < WARGCOUNT(wp); ++i)
8240 if (WARGLIST(wp)[i].ae_fname != NULL)
8241 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242}
8243#endif
8244
8245/*
8246 * ":preserve".
8247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008249ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008251 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 ml_preserve(curbuf, TRUE);
8253}
8254
8255/*
8256 * ":recover".
8257 */
8258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8262 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008263 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8264 | CCGD_MULTWIN
8265 | (eap->forceit ? CCGD_FORCEIT : 0)
8266 | CCGD_EXCMD)
8267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 && (*eap->arg == NUL
8269 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8270 ml_recover();
8271 recoverymode = FALSE;
8272}
8273
8274/*
8275 * Command modifier used in a wrong way.
8276 */
8277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008278ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
8280 eap->errmsg = e_invcmd;
8281}
8282
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283/*
8284 * :sview [+command] file split window with new file, read-only
8285 * :split [[+command] file] split window with current or new file
8286 * :vsplit [[+command] file] split window vertically with current or new file
8287 * :new [[+command] file] split window with no or new file
8288 * :vnew [[+command] file] split vertically window with no or new file
8289 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008290 *
8291 * :tabedit open new Tab page with empty window
8292 * :tabedit [+command] file open new Tab page and edit "file"
8293 * :tabnew [[+command] file] just like :tabedit
8294 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 */
8296 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008297ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008299 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008300#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008302#endif
8303#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008305#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008306 int use_tab = eap->cmdidx == CMD_tabedit
8307 || eap->cmdidx == CMD_tabfind
8308 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
Bram Moolenaar4033c552017-09-16 20:54:51 +02008310#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313
Bram Moolenaar4033c552017-09-16 20:54:51 +02008314#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008316 * quickfix windows. But it's OK when doing ":tab split". */
8317 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
8319 if (eap->cmdidx == CMD_split)
8320 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (eap->cmdidx == CMD_vsplit)
8322 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaar4033c552017-09-16 20:54:51 +02008326#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008327 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
8329 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8330 FNAME_MESS, TRUE, curbuf->b_ffname);
8331 if (fname == NULL)
8332 goto theend;
8333 eap->arg = fname;
8334 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008335# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008336 else
8337# endif
8338#endif
8339#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 && eap->cmdidx != CMD_new)
8343 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008344 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008345# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008346 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008347# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008348 au_has_group((char_u *)"FileExplorer"))
8349 {
8350 /* No browsing supported but we do have the file explorer:
8351 * Edit the directory. */
8352 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8353 eap->arg = (char_u *)".";
8354 }
8355 else
8356 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008357 fname = do_browse(0, (char_u *)(use_tab
8358 ? _("Edit File in new tab page")
8359 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008361 if (fname == NULL)
8362 goto theend;
8363 eap->arg = fname;
8364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 }
8366 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008369 /*
8370 * Either open new tab page or split the window.
8371 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008372 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008373 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008374 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8375 : eap->addr_count == 0 ? 0
8376 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008377 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008378 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008379
8380 /* set the alternate buffer for the window we came from */
8381 if (curwin != old_curwin
8382 && win_valid(old_curwin)
8383 && old_curwin->w_buffer != curbuf
8384 && !cmdmod.keepalt)
8385 old_curwin->w_alt_fnum = curbuf->b_fnum;
8386 }
8387 }
8388 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 /* Reset 'scrollbind' when editing another file, but keep it when
8392 * doing ":split" without arguments. */
8393 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008394# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008398 {
8399 RESET_BINDING(curwin);
8400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 else
8402 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 do_exedit(eap, old_curwin);
8404 }
8405
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008406# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008410# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411theend:
8412 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008415
8416/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008417 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008418 */
8419 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008420tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008421{
8422 exarg_T ea;
8423
8424 vim_memset(&ea, 0, sizeof(ea));
8425 ea.cmdidx = CMD_tabnew;
8426 ea.cmd = (char_u *)"tabn";
8427 ea.arg = (char_u *)"";
8428 ex_splitview(&ea);
8429}
8430
8431/*
8432 * :tabnext command
8433 */
8434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008435ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008437 int tab_number;
8438
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008439 switch (eap->cmdidx)
8440 {
8441 case CMD_tabfirst:
8442 case CMD_tabrewind:
8443 goto_tabpage(1);
8444 break;
8445 case CMD_tablast:
8446 goto_tabpage(9999);
8447 break;
8448 case CMD_tabprevious:
8449 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008450 if (eap->arg && *eap->arg != NUL)
8451 {
8452 char_u *p = eap->arg;
8453 char_u *p_save = p;
8454
8455 tab_number = getdigits(&p);
8456 if (p == p_save || *p_save == '-' || *p != NUL
8457 || tab_number == 0)
8458 {
8459 /* No numbers as argument. */
8460 eap->errmsg = e_invarg;
8461 return;
8462 }
8463 }
8464 else
8465 {
8466 if (eap->addr_count == 0)
8467 tab_number = 1;
8468 else
8469 {
8470 tab_number = eap->line2;
8471 if (tab_number < 1)
8472 {
8473 eap->errmsg = e_invrange;
8474 return;
8475 }
8476 }
8477 }
8478 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008479 break;
8480 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008481 tab_number = get_tabpage_arg(eap);
8482 if (eap->errmsg == NULL)
8483 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008484 break;
8485 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008486}
8487
8488/*
8489 * :tabmove command
8490 */
8491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008492ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008493{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008494 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008495
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008496 tab_number = get_tabpage_arg(eap);
8497 if (eap->errmsg == NULL)
8498 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008499}
8500
8501/*
8502 * :tabs command: List tabs and their contents.
8503 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008505ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008506{
8507 tabpage_T *tp;
8508 win_T *wp;
8509 int tabcount = 1;
8510
8511 msg_start();
8512 msg_scroll = TRUE;
8513 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8514 {
8515 msg_putchar('\n');
8516 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008517 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008518 out_flush(); /* output one line at a time */
8519 ui_breakcheck();
8520
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008521 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008522 wp = firstwin;
8523 else
8524 wp = tp->tp_firstwin;
8525 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8526 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008527 msg_putchar('\n');
8528 msg_putchar(wp == curwin ? '>' : ' ');
8529 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008530 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8531 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008532 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008533 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008534 else
8535 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8536 IObuff, IOSIZE, TRUE);
8537 msg_outtrans(IObuff);
8538 out_flush(); /* output one line at a time */
8539 ui_breakcheck();
8540 }
8541 }
8542}
8543
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544/*
8545 * ":mode": Set screen mode.
8546 * If no argument given, just get the screen size and redraw.
8547 */
8548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008549ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 if (*eap->arg == NUL)
8552 shell_resized();
8553 else
8554 mch_screenmode(eap->arg);
8555}
8556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557/*
8558 * ":resize".
8559 * set, increment or decrement current window height
8560 */
8561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008562ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 int n;
8565 win_T *wp = curwin;
8566
8567 if (eap->addr_count > 0)
8568 {
8569 n = eap->line2;
8570 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8571 ;
8572 }
8573
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008574# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 if (cmdmod.split & WSP_VERT)
8579 {
8580 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008581 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8583 n = 9999;
8584 win_setwidth_win((int)n, wp);
8585 }
8586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 {
8588 if (*eap->arg == '-' || *eap->arg == '+')
8589 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008590 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 n = 9999;
8592 win_setheight_win((int)n, wp);
8593 }
8594}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596/*
8597 * ":find [+command] <file>" command.
8598 */
8599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008600ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602#ifdef FEAT_SEARCHPATH
8603 char_u *fname;
8604 int count;
8605
8606 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8607 TRUE, curbuf->b_ffname);
8608 if (eap->addr_count > 0)
8609 {
8610 /* Repeat finding the file "count" times. This matters when it
8611 * appears several times in the path. */
8612 count = eap->line2;
8613 while (fname != NULL && --count > 0)
8614 {
8615 vim_free(fname);
8616 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8617 FALSE, curbuf->b_ffname);
8618 }
8619 }
8620
8621 if (fname != NULL)
8622 {
8623 eap->arg = fname;
8624#endif
8625 do_exedit(eap, NULL);
8626#ifdef FEAT_SEARCHPATH
8627 vim_free(fname);
8628 }
8629#endif
8630}
8631
8632/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008633 * ":open" simulation: for now just work like ":visual".
8634 */
8635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008636ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008637{
8638 regmatch_T regmatch;
8639 char_u *p;
8640
8641 curwin->w_cursor.lnum = eap->line2;
8642 beginline(BL_SOL | BL_FIX);
8643 if (*eap->arg == '/')
8644 {
8645 /* ":open /pattern/": put cursor in column found with pattern */
8646 ++eap->arg;
8647 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8648 *p = NUL;
8649 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8650 if (regmatch.regprog != NULL)
8651 {
8652 regmatch.rm_ic = p_ic;
8653 p = ml_get_curline();
8654 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008655 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008656 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008657 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008658 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008659 }
8660 /* Move to the NUL, ignore any other arguments. */
8661 eap->arg += STRLEN(eap->arg);
8662 }
8663 check_cursor();
8664
8665 eap->cmdidx = CMD_visual;
8666 do_exedit(eap, NULL);
8667}
8668
8669/*
8670 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 */
8672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008673ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675 do_exedit(eap, NULL);
8676}
8677
8678/*
8679 * ":edit <file>" command and alikes.
8680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008682do_exedit(
8683 exarg_T *eap,
8684 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008688 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
8690 /*
8691 * ":vi" command ends Ex mode.
8692 */
8693 if (exmode_active && (eap->cmdidx == CMD_visual
8694 || eap->cmdidx == CMD_view))
8695 {
8696 exmode_active = FALSE;
8697 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008698 {
8699 /* Special case: ":global/pat/visual\NLvi-commands" */
8700 if (global_busy)
8701 {
8702 int rd = RedrawingDisabled;
8703 int nwr = no_wait_return;
8704 int ms = msg_scroll;
8705#ifdef FEAT_GUI
8706 int he = hold_gui_events;
8707#endif
8708
8709 if (eap->nextcmd != NULL)
8710 {
8711 stuffReadbuff(eap->nextcmd);
8712 eap->nextcmd = NULL;
8713 }
8714
8715 if (exmode_was != EXMODE_VIM)
8716 settmode(TMODE_RAW);
8717 RedrawingDisabled = 0;
8718 no_wait_return = 0;
8719 need_wait_return = FALSE;
8720 msg_scroll = 0;
8721#ifdef FEAT_GUI
8722 hold_gui_events = 0;
8723#endif
8724 must_redraw = CLEAR;
8725
8726 main_loop(FALSE, TRUE);
8727
8728 RedrawingDisabled = rd;
8729 no_wait_return = nwr;
8730 msg_scroll = ms;
8731#ifdef FEAT_GUI
8732 hold_gui_events = he;
8733#endif
8734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 }
8738
8739 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008740 || eap->cmdidx == CMD_tabnew
8741 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008742 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008744 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 setpcmark();
8746 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008747 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8748 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008750 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 || *eap->arg != NUL
8752#ifdef FEAT_BROWSE
8753 || cmdmod.browse
8754#endif
8755 )
8756 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008757 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8758 * can bring us here, others are stopped earlier. */
8759 if (*eap->arg != NUL && curbuf_locked())
8760 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 n = readonlymode;
8763 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8764 readonlymode = TRUE;
8765 else if (eap->cmdidx == CMD_enew)
8766 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8767 empty buffer */
8768 setpcmark();
8769 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8770 NULL, eap,
8771 /* ":edit" goes to first line if Vi compatible */
8772 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8773 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8774 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008775 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008777 /* after a split we can use an existing buffer */
8778 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008780 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {
8782 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 if (old_curwin != NULL)
8784 {
8785 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008786 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008788#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008789 cleanup_T cs;
8790
8791 /* Reset the error/interrupt/exception state here so that
8792 * aborting() returns FALSE when closing a window. */
8793 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008794#endif
8795#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008797#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008798 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008799
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008800#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008801 /* Restore the error/interrupt/exception state if not
8802 * discarded by a new aborting error, interrupt, or
8803 * uncaught exception. */
8804 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 }
8807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 }
8809 else if (readonlymode && curbuf->b_nwindows == 1)
8810 {
8811 /* When editing an already visited buffer, 'readonly' won't be set
8812 * but the previous value is kept. With ":view" and ":sview" we
8813 * want the file to be readonly, except when another window is
8814 * editing the same buffer. */
8815 curbuf->b_p_ro = TRUE;
8816 }
8817 readonlymode = n;
8818 }
8819 else
8820 {
8821 if (eap->do_ecmd_cmd != NULL)
8822 do_cmdline_cmd(eap->do_ecmd_cmd);
8823#ifdef FEAT_TITLE
8824 n = curwin->w_arg_idx_invalid;
8825#endif
8826 check_arg_idx(curwin);
8827#ifdef FEAT_TITLE
8828 if (n != curwin->w_arg_idx_invalid)
8829 maketitle();
8830#endif
8831 }
8832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 /*
8834 * if ":split file" worked, set alternate file name in old window to new
8835 * file
8836 */
8837 if (old_curwin != NULL
8838 && *eap->arg != NUL
8839 && curwin != old_curwin
8840 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008841 && old_curwin->w_buffer != curbuf
8842 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
8845 ex_no_reprint = TRUE;
8846}
8847
8848#ifndef FEAT_GUI
8849/*
8850 * ":gui" and ":gvim" when there is no GUI.
8851 */
8852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008853ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855 eap->errmsg = e_nogvim;
8856}
8857#endif
8858
8859#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008861ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 gui_make_tearoff(eap->arg);
8864}
8865#endif
8866
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008867#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8868 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008870ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008872# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8873 if (gui.in_use)
8874 gui_make_popup(eap->arg, eap->forceit);
8875# ifdef FEAT_TERM_POPUP_MENU
8876 else
8877# endif
8878# endif
8879# ifdef FEAT_TERM_POPUP_MENU
8880 pum_make_popup(eap->arg, eap->forceit);
8881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882}
8883#endif
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008886ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008889 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008891 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892}
8893
8894/*
8895 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8896 * offset.
8897 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008900ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008903 win_T *save_curwin = curwin;
8904 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 long topline;
8906 long y;
8907 linenr_T old_linenr = curwin->w_cursor.lnum;
8908
8909 setpcmark();
8910
8911 /*
8912 * determine max topline
8913 */
8914 if (curwin->w_p_scb)
8915 {
8916 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008917 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
8919 if (wp->w_p_scb && wp->w_buffer)
8920 {
8921 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8922 if (topline > y)
8923 topline = y;
8924 }
8925 }
8926 if (topline < 1)
8927 topline = 1;
8928 }
8929 else
8930 {
8931 topline = 1;
8932 }
8933
8934
8935 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008936 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008938 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 {
8940 if (curwin->w_p_scb)
8941 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008942 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 y = topline - curwin->w_topline;
8944 if (y > 0)
8945 scrollup(y, TRUE);
8946 else
8947 scrolldown(-y, TRUE);
8948 curwin->w_scbind_pos = topline;
8949 redraw_later(VALID);
8950 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
8953 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008954 curwin = save_curwin;
8955 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 if (curwin->w_p_scb)
8957 {
8958 did_syncbind = TRUE;
8959 checkpcmark();
8960 if (old_linenr != curwin->w_cursor.lnum)
8961 {
8962 char_u ctrl_o[2];
8963
8964 ctrl_o[0] = Ctrl_O;
8965 ctrl_o[1] = 0;
8966 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8967 }
8968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969}
8970
8971
8972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008973ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008975 int i;
8976 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8977 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
8979 if (eap->usefilter) /* :r!cmd */
8980 do_bang(1, eap, FALSE, FALSE, TRUE);
8981 else
8982 {
8983 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8984 return;
8985
8986#ifdef FEAT_BROWSE
8987 if (cmdmod.browse)
8988 {
8989 char_u *browseFile;
8990
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008991 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 NULL, NULL, NULL, curbuf);
8993 if (browseFile != NULL)
8994 {
8995 i = readfile(browseFile, NULL,
8996 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8997 vim_free(browseFile);
8998 }
8999 else
9000 i = OK;
9001 }
9002 else
9003#endif
9004 if (*eap->arg == NUL)
9005 {
9006 if (check_fname() == FAIL) /* check for no file name */
9007 return;
9008 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9009 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9010 }
9011 else
9012 {
9013 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9014 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9015 i = readfile(eap->arg, NULL,
9016 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9017
9018 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009019 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009021#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 if (!aborting())
9023#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009024 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 }
9026 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009027 {
9028 if (empty && exmode_active)
9029 {
9030 /* Delete the empty line that remains. Historically ex does
9031 * this but vi doesn't. */
9032 if (eap->line2 == 0)
9033 lnum = curbuf->b_ml.ml_line_count;
9034 else
9035 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009036 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009037 {
9038 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009039 if (curwin->w_cursor.lnum > 1
9040 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009041 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009042 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009043 }
9044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 }
9048}
9049
Bram Moolenaarea408852005-06-25 22:49:46 +00009050static char_u *prev_dir = NULL;
9051
9052#if defined(EXITFREE) || defined(PROTO)
9053 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009054free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009055{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009056 VIM_CLEAR(prev_dir);
9057 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009058}
9059#endif
9060
Bram Moolenaarf4258302013-06-02 18:20:17 +02009061/*
9062 * Deal with the side effects of changing the current directory.
9063 * When "local" is TRUE then this was after an ":lcd" command.
9064 */
9065 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009066post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009067{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009068 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009069 if (local)
9070 {
9071 /* If still in global directory, need to remember current
9072 * directory as global directory. */
9073 if (globaldir == NULL && prev_dir != NULL)
9074 globaldir = vim_strsave(prev_dir);
9075 /* Remember this local directory for the window. */
9076 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9077 curwin->w_localdir = vim_strsave(NameBuff);
9078 }
9079 else
9080 {
9081 /* We are now in the global directory, no need to remember its
9082 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009083 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009084 }
9085
9086 shorten_fnames(TRUE);
9087}
9088
Bram Moolenaarea408852005-06-25 22:49:46 +00009089
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090/*
9091 * ":cd", ":lcd", ":chdir" and ":lchdir".
9092 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009093 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009094ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009096 char_u *new_dir;
9097 char_u *tofree;
9098 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099
9100 new_dir = eap->arg;
9101#if !defined(UNIX) && !defined(VMS)
9102 /* for non-UNIX ":cd" means: print current directory */
9103 if (*new_dir == NUL)
9104 ex_pwd(NULL);
9105 else
9106#endif
9107 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009108 if (allbuf_locked())
9109 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009110 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9111 && !eap->forceit)
9112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009113 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009114 return;
9115 }
9116
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 /* ":cd -": Change to previous directory */
9118 if (STRCMP(new_dir, "-") == 0)
9119 {
9120 if (prev_dir == NULL)
9121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009122 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 return;
9124 }
9125 new_dir = prev_dir;
9126 }
9127
9128 /* Save current directory for next ":cd -" */
9129 tofree = prev_dir;
9130 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9131 prev_dir = vim_strsave(NameBuff);
9132 else
9133 prev_dir = NULL;
9134
9135#if defined(UNIX) || defined(VMS)
9136 /* for UNIX ":cd" means: go to home directory */
9137 if (*new_dir == NUL)
9138 {
9139 /* use NameBuff for home directory name */
9140# ifdef VMS
9141 char_u *p;
9142
9143 p = mch_getenv((char_u *)"SYS$LOGIN");
9144 if (p == NULL || *p == NUL) /* empty is the same as not set */
9145 NameBuff[0] = NUL;
9146 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009147 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148# else
9149 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9150# endif
9151 new_dir = NameBuff;
9152 }
9153#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009154 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009155 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009156 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009157 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 else
9159 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009160 int is_local_chdir = eap->cmdidx == CMD_lcd
9161 || eap->cmdidx == CMD_lchdir;
9162
9163 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
9165 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009166 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009168
9169 if (dir_differs)
9170 apply_autocmds(EVENT_DIRCHANGED,
9171 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9172 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
9174 vim_free(tofree);
9175 }
9176}
9177
9178/*
9179 * ":pwd".
9180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009182ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
9184 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9185 {
9186#ifdef BACKSLASH_IN_FILENAME
9187 slash_adjust(NameBuff);
9188#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009189 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
9191 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009192 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193}
9194
9195/*
9196 * ":=".
9197 */
9198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009199ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009201 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009202 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203}
9204
9205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009206ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009208 int n;
9209 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
9211 if (cursor_valid())
9212 {
9213 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9214 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009215 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009217
9218 len = eap->line2;
9219 switch (*eap->arg)
9220 {
9221 case 'm': break;
9222 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009223 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009224 }
9225 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226}
9227
9228/*
9229 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9230 */
9231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009232do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
9234 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009235 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009238 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009239 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009241 wait_now = msec - done > 1000L ? 1000L : msec - done;
9242#ifdef FEAT_TIMERS
9243 {
9244 long due_time = check_due_timer();
9245
9246 if (due_time > 0 && due_time < wait_now)
9247 wait_now = due_time;
9248 }
9249#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009250#ifdef FEAT_JOB_CHANNEL
9251 if (has_any_channel() && wait_now > 100L)
9252 wait_now = 100L;
9253#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009254 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009255#ifdef FEAT_JOB_CHANNEL
9256 if (has_any_channel())
9257 ui_breakcheck_force(TRUE);
9258 else
9259#endif
9260 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009261#ifdef MESSAGE_QUEUE
9262 /* Process the netbeans and clientserver messages that may have been
9263 * received in the call to ui_breakcheck() when the GUI is in use. This
9264 * may occur when running a test case. */
9265 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009268
9269 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9270 // input buffer, otherwise a following call to input() fails.
9271 if (got_int)
9272 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273}
9274
9275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009276do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
9278 int mode;
9279 char_u *cmdp;
9280
9281 cmdp = eap->cmd;
9282 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9283
9284 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9285 eap->arg, mode, isabbrev))
9286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009287 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009289 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 break;
9291 }
9292}
9293
9294/*
9295 * ":winsize" command (obsolete).
9296 */
9297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009298ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 int w, h;
9301 char_u *arg = eap->arg;
9302 char_u *p;
9303
9304 w = getdigits(&arg);
9305 arg = skipwhite(arg);
9306 p = arg;
9307 h = getdigits(&arg);
9308 if (*p != NUL && *arg == NUL)
9309 set_shellsize(w, h, TRUE);
9310 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009311 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312}
9313
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009315ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316{
9317 int xchar = NUL;
9318 char_u *p;
9319
9320 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9321 {
9322 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9323 if (eap->arg[1] == NUL)
9324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009325 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 return;
9327 }
9328 xchar = eap->arg[1];
9329 p = eap->arg + 2;
9330 }
9331 else
9332 p = eap->arg + 1;
9333
9334 eap->nextcmd = check_nextcmd(p);
9335 p = skipwhite(p);
9336 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009337 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009338 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 {
9340 /* Pass flags on for ":vertical wincmd ]". */
9341 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009342 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9344 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009345 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 }
9347}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
Bram Moolenaar843ee412004-06-30 16:16:41 +00009349#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350/*
9351 * ":winpos".
9352 */
9353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009354ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 int x, y;
9357 char_u *arg = eap->arg;
9358 char_u *p;
9359
9360 if (*arg == NUL)
9361 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009362# if defined(FEAT_GUI) || defined(MSWIN)
9363# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009365# else
9366 if (mch_get_winpos(&x, &y) != FAIL)
9367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 {
9369 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009370 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 }
9372 else
9373# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009374 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 }
9376 else
9377 {
9378 x = getdigits(&arg);
9379 arg = skipwhite(arg);
9380 p = arg;
9381 y = getdigits(&arg);
9382 if (*p == NUL || *arg != NUL)
9383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009384 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 return;
9386 }
9387# ifdef FEAT_GUI
9388 if (gui.in_use)
9389 gui_mch_set_winpos(x, y);
9390 else if (gui.starting)
9391 {
9392 /* Remember the coordinates for when the window is opened. */
9393 gui_win_x = x;
9394 gui_win_y = y;
9395 }
9396# ifdef HAVE_TGETENT
9397 else
9398# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009399# else
9400# ifdef MSWIN
9401 mch_set_winpos(x, y);
9402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403# endif
9404# ifdef HAVE_TGETENT
9405 if (*T_CWP)
9406 term_set_winpos(x, y);
9407# endif
9408 }
9409}
9410#endif
9411
9412/*
9413 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9414 */
9415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009416ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417{
9418 oparg_T oa;
9419
9420 clear_oparg(&oa);
9421 oa.regname = eap->regname;
9422 oa.start.lnum = eap->line1;
9423 oa.end.lnum = eap->line2;
9424 oa.line_count = eap->line2 - eap->line1 + 1;
9425 oa.motion_type = MLINE;
9426#ifdef FEAT_VIRTUALEDIT
9427 virtual_op = FALSE;
9428#endif
9429 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9430 {
9431 setpcmark();
9432 curwin->w_cursor.lnum = eap->line1;
9433 beginline(BL_SOL | BL_FIX);
9434 }
9435
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009436 if (VIsual_active)
9437 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009438
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 switch (eap->cmdidx)
9440 {
9441 case CMD_delete:
9442 oa.op_type = OP_DELETE;
9443 op_delete(&oa);
9444 break;
9445
9446 case CMD_yank:
9447 oa.op_type = OP_YANK;
9448 (void)op_yank(&oa, FALSE, TRUE);
9449 break;
9450
9451 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009452 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009454 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9455#else
9456 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009458 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 oa.op_type = OP_RSHIFT;
9460 else
9461 oa.op_type = OP_LSHIFT;
9462 op_shift(&oa, FALSE, eap->amount);
9463 break;
9464 }
9465#ifdef FEAT_VIRTUALEDIT
9466 virtual_op = MAYBE;
9467#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009468 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471/*
9472 * ":put".
9473 */
9474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009475ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 /* ":0put" works like ":1put!". */
9478 if (eap->line2 == 0)
9479 {
9480 eap->line2 = 1;
9481 eap->forceit = TRUE;
9482 }
9483 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009484 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9485 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486}
9487
9488/*
9489 * Handle ":copy" and ":move".
9490 */
9491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009492ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 long n;
9495
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009496 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (eap->arg == NULL) /* error detected */
9498 {
9499 eap->nextcmd = NULL;
9500 return;
9501 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009502 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503
9504 /*
9505 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9506 */
9507 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009509 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 return;
9511 }
9512
9513 if (eap->cmdidx == CMD_move)
9514 {
9515 if (do_move(eap->line1, eap->line2, n) == FAIL)
9516 return;
9517 }
9518 else
9519 ex_copy(eap->line1, eap->line2, n);
9520 u_clearline();
9521 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009522 ex_may_print(eap);
9523}
9524
9525/*
9526 * Print the current line if flags were given to the Ex command.
9527 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009528 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009529ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009530{
9531 if (eap->flags != 0)
9532 {
9533 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9534 (eap->flags & EXFLAG_LIST));
9535 ex_no_reprint = TRUE;
9536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537}
9538
9539/*
9540 * ":smagic" and ":snomagic".
9541 */
9542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009543ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545 int magic_save = p_magic;
9546
9547 p_magic = (eap->cmdidx == CMD_smagic);
9548 do_sub(eap);
9549 p_magic = magic_save;
9550}
9551
9552/*
9553 * ":join".
9554 */
9555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009556ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558 curwin->w_cursor.lnum = eap->line1;
9559 if (eap->line1 == eap->line2)
9560 {
9561 if (eap->addr_count >= 2) /* :2,2join does nothing */
9562 return;
9563 if (eap->line2 == curbuf->b_ml.ml_line_count)
9564 {
9565 beep_flush();
9566 return;
9567 }
9568 ++eap->line2;
9569 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009570 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009572 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573}
9574
9575/*
9576 * ":[addr]@r" or ":[addr]*r": execute register
9577 */
9578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009579ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009582 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583
9584 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009585 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586
9587#ifdef USE_ON_FLY_SCROLL
9588 dont_scroll = TRUE; /* disallow scrolling here */
9589#endif
9590
9591 /* get the register name. No name means to use the previous one */
9592 c = *eap->arg;
9593 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9594 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009595 /* Put the register in the typeahead buffer with the "silent" flag. */
9596 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9597 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 else
9602 {
9603 int save_efr = exec_from_reg;
9604
9605 exec_from_reg = TRUE;
9606
9607 /*
9608 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009609 * Continue until the stuff buffer is empty and all added characters
9610 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009612 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9614
9615 exec_from_reg = save_efr;
9616 }
9617}
9618
9619/*
9620 * ":!".
9621 */
9622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009623ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9626}
9627
9628/*
9629 * ":undo".
9630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009632ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009634 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009635 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009636 else
9637 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009640#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009642ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009643{
9644 char_u hash[UNDO_HASH_SIZE];
9645
9646 u_compute_hash(hash);
9647 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9648}
9649
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009651ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009652{
9653 char_u hash[UNDO_HASH_SIZE];
9654
9655 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009656 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009657}
9658#endif
9659
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660/*
9661 * ":redo".
9662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009664ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666 u_redo(1);
9667}
9668
9669/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009670 * ":earlier" and ":later".
9671 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009673ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009674{
9675 long count = 0;
9676 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009677 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009678 char_u *p = eap->arg;
9679
9680 if (*p == NUL)
9681 count = 1;
9682 else if (isdigit(*p))
9683 {
9684 count = getdigits(&p);
9685 switch (*p)
9686 {
9687 case 's': ++p; sec = TRUE; break;
9688 case 'm': ++p; sec = TRUE; count *= 60; break;
9689 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009690 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9691 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009692 }
9693 }
9694
9695 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009696 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009697 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009698 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9699 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009700}
9701
9702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 * ":redir": start/stop redirection.
9704 */
9705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009706ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 char *mode;
9709 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009710 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
Bram Moolenaarba768492016-07-08 15:32:54 +02009712#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009713 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009715 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009716 return;
9717 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009718#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009719
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 if (STRICMP(eap->arg, "END") == 0)
9721 close_redir();
9722 else
9723 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009724 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009726 ++arg;
9727 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009729 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 mode = "a";
9731 }
9732 else
9733 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009734 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735
9736 close_redir();
9737
9738 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009739 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (fname == NULL)
9741 return;
9742#ifdef FEAT_BROWSE
9743 if (cmdmod.browse)
9744 {
9745 char_u *browseFile;
9746
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009747 browseFile = do_browse(BROWSE_SAVE,
9748 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009749 fname, NULL, NULL,
9750 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (browseFile == NULL)
9752 return; /* operation cancelled */
9753 vim_free(fname);
9754 fname = browseFile;
9755 eap->forceit = TRUE; /* since dialog already asked */
9756 }
9757#endif
9758
9759 redir_fd = open_exfile(fname, eap->forceit, mode);
9760 vim_free(fname);
9761 }
9762#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009763 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 {
9765 /* redirect to a register a-z (resp. A-Z for appending) */
9766 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009767 ++arg;
9768 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009770 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009771 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009773 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009775 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009776 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009777 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009778 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009780 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009781 if (*arg == '>')
9782 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009783 /* Make register empty when not using @A-@Z and the
9784 * command is valid. */
9785 if (*arg == NUL && !isupper(redir_reg))
9786 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009788 }
9789 if (*arg != NUL)
9790 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009791 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009792 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009793 }
9794 }
9795 else if (*arg == '=' && arg[1] == '>')
9796 {
9797 int append;
9798
9799 /* redirect to a variable */
9800 close_redir();
9801 arg += 2;
9802
9803 if (*arg == '>')
9804 {
9805 ++arg;
9806 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
9808 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009809 append = FALSE;
9810
9811 if (var_redir_start(skipwhite(arg), append) == OK)
9812 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
9814#endif
9815
9816 /* TODO: redirect to a buffer */
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009819 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009821
9822 /* Make sure redirection is not off. Can happen for cmdline completion
9823 * that indirectly invokes a command to catch its output. */
9824 if (redir_fd != NULL
9825#ifdef FEAT_EVAL
9826 || redir_reg || redir_vname
9827#endif
9828 )
9829 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830}
9831
9832/*
9833 * ":redraw": force redraw
9834 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009835 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009836ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837{
9838 int r = RedrawingDisabled;
9839 int p = p_lz;
9840
9841 RedrawingDisabled = 0;
9842 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009843 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009845 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846#ifdef FEAT_TITLE
9847 if (need_maketitle)
9848 maketitle();
9849#endif
9850 RedrawingDisabled = r;
9851 p_lz = p;
9852
9853 /* Reset msg_didout, so that a message that's there is overwritten. */
9854 msg_didout = FALSE;
9855 msg_col = 0;
9856
Bram Moolenaar56667a52013-07-17 11:54:28 +02009857 /* No need to wait after an intentional redraw. */
9858 need_wait_return = FALSE;
9859
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 out_flush();
9861}
9862
9863/*
9864 * ":redrawstatus": force redraw of status line(s)
9865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009867ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 int r = RedrawingDisabled;
9870 int p = p_lz;
9871
9872 RedrawingDisabled = 0;
9873 p_lz = FALSE;
9874 if (eap->forceit)
9875 status_redraw_all();
9876 else
9877 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009878 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 RedrawingDisabled = r;
9880 p_lz = p;
9881 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882}
9883
Bram Moolenaare12bab32019-01-08 22:02:56 +01009884/*
9885 * ":redrawtabline": force redraw of the tabline
9886 */
9887 static void
9888ex_redrawtabline(exarg_T *eap UNUSED)
9889{
9890 int r = RedrawingDisabled;
9891 int p = p_lz;
9892
9893 RedrawingDisabled = 0;
9894 p_lz = FALSE;
9895
9896 draw_tabline();
9897
9898 RedrawingDisabled = r;
9899 p_lz = p;
9900 out_flush();
9901}
9902
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009904close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905{
9906 if (redir_fd != NULL)
9907 {
9908 fclose(redir_fd);
9909 redir_fd = NULL;
9910 }
9911#ifdef FEAT_EVAL
9912 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009913 if (redir_vname)
9914 {
9915 var_redir_stop();
9916 redir_vname = 0;
9917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918#endif
9919}
9920
9921#if defined(FEAT_SESSION) && defined(USE_CRNL)
9922# define MKSESSION_NL
9923static int mksession_nl = FALSE; /* use NL only in put_eol() */
9924#endif
9925
9926/*
9927 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9928 */
9929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009930ex_mkrc(
9931 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933 FILE *fd;
9934 int failed = FALSE;
9935 char_u *fname;
9936#ifdef FEAT_BROWSE
9937 char_u *browseFile = NULL;
9938#endif
9939#ifdef FEAT_SESSION
9940 int view_session = FALSE;
9941 int using_vdir = FALSE; /* using 'viewdir'? */
9942 char_u *viewFile = NULL;
9943 unsigned *flagp;
9944#endif
9945
9946 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9947 {
9948#ifdef FEAT_SESSION
9949 view_session = TRUE;
9950#else
9951 ex_ni(eap);
9952 return;
9953#endif
9954 }
9955
9956#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009957 /* Use the short file name until ":lcd" is used. We also don't use the
9958 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009959 did_lcd = FALSE;
9960
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9962 if (eap->cmdidx == CMD_mkview
9963 && (*eap->arg == NUL
9964 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9965 {
9966 eap->forceit = TRUE;
9967 fname = get_view_file(*eap->arg);
9968 if (fname == NULL)
9969 return;
9970 viewFile = fname;
9971 using_vdir = TRUE;
9972 }
9973 else
9974#endif
9975 if (*eap->arg != NUL)
9976 fname = eap->arg;
9977 else if (eap->cmdidx == CMD_mkvimrc)
9978 fname = (char_u *)VIMRC_FILE;
9979#ifdef FEAT_SESSION
9980 else if (eap->cmdidx == CMD_mksession)
9981 fname = (char_u *)SESSION_FILE;
9982#endif
9983 else
9984 fname = (char_u *)EXRC_FILE;
9985
9986#ifdef FEAT_BROWSE
9987 if (cmdmod.browse)
9988 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009989 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990# ifdef FEAT_SESSION
9991 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9992 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9993# endif
9994 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009995 fname, (char_u *)"vim", NULL,
9996 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 if (browseFile == NULL)
9998 goto theend;
9999 fname = browseFile;
10000 eap->forceit = TRUE; /* since dialog already asked */
10001 }
10002#endif
10003
10004#if defined(FEAT_SESSION) && defined(vim_mkdir)
10005 /* When using 'viewdir' may have to create the directory. */
10006 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010007 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008#endif
10009
10010 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10011 if (fd != NULL)
10012 {
10013#ifdef FEAT_SESSION
10014 if (eap->cmdidx == CMD_mkview)
10015 flagp = &vop_flags;
10016 else
10017 flagp = &ssop_flags;
10018#endif
10019
10020#ifdef MKSESSION_NL
10021 /* "unix" in 'sessionoptions': use NL line separator */
10022 if (view_session && (*flagp & SSOP_UNIX))
10023 mksession_nl = TRUE;
10024#endif
10025
10026 /* Write the version command for :mkvimrc */
10027 if (eap->cmdidx == CMD_mkvimrc)
10028 (void)put_line(fd, "version 6.0");
10029
10030#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010031 if (eap->cmdidx == CMD_mksession)
10032 {
10033 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10034 failed = TRUE;
10035 }
10036
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 if (eap->cmdidx != CMD_mkview)
10038#endif
10039 {
10040 /* Write setting 'compatible' first, because it has side effects.
10041 * For that same reason only do it when needed. */
10042 if (p_cp)
10043 (void)put_line(fd, "if !&cp | set cp | endif");
10044 else
10045 (void)put_line(fd, "if &cp | set nocp | endif");
10046 }
10047
10048#ifdef FEAT_SESSION
10049 if (!view_session
10050 || (eap->cmdidx == CMD_mksession
10051 && (*flagp & SSOP_OPTIONS)))
10052#endif
10053 failed |= (makemap(fd, NULL) == FAIL
10054 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10055
10056#ifdef FEAT_SESSION
10057 if (!failed && view_session)
10058 {
10059 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10060 failed = TRUE;
10061 if (eap->cmdidx == CMD_mksession)
10062 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010063 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064
Bram Moolenaard9462e32011-04-11 21:35:11 +020010065 dirnow = alloc(MAXPATHL);
10066 if (dirnow == NULL)
10067 failed = TRUE;
10068 else
10069 {
10070 /*
10071 * Change to session file's dir.
10072 */
10073 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010075 *dirnow = NUL;
10076 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10077 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010078 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010079 shorten_fnames(TRUE);
10080 }
10081 else if (*dirnow != NUL
10082 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10083 {
10084 if (mch_chdir((char *)globaldir) == 0)
10085 shorten_fnames(TRUE);
10086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaard9462e32011-04-11 21:35:11 +020010088 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
Bram Moolenaard9462e32011-04-11 21:35:11 +020010090 /* restore original dir */
10091 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010093 {
10094 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010095 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010096 shorten_fnames(TRUE);
10097 }
10098 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
10100 }
10101 else
10102 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010103 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10104 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 }
10106 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10107 == FAIL)
10108 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010109# ifdef FEAT_SEARCH_EXTRA
10110 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10111 failed = TRUE;
10112# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010113 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10114 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010115 if (eap->cmdidx == CMD_mksession)
10116 {
10117 if (put_line(fd, "unlet SessionLoad") == FAIL)
10118 failed = TRUE;
10119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 }
10121#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010122 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10123 failed = TRUE;
10124
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 failed |= fclose(fd);
10126
10127 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010128 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10130 else if (eap->cmdidx == CMD_mksession)
10131 {
10132 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010133 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134
Bram Moolenaard9462e32011-04-11 21:35:11 +020010135 tbuf = alloc(MAXPATHL);
10136 if (tbuf != NULL)
10137 {
10138 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10139 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10140 vim_free(tbuf);
10141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 }
10143#endif
10144#ifdef MKSESSION_NL
10145 mksession_nl = FALSE;
10146#endif
10147 }
10148
10149#ifdef FEAT_BROWSE
10150theend:
10151 vim_free(browseFile);
10152#endif
10153#ifdef FEAT_SESSION
10154 vim_free(viewFile);
10155#endif
10156}
10157
Bram Moolenaardf177f62005-02-22 08:39:57 +000010158#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10159 || defined(PROTO)
10160 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010161vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010162{
10163 if (vim_mkdir(name, prot) != 0)
10164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010165 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010166 return FAIL;
10167 }
10168 return OK;
10169}
10170#endif
10171
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172/*
10173 * Open a file for writing for an Ex command, with some checks.
10174 * Return file descriptor, or NULL on failure.
10175 */
10176 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010177open_exfile(
10178 char_u *fname,
10179 int forceit,
10180 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 FILE *fd;
10183
10184#ifdef UNIX
10185 /* with Unix it is possible to open a directory */
10186 if (mch_isdir(fname))
10187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010188 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 return NULL;
10190 }
10191#endif
10192 if (!forceit && *mode != 'a' && vim_fexists(fname))
10193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010194 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 return NULL;
10196 }
10197
10198 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010199 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200
10201 return fd;
10202}
10203
10204/*
10205 * ":mark" and ":k".
10206 */
10207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010208ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 pos_T pos;
10211
10212 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010213 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010215 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 else
10217 {
10218 pos = curwin->w_cursor; /* save curwin->w_cursor */
10219 curwin->w_cursor.lnum = eap->line2;
10220 beginline(BL_WHITE | BL_FIX);
10221 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010222 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10224 }
10225}
10226
10227/*
10228 * Update w_topline, w_leftcol and the cursor position.
10229 */
10230 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010231update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232{
10233 check_cursor(); /* put cursor on valid line */
10234 update_topline();
10235 if (!curwin->w_p_wrap)
10236 validate_cursor();
10237 update_curswant();
10238}
10239
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010241 * Save the current State and go to Normal mode.
10242 * Return TRUE if the typeahead could be saved.
10243 */
10244 int
10245save_current_state(save_state_T *sst)
10246{
10247 sst->save_msg_scroll = msg_scroll;
10248 sst->save_restart_edit = restart_edit;
10249 sst->save_msg_didout = msg_didout;
10250 sst->save_State = State;
10251 sst->save_insertmode = p_im;
10252 sst->save_finish_op = finish_op;
10253 sst->save_opcount = opcount;
10254
10255 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10256 restart_edit = 0; /* don't go to Insert mode */
10257 p_im = FALSE; /* don't use 'insertmode' */
10258
10259 /*
10260 * Save the current typeahead. This is required to allow using ":normal"
10261 * from an event handler and makes sure we don't hang when the argument
10262 * ends with half a command.
10263 */
10264 save_typeahead(&sst->tabuf);
10265 return sst->tabuf.typebuf_valid;
10266}
10267
10268 void
10269restore_current_state(save_state_T *sst)
10270{
10271 /* Restore the previous typeahead. */
10272 restore_typeahead(&sst->tabuf);
10273
10274 msg_scroll = sst->save_msg_scroll;
10275 restart_edit = sst->save_restart_edit;
10276 p_im = sst->save_insertmode;
10277 finish_op = sst->save_finish_op;
10278 opcount = sst->save_opcount;
10279 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10280
10281 /* Restore the state (needed when called from a function executed for
10282 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10283 State = sst->save_State;
10284#ifdef CURSOR_SHAPE
10285 ui_cursor_shape(); /* may show different cursor shape */
10286#endif
10287}
10288
10289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 * ":normal[!] {commands}": Execute normal mode commands.
10291 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010292 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010293ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010295 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 char_u *arg = NULL;
10297 int l;
10298 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010300 if (ex_normal_lock > 0)
10301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010302 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010303 return;
10304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 if (ex_normal_busy >= p_mmd)
10306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010307 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 return;
10309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 /*
10312 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10313 * this for the K_SPECIAL leading byte, otherwise special keys will not
10314 * work.
10315 */
10316 if (has_mbyte)
10317 {
10318 int len = 0;
10319
10320 /* Count the number of characters to be escaped. */
10321 for (p = eap->arg; *p != NUL; ++p)
10322 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010323#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 if (*p == CSI) /* leadbyte CSI */
10325 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +010010326#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010327 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +010010329#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +010010331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 )
10333 len += 2;
10334 }
10335 if (len > 0)
10336 {
10337 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10338 if (arg != NULL)
10339 {
10340 len = 0;
10341 for (p = eap->arg; *p != NUL; ++p)
10342 {
10343 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +010010344#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 if (*p == CSI)
10346 {
10347 arg[len++] = KS_EXTRA;
10348 arg[len++] = (int)KE_CSI;
10349 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010350#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010351 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 {
10353 arg[len++] = *++p;
10354 if (*p == K_SPECIAL)
10355 {
10356 arg[len++] = KS_SPECIAL;
10357 arg[len++] = KE_FILLER;
10358 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010359#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 else if (*p == CSI)
10361 {
10362 arg[len++] = KS_EXTRA;
10363 arg[len++] = (int)KE_CSI;
10364 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 }
10367 arg[len] = NUL;
10368 }
10369 }
10370 }
10371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010373 ++ex_normal_busy;
10374 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 {
10376 /*
10377 * Repeat the :normal command for each line in the range. When no
10378 * range given, execute it just once, without positioning the cursor
10379 * first.
10380 */
10381 do
10382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 if (eap->addr_count != 0)
10384 {
10385 curwin->w_cursor.lnum = eap->line1++;
10386 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010387 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 }
10389
Bram Moolenaar13505972019-01-24 15:04:48 +010010390 exec_normal_cmd(arg != NULL
10391 ? arg
10392 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
10394 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10395 }
10396
10397 /* Might not return to the main loop when in an event handler. */
10398 update_topline_cursor();
10399
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010400 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010402#ifdef FEAT_MOUSE
10403 setmouse();
10404#endif
10405#ifdef CURSOR_SHAPE
10406 ui_cursor_shape(); /* may show different cursor shape */
10407#endif
10408
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410}
10411
10412/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010413 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 */
10415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010416ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010418 if (eap->forceit)
10419 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010420 /* cursor line can be zero on startup */
10421 if (!curwin->w_cursor.lnum)
10422 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010423 coladvance((colnr_T)MAXCOL);
10424 curwin->w_curswant = MAXCOL;
10425 curwin->w_set_curswant = FALSE;
10426 }
10427
Bram Moolenaara40c5002005-01-09 21:16:21 +000010428 /* Ignore the command when already in Insert mode. Inserting an
10429 * expression register that invokes a function can do this. */
10430 if (State & INSERT)
10431 return;
10432
Bram Moolenaar64969662005-12-14 21:59:55 +000010433 if (eap->cmdidx == CMD_startinsert)
10434 restart_edit = 'a';
10435 else if (eap->cmdidx == CMD_startreplace)
10436 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010438 restart_edit = 'V';
10439
10440 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010442 if (eap->cmdidx == CMD_startinsert)
10443 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 curwin->w_curswant = 0; /* avoid MAXCOL */
10445 }
10446}
10447
10448/*
10449 * ":stopinsert"
10450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010452ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453{
10454 restart_edit = 0;
10455 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010456 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010459/*
10460 * Execute normal mode command "cmd".
10461 * "remap" can be REMAP_NONE or REMAP_YES.
10462 */
10463 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010464exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010465{
Bram Moolenaar25281632016-01-21 23:32:32 +010010466 /* Stuff the argument into the typeahead buffer. */
10467 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010468 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010469}
Bram Moolenaar25281632016-01-21 23:32:32 +010010470
Bram Moolenaar25281632016-01-21 23:32:32 +010010471/*
10472 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010473 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010474 */
10475 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010476exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010477{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010478 oparg_T oa;
10479
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010480 clear_oparg(&oa);
10481 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010482 while ((!stuff_empty()
10483 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10484 || (use_vpeekc && vpeekc() != NUL))
10485 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010486 {
10487 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010488#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010489 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010490 && oa.op_type == OP_NOP && oa.regname == NUL
10491 && !VIsual_active)
10492 {
10493 /* If terminal_loop() returns OK we got a key that is handled
10494 * in Normal model. With FAIL we first need to position the
10495 * cursor and the screen needs to be redrawn. */
10496 if (terminal_loop(TRUE) == OK)
10497 normal_cmd(&oa, TRUE);
10498 }
10499 else
10500#endif
10501 /* execute a Normal mode cmd */
10502 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010503 }
10504}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010505
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506#ifdef FEAT_FIND_ID
10507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010508ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10511 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10512 (linenr_T)1, (linenr_T)MAXLNUM);
10513}
10514
Bram Moolenaar4033c552017-09-16 20:54:51 +020010515#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516/*
10517 * ":psearch"
10518 */
10519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010520ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521{
10522 g_do_tagpreview = p_pvh;
10523 ex_findpat(eap);
10524 g_do_tagpreview = 0;
10525}
10526#endif
10527
10528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010529ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530{
10531 int whole = TRUE;
10532 long n;
10533 char_u *p;
10534 int action;
10535
10536 switch (cmdnames[eap->cmdidx].cmd_name[2])
10537 {
10538 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10539 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10540 action = ACTION_GOTO;
10541 else
10542 action = ACTION_SHOW;
10543 break;
10544 case 'i': /* ":ilist" and ":dlist" */
10545 action = ACTION_SHOW_ALL;
10546 break;
10547 case 'u': /* ":ijump" and ":djump" */
10548 action = ACTION_GOTO;
10549 break;
10550 default: /* ":isplit" and ":dsplit" */
10551 action = ACTION_SPLIT;
10552 break;
10553 }
10554
10555 n = 1;
10556 if (vim_isdigit(*eap->arg)) /* get count */
10557 {
10558 n = getdigits(&eap->arg);
10559 eap->arg = skipwhite(eap->arg);
10560 }
10561 if (*eap->arg == '/') /* Match regexp, not just whole words */
10562 {
10563 whole = FALSE;
10564 ++eap->arg;
10565 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10566 if (*p)
10567 {
10568 *p++ = NUL;
10569 p = skipwhite(p);
10570
10571 /* Check for trailing illegal characters */
10572 if (!ends_excmd(*p))
10573 eap->errmsg = e_trailing;
10574 else
10575 eap->nextcmd = check_nextcmd(p);
10576 }
10577 }
10578 if (!eap->skip)
10579 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10580 whole, !eap->forceit,
10581 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10582 n, action, eap->line1, eap->line2);
10583}
10584#endif
10585
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586
Bram Moolenaar4033c552017-09-16 20:54:51 +020010587#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588/*
10589 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10590 */
10591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010592ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010594 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10596}
10597
10598/*
10599 * ":pedit"
10600 */
10601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010602ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 win_T *curwin_save = curwin;
10605
10606 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010607 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010608 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 do_exedit(eap, NULL);
10610 keep_help_flag = FALSE;
10611 if (curwin != curwin_save && win_valid(curwin_save))
10612 {
10613 /* Return cursor to where we were */
10614 validate_cursor();
10615 redraw_later(VALID);
10616 win_enter(curwin_save, TRUE);
10617 }
10618 g_do_tagpreview = 0;
10619}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621
10622/*
10623 * ":stag", ":stselect" and ":stjump".
10624 */
10625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010626ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627{
10628 postponed_split = -1;
10629 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010630 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10632 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010633 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635
10636/*
10637 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10638 */
10639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010640ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641{
10642 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10643}
10644
10645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010646ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647{
10648 int cmd;
10649
10650 switch (name[1])
10651 {
10652 case 'j': cmd = DT_JUMP; /* ":tjump" */
10653 break;
10654 case 's': cmd = DT_SELECT; /* ":tselect" */
10655 break;
10656 case 'p': cmd = DT_PREV; /* ":tprevious" */
10657 break;
10658 case 'N': cmd = DT_PREV; /* ":tNext" */
10659 break;
10660 case 'n': cmd = DT_NEXT; /* ":tnext" */
10661 break;
10662 case 'o': cmd = DT_POP; /* ":pop" */
10663 break;
10664 case 'f': /* ":tfirst" */
10665 case 'r': cmd = DT_FIRST; /* ":trewind" */
10666 break;
10667 case 'l': cmd = DT_LAST; /* ":tlast" */
10668 break;
10669 default: /* ":tag" */
10670#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010671 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010673 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 return;
10675 }
10676#endif
10677 cmd = DT_TAG;
10678 break;
10679 }
10680
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010681 if (name[0] == 'l')
10682 {
10683#ifndef FEAT_QUICKFIX
10684 ex_ni(eap);
10685 return;
10686#else
10687 cmd = DT_LTAG;
10688#endif
10689 }
10690
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10692 eap->forceit, TRUE);
10693}
10694
10695/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010696 * Check "str" for starting with a special cmdline variable.
10697 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10698 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10699 */
10700 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010701find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010702{
10703 int len;
10704 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010705 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010706 "%",
10707#define SPEC_PERC 0
10708 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010709#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010710 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010711#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010712 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010713#define SPEC_CCWORD (SPEC_CWORD + 1)
10714 "<cexpr>", /* expr under cursor */
10715#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010716 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010717#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010718 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010719#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010720 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010721#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010722 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010723#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010724 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010725#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010726 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010727#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010728 "<sflnum>", /* script file line number */
10729#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010730#ifdef FEAT_CLIENTSERVER
10731 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010732# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010733#endif
10734 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010735
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010736 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010737 {
10738 len = (int)STRLEN(spec_str[i]);
10739 if (STRNCMP(src, spec_str[i], len) == 0)
10740 {
10741 *usedlen = len;
10742 return i;
10743 }
10744 }
10745 return -1;
10746}
10747
10748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 * Evaluate cmdline variables.
10750 *
10751 * change '%' to curbuf->b_ffname
10752 * '#' to curwin->w_altfile
10753 * '<cword>' to word under the cursor
10754 * '<cWORD>' to WORD under the cursor
10755 * '<cfile>' to path name under the cursor
10756 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010757 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 * '<afile>' to file name for autocommand
10759 * '<abuf>' to buffer number for autocommand
10760 * '<amatch>' to matching name for autocommand
10761 *
10762 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10763 * "" for error without a message) and NULL is returned.
10764 * Returns an allocated string if a valid match was found.
10765 * Returns NULL if no match was found. "usedlen" then still contains the
10766 * number of characters to skip.
10767 */
10768 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010769eval_vars(
10770 char_u *src, /* pointer into commandline */
10771 char_u *srcstart, /* beginning of valid memory for src */
10772 int *usedlen, /* characters after src that are used */
10773 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010774 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010775 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010776 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777{
10778 int i;
10779 char_u *s;
10780 char_u *result;
10781 char_u *resultbuf = NULL;
10782 int resultlen;
10783 buf_T *buf;
10784 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10785 int spec_idx;
10786#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010787 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 int skip_mod = FALSE;
10789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791
10792 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010793 if (escaped != NULL)
10794 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795
10796 /*
10797 * Check if there is something to do.
10798 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010799 spec_idx = find_cmdline_var(src, usedlen);
10800 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 {
10802 *usedlen = 1;
10803 return NULL;
10804 }
10805
10806 /*
10807 * Skip when preceded with a backslash "\%" and "\#".
10808 * Note: In "\\%" the % is also not recognized!
10809 */
10810 if (src > srcstart && src[-1] == '\\')
10811 {
10812 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010813 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 return NULL;
10815 }
10816
10817 /*
10818 * word or WORD under cursor
10819 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010820 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10821 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010823 resultlen = find_ident_under_cursor(&result,
10824 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10825 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10826 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 if (resultlen == 0)
10828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010829 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 return NULL;
10831 }
10832 }
10833
10834 /*
10835 * '#': Alternate file name
10836 * '%': Current file name
10837 * File name under the cursor
10838 * File name for autocommand
10839 * and following modifiers
10840 */
10841 else
10842 {
10843 switch (spec_idx)
10844 {
10845 case SPEC_PERC: /* '%': current file */
10846 if (curbuf->b_fname == NULL)
10847 {
10848 result = (char_u *)"";
10849 valid = 0; /* Must have ":p:h" to be valid */
10850 }
10851 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010852 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010854#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010855 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010856#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 break;
10859
10860 case SPEC_HASH: /* '#' or "#99": alternate file */
10861 if (src[1] == '#') /* "##": the argument list */
10862 {
10863 result = arg_all();
10864 resultbuf = result;
10865 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010866 if (escaped != NULL)
10867 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868#ifdef FEAT_MODIFY_FNAME
10869 skip_mod = TRUE;
10870#endif
10871 break;
10872 }
10873 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010874 if (*s == '<') /* "#<99" uses v:oldfiles */
10875 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010877 if (s == src + 2 && src[1] == '-')
10878 /* just a minus sign, don't skip over it */
10879 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 *usedlen = (int)(s - src); /* length of what we expand */
10881
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010882 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010884 if (*usedlen < 2)
10885 {
10886 /* Should we give an error message for #<text? */
10887 *usedlen = 1;
10888 return NULL;
10889 }
10890#ifdef FEAT_EVAL
10891 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10892 (long)i);
10893 if (result == NULL)
10894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010895 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010896 return NULL;
10897 }
10898#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010899 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 }
10903 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010904 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010905 if (i == 0 && src[1] == '<' && *usedlen > 1)
10906 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010907 buf = buflist_findnr(i);
10908 if (buf == NULL)
10909 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010910 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010911 return NULL;
10912 }
10913 if (lnump != NULL)
10914 *lnump = ECMD_LAST;
10915 if (buf->b_fname == NULL)
10916 {
10917 result = (char_u *)"";
10918 valid = 0; /* Must have ":p:h" to be valid */
10919 }
10920 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010921 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010922 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010923#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010924 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010925#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010926 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 break;
10929
10930#ifdef FEAT_SEARCHPATH
10931 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010932 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 if (result == NULL)
10934 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010935 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 return NULL;
10937 }
10938 resultbuf = result; /* remember allocated string */
10939 break;
10940#endif
10941
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 case SPEC_AFILE: /* file name for autocommand */
10943 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010944 if (result != NULL && !autocmd_fname_full)
10945 {
10946 /* Still need to turn the fname into a full path. It is
10947 * postponed to avoid a delay when <afile> is not used. */
10948 autocmd_fname_full = TRUE;
10949 result = FullName_save(autocmd_fname, FALSE);
10950 vim_free(autocmd_fname);
10951 autocmd_fname = result;
10952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 if (result == NULL)
10954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010955 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 return NULL;
10957 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010958 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 break;
10960
10961 case SPEC_ABUF: /* buffer number for autocommand */
10962 if (autocmd_bufnr <= 0)
10963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010964 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 return NULL;
10966 }
10967 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10968 result = strbuf;
10969 break;
10970
10971 case SPEC_AMATCH: /* match name for autocommand */
10972 result = autocmd_match;
10973 if (result == NULL)
10974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010975 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 return NULL;
10977 }
10978 break;
10979
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 case SPEC_SFILE: /* file name for ":so" command */
10981 result = sourcing_name;
10982 if (result == NULL)
10983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010984 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 return NULL;
10986 }
10987 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010988
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010989 case SPEC_SLNUM: /* line in file for ":so" command */
10990 if (sourcing_name == NULL || sourcing_lnum == 0)
10991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010992 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010993 return NULL;
10994 }
10995 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10996 result = strbuf;
10997 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010998
10999#ifdef FEAT_EVAL
11000 case SPEC_SFLNUM: /* line in script file */
11001 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011003 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011004 return NULL;
11005 }
11006 sprintf((char *)strbuf, "%ld",
11007 (long)(current_sctx.sc_lnum + sourcing_lnum));
11008 result = strbuf;
11009 break;
11010#endif
11011
11012#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011014 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11015 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 result = strbuf;
11017 break;
11018#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011019
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011020 default:
11021 result = (char_u *)""; /* avoid gcc warning */
11022 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 }
11024
11025 resultlen = (int)STRLEN(result); /* length of new string */
11026 if (src[*usedlen] == '<') /* remove the file name extension */
11027 {
11028 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 resultlen = (int)(s - result);
11031 }
11032#ifdef FEAT_MODIFY_FNAME
11033 else if (!skip_mod)
11034 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011035 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 &resultlen);
11037 if (result == NULL)
11038 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011039 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 return NULL;
11041 }
11042 }
11043#endif
11044 }
11045
11046 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11047 {
11048 if (valid != VALID_HEAD + VALID_PATH)
11049 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011050 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011052 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 result = NULL;
11054 }
11055 else
11056 result = vim_strnsave(result, resultlen);
11057 vim_free(resultbuf);
11058 return result;
11059}
11060
11061/*
11062 * Concatenate all files in the argument list, separated by spaces, and return
11063 * it in one allocated string.
11064 * Spaces and backslashes in the file names are escaped with a backslash.
11065 * Returns NULL when out of memory.
11066 */
11067 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011068arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069{
11070 int len;
11071 int idx;
11072 char_u *retval = NULL;
11073 char_u *p;
11074
11075 /*
11076 * Do this loop two times:
11077 * first time: compute the total length
11078 * second time: concatenate the names
11079 */
11080 for (;;)
11081 {
11082 len = 0;
11083 for (idx = 0; idx < ARGCOUNT; ++idx)
11084 {
11085 p = alist_name(&ARGLIST[idx]);
11086 if (p != NULL)
11087 {
11088 if (len > 0)
11089 {
11090 /* insert a space in between names */
11091 if (retval != NULL)
11092 retval[len] = ' ';
11093 ++len;
11094 }
11095 for ( ; *p != NUL; ++p)
11096 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011097 if (*p == ' '
11098#ifndef BACKSLASH_IN_FILENAME
11099 || *p == '\\'
11100#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011101 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 {
11103 /* insert a backslash */
11104 if (retval != NULL)
11105 retval[len] = '\\';
11106 ++len;
11107 }
11108 if (retval != NULL)
11109 retval[len] = *p;
11110 ++len;
11111 }
11112 }
11113 }
11114
11115 /* second time: break here */
11116 if (retval != NULL)
11117 {
11118 retval[len] = NUL;
11119 break;
11120 }
11121
11122 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011123 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 if (retval == NULL)
11125 break;
11126 }
11127
11128 return retval;
11129}
11130
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131/*
11132 * Expand the <sfile> string in "arg".
11133 *
11134 * Returns an allocated string, or NULL for any error.
11135 */
11136 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011137expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011139 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 int len;
11141 char_u *result;
11142 char_u *newres;
11143 char_u *repl;
11144 int srclen;
11145 char_u *p;
11146
11147 result = vim_strsave(arg);
11148 if (result == NULL)
11149 return NULL;
11150
11151 for (p = result; *p; )
11152 {
11153 if (STRNCMP(p, "<sfile>", 7) != 0)
11154 ++p;
11155 else
11156 {
11157 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011158 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 if (errormsg != NULL)
11160 {
11161 if (*errormsg)
11162 emsg(errormsg);
11163 vim_free(result);
11164 return NULL;
11165 }
11166 if (repl == NULL) /* no match (cannot happen) */
11167 {
11168 p += srclen;
11169 continue;
11170 }
11171 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11172 newres = alloc(len);
11173 if (newres == NULL)
11174 {
11175 vim_free(repl);
11176 vim_free(result);
11177 return NULL;
11178 }
11179 mch_memmove(newres, result, (size_t)(p - result));
11180 STRCPY(newres + (p - result), repl);
11181 len = (int)STRLEN(newres);
11182 STRCAT(newres, p + srclen);
11183 vim_free(repl);
11184 vim_free(result);
11185 result = newres;
11186 p = newres + len; /* continue after the match */
11187 }
11188 }
11189
11190 return result;
11191}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192
11193#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011194static int ses_winsizes(FILE *fd, int restore_size,
11195 win_T *tab_firstwin);
11196static int ses_win_rec(FILE *fd, frame_T *fr);
11197static frame_T *ses_skipframe(frame_T *fr);
11198static int ses_do_frame(frame_T *fr);
11199static int ses_do_win(win_T *wp);
11200static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11201static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011202static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203
11204/*
11205 * Write openfile commands for the current buffers to an .exrc file.
11206 * Return FAIL on error, OK otherwise.
11207 */
11208 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011209makeopens(
11210 FILE *fd,
11211 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212{
11213 buf_T *buf;
11214 int only_save_windows = TRUE;
11215 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 int restore_size = TRUE;
11217 win_T *wp;
11218 char_u *sname;
11219 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011220 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011221 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011222 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011223 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011224 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011225 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226
11227 if (ssop_flags & SSOP_BUFFERS)
11228 only_save_windows = FALSE; /* Save ALL buffers */
11229
11230 /*
11231 * Begin by setting the this_session variable, and then other
11232 * sessionable variables.
11233 */
11234#ifdef FEAT_EVAL
11235 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11236 return FAIL;
11237 if (ssop_flags & SSOP_GLOBALS)
11238 if (store_session_globals(fd) == FAIL)
11239 return FAIL;
11240#endif
11241
11242 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011243 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 */
11245 if (put_line(fd, "silent only") == FAIL)
11246 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011247 if ((ssop_flags & SSOP_TABPAGES)
11248 && put_line(fd, "silent tabonly") == FAIL)
11249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250
11251 /*
11252 * Now a :cd command to the session directory or the current directory
11253 */
11254 if (ssop_flags & SSOP_SESDIR)
11255 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011256 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11257 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 return FAIL;
11259 }
11260 else if (ssop_flags & SSOP_CURDIR)
11261 {
11262 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11263 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011264 || fputs("cd ", fd) < 0
11265 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11266 || put_eol(fd) == FAIL)
11267 {
11268 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 vim_free(sname);
11272 }
11273
11274 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011275 * If there is an empty, unnamed buffer we will wipe it out later.
11276 * Remember the buffer number.
11277 */
11278 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11279 return FAIL;
11280 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11281 return FAIL;
11282 if (put_line(fd, "endif") == FAIL)
11283 return FAIL;
11284
11285 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 * Now save the current files, current buffer first.
11287 */
11288 if (put_line(fd, "set shortmess=aoO") == FAIL)
11289 return FAIL;
11290
11291 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011292 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 {
11294 if (!(only_save_windows && buf->b_nwindows == 0)
11295 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011296#ifdef FEAT_TERMINAL
11297 /* skip terminal buffers: finished ones are not useful, others
11298 * will be resurrected and result in a new buffer */
11299 && !bt_terminal(buf)
11300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 && buf->b_fname != NULL
11302 && buf->b_p_bl)
11303 {
11304 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11305 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011306 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 return FAIL;
11308 }
11309 }
11310
11311 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011312 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11314 return FAIL;
11315
11316 if (ssop_flags & SSOP_RESIZE)
11317 {
11318 /* Note: after the restore we still check it worked!*/
11319 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11320 || put_eol(fd) == FAIL)
11321 return FAIL;
11322 }
11323
11324#ifdef FEAT_GUI
11325 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11326 {
11327 int x, y;
11328
11329 if (gui_mch_get_winpos(&x, &y) == OK)
11330 {
11331 /* Note: after the restore we still check it worked!*/
11332 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11333 return FAIL;
11334 }
11335 }
11336#endif
11337
11338 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011339 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11340 * will be displayed when creating the next tab. That resizes the windows
11341 * in the first tab, which may cause problems. Set 'showtabline' to 2
11342 * temporarily to avoid that.
11343 */
11344 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11345 {
11346 if (put_line(fd, "set stal=2") == FAIL)
11347 return FAIL;
11348 restore_stal = TRUE;
11349 }
11350
11351 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011352 * May repeat putting Windows for each tab, when "tabpages" is in
11353 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011354 * Don't use goto_tabpage(), it may change directory and trigger
11355 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011357 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011358 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011359 if ((ssop_flags & SSOP_TABPAGES))
11360 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011361 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011362
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011363 // Similar to ses_win_rec() below, populate the tab pages first so
11364 // later local options won't be copied to the new tabs.
11365 FOR_ALL_TABPAGES(tp)
11366 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011367 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011368 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011369 return FAIL;
11370 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011371 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011373 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011374 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011375
Bram Moolenaar18144c82006-04-12 21:52:12 +000011376 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011378 tabpage_T *tp = find_tabpage(tabnr);
11379
11380 if (tp == NULL)
11381 break; /* done all tab pages */
11382 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011383 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011384 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011385 tab_topframe = topframe;
11386 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011387 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011388 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011389 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011390 tab_topframe = tp->tp_topframe;
11391 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011392 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011393 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395
Bram Moolenaar18144c82006-04-12 21:52:12 +000011396 /*
11397 * Before creating the window layout, try loading one file. If this
11398 * is aborted we don't end up with a number of useless windows.
11399 * This may have side effects! (e.g., compressed or network file).
11400 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011401 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011402 {
11403 if (ses_do_win(wp)
11404 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011405 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011406#ifdef FEAT_QUICKFIX
11407 && !bt_nofile(wp->w_buffer)
11408#endif
11409 )
11410 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011411 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11412 return FAIL;
11413 need_tabnext = FALSE;
11414
11415 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011416 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11417 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011418 return FAIL;
11419 if (!wp->w_arg_idx_invalid)
11420 edited_win = wp;
11421 break;
11422 }
11423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011425 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011426 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011427 return FAIL;
11428
Bram Moolenaar18144c82006-04-12 21:52:12 +000011429 /*
11430 * Save current window layout.
11431 */
11432 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011434 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011436 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11437 return FAIL;
11438 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440
Bram Moolenaar18144c82006-04-12 21:52:12 +000011441 /*
11442 * Check if window sizes can be restored (no windows omitted).
11443 * Remember the window number of the current window after restoring.
11444 */
11445 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011446 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011447 {
11448 if (ses_do_win(wp))
11449 ++nr;
11450 else
11451 restore_size = FALSE;
11452 if (curwin == wp)
11453 cnr = nr;
11454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455
Bram Moolenaar18144c82006-04-12 21:52:12 +000011456 /* Go to the first window. */
11457 if (put_line(fd, "wincmd t") == FAIL)
11458 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011459
Bram Moolenaar18144c82006-04-12 21:52:12 +000011460 /*
11461 * If more than one window, see if sizes can be restored.
11462 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11463 * resized when moving between windows.
11464 * Do this before restoring the view, so that the topline and the
11465 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011466 * winminheight and winminwidth need to be set to avoid an error if the
11467 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011468 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011469 if (put_line(fd, "set winminheight=0") == FAIL
11470 || put_line(fd, "set winheight=1") == FAIL
11471 || put_line(fd, "set winminwidth=0") == FAIL
11472 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011473 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011474 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476
Bram Moolenaar18144c82006-04-12 21:52:12 +000011477 /*
11478 * Restore the view of the window (options, file, cursor, etc.).
11479 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011480 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011481 {
11482 if (!ses_do_win(wp))
11483 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011484 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11485 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011486 return FAIL;
11487 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11488 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011489 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011490 }
11491
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011492 /* The argument index in the first tab page is zero, need to set it in
11493 * each window. For further tab pages it's the window where we do
11494 * "tabedit". */
11495 cur_arg_idx = next_arg_idx;
11496
Bram Moolenaar18144c82006-04-12 21:52:12 +000011497 /*
11498 * Restore cursor to the current window if it's not the first one.
11499 */
11500 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11501 || put_eol(fd) == FAIL))
11502 return FAIL;
11503
11504 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011505 * Restore window sizes again after jumping around in windows, because
11506 * the current window has a minimum size while others may not.
11507 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011508 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011509 return FAIL;
11510
Bram Moolenaar18144c82006-04-12 21:52:12 +000011511 /* Don't continue in another tab page when doing only the current one
11512 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011513 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011514 break;
11515 }
11516
11517 if (ssop_flags & SSOP_TABPAGES)
11518 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011519 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11520 || put_eol(fd) == FAIL)
11521 return FAIL;
11522 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011523 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11524 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011525
Bram Moolenaar9c102382006-05-03 21:26:49 +000011526 /*
11527 * Wipe out an empty unnamed buffer we started in.
11528 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011529 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011530 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011531 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011532 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011533 return FAIL;
11534 if (put_line(fd, "endif") == FAIL)
11535 return FAIL;
11536 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11537 return FAIL;
11538
11539 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11540 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11541 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11542 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011543 /* Re-apply 'winminheight' and 'winminwidth'. */
11544 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11545 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11546 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547
11548 /*
11549 * Lastly, execute the x.vim file if it exists.
11550 */
11551 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11552 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011553 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 || put_line(fd, "endif") == FAIL)
11555 return FAIL;
11556
11557 return OK;
11558}
11559
11560 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011561ses_winsizes(
11562 FILE *fd,
11563 int restore_size,
11564 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565{
11566 int n = 0;
11567 win_T *wp;
11568
11569 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11570 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011571 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 {
11573 if (!ses_do_win(wp))
11574 continue;
11575 ++n;
11576
11577 /* restore height when not full height */
11578 if (wp->w_height + wp->w_status_height < topframe->fr_height
11579 && (fprintf(fd,
11580 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11581 n, (long)wp->w_height, Rows / 2, Rows) < 0
11582 || put_eol(fd) == FAIL))
11583 return FAIL;
11584
11585 /* restore width when not full width */
11586 if (wp->w_width < Columns && (fprintf(fd,
11587 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11588 n, (long)wp->w_width, Columns / 2, Columns) < 0
11589 || put_eol(fd) == FAIL))
11590 return FAIL;
11591 }
11592 }
11593 else
11594 {
11595 /* Just equalise window sizes */
11596 if (put_line(fd, "wincmd =") == FAIL)
11597 return FAIL;
11598 }
11599 return OK;
11600}
11601
11602/*
11603 * Write commands to "fd" to recursively create windows for frame "fr",
11604 * horizontally and vertically split.
11605 * After the commands the last window in the frame is the current window.
11606 * Returns FAIL when writing the commands to "fd" fails.
11607 */
11608 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011609ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610{
11611 frame_T *frc;
11612 int count = 0;
11613
11614 if (fr->fr_layout != FR_LEAF)
11615 {
11616 /* Find first frame that's not skipped and then create a window for
11617 * each following one (first frame is already there). */
11618 frc = ses_skipframe(fr->fr_child);
11619 if (frc != NULL)
11620 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11621 {
11622 /* Make window as big as possible so that we have lots of room
11623 * to split. */
11624 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11625 || put_line(fd, fr->fr_layout == FR_COL
11626 ? "split" : "vsplit") == FAIL)
11627 return FAIL;
11628 ++count;
11629 }
11630
11631 /* Go back to the first window. */
11632 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11633 ? "%dwincmd k" : "%dwincmd h", count) < 0
11634 || put_eol(fd) == FAIL))
11635 return FAIL;
11636
11637 /* Recursively create frames/windows in each window of this column or
11638 * row. */
11639 frc = ses_skipframe(fr->fr_child);
11640 while (frc != NULL)
11641 {
11642 ses_win_rec(fd, frc);
11643 frc = ses_skipframe(frc->fr_next);
11644 /* Go to next window. */
11645 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11646 return FAIL;
11647 }
11648 }
11649 return OK;
11650}
11651
11652/*
11653 * Skip frames that don't contain windows we want to save in the Session.
11654 * Returns NULL when there none.
11655 */
11656 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011657ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658{
11659 frame_T *frc;
11660
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011661 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 if (ses_do_frame(frc))
11663 break;
11664 return frc;
11665}
11666
11667/*
11668 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11669 * the Session.
11670 */
11671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011672ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673{
11674 frame_T *frc;
11675
11676 if (fr->fr_layout == FR_LEAF)
11677 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011678 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 if (ses_do_frame(frc))
11680 return TRUE;
11681 return FALSE;
11682}
11683
11684/*
11685 * Return non-zero if window "wp" is to be stored in the Session.
11686 */
11687 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011688ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011690#ifdef FEAT_TERMINAL
11691 if (bt_terminal(wp->w_buffer))
11692 return !term_is_finished(wp->w_buffer)
11693 && (ssop_flags & SSOP_TERMINAL)
11694 && term_should_restore(wp->w_buffer);
11695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 if (wp->w_buffer->b_fname == NULL
11697#ifdef FEAT_QUICKFIX
11698 /* When 'buftype' is "nofile" can't restore the window contents. */
11699 || bt_nofile(wp->w_buffer)
11700#endif
11701 )
11702 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011703 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 return (ssop_flags & SSOP_HELP);
11705 return TRUE;
11706}
11707
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011708 static int
11709put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11710{
11711 int r;
11712
11713 if (wp->w_curswant == MAXCOL)
11714 r = fprintf(fd, "%snormal! $", spaces);
11715 else
11716 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11717 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11718}
11719
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720/*
11721 * Write commands to "fd" to restore the view of a window.
11722 * Caller must make sure 'scrolloff' is zero.
11723 */
11724 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011725put_view(
11726 FILE *fd,
11727 win_T *wp,
11728 int add_edit, /* add ":edit" command to view */
11729 unsigned *flagp, /* vop_flags or ssop_flags */
11730 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011731 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732{
11733 win_T *save_curwin;
11734 int f;
11735 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011736 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737
11738 /* Always restore cursor position for ":mksession". For ":mkview" only
11739 * when 'viewoptions' contains "cursor". */
11740 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11741
11742 /*
11743 * Local argument list.
11744 */
11745 if (wp->w_alist == &global_alist)
11746 {
11747 if (put_line(fd, "argglobal") == FAIL)
11748 return FAIL;
11749 }
11750 else
11751 {
11752 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11753 flagp == &vop_flags
11754 || !(*flagp & SSOP_CURDIR)
11755 || wp->w_localdir != NULL, flagp) == FAIL)
11756 return FAIL;
11757 }
11758
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011759 /* Only when part of a session: restore the argument index. Some
11760 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011761 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011762 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011764 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 || put_eol(fd) == FAIL)
11766 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011767 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 }
11769
11770 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011771 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011773# ifdef FEAT_TERMINAL
11774 if (bt_terminal(wp->w_buffer))
11775 {
11776 if (term_write_session(fd, wp) == FAIL)
11777 return FAIL;
11778 }
11779 else
11780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 /*
11782 * Load the file.
11783 */
11784 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011785# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 )
11789 {
11790 /*
11791 * Editing a file in this buffer: use ":edit file".
11792 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011793 *
11794 * Note, if a buffer for that file already exists, use :badd to
11795 * edit that buffer, to not lose folding information (:edit resets
11796 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010011798 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011799 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010011800 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011801 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11802 || fputs(" | else | edit ", fd) < 0
11803 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11804 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011805 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 return FAIL;
11807 }
11808 else
11809 {
11810 /* No file in this buffer, just make it empty. */
11811 if (put_line(fd, "enew") == FAIL)
11812 return FAIL;
11813#ifdef FEAT_QUICKFIX
11814 if (wp->w_buffer->b_ffname != NULL)
11815 {
11816 /* The buffer does have a name, but it's not a file name. */
11817 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011818 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 return FAIL;
11820 }
11821#endif
11822 do_cursor = FALSE;
11823 }
11824 }
11825
11826 /*
11827 * Local mappings and abbreviations.
11828 */
11829 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11830 && makemap(fd, wp->w_buffer) == FAIL)
11831 return FAIL;
11832
11833 /*
11834 * Local options. Need to go to the window temporarily.
11835 * Store only local values when using ":mkview" and when ":mksession" is
11836 * used and 'sessionoptions' doesn't include "options".
11837 * Some folding options are always stored when "folds" is included,
11838 * otherwise the folds would not be restored correctly.
11839 */
11840 save_curwin = curwin;
11841 curwin = wp;
11842 curbuf = curwin->w_buffer;
11843 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11844 f = makeset(fd, OPT_LOCAL,
11845 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11846#ifdef FEAT_FOLDING
11847 else if (*flagp & SSOP_FOLDS)
11848 f = makefoldset(fd);
11849#endif
11850 else
11851 f = OK;
11852 curwin = save_curwin;
11853 curbuf = curwin->w_buffer;
11854 if (f == FAIL)
11855 return FAIL;
11856
11857#ifdef FEAT_FOLDING
11858 /*
11859 * Save Folds when 'buftype' is empty and for help files.
11860 */
11861 if ((*flagp & SSOP_FOLDS)
11862 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011863 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 {
11865 if (put_folds(fd, wp) == FAIL)
11866 return FAIL;
11867 }
11868#endif
11869
11870 /*
11871 * Set the cursor after creating folds, since that moves the cursor.
11872 */
11873 if (do_cursor)
11874 {
11875
11876 /* Restore the cursor line in the file and relatively in the
11877 * window. Don't use "G", it changes the jumplist. */
11878 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11879 (long)wp->w_cursor.lnum,
11880 (long)(wp->w_cursor.lnum - wp->w_topline),
11881 (long)wp->w_height / 2, (long)wp->w_height) < 0
11882 || put_eol(fd) == FAIL
11883 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11884 || put_line(fd, "exe s:l") == FAIL
11885 || put_line(fd, "normal! zt") == FAIL
11886 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11887 || put_eol(fd) == FAIL)
11888 return FAIL;
11889 /* Restore the cursor column and left offset when not wrapping. */
11890 if (wp->w_cursor.col == 0)
11891 {
11892 if (put_line(fd, "normal! 0") == FAIL)
11893 return FAIL;
11894 }
11895 else
11896 {
11897 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11898 {
11899 if (fprintf(fd,
11900 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011901 (long)wp->w_virtcol + 1,
11902 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 (long)wp->w_width / 2, (long)wp->w_width) < 0
11904 || put_eol(fd) == FAIL
11905 || put_line(fd, "if s:c > 0") == FAIL
11906 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011907 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11908 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 || put_eol(fd) == FAIL
11910 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011911 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 || put_line(fd, "endif") == FAIL)
11913 return FAIL;
11914 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011915 else if (put_view_curpos(fd, wp, "") == FAIL)
11916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 }
11918 }
11919
11920 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011921 * Local directory, if the current flag is not view options or the "curdir"
11922 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011924 if (wp->w_localdir != NULL
11925 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 {
11927 if (fputs("lcd ", fd) < 0
11928 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11929 || put_eol(fd) == FAIL)
11930 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011931 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 }
11933
11934 return OK;
11935}
11936
11937/*
11938 * Write an argument list to the session file.
11939 * Returns FAIL if writing fails.
11940 */
11941 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011942ses_arglist(
11943 FILE *fd,
11944 char *cmd,
11945 garray_T *gap,
11946 int fullname, /* TRUE: use full path name */
11947 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948{
11949 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011950 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 char_u *s;
11952
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011953 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11954 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011955 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 return FAIL;
11957 for (i = 0; i < gap->ga_len; ++i)
11958 {
11959 /* NULL file names are skipped (only happens when out of memory). */
11960 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11961 if (s != NULL)
11962 {
11963 if (fullname)
11964 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011965 buf = alloc(MAXPATHL);
11966 if (buf != NULL)
11967 {
11968 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11969 s = buf;
11970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011972 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011973 || ses_put_fname(fd, s, flagp) == FAIL
11974 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011975 {
11976 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011978 }
11979 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 }
11981 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011982 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983}
11984
11985/*
11986 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011987 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 * Returns FAIL if writing fails.
11989 */
11990 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011991ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992{
11993 char_u *name;
11994
11995 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011996 * the session file will be sourced.
11997 * Don't do this for ":mkview", we don't know the current directory.
11998 * Don't do this after ":lcd", we don't keep track of what the current
11999 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 if (buf->b_sfname != NULL
12001 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012002 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012003#ifdef FEAT_AUTOCHDIR
12004 && !p_acd
12005#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012006 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 name = buf->b_sfname;
12008 else
12009 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012010 if (ses_put_fname(fd, name, flagp) == FAIL
12011 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 return FAIL;
12013 return OK;
12014}
12015
12016/*
12017 * Write a file name to the session file.
12018 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12019 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012020 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 */
12022 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012023ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024{
12025 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012026 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028
12029 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012030 if (sname == NULL)
12031 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012033 if (*flagp & SSOP_SLASH)
12034 {
12035 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012036 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012037 if (*p == '\\')
12038 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012040
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012041 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012042 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012044 if (p == NULL)
12045 return FAIL;
12046
12047 /* write the result */
12048 if (fputs((char *)p, fd) < 0)
12049 retval = FAIL;
12050
12051 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 return retval;
12053}
12054
12055/*
12056 * ":loadview [nr]"
12057 */
12058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012059ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060{
12061 char_u *fname;
12062
12063 fname = get_view_file(*eap->arg);
12064 if (fname != NULL)
12065 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012066 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 vim_free(fname);
12068 }
12069}
12070
12071/*
12072 * Get the name of the view file for the current buffer.
12073 */
12074 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012075get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076{
12077 int len = 0;
12078 char_u *p, *s;
12079 char_u *retval;
12080 char_u *sname;
12081
12082 if (curbuf->b_ffname == NULL)
12083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012084 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 return NULL;
12086 }
12087 sname = home_replace_save(NULL, curbuf->b_ffname);
12088 if (sname == NULL)
12089 return NULL;
12090
12091 /*
12092 * We want a file name without separators, because we're not going to make
12093 * a directory.
12094 * "normal" path separator -> "=+"
12095 * "=" -> "=="
12096 * ":" path separator -> "=-"
12097 */
12098 for (p = sname; *p; ++p)
12099 if (*p == '=' || vim_ispathsep(*p))
12100 ++len;
12101 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12102 if (retval != NULL)
12103 {
12104 STRCPY(retval, p_vdir);
12105 add_pathsep(retval);
12106 s = retval + STRLEN(retval);
12107 for (p = sname; *p; ++p)
12108 {
12109 if (*p == '=')
12110 {
12111 *s++ = '=';
12112 *s++ = '=';
12113 }
12114 else if (vim_ispathsep(*p))
12115 {
12116 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012117#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 if (*p == ':')
12119 *s++ = '-';
12120 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012122 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 }
12124 else
12125 *s++ = *p;
12126 }
12127 *s++ = '=';
12128 *s++ = c;
12129 STRCPY(s, ".vim");
12130 }
12131
12132 vim_free(sname);
12133 return retval;
12134}
12135
12136#endif /* FEAT_SESSION */
12137
12138/*
12139 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12140 * Return FAIL for a write error.
12141 */
12142 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012143put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144{
12145 if (
12146#ifdef USE_CRNL
12147 (
12148# ifdef MKSESSION_NL
12149 !mksession_nl &&
12150# endif
12151 (putc('\r', fd) < 0)) ||
12152#endif
12153 (putc('\n', fd) < 0))
12154 return FAIL;
12155 return OK;
12156}
12157
12158/*
12159 * Write a line to "fd".
12160 * Return FAIL for a write error.
12161 */
12162 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012163put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164{
12165 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12166 return FAIL;
12167 return OK;
12168}
12169
12170#ifdef FEAT_VIMINFO
12171/*
12172 * ":rviminfo" and ":wviminfo".
12173 */
12174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012175ex_viminfo(
12176 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177{
12178 char_u *save_viminfo;
12179
12180 save_viminfo = p_viminfo;
12181 if (*p_viminfo == NUL)
12182 p_viminfo = (char_u *)"'100";
12183 if (eap->cmdidx == CMD_rviminfo)
12184 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012185 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12186 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012187 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 }
12189 else
12190 write_viminfo(eap->arg, eap->forceit);
12191 p_viminfo = save_viminfo;
12192}
12193#endif
12194
12195#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012196/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012197 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012198 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012201dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 if (fname == NULL)
12204 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012205 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206}
12207#endif
12208
12209/*
12210 * ":behave {mswin,xterm}"
12211 */
12212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012213ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214{
12215 if (STRCMP(eap->arg, "mswin") == 0)
12216 {
12217 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12218 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12219 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12220 set_option_value((char_u *)"keymodel", 0L,
12221 (char_u *)"startsel,stopsel", 0);
12222 }
12223 else if (STRCMP(eap->arg, "xterm") == 0)
12224 {
12225 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12226 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12227 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12228 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12229 }
12230 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012231 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232}
12233
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012234#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12235/*
12236 * Function given to ExpandGeneric() to obtain the possible arguments of the
12237 * ":behave {mswin,xterm}" command.
12238 */
12239 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012240get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012241{
12242 if (idx == 0)
12243 return (char_u *)"mswin";
12244 if (idx == 1)
12245 return (char_u *)"xterm";
12246 return NULL;
12247}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012248
12249/*
12250 * Function given to ExpandGeneric() to obtain the possible arguments of the
12251 * ":messages {clear}" command.
12252 */
12253 char_u *
12254get_messages_arg(expand_T *xp UNUSED, int idx)
12255{
12256 if (idx == 0)
12257 return (char_u *)"clear";
12258 return NULL;
12259}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012260#endif
12261
Bram Moolenaar113e1072019-01-20 15:30:40 +010012262#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012263 char_u *
12264get_mapclear_arg(expand_T *xp UNUSED, int idx)
12265{
12266 if (idx == 0)
12267 return (char_u *)"<buffer>";
12268 return NULL;
12269}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012270#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012271
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272static int filetype_detect = FALSE;
12273static int filetype_plugin = FALSE;
12274static int filetype_indent = FALSE;
12275
12276/*
12277 * ":filetype [plugin] [indent] {on,off,detect}"
12278 * on: Load the filetype.vim file to install autocommands for file types.
12279 * off: Load the ftoff.vim file to remove all autocommands for file types.
12280 * plugin on: load filetype.vim and ftplugin.vim
12281 * plugin off: load ftplugof.vim
12282 * indent on: load filetype.vim and indent.vim
12283 * indent off: load indoff.vim
12284 */
12285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012286ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287{
12288 char_u *arg = eap->arg;
12289 int plugin = FALSE;
12290 int indent = FALSE;
12291
12292 if (*eap->arg == NUL)
12293 {
12294 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012295 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 filetype_detect ? "ON" : "OFF",
12297 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12298 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12299 return;
12300 }
12301
12302 /* Accept "plugin" and "indent" in any order. */
12303 for (;;)
12304 {
12305 if (STRNCMP(arg, "plugin", 6) == 0)
12306 {
12307 plugin = TRUE;
12308 arg = skipwhite(arg + 6);
12309 continue;
12310 }
12311 if (STRNCMP(arg, "indent", 6) == 0)
12312 {
12313 indent = TRUE;
12314 arg = skipwhite(arg + 6);
12315 continue;
12316 }
12317 break;
12318 }
12319 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12320 {
12321 if (*arg == 'o' || !filetype_detect)
12322 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012323 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 filetype_detect = TRUE;
12325 if (plugin)
12326 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012327 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 filetype_plugin = TRUE;
12329 }
12330 if (indent)
12331 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012332 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 filetype_indent = TRUE;
12334 }
12335 }
12336 if (*arg == 'd')
12337 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012338 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012339 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 }
12341 }
12342 else if (STRCMP(arg, "off") == 0)
12343 {
12344 if (plugin || indent)
12345 {
12346 if (plugin)
12347 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012348 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 filetype_plugin = FALSE;
12350 }
12351 if (indent)
12352 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012353 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 filetype_indent = FALSE;
12355 }
12356 }
12357 else
12358 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012359 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 filetype_detect = FALSE;
12361 }
12362 }
12363 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012364 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365}
12366
12367/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012368 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 */
12370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012371ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372{
12373 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012374 {
12375 char_u *arg = eap->arg;
12376
12377 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12378 arg += 9;
12379
12380 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12381 if (arg != eap->arg)
12382 did_filetype = FALSE;
12383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012387ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388{
12389#ifdef FEAT_DIGRAPHS
12390 if (*eap->arg != NUL)
12391 putdigraph(eap->arg);
12392 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012393 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012395 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396#endif
12397}
12398
12399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012400ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
12402 int flags = 0;
12403
12404 if (eap->cmdidx == CMD_setlocal)
12405 flags = OPT_LOCAL;
12406 else if (eap->cmdidx == CMD_setglobal)
12407 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012408#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 if (cmdmod.browse && flags == 0)
12410 ex_options(eap);
12411 else
12412#endif
12413 (void)do_set(eap->arg, flags);
12414}
12415
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012416#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12417 void
12418set_no_hlsearch(int flag)
12419{
12420 no_hlsearch = flag;
12421# ifdef FEAT_EVAL
12422 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12423# endif
12424}
12425
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426/*
12427 * ":nohlsearch"
12428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012430ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012432 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012433 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434}
12435
12436/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012437 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 * Sets nextcmd to the start of the next command, if any. Also called when
12439 * skipping commands to find the next command.
12440 */
12441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012442ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443{
12444 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012445 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 char_u *end;
12447 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012448 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012449
12450 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012451 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012452 else
12453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012454 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012455 return;
12456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457
12458 /* First clear any old pattern. */
12459 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012460 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461
12462 if (ends_excmd(*eap->arg))
12463 end = eap->arg;
12464 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012465 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 end = eap->arg + 4;
12467 else
12468 {
12469 p = skiptowhite(eap->arg);
12470 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012471 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 p = skipwhite(p);
12473 if (*p == NUL)
12474 {
12475 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012476 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012477 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 return;
12479 }
12480 end = skip_regexp(p + 1, *p, TRUE, NULL);
12481 if (!eap->skip)
12482 {
12483 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12484 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012485 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 eap->errmsg = e_trailing;
12487 return;
12488 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012489 if (*end != *p)
12490 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012491 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012492 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012493 return;
12494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495
12496 c = *end;
12497 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012498 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012499 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012500 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 }
12502 }
12503 eap->nextcmd = find_nextcmd(end);
12504}
12505#endif
12506
12507#ifdef FEAT_CRYPT
12508/*
12509 * ":X": Get crypt key
12510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012512ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012514 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012515 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516}
12517#endif
12518
12519#ifdef FEAT_FOLDING
12520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012521ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522{
12523 if (foldManualAllowed(TRUE))
12524 foldCreate(eap->line1, eap->line2);
12525}
12526
12527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012528ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529{
12530 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12531 eap->forceit, FALSE);
12532}
12533
12534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012535ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536{
12537 linenr_T lnum;
12538
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012539#ifdef FEAT_CLIPBOARD
12540 start_global_changes();
12541#endif
12542
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 /* First set the marks for all lines closed/open. */
12544 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12545 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12546 ml_setmarked(lnum);
12547
12548 /* Execute the command on the marked lines. */
12549 global_exe(eap->arg);
12550 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012551#ifdef FEAT_CLIPBOARD
12552 end_global_changes();
12553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554}
12555#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012556
Bram Moolenaar39665952018-08-15 20:59:48 +020012557#ifdef FEAT_QUICKFIX
12558/*
12559 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12560 * instead of a quickfix command.
12561 */
12562 int
12563is_loclist_cmd(int cmdidx)
12564{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012565 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012566 return FALSE;
12567 return cmdnames[cmdidx].cmd_name[0] == 'l';
12568}
12569#endif
12570
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012571# if defined(FEAT_TIMERS) || defined(PROTO)
12572 int
12573get_pressedreturn(void)
12574{
12575 return ex_pressedreturn;
12576}
12577
12578 void
12579set_pressedreturn(int val)
12580{
12581 ex_pressedreturn = val;
12582}
12583#endif