blob: 6e61cca9b3604e471549ab6683e55bad1af4782b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020032 sctx_T uc_script_ctx; /* SCTX where the command was defined */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010033# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
64#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010065static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010067static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static int if_level = 0; /* depth in :if */
69#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020070static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void ex_autocmd(exarg_T *eap);
85static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_bunload(exarg_T *eap);
87static void ex_buffer(exarg_T *eap);
88static void ex_bmodified(exarg_T *eap);
89static void ex_bnext(exarg_T *eap);
90static void ex_bprevious(exarg_T *eap);
91static void ex_brewind(exarg_T *eap);
92static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *getargcmd(char_u **);
94static char_u *skip_cmd_arg(char_u *p, int rembs);
95static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifndef FEAT_QUICKFIX
97# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000098# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# define ex_cc ex_ni
100# define ex_cnext ex_ni
101# define ex_cfile ex_ni
102# define qf_list ex_ni
103# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200104# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000106# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109# define ex_cclose ex_ni
110# define ex_copen ex_ni
111# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200112# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000114#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
115# define ex_cexpr ex_ni
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200118static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200120#if !defined(FEAT_PERL) \
121 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
122 || !defined(FEAT_TCL) \
123 || !defined(FEAT_RUBY) \
124 || !defined(FEAT_LUA) \
125 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000126# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100129static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000131#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000133#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100134static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
135static void ex_highlight(exarg_T *eap);
136static void ex_colorscheme(exarg_T *eap);
137static void ex_quit(exarg_T *eap);
138static void ex_cquit(exarg_T *eap);
139static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_close(exarg_T *eap);
141static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
142static void ex_only(exarg_T *eap);
143static void ex_resize(exarg_T *eap);
144static void ex_stag(exarg_T *eap);
145static void ex_tabclose(exarg_T *eap);
146static void ex_tabonly(exarg_T *eap);
147static void ex_tabnext(exarg_T *eap);
148static void ex_tabmove(exarg_T *eap);
149static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200150#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static void ex_pclose(exarg_T *eap);
152static void ex_ptag(exarg_T *eap);
153static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#else
155# define ex_pclose ex_ni
156# define ex_ptag ex_ni
157# define ex_pedit ex_ni
158#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ex_hide(exarg_T *eap);
160static void ex_stop(exarg_T *eap);
161static void ex_exit(exarg_T *eap);
162static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100164static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#else
166# define ex_goto ex_ni
167#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100168static void ex_shell(exarg_T *eap);
169static void ex_preserve(exarg_T *eap);
170static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100171static void ex_mode(exarg_T *eap);
172static void ex_wrongmodifier(exarg_T *eap);
173static void ex_find(exarg_T *eap);
174static void ex_open(exarg_T *eap);
175static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#ifndef FEAT_GUI
177# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100178static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100180#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
183# define ex_tearoff ex_ni
184#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100185#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
186 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
189# define ex_popup ex_ni
190#endif
191#ifndef FEAT_GUI_MSWIN
192# define ex_simalt ex_ni
193#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000194#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# define gui_mch_find_dialog ex_ni
196# define gui_mch_replace_dialog ex_ni
197#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000198#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# define ex_helpfind ex_ni
200#endif
201#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100202# define ex_cscope ex_ni
203# define ex_scscope ex_ni
204# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
206#ifndef FEAT_SYN_HL
207# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200208# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000209#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100210#ifndef FEAT_EVAL
211# define ex_packadd ex_ni
212# define ex_packloadall ex_ni
213#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200214#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200215# define ex_syntime ex_ni
216#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000217#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000218# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000219# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000220# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000221# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000222# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000223#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200224#ifndef FEAT_PERSISTENT_UNDO
225# define ex_rundo ex_ni
226# define ex_wundo ex_ni
227#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200228#ifndef FEAT_LUA
229# define ex_lua ex_script_ni
230# define ex_luado ex_ni
231# define ex_luafile ex_ni
232#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000233#ifndef FEAT_MZSCHEME
234# define ex_mzscheme ex_script_ni
235# define ex_mzfile ex_ni
236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifndef FEAT_PERL
238# define ex_perl ex_script_ni
239# define ex_perldo ex_ni
240#endif
241#ifndef FEAT_PYTHON
242# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200243# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# define ex_pyfile ex_ni
245#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200246#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200247# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200248# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200249# define ex_py3file ex_ni
250#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100251#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
252# define ex_pyx ex_script_ni
253# define ex_pyxdo ex_ni
254# define ex_pyxfile ex_ni
255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifndef FEAT_TCL
257# define ex_tcl ex_script_ni
258# define ex_tcldo ex_ni
259# define ex_tclfile ex_ni
260#endif
261#ifndef FEAT_RUBY
262# define ex_ruby ex_script_ni
263# define ex_rubydo ex_ni
264# define ex_rubyfile ex_ni
265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifndef FEAT_KEYMAP
267# define ex_loadkeymap ex_ni
268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static void ex_swapname(exarg_T *eap);
270static void ex_syncbind(exarg_T *eap);
271static void ex_read(exarg_T *eap);
272static void ex_pwd(exarg_T *eap);
273static void ex_equal(exarg_T *eap);
274static void ex_sleep(exarg_T *eap);
275static void do_exmap(exarg_T *eap, int isabbrev);
276static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000278#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100279static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280#else
281# define ex_winpos ex_ni
282#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100283static void ex_operators(exarg_T *eap);
284static void ex_put(exarg_T *eap);
285static void ex_copymove(exarg_T *eap);
286static void ex_submagic(exarg_T *eap);
287static void ex_join(exarg_T *eap);
288static void ex_at(exarg_T *eap);
289static void ex_bang(exarg_T *eap);
290static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200291#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100292static void ex_wundo(exarg_T *eap);
293static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200294#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100295static void ex_redo(exarg_T *eap);
296static void ex_later(exarg_T *eap);
297static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100298static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100299static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100300static void close_redir(void);
301static void ex_mkrc(exarg_T *eap);
302static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#ifdef FEAT_USR_CMDS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100304static char *uc_fun_cmd(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static void ex_startinsert(exarg_T *eap);
308static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100310static void ex_checkpath(exarg_T *eap);
311static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#else
313# define ex_findpat ex_ni
314# define ex_checkpath ex_ni
315#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200316#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#else
319# define ex_psearch ex_ni
320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_tag(exarg_T *eap);
322static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifndef FEAT_EVAL
324# define ex_scriptnames ex_ni
325# define ex_finish ex_ni
326# define ex_echo ex_ni
327# define ex_echohl ex_ni
328# define ex_execute ex_ni
329# define ex_call ex_ni
330# define ex_if ex_ni
331# define ex_endif ex_ni
332# define ex_else ex_ni
333# define ex_while ex_ni
334# define ex_continue ex_ni
335# define ex_break ex_ni
336# define ex_endwhile ex_ni
337# define ex_throw ex_ni
338# define ex_try ex_ni
339# define ex_catch ex_ni
340# define ex_finally ex_ni
341# define ex_endtry ex_ni
342# define ex_endfunction ex_ni
343# define ex_let ex_ni
344# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000345# define ex_lockvar ex_ni
346# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_function ex_ni
348# define ex_delfunction ex_ni
349# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000350# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100352static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100354static int makeopens(FILE *fd, char_u *dirnow);
355static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
356static void ex_loadview(exarg_T *eap);
357static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000358static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#else
360# define ex_loadview ex_ni
361#endif
362#ifndef FEAT_EVAL
363# define ex_compiler ex_ni
364#endif
365#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100366static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#else
368# define ex_viminfo ex_ni
369#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100370static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100371static void ex_filetype(exarg_T *eap);
372static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000374# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375# define ex_diffpatch ex_ni
376# define ex_diffgetput ex_ni
377# define ex_diffsplit ex_ni
378# define ex_diffthis ex_ni
379# define ex_diffupdate ex_ni
380#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100381static void ex_digraphs(exarg_T *eap);
382static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100383#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# define ex_options ex_ni
385#endif
386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100387static void ex_nohlsearch(exarg_T *eap);
388static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#else
390# define ex_nohlsearch ex_ni
391# define ex_match ex_ni
392#endif
393#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100394static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#else
396# define ex_X ex_ni
397#endif
398#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100399static void ex_fold(exarg_T *eap);
400static void ex_foldopen(exarg_T *eap);
401static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_fold ex_ni
404# define ex_foldopen ex_ni
405# define ex_folddo ex_ni
406#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100407#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# define ex_language ex_ni
409#endif
410#ifndef FEAT_SIGNS
411# define ex_sign ex_ni
412#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000413#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200414# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000415# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200416# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419#ifndef FEAT_EVAL
420# define ex_debug ex_ni
421# define ex_breakadd ex_ni
422# define ex_debuggreedy ex_ni
423# define ex_breakdel ex_ni
424# define ex_breaklist ex_ni
425#endif
426
427#ifndef FEAT_CMDHIST
428# define ex_history ex_ni
429#endif
430#ifndef FEAT_JUMPLIST
431# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200432# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433# define ex_changes ex_ni
434#endif
435
Bram Moolenaar05159a02005-02-26 23:04:13 +0000436#ifndef FEAT_PROFILE
437# define ex_profile ex_ni
438#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200439#ifndef FEAT_TERMINAL
440# define ex_terminal ex_ni
441#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443/*
444 * Declare cmdnames[].
445 */
446#define DO_DECLARE_EXCMD
447#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200448#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100449
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450static char_u dollar_command[2] = {'$', 0};
451
452
453#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000454/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455typedef struct
456{
457 char_u *line; /* command line */
458 linenr_T lnum; /* sourcing_lnum of the line */
459} wcmd_T;
460
461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000462 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000464 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000466struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 garray_T *lines_gap; /* growarray with line info */
469 int current_line; /* last read line from growarray */
470 int repeating; /* TRUE when looping a second time */
471 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100472 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 void *cookie;
474};
475
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100476static char_u *get_loop_line(int c, void *cookie, int indent);
477static int store_loop_line(garray_T *gap, char_u *line);
478static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000479
480/* Struct to save a few things while debugging. Used in do_cmdline() only. */
481struct dbg_stuff
482{
483 int trylevel;
484 int force_abort;
485 except_T *caught_stack;
486 char_u *vv_exception;
487 char_u *vv_throwpoint;
488 int did_emsg;
489 int got_int;
490 int did_throw;
491 int need_rethrow;
492 int check_cstack;
493 except_T *current_exception;
494};
495
Bram Moolenaared203462004-06-16 11:19:22 +0000496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100497save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000498{
499 dsp->trylevel = trylevel; trylevel = 0;
500 dsp->force_abort = force_abort; force_abort = FALSE;
501 dsp->caught_stack = caught_stack; caught_stack = NULL;
502 dsp->vv_exception = v_exception(NULL);
503 dsp->vv_throwpoint = v_throwpoint(NULL);
504
505 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
506 dsp->did_emsg = did_emsg; did_emsg = FALSE;
507 dsp->got_int = got_int; got_int = FALSE;
508 dsp->did_throw = did_throw; did_throw = FALSE;
509 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
510 dsp->check_cstack = check_cstack; check_cstack = FALSE;
511 dsp->current_exception = current_exception; current_exception = NULL;
512}
513
514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100515restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000516{
517 suppress_errthrow = FALSE;
518 trylevel = dsp->trylevel;
519 force_abort = dsp->force_abort;
520 caught_stack = dsp->caught_stack;
521 (void)v_exception(dsp->vv_exception);
522 (void)v_throwpoint(dsp->vv_throwpoint);
523 did_emsg = dsp->did_emsg;
524 got_int = dsp->got_int;
525 did_throw = dsp->did_throw;
526 need_rethrow = dsp->need_rethrow;
527 check_cstack = dsp->check_cstack;
528 current_exception = dsp->current_exception;
529}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
533 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
534 * command is given.
535 */
536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100537do_exmode(
538 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 int save_msg_scroll;
541 int prev_msg_row;
542 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100543 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544
545 if (improved)
546 exmode_active = EXMODE_VIM;
547 else
548 exmode_active = EXMODE_NORMAL;
549 State = NORMAL;
550
551 /* When using ":global /pat/ visual" and then "Q" we return to continue
552 * the :global command. */
553 if (global_busy)
554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 save_msg_scroll = msg_scroll;
557 ++RedrawingDisabled; /* don't redisplay the window */
558 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_GUI
560 /* Ignore scrollbar and mouse events in Ex mode */
561 ++hold_gui_events;
562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar32526b32019-01-19 17:43:09 +0100564 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 while (exmode_active)
566 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000567 /* Check for a ":normal" command and no more characters left. */
568 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
569 {
570 exmode_active = FALSE;
571 break;
572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 msg_scroll = TRUE;
574 need_wait_return = FALSE;
575 ex_pressedreturn = FALSE;
576 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100577 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 prev_msg_row = msg_row;
579 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (improved)
581 {
582 cmdline_row = msg_row;
583 do_cmdline(NULL, getexline, NULL, 0);
584 }
585 else
586 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
587 lines_left = Rows - 1;
588
Bram Moolenaardf177f62005-02-22 08:39:57 +0000589 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100590 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000592 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100593 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000596 if (ex_pressedreturn)
597 {
598 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000599 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000600 msg_row = prev_msg_row;
601 if (prev_msg_row == Rows - 1)
602 msg_row--;
603 }
604 msg_col = 0;
605 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
606 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000609 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
610 {
611 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100612 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100614 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
617
618#ifdef FEAT_GUI
619 --hold_gui_events;
620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 --RedrawingDisabled;
622 --no_wait_return;
623 update_screen(CLEAR);
624 need_wait_return = FALSE;
625 msg_scroll = save_msg_scroll;
626}
627
628/*
629 * Execute a simple command line. Used for translated commands like "*".
630 */
631 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100632do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 return do_cmdline(cmd, NULL, NULL,
635 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
636}
637
638/*
639 * do_cmdline(): execute one Ex command line
640 *
641 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100642 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 * 2. Split up in parts separated with '|'.
644 *
645 * This function can be called recursively!
646 *
647 * flags:
648 * DOCMD_VERBOSE - The command will be included in the error message.
649 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100650 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 * DOCMD_KEYTYPED - Don't reset KeyTyped.
652 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
653 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
654 *
655 * return FAIL if cmdline could not be executed, OK otherwise
656 */
657 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100658do_cmdline(
659 char_u *cmdline,
660 char_u *(*fgetline)(int, void *, int),
661 void *cookie, /* argument for fgetline() */
662 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 char_u *next_cmdline; /* next cmd to execute */
665 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100666 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 static int recursive = 0; /* recursive depth */
668 int msg_didout_before_start = 0;
669 int count = 0; /* line number count */
670 int did_inc = FALSE; /* incremented RedrawingDisabled */
671 int retval = OK;
672#ifdef FEAT_EVAL
673 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000674 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int current_line = 0; /* active line in lines_ga */
676 char_u *fname = NULL; /* function or script name */
677 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
678 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000679 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int initial_trylevel;
681 struct msglist **saved_msg_list = NULL;
682 struct msglist *private_msg_list;
683
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100684 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100685 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# define cmd_cookie cookie
693#endif
694 static int call_depth = 0; /* recursiveness */
695
696#ifdef FEAT_EVAL
697 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
698 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000699 * This ensures that the do_errthrow() call in do_one_cmd() does not
700 * combine the messages stored by an earlier invocation of do_one_cmd()
701 * with the command name of the later one. This would happen when
702 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 saved_msg_list = msg_list;
704 msg_list = &private_msg_list;
705 private_msg_list = NULL;
706#endif
707
708 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100709 * here. The value of 200 allows nested function calls, ":source", etc.
710 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100711 if (call_depth >= 200
712#ifdef FEAT_EVAL
713 && call_depth >= p_mfd
714#endif
715 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_EVAL
719 /* When converting to an exception, we do not include the command name
720 * since this is not an error of the specific command. */
721 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
722 msg_list = saved_msg_list;
723#endif
724 return FAIL;
725 }
726 ++call_depth;
727
728#ifdef FEAT_EVAL
729 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000730 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 cstack.cs_trylevel = 0;
732 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000733 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
735
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ++ex_nesting_level;
742
743 /* Get the function or script name and the address where the next breakpoint
744 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000745 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 fname = func_name(real_cookie);
748 breakpoint = func_breakpoint(real_cookie);
749 dbg_tick = func_dbg_tick(real_cookie);
750 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
753 fname = sourcing_name;
754 breakpoint = source_breakpoint(real_cookie);
755 dbg_tick = source_dbg_tick(real_cookie);
756 }
757
758 /*
759 * Initialize "force_abort" and "suppress_errthrow" at the top level.
760 */
761 if (!recursive)
762 {
763 force_abort = FALSE;
764 suppress_errthrow = FALSE;
765 }
766
767 /*
768 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000769 * exception handling (used when debugging). Otherwise clear it to avoid
770 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000772 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000773 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000774 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200775 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 initial_trylevel = trylevel;
778
779 /*
780 * "did_throw" will be set to TRUE when an exception is being thrown.
781 */
782 did_throw = FALSE;
783#endif
784 /*
785 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000786 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * If force_abort is set, we cancel everything.
788 */
789 did_emsg = FALSE;
790
791 /*
792 * KeyTyped is only set when calling vgetc(). Reset it here when not
793 * calling vgetc() (sourced command lines).
794 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100795 if (!(flags & DOCMD_KEYTYPED)
796 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 KeyTyped = FALSE;
798
799 /*
800 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000801 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * - for multiple commands on one line, separated with '|'
803 * - when repeating until there are no more lines (for ":source")
804 */
805 next_cmdline = cmdline;
806 do
807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#endif
811
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000812 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if (next_cmdline == NULL
814#ifdef FEAT_EVAL
815 && !force_abort
816 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#endif
819 )
820 did_emsg = FALSE;
821
822 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 * 3. If a line is given: Make a copy, so we can mess with it.
826 */
827
828#ifdef FEAT_EVAL
829 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000830 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {
832 /* Each '|' separated command is stored separately in lines_ga, to
833 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100834 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836 /* Check if a function has returned or, unless it has an unclosed
837 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000841 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 func_line_end(real_cookie);
843# endif
844 if (func_has_ended(real_cookie))
845 {
846 retval = FAIL;
847 break;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000851 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 script_line_end();
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100857 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 retval = FAIL;
860 break;
861 }
862
863 /* If breakpoints have been added/deleted need to check for it. */
864 if (breakpoint != NULL && dbg_tick != NULL
865 && *dbg_tick != debug_tick)
866 {
867 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 fname, sourcing_lnum);
870 *dbg_tick = debug_tick;
871 }
872
873 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
874 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
875
876 /* Did we encounter a breakpoint? */
877 if (breakpoint != NULL && *breakpoint != 0
878 && *breakpoint <= sourcing_lnum)
879 {
880 dbg_breakpoint(fname, sourcing_lnum);
881 /* Find next breakpoint. */
882 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 fname, sourcing_lnum);
885 *dbg_tick = debug_tick;
886 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000888 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000889 {
890 if (getline_is_func)
891 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100892 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 script_line_start();
894 }
895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100901 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 * below, so that it stores lines in or reads them from
903 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 * while/for loop. */
905 cmd_getline = get_loop_line;
906 cmd_cookie = (void *)&cmd_loop_cookie;
907 cmd_loop_cookie.lines_gap = &lines_ga;
908 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000910 cmd_loop_cookie.cookie = cookie;
911 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 else
914 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100915 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 cmd_cookie = cookie;
917 }
918#endif
919
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100920 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (next_cmdline == NULL)
922 {
923 /*
924 * Need to set msg_didout for the first line after an ":if",
925 * otherwise the ":if" will be overwritten.
926 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100927 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#ifdef FEAT_EVAL
931 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
932#else
933 0
934#endif
935 )) == NULL)
936 {
937 /* Don't call wait_return for aborted command line. The NULL
938 * returned for the end of a sourced file or executed function
939 * doesn't do this. */
940 if (KeyTyped && !(flags & DOCMD_REPEAT))
941 need_wait_return = FALSE;
942 retval = FAIL;
943 break;
944 }
945 used_getline = TRUE;
946
947 /*
948 * Keep the first typed line. Clear it when more lines are typed.
949 */
950 if (flags & DOCMD_KEEPLINE)
951 {
952 vim_free(repeat_cmdline);
953 if (count == 0)
954 repeat_cmdline = vim_strsave(next_cmdline);
955 else
956 repeat_cmdline = NULL;
957 }
958 }
959
960 /* 3. Make a copy of the command so we can mess with it. */
961 else if (cmdline_copy == NULL)
962 {
963 next_cmdline = vim_strsave(next_cmdline);
964 if (next_cmdline == NULL)
965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100966 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 retval = FAIL;
968 break;
969 }
970 }
971 cmdline_copy = next_cmdline;
972
973#ifdef FEAT_EVAL
974 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 * Save the current line when inside a ":while" or ":for", and when
976 * the command looks like a ":while" or ":for", because we may need it
977 * later. When there is a '|' and another command, it is stored
978 * separately, because we need to be able to jump back to it from an
979 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 if (current_line == lines_ga.ga_len
982 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 retval = FAIL;
987 break;
988 }
989 }
990 did_endif = FALSE;
991#endif
992
993 if (count++ == 0)
994 {
995 /*
996 * All output from the commands is put below each other, without
997 * waiting for a return. Don't do this when executing commands
998 * from a script or when being called recursive (e.g. for ":e
999 * +command file").
1000 */
1001 if (!(flags & DOCMD_NOWAIT) && !recursive)
1002 {
1003 msg_didout_before_start = msg_didout;
1004 msg_didany = FALSE; /* no output yet */
1005 msg_start();
1006 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001007 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 ++RedrawingDisabled;
1009 did_inc = TRUE;
1010 }
1011 }
1012
1013 if (p_verbose >= 15 && sourcing_name != NULL)
1014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001016 verbose_enter_scroll();
1017
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001020 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001021 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001022
1023 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 --no_wait_return;
1025 }
1026
1027 /*
1028 * 2. Execute one '|' separated command.
1029 * do_one_cmd() will return NULL if there is no trailing '|'.
1030 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1031 */
1032 ++recursive;
1033 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1034#ifdef FEAT_EVAL
1035 &cstack,
1036#endif
1037 cmd_getline, cmd_cookie);
1038 --recursive;
1039
1040#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cmd_cookie == (void *)&cmd_loop_cookie)
1042 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045#endif
1046
1047 if (next_cmdline == NULL)
1048 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001049 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#ifdef FEAT_CMDHIST
1051 /*
1052 * If the command was typed, remember it for the ':' register.
1053 * Do this AFTER executing the command to make :@: work.
1054 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001055 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && new_last_cmdline != NULL)
1057 {
1058 vim_free(last_cmdline);
1059 last_cmdline = new_last_cmdline;
1060 new_last_cmdline = NULL;
1061 }
1062#endif
1063 }
1064 else
1065 {
1066 /* need to copy the command after the '|' to cmdline_copy, for the
1067 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001068 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 next_cmdline = cmdline_copy;
1070 }
1071
1072
1073#ifdef FEAT_EVAL
1074 /* reset did_emsg for a function that is not aborted by an error */
1075 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 && !func_has_abort(real_cookie))
1078 did_emsg = FALSE;
1079
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 ++current_line;
1083
1084 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 * An ":endwhile", ":endfor" and ":continue" is handled here.
1086 * If we were executing commands, jump back to the ":while" or
1087 * ":for".
1088 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 /* Jump back to the matching ":while" or ":for". Be careful
1095 * not to use a cs_line[] from an entry that isn't a ":while"
1096 * or ":for": It would make "current_line" invalid and can
1097 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (!did_emsg && !got_int && !did_throw
1099 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 && (cstack.cs_flags[cstack.cs_idx]
1101 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && cstack.cs_line[cstack.cs_idx] >= 0
1103 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1104 {
1105 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 /* remember we jumped there */
1107 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 line_breakcheck(); /* check if CTRL-C typed */
1109
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 /* Check for the next breakpoint at or after the ":while"
1111 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (breakpoint != NULL)
1113 {
1114 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001115 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 fname,
1117 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1118 *dbg_tick = debug_tick;
1119 }
1120 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001125 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1126 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1137 }
1138 }
1139
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001140 /* Check for the next breakpoint after a watchexpression */
1141 if (breakpoint != NULL && has_watchexpr())
1142 {
1143 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1144 *dbg_tick = debug_tick;
1145 }
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /*
1148 * When not inside any ":while" loop, clear remembered lines.
1149 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 if (lines_ga.ga_len > 0)
1153 {
1154 sourcing_lnum =
1155 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1156 free_cmdlines(&lines_ga);
1157 }
1158 current_line = 0;
1159 }
1160
1161 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1163 * being restored at the ":endtry". Reset them here and set the
1164 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1165 * This includes the case where a missing ":endif", ":endwhile" or
1166 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 cstack.cs_lflags &= ~CSL_HAD_FINA;
1171 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1172 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 did_throw ? (void *)current_exception : NULL);
1174 did_emsg = got_int = did_throw = FALSE;
1175 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1176 }
1177
1178 /* Update global "trylevel" for recursive calls to do_cmdline() from
1179 * within this loop. */
1180 trylevel = initial_trylevel + cstack.cs_trylevel;
1181
1182 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001183 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 * files) is aborted because of an error, an interrupt, or an uncaught
1185 * exception, cancel everything. If it is left normally, reset
1186 * force_abort to get the non-EH compatible abortion behavior for
1187 * the rest of the script.
1188 */
1189 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1190 force_abort = FALSE;
1191
1192 /* Convert an interrupt to an exception if appropriate. */
1193 (void)do_intthrow(&cstack);
1194#endif /* FEAT_EVAL */
1195
1196 }
1197 /*
1198 * Continue executing command lines when:
1199 * - no CTRL-C typed, no aborting error, no exception thrown or try
1200 * conditionals need to be checked for executing finally clauses or
1201 * catching an interrupt exception
1202 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 * looping for ":source" command or function call.
1205 */
1206 while (!((got_int
1207#ifdef FEAT_EVAL
1208 || (did_emsg && force_abort) || did_throw
1209#endif
1210 )
1211#ifdef FEAT_EVAL
1212 && cstack.cs_trylevel == 0
1213#endif
1214 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001215 && !(did_emsg
1216#ifdef FEAT_EVAL
1217 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001218 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001219 * the :endtry to be missed. */
1220 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1221#endif
1222 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001223 && (getline_equal(fgetline, cookie, getexmodeline)
1224 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 && (next_cmdline != NULL
1226#ifdef FEAT_EVAL
1227 || cstack.cs_idx >= 0
1228#endif
1229 || (flags & DOCMD_REPEAT)));
1230
1231 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001232 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#ifdef FEAT_EVAL
1234 free_cmdlines(&lines_ga);
1235 ga_clear(&lines_ga);
1236
1237 if (cstack.cs_idx >= 0)
1238 {
1239 /*
1240 * If a sourced file or executed function ran to its end, report the
1241 * unclosed conditional.
1242 */
1243 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001244 && ((getline_equal(fgetline, cookie, getsourceline)
1245 && !source_finished(fgetline, cookie))
1246 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 && !func_has_ended(real_cookie))))
1248 {
1249 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001256 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
1259 /*
1260 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1261 * ":endtry" in a sourced file or executed function. If the try
1262 * conditional is in its finally clause, ignore anything pending.
1263 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001264 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 */
1266 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001267 {
1268 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1269
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001270 if (idx >= 0)
1271 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001272 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1273 &cstack.cs_looplevel);
1274 }
1275 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 trylevel = initial_trylevel;
1277 }
1278
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001279 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1280 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001282 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 ? (char_u *)"endfunction" : (char_u *)NULL);
1284
1285 if (trylevel == 0)
1286 {
1287 /*
1288 * When an exception is being thrown out of the outermost try
1289 * conditional, discard the uncaught exception, disable the conversion
1290 * of interrupts or errors to exceptions, and ensure that no more
1291 * commands are executed.
1292 */
1293 if (did_throw)
1294 {
1295 void *p = NULL;
1296 char_u *saved_sourcing_name;
1297 int saved_sourcing_lnum;
1298 struct msglist *messages = NULL, *next;
1299
1300 /*
1301 * If the uncaught exception is a user exception, report it as an
1302 * error. If it is an error exception, display the saved error
1303 * message now. For an interrupt exception, do nothing; the
1304 * interrupt message is given elsewhere.
1305 */
1306 switch (current_exception->type)
1307 {
1308 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001309 vim_snprintf((char *)IObuff, IOSIZE,
1310 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 current_exception->value);
1312 p = vim_strsave(IObuff);
1313 break;
1314 case ET_ERROR:
1315 messages = current_exception->messages;
1316 current_exception->messages = NULL;
1317 break;
1318 case ET_INTERRUPT:
1319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321
1322 saved_sourcing_name = sourcing_name;
1323 saved_sourcing_lnum = sourcing_lnum;
1324 sourcing_name = current_exception->throw_name;
1325 sourcing_lnum = current_exception->throw_lnum;
1326 current_exception->throw_name = NULL;
1327
1328 discard_current_exception(); /* uses IObuff if 'verbose' */
1329 suppress_errthrow = TRUE;
1330 force_abort = TRUE;
1331
1332 if (messages != NULL)
1333 {
1334 do
1335 {
1336 next = messages->next;
1337 emsg(messages->msg);
1338 vim_free(messages->msg);
1339 vim_free(messages);
1340 messages = next;
1341 }
1342 while (messages != NULL);
1343 }
1344 else if (p != NULL)
1345 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001346 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 vim_free(p);
1348 }
1349 vim_free(sourcing_name);
1350 sourcing_name = saved_sourcing_name;
1351 sourcing_lnum = saved_sourcing_lnum;
1352 }
1353
1354 /*
1355 * On an interrupt or an aborting error not converted to an exception,
1356 * disable the conversion of errors to exceptions. (Interrupts are not
1357 * converted any more, here.) This enables also the interrupt message
1358 * when force_abort is set and did_emsg unset in case of an interrupt
1359 * from a finally clause after an error.
1360 */
1361 else if (got_int || (did_emsg && force_abort))
1362 suppress_errthrow = TRUE;
1363 }
1364
1365 /*
1366 * The current cstack will be freed when do_cmdline() returns. An uncaught
1367 * exception will have to be rethrown in the previous cstack. If a function
1368 * has just returned or a script file was just finished and the previous
1369 * cstack belongs to the same function or, respectively, script file, it
1370 * will have to be checked for finally clauses to be executed due to the
1371 * ":return" or ":finish". This is done in do_one_cmd().
1372 */
1373 if (did_throw)
1374 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001375 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001377 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 && ex_nesting_level > func_level(real_cookie) + 1))
1379 {
1380 if (!did_throw)
1381 check_cstack = TRUE;
1382 }
1383 else
1384 {
1385 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001386 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 --ex_nesting_level;
1388 /*
1389 * Go to debug mode when returning from a function in which we are
1390 * single-stepping.
1391 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001392 if ((getline_equal(fgetline, cookie, getsourceline)
1393 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001395 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ? (char_u *)_("End of sourced file")
1397 : (char_u *)_("End of function"));
1398 }
1399
1400 /*
1401 * Restore the exception environment (done after returning from the
1402 * debugger).
1403 */
1404 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001405 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
1407 msg_list = saved_msg_list;
1408#endif /* FEAT_EVAL */
1409
1410 /*
1411 * If there was too much output to fit on the command line, ask the user to
1412 * hit return before redrawing the screen. With the ":global" command we do
1413 * this only once after the command is finished.
1414 */
1415 if (did_inc)
1416 {
1417 --RedrawingDisabled;
1418 --no_wait_return;
1419 msg_scroll = FALSE;
1420
1421 /*
1422 * When just finished an ":if"-":else" which was typed, no need to
1423 * wait for hit-return. Also for an error situation.
1424 */
1425 if (retval == FAIL
1426#ifdef FEAT_EVAL
1427 || (did_endif && KeyTyped && !did_emsg)
1428#endif
1429 )
1430 {
1431 need_wait_return = FALSE;
1432 msg_didany = FALSE; /* don't wait when restarting edit */
1433 }
1434 else if (need_wait_return)
1435 {
1436 /*
1437 * The msg_start() above clears msg_didout. The wait_return we do
1438 * here should not overwrite the command that may be shown before
1439 * doing that.
1440 */
1441 msg_didout |= msg_didout_before_start;
1442 wait_return(FALSE);
1443 }
1444 }
1445
Bram Moolenaar2a942252012-11-28 23:03:07 +01001446#ifdef FEAT_EVAL
1447 did_endif = FALSE; /* in case do_cmdline used recursively */
1448#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 /*
1450 * Reset if_level, in case a sourced script file contains more ":if" than
1451 * ":endif" (could be ":if x | foo | endif").
1452 */
1453 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001454#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 --call_depth;
1457 return retval;
1458}
1459
1460#ifdef FEAT_EVAL
1461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001462 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001467 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 wcmd_T *wp;
1469 char_u *line;
1470
1471 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1472 {
1473 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001474 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001476 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (cp->getline == NULL)
1478 line = getcmdline(c, 0L, indent);
1479 else
1480 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001481 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 ++cp->current_line;
1483
1484 return line;
1485 }
1486
1487 KeyTyped = FALSE;
1488 ++cp->current_line;
1489 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1490 sourcing_lnum = wp->lnum;
1491 return vim_strsave(wp->line);
1492}
1493
1494/*
1495 * Store a line in "gap" so that a ":while" loop can execute it again.
1496 */
1497 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 if (ga_grow(gap, 1) == FAIL)
1501 return FAIL;
1502 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1503 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1504 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 return OK;
1506}
1507
1508/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 while (gap->ga_len > 0)
1515 {
1516 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1517 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519}
1520#endif
1521
1522/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1524 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001527getline_equal(
1528 char_u *(*fgetline)(int, void *, int),
1529 void *cookie UNUSED, /* argument for fgetline() */
1530 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001533 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001537 * function that's originally used to obtain the lines. This may be
1538 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540 cp = (struct loop_cookie *)cookie;
1541 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 gp = cp->getline;
1544 cp = cp->cookie;
1545 }
1546 return gp == func;
1547#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001548 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
1550}
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001553 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 * getline function. Otherwise return "cookie".
1555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001557getline_cookie(
1558 char_u *(*fgetline)(int, void *, int) UNUSED,
1559 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Bram Moolenaar13505972019-01-24 15:04:48 +01001561#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001562 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaar89d40322006-08-29 15:30:07 +00001565 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001566 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001568 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001569 cp = (struct loop_cookie *)cookie;
1570 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572 gp = cp->getline;
1573 cp = cp->cookie;
1574 }
1575 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001576#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001579}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581
1582/*
1583 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1584 * ":bwipeout", etc.
1585 * Returns the buffer number.
1586 */
1587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001589{
1590 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001592 int count = offset;
1593
1594 buf = firstbuf;
1595 while (buf->b_next != NULL && buf->b_fnum < lnum)
1596 buf = buf->b_next;
1597 while (count != 0)
1598 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001599 count += (offset < 0) ? 1 : -1;
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001603 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001604 if (addr_type == ADDR_LOADED_BUFFERS)
1605 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001606 while (buf->b_ml.ml_mfp == NULL)
1607 {
1608 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1609 if (nextbuf == NULL)
1610 break;
1611 buf = nextbuf;
1612 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001614 /* we might have gone too far, last buffer is not loadedd */
1615 if (addr_type == ADDR_LOADED_BUFFERS)
1616 while (buf->b_ml.ml_mfp == NULL)
1617 {
1618 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
1620 break;
1621 buf = nextbuf;
1622 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 return buf->b_fnum;
1624}
1625
Bram Moolenaarf240e182014-11-27 18:33:02 +01001626 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001627current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001628{
1629 win_T *wp;
1630 int nr = 0;
1631
Bram Moolenaar29323592016-07-24 22:04:11 +02001632 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001633 {
1634 ++nr;
1635 if (wp == win)
1636 break;
1637 }
1638 return nr;
1639}
1640
1641 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001643{
1644 tabpage_T *tp;
1645 int nr = 0;
1646
Bram Moolenaar29323592016-07-24 22:04:11 +02001647 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648 {
1649 ++nr;
1650 if (tp == tab)
1651 break;
1652 }
1653 return nr;
1654}
1655
1656# define CURRENT_WIN_NR current_win_nr(curwin)
1657# define LAST_WIN_NR current_win_nr(NULL)
1658# define CURRENT_TAB_NR current_tab_nr(curtab)
1659# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
1662 * Execute one Ex command.
1663 *
1664 * If 'sourcing' is TRUE, the command will be included in the error message.
1665 *
1666 * 1. skip comment lines and leading space
1667 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001668 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001669 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001670 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001671 * 6. parse arguments
1672 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001674 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 *
1676 * This function may be called recursively!
1677 */
1678#if (_MSC_VER == 1200)
1679/*
Bram Moolenaared203462004-06-16 11:19:22 +00001680 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001682 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683#endif
1684 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001685do_one_cmd(
1686 char_u **cmdlinep,
1687 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001689 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001691 char_u *(*fgetline)(int, void *, int),
1692 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 char_u *p;
1695 linenr_T lnum;
1696 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001697 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001698 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001700 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 cmdmod_T save_cmdmod;
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 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002010 // 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
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002015 if (restricted != 0 && (ea.argt & RESTRICT))
2016 {
2017 errormsg = _("E981: Command not allowed in rvim");
2018 goto doend;
2019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2021 {
2022 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002023 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 goto doend;
2025 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002026
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002027 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002028 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002030 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002031 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 goto doend;
2033 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002034
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002035 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002036 * Do allow ":checktime" (it is postponed).
2037 * Do allow ":edit" (check for an argument later).
2038 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002040 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002041 && ea.cmdidx != CMD_edit
2042 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002043 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 && curbuf_locked())
2045 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2048 {
2049 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002050 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 goto doend;
2052 }
2053 }
2054
2055 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002057 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 goto doend;
2059 }
2060
2061 /*
2062 * Don't complain about the range if it is not used
2063 * (could happen if line_count is accidentally set to 0).
2064 */
2065 if (!ea.skip && !ni)
2066 {
2067 /*
2068 * If the range is backwards, ask for confirmation and, if given, swap
2069 * ea.line1 & ea.line2 so it's forwards again.
2070 * When global command is busy, don't ask, will fail below.
2071 */
2072 if (!global_busy && ea.line1 > ea.line2)
2073 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002074 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002076 if (sourcing || exmode_active)
2077 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002078 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002079 goto doend;
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (ask_yesno((char_u *)
2082 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002083 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 lnum = ea.line1;
2086 ea.line1 = ea.line2;
2087 ea.line2 = lnum;
2088 }
2089 if ((errormsg = invalid_range(&ea)) != NULL)
2090 goto doend;
2091 }
2092
2093 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2094 ea.line2 = 1;
2095
2096 correct_range(&ea);
2097
2098#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002099 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2100 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 /* Put the first line at the start of a closed fold, put the last line
2103 * at the end of a closed fold. */
2104 (void)hasFolding(ea.line1, &ea.line1, NULL);
2105 (void)hasFolding(ea.line2, NULL, &ea.line2);
2106 }
2107#endif
2108
2109#ifdef FEAT_QUICKFIX
2110 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002111 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 * option here, so things like % get expanded.
2113 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002114 p = replace_makeprg(&ea, p, cmdlinep);
2115 if (p == NULL)
2116 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
2118
2119 /*
2120 * Skip to start of argument.
2121 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2122 */
2123 if (ea.cmdidx == CMD_bang)
2124 ea.arg = p;
2125 else
2126 ea.arg = skipwhite(p);
2127
Bram Moolenaar379fb762018-08-30 15:58:28 +02002128 // ":file" cannot be run with an argument when "curbuf_lock" is set
2129 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2130 goto doend;
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 /*
2133 * Check for "++opt=val" argument.
2134 * Must be first, allow ":w ++enc=utf8 !cmd"
2135 */
2136 if (ea.argt & ARGOPT)
2137 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2138 if (getargopt(&ea) == FAIL && !ni)
2139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002140 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 goto doend;
2142 }
2143
2144 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2145 {
2146 if (*ea.arg == '>') /* append */
2147 {
2148 if (*++ea.arg != '>') /* typed wrong */
2149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002150 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 goto doend;
2152 }
2153 ea.arg = skipwhite(ea.arg + 1);
2154 ea.append = TRUE;
2155 }
2156 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2157 {
2158 ++ea.arg;
2159 ea.usefilter = TRUE;
2160 }
2161 }
2162
2163 if (ea.cmdidx == CMD_read)
2164 {
2165 if (ea.forceit)
2166 {
2167 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2168 ea.forceit = FALSE;
2169 }
2170 else if (*ea.arg == '!') /* :r !filter */
2171 {
2172 ++ea.arg;
2173 ea.usefilter = TRUE;
2174 }
2175 }
2176
2177 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2178 {
2179 ea.amount = 1;
2180 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2181 {
2182 ++ea.arg;
2183 ++ea.amount;
2184 }
2185 ea.arg = skipwhite(ea.arg);
2186 }
2187
2188 /*
2189 * Check for "+command" argument, before checking for next command.
2190 * Don't do this for ":read !cmd" and ":write !cmd".
2191 */
2192 if ((ea.argt & EDITCMD) && !ea.usefilter)
2193 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2194
2195 /*
2196 * Check for '|' to separate commands and '"' to start comments.
2197 * Don't do this for ":read !cmd" and ":write !cmd".
2198 */
2199 if ((ea.argt & TRLBAR) && !ea.usefilter)
2200 separate_nextcmd(&ea);
2201
2202 /*
2203 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002204 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2205 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002207 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002208 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002209 || ea.cmdidx == CMD_global
2210 || ea.cmdidx == CMD_vglobal
2211 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 for (p = ea.arg; *p; ++p)
2214 {
2215 /* Remove one backslash before a newline, so that it's possible to
2216 * pass a newline to the shell and also a newline that is preceded
2217 * with a backslash. This makes it impossible to end a shell
2218 * command in a backslash, but that doesn't appear useful.
2219 * Halving the number of backslashes is incompatible with previous
2220 * versions. */
2221 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002222 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 else if (*p == '\n')
2224 {
2225 ea.nextcmd = p + 1;
2226 *p = NUL;
2227 break;
2228 }
2229 }
2230 }
2231
2232 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2233 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002234 buf_T *buf;
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002237 switch (ea.addr_type)
2238 {
2239 case ADDR_LINES:
2240 ea.line2 = curbuf->b_ml.ml_line_count;
2241 break;
2242 case ADDR_LOADED_BUFFERS:
2243 buf = firstbuf;
2244 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2245 buf = buf->b_next;
2246 ea.line1 = buf->b_fnum;
2247 buf = lastbuf;
2248 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2249 buf = buf->b_prev;
2250 ea.line2 = buf->b_fnum;
2251 break;
2252 case ADDR_BUFFERS:
2253 ea.line1 = firstbuf->b_fnum;
2254 ea.line2 = lastbuf->b_fnum;
2255 break;
2256 case ADDR_WINDOWS:
2257 ea.line2 = LAST_WIN_NR;
2258 break;
2259 case ADDR_TABS:
2260 ea.line2 = LAST_TAB_NR;
2261 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002262 case ADDR_TABS_RELATIVE:
2263 ea.line2 = 1;
2264 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002265 case ADDR_ARGUMENTS:
2266 if (ARGCOUNT == 0)
2267 ea.line1 = ea.line2 = 0;
2268 else
2269 ea.line2 = ARGCOUNT;
2270 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002271#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002272 case ADDR_QUICKFIX:
2273 ea.line2 = qf_get_size(&ea);
2274 if (ea.line2 == 0)
2275 ea.line2 = 1;
2276 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002277#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 /* accept numbered register only when no count allowed (:put) */
2282 if ( (ea.argt & REGSTR)
2283 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002284 /* Do not allow register = for user commands */
2285 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2287 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002288#ifndef FEAT_CLIPBOARD
2289 /* check these explicitly for a more specific error message */
2290 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002292 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002293 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002296 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2297 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002298 {
2299 ea.regname = *ea.arg++;
2300#ifdef FEAT_EVAL
2301 /* for '=' register: accept the rest of the line as an expression */
2302 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2303 {
2304 set_expr_line(vim_strsave(ea.arg));
2305 ea.arg += STRLEN(ea.arg);
2306 }
2307#endif
2308 ea.arg = skipwhite(ea.arg);
2309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311
2312 /*
2313 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2314 * count, it's a buffer name.
2315 */
2316 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2317 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002318 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 n = getdigits(&ea.arg);
2321 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002322 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002324 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 goto doend;
2326 }
2327 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2328 {
2329 ea.line2 = n;
2330 if (ea.addr_count == 0)
2331 ea.addr_count = 1;
2332 }
2333 else
2334 {
2335 ea.line1 = ea.line2;
2336 ea.line2 += n - 1;
2337 ++ea.addr_count;
2338 /*
2339 * Be vi compatible: no error message for out of range.
2340 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002341 if (ea.addr_type == ADDR_LINES
2342 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 ea.line2 = curbuf->b_ml.ml_line_count;
2344 }
2345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002346
2347 /*
2348 * Check for flags: 'l', 'p' and '#'.
2349 */
2350 if (ea.argt & EXFLAGS)
2351 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002353 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002354 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002356 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 goto doend;
2358 }
2359
2360 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002362 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 goto doend;
2364 }
2365
2366#ifdef FEAT_EVAL
2367 /*
2368 * Skip the command when it's not going to be executed.
2369 * The commands like :if, :endif, etc. always need to be executed.
2370 * Also make an exception for commands that handle a trailing command
2371 * themselves.
2372 */
2373 if (ea.skip)
2374 {
2375 switch (ea.cmdidx)
2376 {
2377 /* commands that need evaluation */
2378 case CMD_while:
2379 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002380 case CMD_for:
2381 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_if:
2383 case CMD_elseif:
2384 case CMD_else:
2385 case CMD_endif:
2386 case CMD_try:
2387 case CMD_catch:
2388 case CMD_finally:
2389 case CMD_endtry:
2390 case CMD_function:
2391 break;
2392
2393 /* Commands that handle '|' themselves. Check: A command should
2394 * either have the TRLBAR flag, appear in this list or appear in
2395 * the list at ":help :bar". */
2396 case CMD_aboveleft:
2397 case CMD_and:
2398 case CMD_belowright:
2399 case CMD_botright:
2400 case CMD_browse:
2401 case CMD_call:
2402 case CMD_confirm:
2403 case CMD_delfunction:
2404 case CMD_djump:
2405 case CMD_dlist:
2406 case CMD_dsearch:
2407 case CMD_dsplit:
2408 case CMD_echo:
2409 case CMD_echoerr:
2410 case CMD_echomsg:
2411 case CMD_echon:
2412 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002413 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_help:
2415 case CMD_hide:
2416 case CMD_ijump:
2417 case CMD_ilist:
2418 case CMD_isearch:
2419 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002420 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_keepjumps:
2422 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002423 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_leftabove:
2425 case CMD_let:
2426 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002427 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002429 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002430 case CMD_noautocmd:
2431 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_perl:
2433 case CMD_psearch:
2434 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002435 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002436 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_return:
2438 case CMD_rightbelow:
2439 case CMD_ruby:
2440 case CMD_silent:
2441 case CMD_smagic:
2442 case CMD_snomagic:
2443 case CMD_substitute:
2444 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002445 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_tcl:
2447 case CMD_throw:
2448 case CMD_tilde:
2449 case CMD_topleft:
2450 case CMD_unlet:
2451 case CMD_verbose:
2452 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002453 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 break;
2455
2456 default: goto doend;
2457 }
2458 }
2459#endif
2460
2461 if (ea.argt & XFILE)
2462 {
2463 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2464 goto doend;
2465 }
2466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 /*
2468 * Accept buffer name. Cannot be used at the same time with a buffer
2469 * number. Don't do this for a user command.
2470 */
2471 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002472 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 /*
2475 * :bdelete, :bwipeout and :bunload take several arguments, separated
2476 * by spaces: find next space (skipping over escaped characters).
2477 * The others take one argument: ignore trailing spaces.
2478 */
2479 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2480 || ea.cmdidx == CMD_bunload)
2481 p = skiptowhite_esc(ea.arg);
2482 else
2483 {
2484 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002485 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 --p;
2487 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002488 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2489 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (ea.line2 < 0) /* failed */
2491 goto doend;
2492 ea.addr_count = 1;
2493 ea.arg = skipwhite(p);
2494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar2be57332018-02-13 18:05:18 +01002496 /* The :try command saves the emsg_silent flag, reset it here when
2497 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002498 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002499 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002500 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002501 if (emsg_silent < 0)
2502 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002503 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002504 }
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002507 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002511 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
2513 /*
2514 * Execute a user-defined command.
2515 */
2516 do_ucmd(&ea);
2517 }
2518 else
2519#endif
2520 {
2521 /*
2522 * Call the function to execute the command.
2523 */
2524 ea.errmsg = NULL;
2525 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2526 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002527 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529
2530#ifdef FEAT_EVAL
2531 /*
2532 * If the command just executed called do_cmdline(), any throw or ":return"
2533 * or ":finish" encountered there must also check the cstack of the still
2534 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2535 * exception, or reanimate a returned function or finished script file and
2536 * return or finish it again.
2537 */
2538 if (need_rethrow)
2539 do_throw(cstack);
2540 else if (check_cstack)
2541 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002542 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002544 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 && current_func_returned())
2546 do_return(&ea, TRUE, FALSE, NULL);
2547 }
2548 need_rethrow = check_cstack = FALSE;
2549#endif
2550
2551doend:
2552 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002555 curwin->w_cursor.col = 0;
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2559 {
2560 if (sourcing)
2561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002562 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002565 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002567 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 emsg(errormsg);
2570 }
2571#ifdef FEAT_EVAL
2572 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002573 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2574 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575#endif
2576
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 if (ea.verbose_save >= 0)
2578 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002579
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002580 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 cmdmod = save_cmdmod;
2582
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 /* messages could be enabled for a serious error, need to check if the
2586 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002587 if (!did_emsg || msg_silent > ea.save_msg_silent)
2588 msg_silent = ea.save_msg_silent;
2589 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (emsg_silent < 0)
2591 emsg_silent = 0;
2592 /* Restore msg_scroll, it's set by file I/O commands, even when no
2593 * message is actually displayed. */
2594 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002595
2596 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2597 * somewhere in the line. Put it back in the first column. */
2598 if (redirecting())
2599 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002602#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002604 --sandbox;
2605#endif
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2608 ea.nextcmd = NULL;
2609
2610#ifdef FEAT_EVAL
2611 --ex_nesting_level;
2612#endif
2613
2614 return ea.nextcmd;
2615}
2616#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002617 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#endif
2619
2620/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 * Parse and skip over command modifiers:
2622 * - update eap->cmd
2623 * - store flags in "cmdmod".
2624 * - Set ex_pressedreturn for an empty command line.
2625 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002626 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 * - set p_verbose for ":verbose"
2628 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002629 * When "skip_only" is TRUE the global variables are not changed, except for
2630 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002631 * Return FAIL when the command is not to be executed.
2632 * May set "errormsg" to an error message.
2633 */
2634 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002635parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002636{
2637 char_u *p;
2638
2639 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2640 eap->verbose_save = -1;
2641 eap->save_msg_silent = -1;
2642
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002643 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 for (;;)
2645 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2647 ++eap->cmd;
2648
2649 /* in ex mode, an empty line works like :+ */
2650 if (*eap->cmd == NUL && exmode_active
2651 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2652 || getline_equal(eap->getline, eap->cookie, getexline))
2653 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2654 {
2655 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002656 if (!skip_only)
2657 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002658 }
2659
2660 /* ignore comment and empty lines */
2661 if (*eap->cmd == '"')
2662 return FAIL;
2663 if (*eap->cmd == NUL)
2664 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002665 if (!skip_only)
2666 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 return FAIL;
2668 }
2669
Bram Moolenaareffed932018-08-14 13:38:17 +02002670 p = skip_range(eap->cmd, NULL);
2671 switch (*p)
2672 {
2673 /* When adding an entry, also modify cmd_exists(). */
2674 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2675 break;
2676 cmdmod.split |= WSP_ABOVE;
2677 continue;
2678
2679 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2680 {
2681 cmdmod.split |= WSP_BELOW;
2682 continue;
2683 }
2684 if (checkforcmd(&eap->cmd, "browse", 3))
2685 {
2686#ifdef FEAT_BROWSE_CMD
2687 cmdmod.browse = TRUE;
2688#endif
2689 continue;
2690 }
2691 if (!checkforcmd(&eap->cmd, "botright", 2))
2692 break;
2693 cmdmod.split |= WSP_BOT;
2694 continue;
2695
2696 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2697 break;
2698#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2699 cmdmod.confirm = TRUE;
2700#endif
2701 continue;
2702
2703 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2704 {
2705 cmdmod.keepmarks = TRUE;
2706 continue;
2707 }
2708 if (checkforcmd(&eap->cmd, "keepalt", 5))
2709 {
2710 cmdmod.keepalt = TRUE;
2711 continue;
2712 }
2713 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2714 {
2715 cmdmod.keeppatterns = TRUE;
2716 continue;
2717 }
2718 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2719 break;
2720 cmdmod.keepjumps = TRUE;
2721 continue;
2722
2723 case 'f': /* only accept ":filter {pat} cmd" */
2724 {
2725 char_u *reg_pat;
2726
2727 if (!checkforcmd(&p, "filter", 4)
2728 || *p == NUL || ends_excmd(*p))
2729 break;
2730 if (*p == '!')
2731 {
2732 cmdmod.filter_force = TRUE;
2733 p = skipwhite(p + 1);
2734 if (*p == NUL || ends_excmd(*p))
2735 break;
2736 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002737 if (skip_only)
2738 p = skip_vimgrep_pat(p, NULL, NULL);
2739 else
2740 // NOTE: This puts a NUL after the pattern.
2741 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002742 if (p == NULL || *p == NUL)
2743 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002744 if (!skip_only)
2745 {
2746 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002748 if (cmdmod.filter_regmatch.regprog == NULL)
2749 break;
2750 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 eap->cmd = p;
2752 continue;
2753 }
2754
2755 /* ":hide" and ":hide | cmd" are not modifiers */
2756 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2757 || *p == NUL || ends_excmd(*p))
2758 break;
2759 eap->cmd = p;
2760 cmdmod.hide = TRUE;
2761 continue;
2762
2763 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2764 {
2765 cmdmod.lockmarks = TRUE;
2766 continue;
2767 }
2768
2769 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2770 break;
2771 cmdmod.split |= WSP_ABOVE;
2772 continue;
2773
2774 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2775 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 {
2778 /* Set 'eventignore' to "all". Restore the
2779 * existing option value later. */
2780 cmdmod.save_ei = vim_strsave(p_ei);
2781 set_string_option_direct((char_u *)"ei", -1,
2782 (char_u *)"all", OPT_FREE, SID_NONE);
2783 }
2784 continue;
2785 }
2786 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2787 break;
2788 cmdmod.noswapfile = TRUE;
2789 continue;
2790
2791 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2792 break;
2793 cmdmod.split |= WSP_BELOW;
2794 continue;
2795
2796 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2797 {
2798#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (!skip_only)
2800 {
2801 if (!eap->did_sandbox)
2802 ++sandbox;
2803 eap->did_sandbox = TRUE;
2804 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002805#endif
2806 continue;
2807 }
2808 if (!checkforcmd(&eap->cmd, "silent", 3))
2809 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 if (!skip_only)
2811 {
2812 if (eap->save_msg_silent == -1)
2813 eap->save_msg_silent = msg_silent;
2814 ++msg_silent;
2815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2817 {
2818 /* ":silent!", but not "silent !cmd" */
2819 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 {
2822 ++emsg_silent;
2823 ++eap->did_esilent;
2824 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 }
2826 continue;
2827
2828 case 't': if (checkforcmd(&p, "tab", 3))
2829 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002830 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002832 long tabnr = get_address(eap, &eap->cmd,
2833 ADDR_TABS, eap->skip,
2834 skip_only, FALSE, 1);
2835 if (tabnr == MAXLNUM)
2836 cmdmod.tab = tabpage_index(curtab) + 1;
2837 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002839 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002841 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002842 return FAIL;
2843 }
2844 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
2847 eap->cmd = p;
2848 continue;
2849 }
2850 if (!checkforcmd(&eap->cmd, "topleft", 2))
2851 break;
2852 cmdmod.split |= WSP_TOP;
2853 continue;
2854
2855 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2856 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002857 if (!skip_only)
2858 {
2859 if (eap->save_msg_silent == -1)
2860 eap->save_msg_silent = msg_silent;
2861 msg_silent = 0;
2862 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 continue;
2864
2865 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2866 {
2867 cmdmod.split |= WSP_VERT;
2868 continue;
2869 }
2870 if (!checkforcmd(&p, "verbose", 4))
2871 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002872 if (!skip_only)
2873 {
2874 if (eap->verbose_save < 0)
2875 eap->verbose_save = p_verbose;
2876 if (vim_isdigit(*eap->cmd))
2877 p_verbose = atoi((char *)eap->cmd);
2878 else
2879 p_verbose = 1;
2880 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 eap->cmd = p;
2882 continue;
2883 }
2884 break;
2885 }
2886
2887 return OK;
2888}
2889
2890/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002891 * Free contents of "cmdmod".
2892 */
2893 static void
2894free_cmdmod(void)
2895{
2896 if (cmdmod.save_ei != NULL)
2897 {
2898 /* Restore 'eventignore' to the value before ":noautocmd". */
2899 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2900 OPT_FREE, SID_NONE);
2901 free_string_option(cmdmod.save_ei);
2902 }
2903
2904 if (cmdmod.filter_regmatch.regprog != NULL)
2905 vim_regfree(cmdmod.filter_regmatch.regprog);
2906}
2907
2908/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002910 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002911 * Return FAIL and set "errormsg" or return OK.
2912 */
2913 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915{
2916 int address_count = 1;
2917 linenr_T lnum;
2918
2919 // Repeat for all ',' or ';' separated addresses.
2920 for (;;)
2921 {
2922 eap->line1 = eap->line2;
2923 switch (eap->addr_type)
2924 {
2925 case ADDR_LINES:
2926 // default is current line number
2927 eap->line2 = curwin->w_cursor.lnum;
2928 break;
2929 case ADDR_WINDOWS:
2930 eap->line2 = CURRENT_WIN_NR;
2931 break;
2932 case ADDR_ARGUMENTS:
2933 eap->line2 = curwin->w_arg_idx + 1;
2934 if (eap->line2 > ARGCOUNT)
2935 eap->line2 = ARGCOUNT;
2936 break;
2937 case ADDR_LOADED_BUFFERS:
2938 case ADDR_BUFFERS:
2939 eap->line2 = curbuf->b_fnum;
2940 break;
2941 case ADDR_TABS:
2942 eap->line2 = CURRENT_TAB_NR;
2943 break;
2944 case ADDR_TABS_RELATIVE:
2945 eap->line2 = 1;
2946 break;
2947#ifdef FEAT_QUICKFIX
2948 case ADDR_QUICKFIX:
2949 eap->line2 = qf_get_cur_valid_idx(eap);
2950 break;
2951#endif
2952 }
2953 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002954 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002955 eap->addr_count == 0, address_count++);
2956 if (eap->cmd == NULL) // error detected
2957 return FAIL;
2958 if (lnum == MAXLNUM)
2959 {
2960 if (*eap->cmd == '%') // '%' - all lines
2961 {
2962 ++eap->cmd;
2963 switch (eap->addr_type)
2964 {
2965 case ADDR_LINES:
2966 eap->line1 = 1;
2967 eap->line2 = curbuf->b_ml.ml_line_count;
2968 break;
2969 case ADDR_LOADED_BUFFERS:
2970 {
2971 buf_T *buf = firstbuf;
2972
2973 while (buf->b_next != NULL
2974 && buf->b_ml.ml_mfp == NULL)
2975 buf = buf->b_next;
2976 eap->line1 = buf->b_fnum;
2977 buf = lastbuf;
2978 while (buf->b_prev != NULL
2979 && buf->b_ml.ml_mfp == NULL)
2980 buf = buf->b_prev;
2981 eap->line2 = buf->b_fnum;
2982 break;
2983 }
2984 case ADDR_BUFFERS:
2985 eap->line1 = firstbuf->b_fnum;
2986 eap->line2 = lastbuf->b_fnum;
2987 break;
2988 case ADDR_WINDOWS:
2989 case ADDR_TABS:
2990 if (IS_USER_CMDIDX(eap->cmdidx))
2991 {
2992 eap->line1 = 1;
2993 eap->line2 = eap->addr_type == ADDR_WINDOWS
2994 ? LAST_WIN_NR : LAST_TAB_NR;
2995 }
2996 else
2997 {
2998 // there is no Vim command which uses '%' and
2999 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003000 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003001 return FAIL;
3002 }
3003 break;
3004 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003005 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003006 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 return FAIL;
3008 case ADDR_ARGUMENTS:
3009 if (ARGCOUNT == 0)
3010 eap->line1 = eap->line2 = 0;
3011 else
3012 {
3013 eap->line1 = 1;
3014 eap->line2 = ARGCOUNT;
3015 }
3016 break;
3017#ifdef FEAT_QUICKFIX
3018 case ADDR_QUICKFIX:
3019 eap->line1 = 1;
3020 eap->line2 = qf_get_size(eap);
3021 if (eap->line2 == 0)
3022 eap->line2 = 1;
3023 break;
3024#endif
3025 }
3026 ++eap->addr_count;
3027 }
3028 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3029 {
3030 pos_T *fp;
3031
3032 // '*' - visual area
3033 if (eap->addr_type != ADDR_LINES)
3034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003035 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003036 return FAIL;
3037 }
3038
3039 ++eap->cmd;
3040 if (!eap->skip)
3041 {
3042 fp = getmark('<', FALSE);
3043 if (check_mark(fp) == FAIL)
3044 return FAIL;
3045 eap->line1 = fp->lnum;
3046 fp = getmark('>', FALSE);
3047 if (check_mark(fp) == FAIL)
3048 return FAIL;
3049 eap->line2 = fp->lnum;
3050 ++eap->addr_count;
3051 }
3052 }
3053 }
3054 else
3055 eap->line2 = lnum;
3056 eap->addr_count++;
3057
3058 if (*eap->cmd == ';')
3059 {
3060 if (!eap->skip)
3061 {
3062 curwin->w_cursor.lnum = eap->line2;
3063 // don't leave the cursor on an illegal line or column
3064 check_cursor();
3065 }
3066 }
3067 else if (*eap->cmd != ',')
3068 break;
3069 ++eap->cmd;
3070 }
3071
3072 // One address given: set start and end lines.
3073 if (eap->addr_count == 1)
3074 {
3075 eap->line1 = eap->line2;
3076 // ... but only implicit: really no address given
3077 if (lnum == MAXLNUM)
3078 eap->addr_count = 0;
3079 }
3080 return OK;
3081}
3082
3083/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003084 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * If there is a match advance "pp" to the argument and return TRUE.
3086 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003087 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088checkforcmd(
3089 char_u **pp, /* start of command */
3090 char *cmd, /* name of command */
3091 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 int i;
3094
3095 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003096 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 break;
3098 if (i >= len && !isalpha((*pp)[i]))
3099 {
3100 *pp = skipwhite(*pp + i);
3101 return TRUE;
3102 }
3103 return FALSE;
3104}
3105
3106/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003107 * Append "cmd" to the error message in IObuff.
3108 * Takes care of limiting the length and handling 0xa0, which would be
3109 * invisible otherwise.
3110 */
3111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003112append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003113{
3114 char_u *s = cmd;
3115 char_u *d;
3116
3117 STRCAT(IObuff, ": ");
3118 d = IObuff + STRLEN(IObuff);
3119 while (*s != NUL && d - IObuff < IOSIZE - 7)
3120 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003121 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003122 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003123 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003124 STRCPY(d, "<a0>");
3125 d += 4;
3126 }
3127 else
3128 MB_COPY_CHAR(s, d);
3129 }
3130 *d = NUL;
3131}
3132
3133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003135 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003137 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Returns NULL for an ambiguous user command.
3139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003141find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 int len;
3144 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003145 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 /*
3148 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003149 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 * - the 'k' command can directly be followed by any character.
3151 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003152 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003154 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 */
3156 p = eap->cmd;
3157 if (*p == 'k')
3158 {
3159 eap->cmdidx = CMD_k;
3160 ++p;
3161 }
3162 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003163 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3164 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 || p[1] == 'g'
3166 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3167 || p[1] == 'I'
3168 || (p[1] == 'r' && p[2] != 'e')))
3169 {
3170 eap->cmdidx = CMD_substitute;
3171 ++p;
3172 }
3173 else
3174 {
3175 while (ASCII_ISALPHA(*p))
3176 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003177 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003178 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003179 while (ASCII_ISALNUM(*p))
3180 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 /* check for non-alpha command */
3183 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3184 ++p;
3185 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003186 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3187 {
3188 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3189 * :delete with the 'l' flag. Same for 'p'. */
3190 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003191 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003192 break;
3193 if (i == len - 1)
3194 {
3195 --len;
3196 if (p[-1] == 'l')
3197 eap->flags |= EXFLAG_LIST;
3198 else
3199 eap->flags |= EXFLAG_PRINT;
3200 }
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003203 if (ASCII_ISLOWER(eap->cmd[0]))
3204 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003205 int c1 = eap->cmd[0];
3206 int c2 = eap->cmd[1];
3207
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003208 if (command_count != (int)CMD_SIZE)
3209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003210 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003211 getout(1);
3212 }
3213
3214 /* Use a precomputed index for fast look-up in cmdnames[]
3215 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003216 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3217 if (ASCII_ISLOWER(c2))
3218 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003221 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3224 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3225 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3226 (size_t)len) == 0)
3227 {
3228#ifdef FEAT_EVAL
3229 if (full != NULL
3230 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3231 *full = TRUE;
3232#endif
3233 break;
3234 }
3235
3236#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003237 /* Look for a user defined command as a last resort. Let ":Print" be
3238 * overruled by a user defined command. */
3239 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3240 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003242 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 while (ASCII_ISALNUM(*p))
3244 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 eap->cmdidx = CMD_SIZE;
3250 }
3251
3252 return p;
3253}
3254
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003255#ifdef FEAT_USR_CMDS
3256/*
3257 * Search for a user command that matches "eap->cmd".
3258 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3259 * Return a pointer to just after the command.
3260 * Return NULL if there is no matching command.
3261 */
3262 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003263find_ucmd(
3264 exarg_T *eap,
3265 char_u *p, /* end of the command (possibly including count) */
3266 int *full, /* set to TRUE for a full match */
3267 expand_T *xp, /* used for completion, NULL otherwise */
3268 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003269{
3270 int len = (int)(p - eap->cmd);
3271 int j, k, matchlen = 0;
3272 ucmd_T *uc;
3273 int found = FALSE;
3274 int possible = FALSE;
3275 char_u *cp, *np; /* Point into typed cmd and test name */
3276 garray_T *gap;
3277 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3278 only full match global is accepted. */
3279
3280 /*
3281 * Look for buffer-local user commands first, then global ones.
3282 */
3283 gap = &curbuf->b_ucmds;
3284 for (;;)
3285 {
3286 for (j = 0; j < gap->ga_len; ++j)
3287 {
3288 uc = USER_CMD_GA(gap, j);
3289 cp = eap->cmd;
3290 np = uc->uc_name;
3291 k = 0;
3292 while (k < len && *np != NUL && *cp++ == *np++)
3293 k++;
3294 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3295 {
3296 /* If finding a second match, the command is ambiguous. But
3297 * not if a buffer-local command wasn't a full match and a
3298 * global command is a full match. */
3299 if (k == len && found && *np != NUL)
3300 {
3301 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 amb_local = TRUE;
3304 }
3305
3306 if (!found || (k == len && *np == NUL))
3307 {
3308 /* If we matched up to a digit, then there could
3309 * be another command including the digit that we
3310 * should use instead.
3311 */
3312 if (k == len)
3313 found = TRUE;
3314 else
3315 possible = TRUE;
3316
3317 if (gap == &ucmds)
3318 eap->cmdidx = CMD_USER;
3319 else
3320 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003321 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003322 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003323 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324
3325# ifdef FEAT_CMDL_COMPL
3326 if (compl != NULL)
3327 *compl = uc->uc_compl;
3328# ifdef FEAT_EVAL
3329 if (xp != NULL)
3330 {
3331 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003332 xp->xp_script_ctx = uc->uc_script_ctx;
3333 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334 }
3335# endif
3336# endif
3337 /* Do not search for further abbreviations
3338 * if this is an exact match. */
3339 matchlen = k;
3340 if (k == len && *np == NUL)
3341 {
3342 if (full != NULL)
3343 *full = TRUE;
3344 amb_local = FALSE;
3345 break;
3346 }
3347 }
3348 }
3349 }
3350
3351 /* Stop if we found a full match or searched all. */
3352 if (j < gap->ga_len || gap == &ucmds)
3353 break;
3354 gap = &ucmds;
3355 }
3356
3357 /* Only found ambiguous matches. */
3358 if (amb_local)
3359 {
3360 if (xp != NULL)
3361 xp->xp_context = EXPAND_UNSUCCESSFUL;
3362 return NULL;
3363 }
3364
3365 /* The match we found may be followed immediately by a number. Move "p"
3366 * back to point to it. */
3367 if (found || possible)
3368 return p + (matchlen - len);
3369 return p;
3370}
3371#endif
3372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374static struct cmdmod
3375{
3376 char *name;
3377 int minlen;
3378 int has_count; /* :123verbose :3tab */
3379} cmdmods[] = {
3380 {"aboveleft", 3, FALSE},
3381 {"belowright", 3, FALSE},
3382 {"botright", 2, FALSE},
3383 {"browse", 3, FALSE},
3384 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003385 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003386 {"hide", 3, FALSE},
3387 {"keepalt", 5, FALSE},
3388 {"keepjumps", 5, FALSE},
3389 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003390 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003391 {"leftabove", 5, FALSE},
3392 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003393 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003394 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003395 {"rightbelow", 6, FALSE},
3396 {"sandbox", 3, FALSE},
3397 {"silent", 3, FALSE},
3398 {"tab", 3, TRUE},
3399 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003400 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003401 {"verbose", 4, TRUE},
3402 {"vertical", 4, FALSE},
3403};
3404
3405/*
3406 * Return length of a command modifier (including optional count).
3407 * Return zero when it's not a modifier.
3408 */
3409 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003410modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003411{
3412 int i, j;
3413 char_u *p = cmd;
3414
3415 if (VIM_ISDIGIT(*cmd))
3416 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003417 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003418 {
3419 for (j = 0; p[j] != NUL; ++j)
3420 if (p[j] != cmdmods[i].name[j])
3421 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003422 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003423 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003424 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003425 }
3426 return 0;
3427}
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429/*
3430 * Return > 0 if an Ex command "name" exists.
3431 * Return 2 if there is an exact match.
3432 * Return 3 if there is an ambiguous match.
3433 */
3434 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003435cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 exarg_T ea;
3438 int full = FALSE;
3439 int i;
3440 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003441 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003444 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 for (j = 0; name[j] != NUL; ++j)
3447 if (name[j] != cmdmods[i].name[j])
3448 break;
3449 if (name[j] == NUL && j >= cmdmods[i].minlen)
3450 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3451 }
3452
Bram Moolenaara9587612006-05-04 21:47:50 +00003453 /* Check built-in commands and user defined commands.
3454 * For ":2match" and ":3match" we need to skip the number. */
3455 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003457 p = find_command(&ea, &full);
3458 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003460 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3461 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003462 if (*skipwhite(p) != NUL)
3463 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3465}
3466#endif
3467
3468/*
3469 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3470 * we don't need/want deleted. Maybe this could be done better if we didn't
3471 * repeat all this stuff. The only problem is that they may not stay
3472 * perfectly compatible with each other, but then the command line syntax
3473 * probably won't change that much -- webb.
3474 */
3475 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003476set_one_cmd_context(
3477 expand_T *xp,
3478 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
3480 char_u *p;
3481 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003482 int len = 0;
3483 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3485 int compl = EXPAND_NOTHING;
3486#endif
3487#ifdef FEAT_CMDL_COMPL
3488 int delim;
3489#endif
3490 int forceit = FALSE;
3491 int usefilter = FALSE; /* filter instead of file name */
3492
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003493 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 xp->xp_pattern = buff;
3495 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003496 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003499 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 */
3501 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3502 ;
3503 xp->xp_pattern = cmd;
3504
3505 if (*cmd == NUL)
3506 return NULL;
3507 if (*cmd == '"') /* ignore comment lines */
3508 {
3509 xp->xp_context = EXPAND_NOTHING;
3510 return NULL;
3511 }
3512
3513/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003514 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 */
3516 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 xp->xp_pattern = cmd;
3518 if (*cmd == NUL)
3519 return NULL;
3520 if (*cmd == '"')
3521 {
3522 xp->xp_context = EXPAND_NOTHING;
3523 return NULL;
3524 }
3525
3526 if (*cmd == '|' || *cmd == '\n')
3527 return cmd + 1; /* There's another command */
3528
3529 /*
3530 * Isolate the command and search for it in the command table.
3531 * Exceptions:
3532 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003533 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3535 */
3536 if (*cmd == 'k' && cmd[1] != 'e')
3537 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 p = cmd + 1;
3540 }
3541 else
3542 {
3543 p = cmd;
3544 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3545 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003546 /* a user command may contain digits */
3547 if (ASCII_ISUPPER(cmd[0]))
3548 while (ASCII_ISALNUM(*p) || *p == '*')
3549 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003550 /* for python 3.x: ":py3*" commands completion */
3551 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003552 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003553 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003554 while (ASCII_ISALPHA(*p) || *p == '*')
3555 ++p;
3556 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003557 /* check for non-alpha command */
3558 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3559 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003560 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
3564 xp->xp_context = EXPAND_UNSUCCESSFUL;
3565 return NULL;
3566 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003567 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003568 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3569 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3570 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 break;
3572
3573#ifdef FEAT_USR_CMDS
3574 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3576 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577#endif
3578 }
3579
3580 /*
3581 * If the cursor is touching the command, and it ends in an alpha-numeric
3582 * character, complete the command name.
3583 */
3584 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3585 return NULL;
3586
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3590 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003591 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 p = cmd + 1;
3593 }
3594#ifdef FEAT_USR_CMDS
3595 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3596 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003597 ea.cmd = cmd;
3598 p = find_ucmd(&ea, p, NULL, xp,
3599# if defined(FEAT_CMDL_COMPL)
3600 &compl
3601# else
3602 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003604 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003605 if (p == NULL)
3606 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608#endif
3609 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003610 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 /* Not still touching the command and it was an illegal one */
3613 xp->xp_context = EXPAND_UNSUCCESSFUL;
3614 return NULL;
3615 }
3616
3617 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3618
3619 if (*p == '!') /* forced commands */
3620 {
3621 forceit = TRUE;
3622 ++p;
3623 }
3624
3625/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003626 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003628 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003629 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 arg = skipwhite(p);
3632
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003633 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 if (*arg == '>') /* append */
3636 {
3637 if (*++arg == '>')
3638 ++arg;
3639 arg = skipwhite(arg);
3640 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003641 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 ++arg;
3644 usefilter = TRUE;
3645 }
3646 }
3647
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003648 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
3650 usefilter = forceit; /* :r! filter if forced */
3651 if (*arg == '!') /* :r !filter */
3652 {
3653 ++arg;
3654 usefilter = TRUE;
3655 }
3656 }
3657
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003658 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 while (*arg == *cmd) /* allow any number of '>' or '<' */
3661 ++arg;
3662 arg = skipwhite(arg);
3663 }
3664
3665 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 /* Check if we're in the +command */
3669 p = arg + 1;
3670 arg = skip_cmd_arg(arg, FALSE);
3671
3672 /* Still touching the command after '+'? */
3673 if (*arg == NUL)
3674 return p;
3675
3676 /* Skip space(s) after +command to get to the real argument */
3677 arg = skipwhite(arg);
3678 }
3679
3680 /*
3681 * Check for '|' to separate commands and '"' to start comments.
3682 * Don't do this for ":read !cmd" and ":write !cmd".
3683 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003684 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
3686 p = arg;
3687 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003688 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 p += 2;
3690 while (*p)
3691 {
3692 if (*p == Ctrl_V)
3693 {
3694 if (p[1] != NUL)
3695 ++p;
3696 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003697 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 || *p == '|' || *p == '\n')
3699 {
3700 if (*(p - 1) != '\\')
3701 {
3702 if (*p == '|' || *p == '\n')
3703 return p + 1;
3704 return NULL; /* It's a comment */
3705 }
3706 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003707 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709 }
3710
3711 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003712 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 vim_strchr((char_u *)"|\"", *arg) == NULL)
3714 return NULL;
3715
3716 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003717 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003719 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003720 while (*p && p < buff + len)
3721 {
3722 if (*p == ' ' || *p == TAB)
3723 {
3724 /* argument starts after a space */
3725 xp->xp_pattern = ++p;
3726 }
3727 else
3728 {
3729 if (*p == '\\' && *(p + 1) != NUL)
3730 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003731 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003732 }
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003735 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003737 int c;
3738 int in_quote = FALSE;
3739 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741 /*
3742 * Allow spaces within back-quotes to count as part of the argument
3743 * being expanded.
3744 */
3745 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746 p = xp->xp_pattern;
3747 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003749 if (has_mbyte)
3750 c = mb_ptr2char(p);
3751 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003752 c = *p;
3753 if (c == '\\' && p[1] != NUL)
3754 ++p;
3755 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
3757 if (!in_quote)
3758 {
3759 xp->xp_pattern = p;
3760 bow = p + 1;
3761 }
3762 in_quote = !in_quote;
3763 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003764 /* An argument can contain just about everything, except
3765 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003766 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003767#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003768 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003769#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003770 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003771 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003772 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003773 while (*p != NUL)
3774 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003775 if (has_mbyte)
3776 c = mb_ptr2char(p);
3777 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003778 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003779 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003780 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003781 if (has_mbyte)
3782 len = (*mb_ptr2len)(p);
3783 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003784 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003785 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003786 }
3787 if (in_quote)
3788 bow = p;
3789 else
3790 xp->xp_pattern = p;
3791 p -= len;
3792 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003793 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795
3796 /*
3797 * If we are still inside the quotes, and we passed a space, just
3798 * expand from there.
3799 */
3800 if (bow != NULL && in_quote)
3801 xp->xp_pattern = bow;
3802 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003803
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003804 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003805 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003806 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003807#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003808 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003809#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003810 /* When still after the command name expand executables. */
3811 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003812 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 /* Check for environment variable */
3816 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003817#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 || *xp->xp_pattern == '%'
3819#endif
3820 )
3821 {
3822 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3823 if (!vim_isIDc(*p))
3824 break;
3825 if (*p == NUL)
3826 {
3827 xp->xp_context = EXPAND_ENV_VARS;
3828 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003829#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3830 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003831 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003832 compl = EXPAND_ENV_VARS;
3833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003836#if defined(FEAT_CMDL_COMPL)
3837 /* Check for user names */
3838 if (*xp->xp_pattern == '~')
3839 {
3840 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3841 ;
3842 /* Complete ~user only if it partially matches a user name.
3843 * A full match ~user<Tab> will be replaced by user's home
3844 * directory i.e. something like ~user<Tab> -> /home/user/ */
3845 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003846 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003847 {
3848 xp->xp_context = EXPAND_USER;
3849 ++xp->xp_pattern;
3850 }
3851 }
3852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854
3855/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003856 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003858 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003860 case CMD_find:
3861 case CMD_sfind:
3862 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003863 if (xp->xp_context == EXPAND_FILES)
3864 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003865 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 case CMD_cd:
3867 case CMD_chdir:
3868 case CMD_lcd:
3869 case CMD_lchdir:
3870 if (xp->xp_context == EXPAND_FILES)
3871 xp->xp_context = EXPAND_DIRECTORIES;
3872 break;
3873 case CMD_help:
3874 xp->xp_context = EXPAND_HELP;
3875 xp->xp_pattern = arg;
3876 break;
3877
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003878 /* Command modifiers: return the argument.
3879 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003881 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_belowright:
3883 case CMD_botright:
3884 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003885 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003886 case CMD_cdo:
3887 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003889 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_folddoclosed:
3891 case CMD_folddoopen:
3892 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003893 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_keepjumps:
3895 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003896 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003897 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003899 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003901 case CMD_noautocmd:
3902 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003904 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003906 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003907 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_topleft:
3909 case CMD_verbose:
3910 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003911 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 return arg;
3913
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003914 case CMD_filter:
3915 if (*arg != NUL)
3916 arg = skip_vimgrep_pat(arg, NULL, NULL);
3917 if (arg == NULL || *arg == NUL)
3918 {
3919 xp->xp_context = EXPAND_NOTHING;
3920 return NULL;
3921 }
3922 return skipwhite(arg);
3923
Bram Moolenaar4f688582007-07-24 12:34:30 +00003924#ifdef FEAT_CMDL_COMPL
3925# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 case CMD_match:
3927 if (*arg == NUL || !ends_excmd(*arg))
3928 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003929 /* also complete "None" */
3930 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 arg = skipwhite(skiptowhite(arg));
3932 if (*arg != NUL)
3933 {
3934 xp->xp_context = EXPAND_NOTHING;
3935 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3936 }
3937 }
3938 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941/*
3942 * All completion for the +cmdline_compl feature goes here.
3943 */
3944
3945# ifdef FEAT_USR_CMDS
3946 case CMD_command:
3947 /* Check for attributes */
3948 while (*arg == '-')
3949 {
3950 arg++; /* Skip "-" */
3951 p = skiptowhite(arg);
3952 if (*p == NUL)
3953 {
3954 /* Cursor is still in the attribute */
3955 p = vim_strchr(arg, '=');
3956 if (p == NULL)
3957 {
3958 /* No "=", so complete attribute names */
3959 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3960 xp->xp_pattern = arg;
3961 return NULL;
3962 }
3963
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003964 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 * their arguments as well.
3966 */
3967 if (STRNICMP(arg, "complete", p - arg) == 0)
3968 {
3969 xp->xp_context = EXPAND_USER_COMPLETE;
3970 xp->xp_pattern = p + 1;
3971 return NULL;
3972 }
3973 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3974 {
3975 xp->xp_context = EXPAND_USER_NARGS;
3976 xp->xp_pattern = p + 1;
3977 return NULL;
3978 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003979 else if (STRNICMP(arg, "addr", p - arg) == 0)
3980 {
3981 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3982 xp->xp_pattern = p + 1;
3983 return NULL;
3984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return NULL;
3986 }
3987 arg = skipwhite(p);
3988 }
3989
3990 /* After the attributes comes the new command name */
3991 p = skiptowhite(arg);
3992 if (*p == NUL)
3993 {
3994 xp->xp_context = EXPAND_USER_COMMANDS;
3995 xp->xp_pattern = arg;
3996 break;
3997 }
3998
3999 /* And finally comes a normal command */
4000 return skipwhite(p);
4001
4002 case CMD_delcommand:
4003 xp->xp_context = EXPAND_USER_COMMANDS;
4004 xp->xp_pattern = arg;
4005 break;
4006# endif
4007
4008 case CMD_global:
4009 case CMD_vglobal:
4010 delim = *arg; /* get the delimiter */
4011 if (delim)
4012 ++arg; /* skip delimiter if there is one */
4013
4014 while (arg[0] != NUL && arg[0] != delim)
4015 {
4016 if (arg[0] == '\\' && arg[1] != NUL)
4017 ++arg;
4018 ++arg;
4019 }
4020 if (arg[0] != NUL)
4021 return arg + 1;
4022 break;
4023 case CMD_and:
4024 case CMD_substitute:
4025 delim = *arg;
4026 if (delim)
4027 {
4028 /* skip "from" part */
4029 ++arg;
4030 arg = skip_regexp(arg, delim, p_magic, NULL);
4031 }
4032 /* skip "to" part */
4033 while (arg[0] != NUL && arg[0] != delim)
4034 {
4035 if (arg[0] == '\\' && arg[1] != NUL)
4036 ++arg;
4037 ++arg;
4038 }
4039 if (arg[0] != NUL) /* skip delimiter */
4040 ++arg;
4041 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4042 ++arg;
4043 if (arg[0] != NUL)
4044 return arg;
4045 break;
4046 case CMD_isearch:
4047 case CMD_dsearch:
4048 case CMD_ilist:
4049 case CMD_dlist:
4050 case CMD_ijump:
4051 case CMD_psearch:
4052 case CMD_djump:
4053 case CMD_isplit:
4054 case CMD_dsplit:
4055 arg = skipwhite(skipdigits(arg)); /* skip count */
4056 if (*arg == '/') /* Match regexp, not just whole words */
4057 {
4058 for (++arg; *arg && *arg != '/'; arg++)
4059 if (*arg == '\\' && arg[1] != NUL)
4060 arg++;
4061 if (*arg)
4062 {
4063 arg = skipwhite(arg + 1);
4064
4065 /* Check for trailing illegal characters */
4066 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4067 xp->xp_context = EXPAND_NOTHING;
4068 else
4069 return arg;
4070 }
4071 }
4072 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004073
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 case CMD_autocmd:
4075 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004077 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 case CMD_set:
4080 set_context_in_set_cmd(xp, arg, 0);
4081 break;
4082 case CMD_setglobal:
4083 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4084 break;
4085 case CMD_setlocal:
4086 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4087 break;
4088 case CMD_tag:
4089 case CMD_stag:
4090 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004091 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 case CMD_tselect:
4093 case CMD_stselect:
4094 case CMD_ptselect:
4095 case CMD_tjump:
4096 case CMD_stjump:
4097 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004098 if (*p_wop != NUL)
4099 xp->xp_context = EXPAND_TAGS_LISTFILES;
4100 else
4101 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 xp->xp_pattern = arg;
4103 break;
4104 case CMD_augroup:
4105 xp->xp_context = EXPAND_AUGROUP;
4106 xp->xp_pattern = arg;
4107 break;
4108#ifdef FEAT_SYN_HL
4109 case CMD_syntax:
4110 set_context_in_syntax_cmd(xp, arg);
4111 break;
4112#endif
4113#ifdef FEAT_EVAL
4114 case CMD_let:
4115 case CMD_if:
4116 case CMD_elseif:
4117 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004118 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 case CMD_echo:
4120 case CMD_echon:
4121 case CMD_execute:
4122 case CMD_echomsg:
4123 case CMD_echoerr:
4124 case CMD_call:
4125 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004126 case CMD_cexpr:
4127 case CMD_caddexpr:
4128 case CMD_cgetexpr:
4129 case CMD_lexpr:
4130 case CMD_laddexpr:
4131 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004132 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
4135 case CMD_unlet:
4136 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4137 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 xp->xp_context = EXPAND_USER_VARS;
4140 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004141
4142 if (*xp->xp_pattern == '$')
4143 {
4144 xp->xp_context = EXPAND_ENV_VARS;
4145 ++xp->xp_pattern;
4146 }
4147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 break;
4149
4150 case CMD_function:
4151 case CMD_delfunction:
4152 xp->xp_context = EXPAND_USER_FUNC;
4153 xp->xp_pattern = arg;
4154 break;
4155
4156 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004157 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159#endif
4160 case CMD_highlight:
4161 set_context_in_highlight_cmd(xp, arg);
4162 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004163#ifdef FEAT_CSCOPE
4164 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004165 case CMD_lcscope:
4166 case CMD_scscope:
4167 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004168 break;
4169#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004170#ifdef FEAT_SIGNS
4171 case CMD_sign:
4172 set_context_in_sign_cmd(xp, arg);
4173 break;
4174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 case CMD_bdelete:
4176 case CMD_bwipeout:
4177 case CMD_bunload:
4178 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4179 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004180 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 case CMD_buffer:
4182 case CMD_sbuffer:
4183 case CMD_checktime:
4184 xp->xp_context = EXPAND_BUFFERS;
4185 xp->xp_pattern = arg;
4186 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#ifdef FEAT_USR_CMDS
4188 case CMD_USER:
4189 case CMD_USER_BUF:
4190 if (compl != EXPAND_NOTHING)
4191 {
4192 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004193 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 {
4195# ifdef FEAT_MENU
4196 if (compl == EXPAND_MENUS)
4197 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4198# endif
4199 if (compl == EXPAND_COMMANDS)
4200 return arg;
4201 if (compl == EXPAND_MAPPINGS)
4202 return set_context_in_map_cmd(xp, (char_u *)"map",
4203 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004204 /* Find start of last argument. */
4205 p = arg;
4206 while (*p)
4207 {
4208 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004209 /* argument starts after a space */
4210 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004211 else if (*p == '\\' && *(p + 1) != NUL)
4212 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004213 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 xp->xp_pattern = arg;
4216 }
4217 xp->xp_context = compl;
4218 }
4219 break;
4220#endif
4221 case CMD_map: case CMD_noremap:
4222 case CMD_nmap: case CMD_nnoremap:
4223 case CMD_vmap: case CMD_vnoremap:
4224 case CMD_omap: case CMD_onoremap:
4225 case CMD_imap: case CMD_inoremap:
4226 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004227 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004228 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004229 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004230 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004232 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 case CMD_unmap:
4234 case CMD_nunmap:
4235 case CMD_vunmap:
4236 case CMD_ounmap:
4237 case CMD_iunmap:
4238 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004239 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004240 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004241 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004242 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004244 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004245 case CMD_mapclear:
4246 case CMD_nmapclear:
4247 case CMD_vmapclear:
4248 case CMD_omapclear:
4249 case CMD_imapclear:
4250 case CMD_cmapclear:
4251 case CMD_lmapclear:
4252 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004253 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004254 case CMD_xmapclear:
4255 xp->xp_context = EXPAND_MAPCLEAR;
4256 xp->xp_pattern = arg;
4257 break;
4258
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 case CMD_abbreviate: case CMD_noreabbrev:
4260 case CMD_cabbrev: case CMD_cnoreabbrev:
4261 case CMD_iabbrev: case CMD_inoreabbrev:
4262 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004263 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 case CMD_unabbreviate:
4265 case CMD_cunabbrev:
4266 case CMD_iunabbrev:
4267 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004268 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269#ifdef FEAT_MENU
4270 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4271 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4272 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4273 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4274 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4275 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4276 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004277 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 case CMD_tmenu: case CMD_tunmenu:
4279 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4280 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4281#endif
4282
4283 case CMD_colorscheme:
4284 xp->xp_context = EXPAND_COLORS;
4285 xp->xp_pattern = arg;
4286 break;
4287
4288 case CMD_compiler:
4289 xp->xp_context = EXPAND_COMPILER;
4290 xp->xp_pattern = arg;
4291 break;
4292
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004293 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004294 xp->xp_context = EXPAND_OWNSYNTAX;
4295 xp->xp_pattern = arg;
4296 break;
4297
4298 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004299 xp->xp_context = EXPAND_FILETYPE;
4300 xp->xp_pattern = arg;
4301 break;
4302
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004303 case CMD_packadd:
4304 xp->xp_context = EXPAND_PACKADD;
4305 xp->xp_pattern = arg;
4306 break;
4307
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004308#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004310 p = skiptowhite(arg);
4311 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 xp->xp_context = EXPAND_LANGUAGE;
4314 xp->xp_pattern = arg;
4315 }
4316 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004317 {
4318 if ( STRNCMP(arg, "messages", p - arg) == 0
4319 || STRNCMP(arg, "ctype", p - arg) == 0
4320 || STRNCMP(arg, "time", p - arg) == 0)
4321 {
4322 xp->xp_context = EXPAND_LOCALES;
4323 xp->xp_pattern = skipwhite(p);
4324 }
4325 else
4326 xp->xp_context = EXPAND_NOTHING;
4327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 break;
4329#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004330#if defined(FEAT_PROFILE)
4331 case CMD_profile:
4332 set_context_in_profile_cmd(xp, arg);
4333 break;
4334#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004335 case CMD_behave:
4336 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004337 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004338 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004340 case CMD_messages:
4341 xp->xp_context = EXPAND_MESSAGES;
4342 xp->xp_pattern = arg;
4343 break;
4344
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004345#if defined(FEAT_CMDHIST)
4346 case CMD_history:
4347 xp->xp_context = EXPAND_HISTORY;
4348 xp->xp_pattern = arg;
4349 break;
4350#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004351#if defined(FEAT_PROFILE)
4352 case CMD_syntime:
4353 xp->xp_context = EXPAND_SYNTIME;
4354 xp->xp_pattern = arg;
4355 break;
4356#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004357
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004358 case CMD_argdelete:
4359 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4360 arg = xp->xp_pattern + 1;
4361 xp->xp_context = EXPAND_ARGLIST;
4362 xp->xp_pattern = arg;
4363 break;
4364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365#endif /* FEAT_CMDL_COMPL */
4366
4367 default:
4368 break;
4369 }
4370 return NULL;
4371}
4372
4373/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004374 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 *
4376 * Backslashed delimiters after / or ? will be skipped, and commands will
4377 * not be expanded between /'s and ?'s or after "'".
4378 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004379 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 * Returns the "cmd" pointer advanced to beyond the range.
4381 */
4382 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004383skip_range(
4384 char_u *cmd,
4385 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004387 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004389 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004391 if (*cmd == '\\')
4392 {
4393 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4394 ++cmd;
4395 else
4396 break;
4397 }
4398 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 if (*++cmd == NUL && ctx != NULL)
4401 *ctx = EXPAND_NOTHING;
4402 }
4403 else if (*cmd == '/' || *cmd == '?')
4404 {
4405 delim = *cmd++;
4406 while (*cmd != NUL && *cmd != delim)
4407 if (*cmd++ == '\\' && *cmd != NUL)
4408 ++cmd;
4409 if (*cmd == NUL && ctx != NULL)
4410 *ctx = EXPAND_NOTHING;
4411 }
4412 if (*cmd != NUL)
4413 ++cmd;
4414 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004415
4416 /* Skip ":" and white space. */
4417 while (*cmd == ':')
4418 cmd = skipwhite(cmd + 1);
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 return cmd;
4421}
4422
4423/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004424 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 *
4426 * Set ptr to the next character after the part that was interpreted.
4427 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004428 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 *
4430 * Return MAXLNUM when no Ex address was found.
4431 */
4432 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004433get_address(
4434 exarg_T *eap UNUSED,
4435 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004436 int addr_type, // flag: one of ADDR_LINES, ...
4437 int skip, // only skip the address, don't use it
4438 int silent, // no errors or side effects
4439 int to_other_file, // flag: may jump to other file
4440 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442 int c;
4443 int i;
4444 long n;
4445 char_u *cmd;
4446 pos_T pos;
4447 pos_T *fp;
4448 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004449 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 cmd = skipwhite(*ptr);
4452 lnum = MAXLNUM;
4453 do
4454 {
4455 switch (*cmd)
4456 {
4457 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004458 ++cmd;
4459 switch (addr_type)
4460 {
4461 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 lnum = curwin->w_cursor.lnum;
4463 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004464 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004465 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 break;
4467 case ADDR_ARGUMENTS:
4468 lnum = curwin->w_arg_idx + 1;
4469 break;
4470 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004471 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 lnum = curbuf->b_fnum;
4473 break;
4474 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004475 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004477 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004478 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004479 cmd = NULL;
4480 goto error;
4481 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004482#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004483 case ADDR_QUICKFIX:
4484 lnum = qf_get_cur_valid_idx(eap);
4485 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004486#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 }
4488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 ++cmd;
4492 switch (addr_type)
4493 {
4494 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 lnum = curbuf->b_ml.ml_line_count;
4496 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004498 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 break;
4500 case ADDR_ARGUMENTS:
4501 lnum = ARGCOUNT;
4502 break;
4503 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004504 buf = lastbuf;
4505 while (buf->b_ml.ml_mfp == NULL)
4506 {
4507 if (buf->b_prev == NULL)
4508 break;
4509 buf = buf->b_prev;
4510 }
4511 lnum = buf->b_fnum;
4512 break;
4513 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 lnum = lastbuf->b_fnum;
4515 break;
4516 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004517 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004519 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004520 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004521 cmd = NULL;
4522 goto error;
4523 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004524#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004525 case ADDR_QUICKFIX:
4526 lnum = qf_get_size(eap);
4527 if (lnum == 0)
4528 lnum = 1;
4529 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004530#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 }
4532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 if (*++cmd == NUL)
4536 {
4537 cmd = NULL;
4538 goto error;
4539 }
4540 if (addr_type != ADDR_LINES)
4541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004542 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004543 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 goto error;
4545 }
4546 if (skip)
4547 ++cmd;
4548 else
4549 {
4550 /* Only accept a mark in another file when it is
4551 * used by itself: ":'M". */
4552 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4553 ++cmd;
4554 if (fp == (pos_T *)-1)
4555 /* Jumped to another file. */
4556 lnum = curwin->w_cursor.lnum;
4557 else
4558 {
4559 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
4561 cmd = NULL;
4562 goto error;
4563 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 lnum = fp->lnum;
4565 }
4566 }
4567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 case '/':
4570 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 c = *cmd++;
4572 if (addr_type != ADDR_LINES)
4573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004574 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004575 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 goto error;
4577 }
4578 if (skip) /* skip "/pat/" */
4579 {
4580 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4581 if (*cmd == c)
4582 ++cmd;
4583 }
4584 else
4585 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004586 int flags;
4587
4588 pos = curwin->w_cursor; // save curwin->w_cursor
4589
4590 // When '/' or '?' follows another address, start from
4591 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004592 if (lnum != MAXLNUM)
4593 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004594
4595 // Start a forward search at the end of the line (unless
4596 // before the first line).
4597 // Start a backward search at the start of the line.
4598 // This makes sure we never match in the current
4599 // line, and can match anywhere in the
4600 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004601 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 curwin->w_cursor.col = MAXCOL;
4603 else
4604 curwin->w_cursor.col = 0;
4605 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004606 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4607 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 {
4609 curwin->w_cursor = pos;
4610 cmd = NULL;
4611 goto error;
4612 }
4613 lnum = curwin->w_cursor.lnum;
4614 curwin->w_cursor = pos;
4615 /* adjust command string pointer */
4616 cmd += searchcmdlen;
4617 }
4618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 ++cmd;
4622 if (addr_type != ADDR_LINES)
4623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004624 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004625 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004626 goto error;
4627 }
4628 if (*cmd == '&')
4629 i = RE_SUBST;
4630 else if (*cmd == '?' || *cmd == '/')
4631 i = RE_SEARCH;
4632 else
4633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004634 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 cmd = NULL;
4636 goto error;
4637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004639 if (!skip)
4640 {
4641 /*
4642 * When search follows another address, start from
4643 * there.
4644 */
4645 if (lnum != MAXLNUM)
4646 pos.lnum = lnum;
4647 else
4648 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004650 /*
4651 * Start the search just like for the above
4652 * do_search().
4653 */
4654 if (*cmd != '?')
4655 pos.col = MAXCOL;
4656 else
4657 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004658 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004659 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004660 *cmd == '?' ? BACKWARD : FORWARD,
4661 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004662 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004663 lnum = pos.lnum;
4664 else
4665 {
4666 cmd = NULL;
4667 goto error;
4668 }
4669 }
4670 ++cmd;
4671 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004674 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4675 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 }
4677
4678 for (;;)
4679 {
4680 cmd = skipwhite(cmd);
4681 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4682 break;
4683
4684 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004685 {
4686 switch (addr_type)
4687 {
4688 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004689 /* "+1" is same as ".+1" */
4690 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004691 break;
4692 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004693 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004694 break;
4695 case ADDR_ARGUMENTS:
4696 lnum = curwin->w_arg_idx + 1;
4697 break;
4698 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004699 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004700 lnum = curbuf->b_fnum;
4701 break;
4702 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004703 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004704 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004705 case ADDR_TABS_RELATIVE:
4706 lnum = 1;
4707 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004708#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004709 case ADDR_QUICKFIX:
4710 lnum = qf_get_cur_valid_idx(eap);
4711 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004712#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004713 }
4714 }
4715
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (VIM_ISDIGIT(*cmd))
4717 i = '+'; /* "number" is same as "+number" */
4718 else
4719 i = *cmd++;
4720 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4721 n = 1;
4722 else
4723 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004724
4725 if (addr_type == ADDR_TABS_RELATIVE)
4726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004727 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004728 cmd = NULL;
4729 goto error;
4730 }
4731 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004732 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004733 lnum = compute_buffer_local_count(
4734 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004736 {
4737#ifdef FEAT_FOLDING
4738 /* Relative line addressing, need to adjust for folded lines
4739 * now, but only do it after the first address. */
4740 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4741 && address_count >= 2)
4742 (void)hasFolding(lnum, NULL, &lnum);
4743#endif
4744 if (i == '-')
4745 lnum -= n;
4746 else
4747 lnum += n;
4748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750 } while (*cmd == '/' || *cmd == '?');
4751
4752error:
4753 *ptr = cmd;
4754 return lnum;
4755}
4756
4757/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004758 * Get flags from an Ex command argument.
4759 */
4760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004761get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004762{
4763 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4764 {
4765 if (*eap->arg == 'l')
4766 eap->flags |= EXFLAG_LIST;
4767 else if (*eap->arg == 'p')
4768 eap->flags |= EXFLAG_PRINT;
4769 else
4770 eap->flags |= EXFLAG_NR;
4771 eap->arg = skipwhite(eap->arg + 1);
4772 }
4773}
4774
4775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 * Function called for command which is Not Implemented. NI!
4777 */
4778 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004779ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004782 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783}
4784
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004785#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786/*
4787 * Function called for script command which is Not Implemented. NI!
4788 * Skips over ":perl <<EOF" constructs.
4789 */
4790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004791ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 if (!eap->skip)
4794 ex_ni(eap);
4795 else
4796 vim_free(script_get(eap, eap->arg));
4797}
4798#endif
4799
4800/*
4801 * Check range in Ex command for validity.
4802 * Return NULL when valid, error message when invalid.
4803 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004804 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004805invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004807 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if ( eap->line1 < 0
4809 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004810 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004811 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004812
4813 if (eap->argt & RANGE)
4814 {
4815 switch(eap->addr_type)
4816 {
4817 case ADDR_LINES:
4818 if (!(eap->argt & NOTADR)
4819 && eap->line2 > curbuf->b_ml.ml_line_count
4820#ifdef FEAT_DIFF
4821 + (eap->cmdidx == CMD_diffget)
4822#endif
4823 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004824 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 break;
4826 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004827 /* add 1 if ARGCOUNT is 0 */
4828 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004829 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830 break;
4831 case ADDR_BUFFERS:
4832 if (eap->line1 < firstbuf->b_fnum
4833 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004834 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004835 break;
4836 case ADDR_LOADED_BUFFERS:
4837 buf = firstbuf;
4838 while (buf->b_ml.ml_mfp == NULL)
4839 {
4840 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004841 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004842 buf = buf->b_next;
4843 }
4844 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004845 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004846 buf = lastbuf;
4847 while (buf->b_ml.ml_mfp == NULL)
4848 {
4849 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004850 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004851 buf = buf->b_prev;
4852 }
4853 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004854 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004855 break;
4856 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004857 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004858 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004859 break;
4860 case ADDR_TABS:
4861 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004862 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004863 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004864 case ADDR_TABS_RELATIVE:
4865 /* Do nothing */
4866 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004867#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004868 case ADDR_QUICKFIX:
4869 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004870 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004871 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004872#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004873 }
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 return NULL;
4876}
4877
4878/*
4879 * Correct the range for zero line number, if required.
4880 */
4881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004882correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
4884 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4885 {
4886 if (eap->line1 == 0)
4887 eap->line1 = 1;
4888 if (eap->line2 == 0)
4889 eap->line2 = 1;
4890 }
4891}
4892
Bram Moolenaar748bf032005-02-02 23:04:36 +00004893#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004894/*
4895 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4896 * pattern. Otherwise return eap->arg.
4897 */
4898 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004899skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004900{
4901 char_u *p = eap->arg;
4902
Bram Moolenaara37420f2006-02-04 22:37:47 +00004903 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4904 || eap->cmdidx == CMD_vimgrepadd
4905 || eap->cmdidx == CMD_lvimgrepadd
4906 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004907 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004908 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004909 if (p == NULL)
4910 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004911 }
4912 return p;
4913}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004914
4915/*
4916 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4917 * in the command line, so that things like % get expanded.
4918 */
4919 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004920replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004921{
4922 char_u *new_cmdline;
4923 char_u *program;
4924 char_u *pos;
4925 char_u *ptr;
4926 int len;
4927 int i;
4928
4929 /*
4930 * Don't do it when ":vimgrep" is used for ":grep".
4931 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004932 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4933 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4934 || eap->cmdidx == CMD_grepadd
4935 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004936 && !grep_internal(eap->cmdidx))
4937 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004938 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4939 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004940 {
4941 if (*curbuf->b_p_gp == NUL)
4942 program = p_gp;
4943 else
4944 program = curbuf->b_p_gp;
4945 }
4946 else
4947 {
4948 if (*curbuf->b_p_mp == NUL)
4949 program = p_mp;
4950 else
4951 program = curbuf->b_p_mp;
4952 }
4953
4954 p = skipwhite(p);
4955
4956 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4957 {
4958 /* replace $* by given arguments */
4959 i = 1;
4960 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4961 ++i;
4962 len = (int)STRLEN(p);
4963 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4964 if (new_cmdline == NULL)
4965 return NULL; /* out of memory */
4966 ptr = new_cmdline;
4967 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4968 {
4969 i = (int)(pos - program);
4970 STRNCPY(ptr, program, i);
4971 STRCPY(ptr += i, p);
4972 ptr += len;
4973 program = pos + 2;
4974 }
4975 STRCPY(ptr, program);
4976 }
4977 else
4978 {
4979 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4980 if (new_cmdline == NULL)
4981 return NULL; /* out of memory */
4982 STRCPY(new_cmdline, program);
4983 STRCAT(new_cmdline, " ");
4984 STRCAT(new_cmdline, p);
4985 }
4986 msg_make(p);
4987
4988 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4989 vim_free(*cmdlinep);
4990 *cmdlinep = new_cmdline;
4991 p = new_cmdline;
4992 }
4993 return p;
4994}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004995#endif
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997/*
4998 * Expand file name in Ex command argument.
4999 * Return FAIL for failure, OK otherwise.
5000 */
5001 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005002expand_filename(
5003 exarg_T *eap,
5004 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005005 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006{
5007 int has_wildcards; /* need to expand wildcards */
5008 char_u *repl;
5009 int srclen;
5010 char_u *p;
5011 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005012 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaar748bf032005-02-02 23:04:36 +00005014#ifdef FEAT_QUICKFIX
5015 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5016 p = skip_grep_pat(eap);
5017#else
5018 p = eap->arg;
5019#endif
5020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 /*
5022 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5023 * the file name contains a wildcard it should not cause expanding.
5024 * (it will be expanded anyway if there is a wildcard before replacing).
5025 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005026 has_wildcards = mch_has_wildcard(p);
5027 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005029#ifdef FEAT_EVAL
5030 /* Skip over `=expr`, wildcards in it are not expanded. */
5031 if (p[0] == '`' && p[1] == '=')
5032 {
5033 p += 2;
5034 (void)skip_expr(&p);
5035 if (*p == '`')
5036 ++p;
5037 continue;
5038 }
5039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /*
5041 * Quick check if this cannot be the start of a special string.
5042 * Also removes backslash before '%', '#' and '<'.
5043 */
5044 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5045 {
5046 ++p;
5047 continue;
5048 }
5049
5050 /*
5051 * Try to find a match at this position.
5052 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005053 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5054 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (*errormsgp != NULL) /* error detected */
5056 return FAIL;
5057 if (repl == NULL) /* no match found */
5058 {
5059 p += srclen;
5060 continue;
5061 }
5062
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005063 /* Wildcards won't be expanded below, the replacement is taken
5064 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5065 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5066 {
5067 char_u *l = repl;
5068
5069 repl = expand_env_save(repl);
5070 vim_free(l);
5071 }
5072
Bram Moolenaar63b92542007-03-27 14:57:09 +00005073 /* Need to escape white space et al. with a backslash.
5074 * Don't do this for:
5075 * - replacement that already has been escaped: "##"
5076 * - shell commands (may have to use quotes instead).
5077 * - non-unix systems when there is a single argument (spaces don't
5078 * separate arguments then).
5079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005081 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 && eap->cmdidx != CMD_grep
5084 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005085 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005086 && eap->cmdidx != CMD_lgrep
5087 && eap->cmdidx != CMD_lgrepadd
5088 && eap->cmdidx != CMD_lmake
5089 && eap->cmdidx != CMD_make
5090 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#ifndef UNIX
5092 && !(eap->argt & NOSPC)
5093#endif
5094 )
5095 {
5096 char_u *l;
5097#ifdef BACKSLASH_IN_FILENAME
5098 /* Don't escape a backslash here, because rem_backslash() doesn't
5099 * remove it later. */
5100 static char_u *nobslash = (char_u *)" \t\"|";
5101# define ESCAPE_CHARS nobslash
5102#else
5103# define ESCAPE_CHARS escape_chars
5104#endif
5105
5106 for (l = repl; *l; ++l)
5107 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5108 {
5109 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5110 if (l != NULL)
5111 {
5112 vim_free(repl);
5113 repl = l;
5114 }
5115 break;
5116 }
5117 }
5118
5119 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005120 if ((eap->usefilter || eap->cmdidx == CMD_bang
5121 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005122 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 char_u *l;
5125
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005126 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (l != NULL)
5128 {
5129 vim_free(repl);
5130 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132 }
5133
5134 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5135 vim_free(repl);
5136 if (p == NULL)
5137 return FAIL;
5138 }
5139
5140 /*
5141 * One file argument: Expand wildcards.
5142 * Don't do this with ":r !command" or ":w !command".
5143 */
5144 if ((eap->argt & NOSPC) && !eap->usefilter)
5145 {
5146 /*
5147 * May do this twice:
5148 * 1. Replace environment variables.
5149 * 2. Replace any other wildcards, remove backslashes.
5150 */
5151 for (n = 1; n <= 2; ++n)
5152 {
5153 if (n == 2)
5154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 /*
5156 * Halve the number of backslashes (this is Vi compatible).
5157 * For Unix and OS/2, when wildcards are expanded, this is
5158 * done by ExpandOne() below.
5159 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005160#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (!has_wildcards)
5162#endif
5163 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165
5166 if (has_wildcards)
5167 {
5168 if (n == 1)
5169 {
5170 /*
5171 * First loop: May expand environment variables. This
5172 * can be done much faster with expand_env() than with
5173 * something else (e.g., calling a shell).
5174 * After expanding environment variables, check again
5175 * if there are still wildcards present.
5176 */
5177 if (vim_strchr(eap->arg, '$') != NULL
5178 || vim_strchr(eap->arg, '~') != NULL)
5179 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005180 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005181 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 has_wildcards = mch_has_wildcard(NameBuff);
5183 p = NameBuff;
5184 }
5185 else
5186 p = NULL;
5187 }
5188 else /* n == 2 */
5189 {
5190 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005191 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
5193 ExpandInit(&xpc);
5194 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005195 if (p_wic)
5196 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005198 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (p == NULL)
5200 return FAIL;
5201 }
5202 if (p != NULL)
5203 {
5204 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5205 p, cmdlinep);
5206 if (n == 2) /* p came from ExpandOne() */
5207 vim_free(p);
5208 }
5209 }
5210 }
5211 }
5212 return OK;
5213}
5214
5215/*
5216 * Replace part of the command line, keeping eap->cmd, eap->arg and
5217 * eap->nextcmd correct.
5218 * "src" points to the part that is to be replaced, of length "srclen".
5219 * "repl" is the replacement string.
5220 * Returns a pointer to the character after the replaced string.
5221 * Returns NULL for failure.
5222 */
5223 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005224repl_cmdline(
5225 exarg_T *eap,
5226 char_u *src,
5227 int srclen,
5228 char_u *repl,
5229 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
5231 int len;
5232 int i;
5233 char_u *new_cmdline;
5234
5235 /*
5236 * The new command line is build in new_cmdline[].
5237 * First allocate it.
5238 * Careful: a "+cmd" argument may have been NUL terminated.
5239 */
5240 len = (int)STRLEN(repl);
5241 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005242 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5244 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5245 return NULL; /* out of memory! */
5246
5247 /*
5248 * Copy the stuff before the expanded part.
5249 * Copy the expanded stuff.
5250 * Copy what came after the expanded part.
5251 * Copy the next commands, if there are any.
5252 */
5253 i = (int)(src - *cmdlinep); /* length of part before match */
5254 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 mch_memmove(new_cmdline + i, repl, (size_t)len);
5257 i += len; /* remember the end of the string */
5258 STRCPY(new_cmdline + i, src + srclen);
5259 src = new_cmdline + i; /* remember where to continue */
5260
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005261 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 i = (int)STRLEN(new_cmdline) + 1;
5264 STRCPY(new_cmdline + i, eap->nextcmd);
5265 eap->nextcmd = new_cmdline + i;
5266 }
5267 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5268 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5269 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5270 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5271 vim_free(*cmdlinep);
5272 *cmdlinep = new_cmdline;
5273
5274 return src;
5275}
5276
5277/*
5278 * Check for '|' to separate commands and '"' to start comments.
5279 */
5280 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005281separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
5283 char_u *p;
5284
Bram Moolenaar86b68352004-12-27 21:59:20 +00005285#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005286 p = skip_grep_pat(eap);
5287#else
5288 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005289#endif
5290
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005291 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 if (*p == Ctrl_V)
5294 {
5295 if (eap->argt & (USECTRLV | XFILE))
5296 ++p; /* skip CTRL-V and next char */
5297 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005298 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005299 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (*p == NUL) /* stop at NUL after CTRL-V */
5301 break;
5302 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005303
5304#ifdef FEAT_EVAL
5305 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005306 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005307 {
5308 p += 2;
5309 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005310 }
5311#endif
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 /* Check for '"': start of comment or '|': next command */
5314 /* :@" and :*" do not start a comment!
5315 * :redir @" doesn't either. */
5316 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5317 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5318 || p != eap->arg)
5319 && (eap->cmdidx != CMD_redir
5320 || p != eap->arg + 1 || p[-1] != '@'))
5321 || *p == '|' || *p == '\n')
5322 {
5323 /*
5324 * We remove the '\' before the '|', unless USECTRLV is used
5325 * AND 'b' is present in 'cpoptions'.
5326 */
5327 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5328 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5329 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005330 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 --p;
5332 }
5333 else
5334 {
5335 eap->nextcmd = check_nextcmd(p);
5336 *p = NUL;
5337 break;
5338 }
5339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5343 del_trailing_spaces(eap->arg);
5344}
5345
5346/*
5347 * get + command from ex argument
5348 */
5349 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005350getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 char_u *arg = *argp;
5353 char_u *command = NULL;
5354
5355 if (*arg == '+') /* +[command] */
5356 {
5357 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005358 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 command = dollar_command;
5360 else
5361 {
5362 command = arg;
5363 arg = skip_cmd_arg(command, TRUE);
5364 if (*arg != NUL)
5365 *arg++ = NUL; /* terminate command with NUL */
5366 }
5367
5368 arg = skipwhite(arg); /* skip over spaces */
5369 *argp = arg;
5370 }
5371 return command;
5372}
5373
5374/*
5375 * Find end of "+command" argument. Skip over "\ " and "\\".
5376 */
5377 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378skip_cmd_arg(
5379 char_u *p,
5380 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
5382 while (*p && !vim_isspace(*p))
5383 {
5384 if (*p == '\\' && p[1] != NUL)
5385 {
5386 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005387 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
5389 ++p;
5390 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005391 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 return p;
5394}
5395
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005396 int
5397get_bad_opt(char_u *p, exarg_T *eap)
5398{
5399 if (STRICMP(p, "keep") == 0)
5400 eap->bad_char = BAD_KEEP;
5401 else if (STRICMP(p, "drop") == 0)
5402 eap->bad_char = BAD_DROP;
5403 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5404 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005405 else
5406 return FAIL;
5407 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005408}
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410/*
5411 * Get "++opt=arg" argument.
5412 * Return FAIL or OK.
5413 */
5414 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 char_u *arg = eap->arg + 2;
5418 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005419 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
5422 /* ":edit ++[no]bin[ary] file" */
5423 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5424 {
5425 if (*arg == 'n')
5426 {
5427 arg += 2;
5428 eap->force_bin = FORCE_NOBIN;
5429 }
5430 else
5431 eap->force_bin = FORCE_BIN;
5432 if (!checkforcmd(&arg, "binary", 3))
5433 return FAIL;
5434 eap->arg = skipwhite(arg);
5435 return OK;
5436 }
5437
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005438 /* ":read ++edit file" */
5439 if (STRNCMP(arg, "edit", 4) == 0)
5440 {
5441 eap->read_edit = TRUE;
5442 eap->arg = skipwhite(arg + 4);
5443 return OK;
5444 }
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (STRNCMP(arg, "ff", 2) == 0)
5447 {
5448 arg += 2;
5449 pp = &eap->force_ff;
5450 }
5451 else if (STRNCMP(arg, "fileformat", 10) == 0)
5452 {
5453 arg += 10;
5454 pp = &eap->force_ff;
5455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 else if (STRNCMP(arg, "enc", 3) == 0)
5457 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005458 if (STRNCMP(arg, "encoding", 8) == 0)
5459 arg += 8;
5460 else
5461 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 pp = &eap->force_enc;
5463 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005464 else if (STRNCMP(arg, "bad", 3) == 0)
5465 {
5466 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005467 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470 if (pp == NULL || *arg != '=')
5471 return FAIL;
5472
5473 ++arg;
5474 *pp = (int)(arg - eap->cmd);
5475 arg = skip_cmd_arg(arg, FALSE);
5476 eap->arg = skipwhite(arg);
5477 *arg = NUL;
5478
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 if (pp == &eap->force_ff)
5480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5482 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005483 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005485 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
5487 /* Make 'fileencoding' lower case. */
5488 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5489 *p = TOLOWER_ASC(*p);
5490 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005491 else
5492 {
5493 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5494 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005495 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005496 return FAIL;
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
5499 return OK;
5500}
5501
5502/*
5503 * ":abbreviate" and friends.
5504 */
5505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 do_exmap(eap, TRUE); /* almost the same as mapping */
5509}
5510
5511/*
5512 * ":map" and friends.
5513 */
5514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 /*
5518 * If we are sourcing .exrc or .vimrc in current directory we
5519 * print the mappings for security reasons.
5520 */
5521 if (secure)
5522 {
5523 secure = 2;
5524 msg_outtrans(eap->cmd);
5525 msg_putchar('\n');
5526 }
5527 do_exmap(eap, FALSE);
5528}
5529
5530/*
5531 * ":unmap" and friends.
5532 */
5533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005534ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 do_exmap(eap, FALSE);
5537}
5538
5539/*
5540 * ":mapclear" and friends.
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5546}
5547
5548/*
5549 * ":abclear" and friends.
5550 */
5551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005552ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5555}
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005558ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005561 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 * directory for security reasons.
5563 */
5564 if (secure)
5565 {
5566 secure = 2;
5567 eap->errmsg = e_curdir;
5568 }
5569 else if (eap->cmdidx == CMD_autocmd)
5570 do_autocmd(eap->arg, eap->forceit);
5571 else
5572 do_augroup(eap->arg, eap->forceit);
5573}
5574
5575/*
5576 * ":doautocmd": Apply the automatic commands to the current buffer.
5577 */
5578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005579ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005581 char_u *arg = eap->arg;
5582 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005583 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005584
Bram Moolenaar1610d052016-06-09 22:53:01 +02005585 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5586 /* Only when there is no <nomodeline>. */
5587 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005588 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591/*
5592 * :[N]bunload[!] [N] [bufname] unload buffer
5593 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5594 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5595 */
5596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005597ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
5599 eap->errmsg = do_bufdel(
5600 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5601 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5602 : DOBUF_UNLOAD, eap->arg,
5603 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5604}
5605
5606/*
5607 * :[N]buffer [N] to buffer N
5608 * :[N]sbuffer [N] to buffer N
5609 */
5610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 if (*eap->arg)
5614 eap->errmsg = e_trailing;
5615 else
5616 {
5617 if (eap->addr_count == 0) /* default is current buffer */
5618 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5619 else
5620 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005621 if (eap->do_ecmd_cmd != NULL)
5622 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624}
5625
5626/*
5627 * :[N]bmodified [N] to next mod. buffer
5628 * :[N]sbmodified [N] to next mod. buffer
5629 */
5630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005631ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005634 if (eap->do_ecmd_cmd != NULL)
5635 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
5637
5638/*
5639 * :[N]bnext [N] to next buffer
5640 * :[N]sbnext [N] split and to next buffer
5641 */
5642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005646 if (eap->do_ecmd_cmd != NULL)
5647 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648}
5649
5650/*
5651 * :[N]bNext [N] to previous buffer
5652 * :[N]bprevious [N] to previous buffer
5653 * :[N]sbNext [N] split and to previous buffer
5654 * :[N]sbprevious [N] split and to previous buffer
5655 */
5656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005657ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005660 if (eap->do_ecmd_cmd != NULL)
5661 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662}
5663
5664/*
5665 * :brewind to first buffer
5666 * :bfirst to first buffer
5667 * :sbrewind split and to first buffer
5668 * :sbfirst split and to first buffer
5669 */
5670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005674 if (eap->do_ecmd_cmd != NULL)
5675 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676}
5677
5678/*
5679 * :blast to last buffer
5680 * :sblast split and to last buffer
5681 */
5682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005683ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005686 if (eap->do_ecmd_cmd != NULL)
5687 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 return (c == NUL || c == '|' || c == '"' || c == '\n');
5694}
5695
5696#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5697 || defined(PROTO)
5698/*
5699 * Return the next command, after the first '|' or '\n'.
5700 * Return NULL if not found.
5701 */
5702 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005703find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
5705 while (*p != '|' && *p != '\n')
5706 {
5707 if (*p == NUL)
5708 return NULL;
5709 ++p;
5710 }
5711 return (p + 1);
5712}
5713#endif
5714
5715/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005716 * Check if *p is a separator between Ex commands, skipping over white space.
5717 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 */
5719 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005720check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005722 char_u *s = skipwhite(p);
5723
5724 if (*s == '|' || *s == '\n')
5725 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 else
5727 return NULL;
5728}
5729
5730/*
5731 * - if there are more files to edit
5732 * - and this is the last window
5733 * - and forceit not used
5734 * - and not repeated twice on a row
5735 * return FAIL and give error message if 'message' TRUE
5736 * return OK otherwise
5737 */
5738 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739check_more(
5740 int message, /* when FALSE check only, no messages */
5741 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742{
5743 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5744
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005745 if (!forceit && only_one_window()
5746 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 if (message)
5749 {
5750#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5751 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5752 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005753 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005755 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5756 NGETTEXT("%d more file to edit. Quit anyway?",
5757 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5759 return OK;
5760 return FAIL;
5761 }
5762#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005763 semsg(NGETTEXT("E173: %d more file to edit",
5764 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 quitmore = 2; /* next try to quit is allowed */
5766 }
5767 return FAIL;
5768 }
5769 return OK;
5770}
5771
5772#ifdef FEAT_CMDL_COMPL
5773/*
5774 * Function given to ExpandGeneric() to obtain the list of command names.
5775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005777get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 if (idx >= (int)CMD_SIZE)
5780# ifdef FEAT_USR_CMDS
5781 return get_user_command_name(idx);
5782# else
5783 return NULL;
5784# endif
5785 return cmdnames[idx].cmd_name;
5786}
5787#endif
5788
5789#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005791uc_add_command(
5792 char_u *name,
5793 size_t name_len,
5794 char_u *rep,
5795 long argt,
5796 long def,
5797 int flags,
5798 int compl,
5799 char_u *compl_arg,
5800 int addr_type,
5801 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803 ucmd_T *cmd = NULL;
5804 char_u *p;
5805 int i;
5806 int cmp = 1;
5807 char_u *rep_buf = NULL;
5808 garray_T *gap;
5809
Bram Moolenaar9c102382006-05-03 21:26:49 +00005810 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 if (rep_buf == NULL)
5812 {
5813 /* Can't replace termcodes - try using the string as is */
5814 rep_buf = vim_strsave(rep);
5815
5816 /* Give up if out of memory */
5817 if (rep_buf == NULL)
5818 return FAIL;
5819 }
5820
5821 /* get address of growarray: global or in curbuf */
5822 if (flags & UC_BUFFER)
5823 {
5824 gap = &curbuf->b_ucmds;
5825 if (gap->ga_itemsize == 0)
5826 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5827 }
5828 else
5829 gap = &ucmds;
5830
5831 /* Search for the command in the already defined commands. */
5832 for (i = 0; i < gap->ga_len; ++i)
5833 {
5834 size_t len;
5835
5836 cmd = USER_CMD_GA(gap, i);
5837 len = STRLEN(cmd->uc_name);
5838 cmp = STRNCMP(name, cmd->uc_name, name_len);
5839 if (cmp == 0)
5840 {
5841 if (name_len < len)
5842 cmp = -1;
5843 else if (name_len > len)
5844 cmp = 1;
5845 }
5846
5847 if (cmp == 0)
5848 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005849 // Command can be replaced with "command!" and when sourcing the
5850 // same script again, but only once.
5851 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5852 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005854 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005855 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 goto fail;
5857 }
5858
Bram Moolenaard23a8232018-02-10 18:45:26 +01005859 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005860#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005861 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 break;
5864 }
5865
5866 /* Stop as soon as we pass the name to add */
5867 if (cmp < 0)
5868 break;
5869 }
5870
5871 /* Extend the array unless we're replacing an existing command */
5872 if (cmp != 0)
5873 {
5874 if (ga_grow(gap, 1) != OK)
5875 goto fail;
5876 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5877 goto fail;
5878
5879 cmd = USER_CMD_GA(gap, i);
5880 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5881
5882 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
5884 cmd->uc_name = p;
5885 }
5886
5887 cmd->uc_rep = rep_buf;
5888 cmd->uc_argt = argt;
5889 cmd->uc_def = def;
5890 cmd->uc_compl = compl;
5891#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005892 cmd->uc_script_ctx = current_sctx;
5893 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894# ifdef FEAT_CMDL_COMPL
5895 cmd->uc_compl_arg = compl_arg;
5896# endif
5897#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005898 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900 return OK;
5901
5902fail:
5903 vim_free(rep_buf);
5904#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5905 vim_free(compl_arg);
5906#endif
5907 return FAIL;
5908}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005911#if defined(FEAT_USR_CMDS)
5912static struct
5913{
5914 int expand;
5915 char *name;
5916} addr_type_complete[] =
5917{
5918 {ADDR_ARGUMENTS, "arguments"},
5919 {ADDR_LINES, "lines"},
5920 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5921 {ADDR_TABS, "tabs"},
5922 {ADDR_BUFFERS, "buffers"},
5923 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005924 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005925 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005926 {-1, NULL}
5927};
5928#endif
5929
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005930#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931/*
5932 * List of names for completion for ":command" with the EXPAND_ flag.
5933 * Must be alphabetical for completion.
5934 */
5935static struct
5936{
5937 int expand;
5938 char *name;
5939} command_complete[] =
5940{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005941 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005943 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005945 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005947 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005948#if defined(FEAT_CSCOPE)
5949 {EXPAND_CSCOPE, "cscope"},
5950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5952 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005953 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#endif
5955 {EXPAND_DIRECTORIES, "dir"},
5956 {EXPAND_ENV_VARS, "environment"},
5957 {EXPAND_EVENTS, "event"},
5958 {EXPAND_EXPRESSION, "expression"},
5959 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005960 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005961 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 {EXPAND_FUNCTIONS, "function"},
5963 {EXPAND_HELP, "help"},
5964 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005965#if defined(FEAT_CMDHIST)
5966 {EXPAND_HISTORY, "history"},
5967#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005968#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005969 {EXPAND_LOCALES, "locale"},
5970#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005971 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {EXPAND_MAPPINGS, "mapping"},
5973 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005974 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005975 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005976#if defined(FEAT_PROFILE)
5977 {EXPAND_SYNTIME, "syntime"},
5978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005980 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005981 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005982#if defined(FEAT_SIGNS)
5983 {EXPAND_SIGN, "sign"},
5984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {EXPAND_TAGS, "tag"},
5986 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005987 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {EXPAND_USER_VARS, "var"},
5989 {0, NULL}
5990};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005993#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005995uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997 int i, j;
5998 int found = FALSE;
5999 ucmd_T *cmd;
6000 int len;
6001 long a;
6002 garray_T *gap;
6003
6004 gap = &curbuf->b_ucmds;
6005 for (;;)
6006 {
6007 for (i = 0; i < gap->ga_len; ++i)
6008 {
6009 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006010 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011
Bram Moolenaard29459b2016-08-26 22:29:11 +02006012 /* Skip commands which don't match the requested prefix and
6013 * commands filtered out. */
6014 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6015 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 continue;
6017
6018 /* Put out the title first time */
6019 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006020 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 found = TRUE;
6022 msg_putchar('\n');
6023 if (got_int)
6024 break;
6025
6026 /* Special cases */
6027 msg_putchar(a & BANG ? '!' : ' ');
6028 msg_putchar(a & REGSTR ? '"' : ' ');
6029 msg_putchar(gap != &ucmds ? 'b' : ' ');
6030 msg_putchar(' ');
6031
Bram Moolenaar8820b482017-03-16 17:23:31 +01006032 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 len = (int)STRLEN(cmd->uc_name) + 4;
6034
6035 do {
6036 msg_putchar(' ');
6037 ++len;
6038 } while (len < 16);
6039
6040 len = 0;
6041
6042 /* Arguments */
6043 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6044 {
6045 case 0: IObuff[len++] = '0'; break;
6046 case (EXTRA): IObuff[len++] = '*'; break;
6047 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6048 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6049 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6050 }
6051
6052 do {
6053 IObuff[len++] = ' ';
6054 } while (len < 5);
6055
6056 /* Range */
6057 if (a & (RANGE|COUNT))
6058 {
6059 if (a & COUNT)
6060 {
6061 /* -count=N */
6062 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6063 len += (int)STRLEN(IObuff + len);
6064 }
6065 else if (a & DFLALL)
6066 IObuff[len++] = '%';
6067 else if (cmd->uc_def >= 0)
6068 {
6069 /* -range=N */
6070 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6071 len += (int)STRLEN(IObuff + len);
6072 }
6073 else
6074 IObuff[len++] = '.';
6075 }
6076
6077 do {
6078 IObuff[len++] = ' ';
6079 } while (len < 11);
6080
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006081 /* Address Type */
6082 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6083 if (addr_type_complete[j].expand != ADDR_LINES
6084 && addr_type_complete[j].expand == cmd->uc_addr_type)
6085 {
6086 STRCPY(IObuff + len, addr_type_complete[j].name);
6087 len += (int)STRLEN(IObuff + len);
6088 break;
6089 }
6090
6091 do {
6092 IObuff[len++] = ' ';
6093 } while (len < 21);
6094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 /* Completion */
6096 for (j = 0; command_complete[j].expand != 0; ++j)
6097 if (command_complete[j].expand == cmd->uc_compl)
6098 {
6099 STRCPY(IObuff + len, command_complete[j].name);
6100 len += (int)STRLEN(IObuff + len);
6101 break;
6102 }
6103
6104 do {
6105 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006106 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107
6108 IObuff[len] = '\0';
6109 msg_outtrans(IObuff);
6110
6111 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006112#ifdef FEAT_EVAL
6113 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006114 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 out_flush();
6117 ui_breakcheck();
6118 if (got_int)
6119 break;
6120 }
6121 if (gap == &ucmds || i < gap->ga_len)
6122 break;
6123 gap = &ucmds;
6124 }
6125
6126 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006127 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128}
6129
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006130 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006131uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6134 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6135 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6136 0xb9, 0x7f, 0};
6137 int i;
6138
6139 for (i = 0; fcmd[i]; ++i)
6140 IObuff[i] = fcmd[i] - 0x40;
6141 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006142 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143}
6144
6145 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006146uc_scan_attr(
6147 char_u *attr,
6148 size_t len,
6149 long *argt,
6150 long *def,
6151 int *flags,
6152 int *compl,
6153 char_u **compl_arg,
6154 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 char_u *p;
6157
6158 if (len == 0)
6159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006160 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 return FAIL;
6162 }
6163
6164 /* First, try the simple attributes (no arguments) */
6165 if (STRNICMP(attr, "bang", len) == 0)
6166 *argt |= BANG;
6167 else if (STRNICMP(attr, "buffer", len) == 0)
6168 *flags |= UC_BUFFER;
6169 else if (STRNICMP(attr, "register", len) == 0)
6170 *argt |= REGSTR;
6171 else if (STRNICMP(attr, "bar", len) == 0)
6172 *argt |= TRLBAR;
6173 else
6174 {
6175 int i;
6176 char_u *val = NULL;
6177 size_t vallen = 0;
6178 size_t attrlen = len;
6179
6180 /* Look for the attribute name - which is the part before any '=' */
6181 for (i = 0; i < (int)len; ++i)
6182 {
6183 if (attr[i] == '=')
6184 {
6185 val = &attr[i + 1];
6186 vallen = len - i - 1;
6187 attrlen = i;
6188 break;
6189 }
6190 }
6191
6192 if (STRNICMP(attr, "nargs", attrlen) == 0)
6193 {
6194 if (vallen == 1)
6195 {
6196 if (*val == '0')
6197 /* Do nothing - this is the default */;
6198 else if (*val == '1')
6199 *argt |= (EXTRA | NOSPC | NEEDARG);
6200 else if (*val == '*')
6201 *argt |= EXTRA;
6202 else if (*val == '?')
6203 *argt |= (EXTRA | NOSPC);
6204 else if (*val == '+')
6205 *argt |= (EXTRA | NEEDARG);
6206 else
6207 goto wrong_nargs;
6208 }
6209 else
6210 {
6211wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006212 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 return FAIL;
6214 }
6215 }
6216 else if (STRNICMP(attr, "range", attrlen) == 0)
6217 {
6218 *argt |= RANGE;
6219 if (vallen == 1 && *val == '%')
6220 *argt |= DFLALL;
6221 else if (val != NULL)
6222 {
6223 p = val;
6224 if (*def >= 0)
6225 {
6226two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006227 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 return FAIL;
6229 }
6230
6231 *def = getdigits(&p);
6232 *argt |= (ZEROR | NOTADR);
6233
6234 if (p != val + vallen || vallen == 0)
6235 {
6236invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006237 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 return FAIL;
6239 }
6240 }
6241 }
6242 else if (STRNICMP(attr, "count", attrlen) == 0)
6243 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006244 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 if (val != NULL)
6247 {
6248 p = val;
6249 if (*def >= 0)
6250 goto two_count;
6251
6252 *def = getdigits(&p);
6253
6254 if (p != val + vallen)
6255 goto invalid_count;
6256 }
6257
6258 if (*def < 0)
6259 *def = 0;
6260 }
6261 else if (STRNICMP(attr, "complete", attrlen) == 0)
6262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 if (val == NULL)
6264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006265 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 return FAIL;
6267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006269 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6270 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006273 else if (STRNICMP(attr, "addr", attrlen) == 0)
6274 {
6275 *argt |= RANGE;
6276 if (val == NULL)
6277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006278 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006279 return FAIL;
6280 }
6281 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6282 == FAIL)
6283 return FAIL;
6284 if (addr_type_arg != ADDR_LINES)
6285 *argt |= (ZEROR | NOTADR) ;
6286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 else
6288 {
6289 char_u ch = attr[len];
6290 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006291 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 attr[len] = ch;
6293 return FAIL;
6294 }
6295 }
6296
6297 return OK;
6298}
6299
Bram Moolenaara850a712009-01-28 14:42:59 +00006300/*
6301 * ":command ..."
6302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006304ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 char_u *name;
6307 char_u *end;
6308 char_u *p;
6309 long argt = 0;
6310 long def = -1;
6311 int flags = 0;
6312 int compl = EXPAND_NOTHING;
6313 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006314 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006316 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317
6318 p = eap->arg;
6319
6320 /* Check for attributes */
6321 while (*p == '-')
6322 {
6323 ++p;
6324 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006325 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6326 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 == FAIL)
6328 return;
6329 p = skipwhite(end);
6330 }
6331
6332 /* Get the name (if any) and skip to the following argument */
6333 name = p;
6334 if (ASCII_ISALPHA(*p))
6335 while (ASCII_ISALNUM(*p))
6336 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006337 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006339 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 return;
6341 }
6342 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006343 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344
6345 /* If there is nothing after the name, and no attributes were specified,
6346 * we are listing commands
6347 */
6348 p = skipwhite(end);
6349 if (!has_attr && ends_excmd(*p))
6350 {
6351 uc_list(name, end - name);
6352 }
6353 else if (!ASCII_ISUPPER(*name))
6354 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006355 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 return;
6357 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006358 else if ((name_len == 1 && *name == 'X')
6359 || (name_len <= 4
6360 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006362 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006363 return;
6364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 else
6366 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006367 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368}
6369
6370/*
6371 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 * Clear all user commands, global and for current buffer.
6373 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006374 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006375ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 uc_clear(&ucmds);
6378 uc_clear(&curbuf->b_ucmds);
6379}
6380
6381/*
6382 * Clear all user commands for "gap".
6383 */
6384 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006385uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 int i;
6388 ucmd_T *cmd;
6389
6390 for (i = 0; i < gap->ga_len; ++i)
6391 {
6392 cmd = USER_CMD_GA(gap, i);
6393 vim_free(cmd->uc_name);
6394 vim_free(cmd->uc_rep);
6395# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6396 vim_free(cmd->uc_compl_arg);
6397# endif
6398 }
6399 ga_clear(gap);
6400}
6401
6402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 int i = 0;
6406 ucmd_T *cmd = NULL;
6407 int cmp = -1;
6408 garray_T *gap;
6409
6410 gap = &curbuf->b_ucmds;
6411 for (;;)
6412 {
6413 for (i = 0; i < gap->ga_len; ++i)
6414 {
6415 cmd = USER_CMD_GA(gap, i);
6416 cmp = STRCMP(eap->arg, cmd->uc_name);
6417 if (cmp <= 0)
6418 break;
6419 }
6420 if (gap == &ucmds || cmp == 0)
6421 break;
6422 gap = &ucmds;
6423 }
6424
6425 if (cmp != 0)
6426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006427 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 return;
6429 }
6430
6431 vim_free(cmd->uc_name);
6432 vim_free(cmd->uc_rep);
6433# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6434 vim_free(cmd->uc_compl_arg);
6435# endif
6436
6437 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438
6439 if (i < gap->ga_len)
6440 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6441}
6442
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006443/*
6444 * split and quote args for <f-args>
6445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006447uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 char_u *buf;
6450 char_u *p;
6451 char_u *q;
6452 int len;
6453
6454 /* Precalculate length */
6455 p = arg;
6456 len = 2; /* Initial and final quotes */
6457
6458 while (*p)
6459 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006460 if (p[0] == '\\' && p[1] == '\\')
6461 {
6462 len += 2;
6463 p += 2;
6464 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006465 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
6467 len += 1;
6468 p += 2;
6469 }
6470 else if (*p == '\\' || *p == '"')
6471 {
6472 len += 2;
6473 p += 1;
6474 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006475 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 {
6477 p = skipwhite(p);
6478 if (*p == NUL)
6479 break;
6480 len += 3; /* "," */
6481 }
6482 else
6483 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006484 int charlen = (*mb_ptr2len)(p);
Bram Moolenaar13505972019-01-24 15:04:48 +01006485
Bram Moolenaardfef1542012-07-10 19:25:10 +02006486 len += charlen;
6487 p += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 }
6489 }
6490
6491 buf = alloc(len + 1);
6492 if (buf == NULL)
6493 {
6494 *lenp = 0;
6495 return buf;
6496 }
6497
6498 p = arg;
6499 q = buf;
6500 *q++ = '"';
6501 while (*p)
6502 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006503 if (p[0] == '\\' && p[1] == '\\')
6504 {
6505 *q++ = '\\';
6506 *q++ = '\\';
6507 p += 2;
6508 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006509 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
6511 *q++ = p[1];
6512 p += 2;
6513 }
6514 else if (*p == '\\' || *p == '"')
6515 {
6516 *q++ = '\\';
6517 *q++ = *p++;
6518 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006519 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
6521 p = skipwhite(p);
6522 if (*p == NUL)
6523 break;
6524 *q++ = '"';
6525 *q++ = ',';
6526 *q++ = '"';
6527 }
6528 else
6529 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006530 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532 }
6533 *q++ = '"';
6534 *q = 0;
6535
6536 *lenp = len;
6537 return buf;
6538}
6539
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006540 static size_t
6541add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6542{
6543 size_t result;
6544
6545 result = STRLEN(mod_str);
6546 if (*multi_mods)
6547 result += 1;
6548 if (buf != NULL)
6549 {
6550 if (*multi_mods)
6551 STRCAT(buf, " ");
6552 STRCAT(buf, mod_str);
6553 }
6554
6555 *multi_mods = 1;
6556
6557 return result;
6558}
6559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560/*
6561 * Check for a <> code in a user command.
6562 * "code" points to the '<'. "len" the length of the <> (inclusive).
6563 * "buf" is where the result is to be added.
6564 * "split_buf" points to a buffer used for splitting, caller should free it.
6565 * "split_len" is the length of what "split_buf" contains.
6566 * Returns the length of the replacement, which has been added to "buf".
6567 * Returns -1 if there was no match, and only the "<" has been copied.
6568 */
6569 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006570uc_check_code(
6571 char_u *code,
6572 size_t len,
6573 char_u *buf,
6574 ucmd_T *cmd, /* the user command we're expanding */
6575 exarg_T *eap, /* ex arguments */
6576 char_u **split_buf,
6577 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 size_t result = 0;
6580 char_u *p = code + 1;
6581 size_t l = len - 2;
6582 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006583 enum {
6584 ct_ARGS,
6585 ct_BANG,
6586 ct_COUNT,
6587 ct_LINE1,
6588 ct_LINE2,
6589 ct_RANGE,
6590 ct_MODS,
6591 ct_REGISTER,
6592 ct_LT,
6593 ct_NONE
6594 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6597 {
6598 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6599 p += 2;
6600 l -= 2;
6601 }
6602
Bram Moolenaar371d5402006-03-20 21:47:49 +00006603 ++l;
6604 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006606 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006608 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006610 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006612 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006614 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006616 else if (STRNICMP(p, "range>", l) == 0)
6617 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006618 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006620 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006622 else if (STRNICMP(p, "mods>", l) == 0)
6623 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 switch (type)
6626 {
6627 case ct_ARGS:
6628 /* Simple case first */
6629 if (*eap->arg == NUL)
6630 {
6631 if (quote == 1)
6632 {
6633 result = 2;
6634 if (buf != NULL)
6635 STRCPY(buf, "''");
6636 }
6637 else
6638 result = 0;
6639 break;
6640 }
6641
6642 /* When specified there is a single argument don't split it.
6643 * Works for ":Cmd %" when % is "a b c". */
6644 if ((eap->argt & NOSPC) && quote == 2)
6645 quote = 1;
6646
6647 switch (quote)
6648 {
6649 case 0: /* No quoting, no splitting */
6650 result = STRLEN(eap->arg);
6651 if (buf != NULL)
6652 STRCPY(buf, eap->arg);
6653 break;
6654 case 1: /* Quote, but don't split */
6655 result = STRLEN(eap->arg) + 2;
6656 for (p = eap->arg; *p; ++p)
6657 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006658 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6659 /* DBCS can contain \ in a trail byte, skip the
6660 * double-byte character. */
6661 ++p;
6662 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006663 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 ++result;
6665 }
6666
6667 if (buf != NULL)
6668 {
6669 *buf++ = '"';
6670 for (p = eap->arg; *p; ++p)
6671 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006672 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6673 /* DBCS can contain \ in a trail byte, copy the
6674 * double-byte character to avoid escaping. */
6675 *buf++ = *p++;
6676 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006677 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 *buf++ = '\\';
6679 *buf++ = *p;
6680 }
6681 *buf = '"';
6682 }
6683
6684 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006685 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 /* This is hard, so only do it once, and cache the result */
6687 if (*split_buf == NULL)
6688 *split_buf = uc_split_args(eap->arg, split_len);
6689
6690 result = *split_len;
6691 if (buf != NULL && result != 0)
6692 STRCPY(buf, *split_buf);
6693
6694 break;
6695 }
6696 break;
6697
6698 case ct_BANG:
6699 result = eap->forceit ? 1 : 0;
6700 if (quote)
6701 result += 2;
6702 if (buf != NULL)
6703 {
6704 if (quote)
6705 *buf++ = '"';
6706 if (eap->forceit)
6707 *buf++ = '!';
6708 if (quote)
6709 *buf = '"';
6710 }
6711 break;
6712
6713 case ct_LINE1:
6714 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006715 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 case ct_COUNT:
6717 {
6718 char num_buf[20];
6719 long num = (type == ct_LINE1) ? eap->line1 :
6720 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006721 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6723 size_t num_len;
6724
6725 sprintf(num_buf, "%ld", num);
6726 num_len = STRLEN(num_buf);
6727 result = num_len;
6728
6729 if (quote)
6730 result += 2;
6731
6732 if (buf != NULL)
6733 {
6734 if (quote)
6735 *buf++ = '"';
6736 STRCPY(buf, num_buf);
6737 buf += num_len;
6738 if (quote)
6739 *buf = '"';
6740 }
6741
6742 break;
6743 }
6744
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006745 case ct_MODS:
6746 {
6747 int multi_mods = 0;
6748 typedef struct {
6749 int *varp;
6750 char *name;
6751 } mod_entry_T;
6752 static mod_entry_T mod_entries[] = {
6753#ifdef FEAT_BROWSE_CMD
6754 {&cmdmod.browse, "browse"},
6755#endif
6756#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6757 {&cmdmod.confirm, "confirm"},
6758#endif
6759 {&cmdmod.hide, "hide"},
6760 {&cmdmod.keepalt, "keepalt"},
6761 {&cmdmod.keepjumps, "keepjumps"},
6762 {&cmdmod.keepmarks, "keepmarks"},
6763 {&cmdmod.keeppatterns, "keeppatterns"},
6764 {&cmdmod.lockmarks, "lockmarks"},
6765 {&cmdmod.noswapfile, "noswapfile"},
6766 {NULL, NULL}
6767 };
6768 int i;
6769
6770 result = quote ? 2 : 0;
6771 if (buf != NULL)
6772 {
6773 if (quote)
6774 *buf++ = '"';
6775 *buf = '\0';
6776 }
6777
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006778 /* :aboveleft and :leftabove */
6779 if (cmdmod.split & WSP_ABOVE)
6780 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6781 /* :belowright and :rightbelow */
6782 if (cmdmod.split & WSP_BELOW)
6783 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6784 /* :botright */
6785 if (cmdmod.split & WSP_BOT)
6786 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006787
6788 /* the modifiers that are simple flags */
6789 for (i = 0; mod_entries[i].varp != NULL; ++i)
6790 if (*mod_entries[i].varp)
6791 result += add_cmd_modifier(buf, mod_entries[i].name,
6792 &multi_mods);
6793
6794 /* TODO: How to support :noautocmd? */
6795#ifdef HAVE_SANDBOX
6796 /* TODO: How to support :sandbox?*/
6797#endif
6798 /* :silent */
6799 if (msg_silent > 0)
6800 result += add_cmd_modifier(buf,
6801 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006802 /* :tab */
6803 if (cmdmod.tab > 0)
6804 result += add_cmd_modifier(buf, "tab", &multi_mods);
6805 /* :topleft */
6806 if (cmdmod.split & WSP_TOP)
6807 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006808 /* TODO: How to support :unsilent?*/
6809 /* :verbose */
6810 if (p_verbose > 0)
6811 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006812 /* :vertical */
6813 if (cmdmod.split & WSP_VERT)
6814 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006815 if (quote && buf != NULL)
6816 {
6817 buf += result - 2;
6818 *buf = '"';
6819 }
6820 break;
6821 }
6822
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 case ct_REGISTER:
6824 result = eap->regname ? 1 : 0;
6825 if (quote)
6826 result += 2;
6827 if (buf != NULL)
6828 {
6829 if (quote)
6830 *buf++ = '\'';
6831 if (eap->regname)
6832 *buf++ = eap->regname;
6833 if (quote)
6834 *buf = '\'';
6835 }
6836 break;
6837
6838 case ct_LT:
6839 result = 1;
6840 if (buf != NULL)
6841 *buf = '<';
6842 break;
6843
6844 default:
6845 /* Not recognized: just copy the '<' and return -1. */
6846 result = (size_t)-1;
6847 if (buf != NULL)
6848 *buf = '<';
6849 break;
6850 }
6851
6852 return result;
6853}
6854
6855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006856do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 char_u *buf;
6859 char_u *p;
6860 char_u *q;
6861
6862 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006863 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006864 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 size_t len, totlen;
6866
6867 size_t split_len = 0;
6868 char_u *split_buf = NULL;
6869 ucmd_T *cmd;
6870#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006871 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872#endif
6873
6874 if (eap->cmdidx == CMD_USER)
6875 cmd = USER_CMD(eap->useridx);
6876 else
6877 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6878
6879 /*
6880 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006881 * First round: "buf" is NULL, compute length, allocate "buf".
6882 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 */
6884 buf = NULL;
6885 for (;;)
6886 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006887 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006888 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006890
6891 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006893 start = vim_strchr(p, '<');
6894 if (start != NULL)
6895 end = vim_strchr(start + 1, '>');
6896 if (buf != NULL)
6897 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006898 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6899 ;
6900 if (*ksp == K_SPECIAL
6901 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006902 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6903# ifdef FEAT_GUI
6904 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6905# endif
6906 ))
6907 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006908 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006909 * KS_SPECIAL KE_FILLER, like for mappings, but
6910 * do_cmdline() doesn't handle that, so convert it back.
6911 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6912 len = ksp - p;
6913 if (len > 0)
6914 {
6915 mch_memmove(q, p, len);
6916 q += len;
6917 }
6918 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6919 p = ksp + 3;
6920 continue;
6921 }
6922 }
6923
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006924 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006925 if (start == NULL || end == NULL)
6926 break;
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 /* Include the '>' */
6929 ++end;
6930
6931 /* Take everything up to the '<' */
6932 len = start - p;
6933 if (buf == NULL)
6934 totlen += len;
6935 else
6936 {
6937 mch_memmove(q, p, len);
6938 q += len;
6939 }
6940
6941 len = uc_check_code(start, end - start, q, cmd, eap,
6942 &split_buf, &split_len);
6943 if (len == (size_t)-1)
6944 {
6945 /* no match, continue after '<' */
6946 p = start + 1;
6947 len = 1;
6948 }
6949 else
6950 p = end;
6951 if (buf == NULL)
6952 totlen += len;
6953 else
6954 q += len;
6955 }
6956 if (buf != NULL) /* second time here, finished */
6957 {
6958 STRCPY(q, p);
6959 break;
6960 }
6961
6962 totlen += STRLEN(p); /* Add on the trailing characters */
6963 buf = alloc((unsigned)(totlen + 1));
6964 if (buf == NULL)
6965 {
6966 vim_free(split_buf);
6967 return;
6968 }
6969 }
6970
6971#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006972 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#endif
6974 (void)do_cmdline(buf, eap->getline, eap->cookie,
6975 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6976#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006977 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978#endif
6979 vim_free(buf);
6980 vim_free(split_buf);
6981}
6982
6983# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6984 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006985get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6988}
6989
6990/*
6991 * Function given to ExpandGeneric() to obtain the list of user command names.
6992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 if (idx < curbuf->b_ucmds.ga_len)
6997 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6998 idx -= curbuf->b_ucmds.ga_len;
6999 if (idx < ucmds.ga_len)
7000 return USER_CMD(idx)->uc_name;
7001 return NULL;
7002}
7003
7004/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007005 * Function given to ExpandGeneric() to obtain the list of user address type names.
7006 */
7007 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007008get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007009{
7010 return (char_u *)addr_type_complete[idx].name;
7011}
7012
7013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 * Function given to ExpandGeneric() to obtain the list of user command
7015 * attributes.
7016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007018get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007021 {"addr", "bang", "bar", "buffer", "complete",
7022 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007024 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 return NULL;
7026 return (char_u *)user_cmd_flags[idx];
7027}
7028
7029/*
7030 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7036
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007037 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 return NULL;
7039 return (char_u *)user_cmd_nargs[idx];
7040}
7041
7042/*
7043 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007046get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 return (char_u *)command_complete[idx].name;
7049}
7050# endif /* FEAT_CMDL_COMPL */
7051
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007052/*
7053 * Parse address type argument
7054 */
7055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007056parse_addr_type_arg(
7057 char_u *value,
7058 int vallen,
7059 long *argt,
7060 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007061{
7062 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007063
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007064 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7065 {
7066 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7067 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7068 if (a && b)
7069 {
7070 *addr_type_arg = addr_type_complete[i].expand;
7071 break;
7072 }
7073 }
7074
7075 if (addr_type_complete[i].expand == -1)
7076 {
7077 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007078
Bram Moolenaar1c465442017-03-12 20:10:05 +01007079 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007080 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007081 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007082 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007083 return FAIL;
7084 }
7085
7086 if (*addr_type_arg != ADDR_LINES)
7087 *argt |= NOTADR;
7088
7089 return OK;
7090}
7091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092#endif /* FEAT_USR_CMDS */
7093
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007094#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7095/*
7096 * Parse a completion argument "value[vallen]".
7097 * The detected completion goes in "*complp", argument type in "*argt".
7098 * When there is an argument, for function and user defined completion, it's
7099 * copied to allocated memory and stored in "*compl_arg".
7100 * Returns FAIL if something is wrong.
7101 */
7102 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007103parse_compl_arg(
7104 char_u *value,
7105 int vallen,
7106 int *complp,
7107 long *argt,
7108 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007109{
7110 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007111# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007112 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007113# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007114 int i;
7115 int valend = vallen;
7116
7117 /* Look for any argument part - which is the part after any ',' */
7118 for (i = 0; i < vallen; ++i)
7119 {
7120 if (value[i] == ',')
7121 {
7122 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007123# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007124 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007125# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007126 valend = i;
7127 break;
7128 }
7129 }
7130
7131 for (i = 0; command_complete[i].expand != 0; ++i)
7132 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007133 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007134 && STRNCMP(value, command_complete[i].name, valend) == 0)
7135 {
7136 *complp = command_complete[i].expand;
7137 if (command_complete[i].expand == EXPAND_BUFFERS)
7138 *argt |= BUFNAME;
7139 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7140 || command_complete[i].expand == EXPAND_FILES)
7141 *argt |= XFILE;
7142 break;
7143 }
7144 }
7145
7146 if (command_complete[i].expand == 0)
7147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007148 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007149 return FAIL;
7150 }
7151
7152# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7153 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7154 && arg != NULL)
7155# else
7156 if (arg != NULL)
7157# endif
7158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007159 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007160 return FAIL;
7161 }
7162
7163# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7164 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7165 && arg == NULL)
7166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007167 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007168 return FAIL;
7169 }
7170
7171 if (arg != NULL)
7172 *compl_arg = vim_strnsave(arg, (int)arglen);
7173# endif
7174 return OK;
7175}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007176
7177 int
7178cmdcomplete_str_to_type(char_u *complete_str)
7179{
7180 int i;
7181
7182 for (i = 0; command_complete[i].expand != 0; ++i)
7183 if (STRCMP(complete_str, command_complete[i].name) == 0)
7184 return command_complete[i].expand;
7185
7186 return EXPAND_NOTHING;
7187}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007188#endif
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007191ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
Bram Moolenaare6850792010-05-14 15:28:44 +02007193 if (*eap->arg == NUL)
7194 {
7195#ifdef FEAT_EVAL
7196 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7197 char_u *p = NULL;
7198
7199 if (expr != NULL)
7200 {
7201 ++emsg_off;
7202 p = eval_to_string(expr, NULL, FALSE);
7203 --emsg_off;
7204 vim_free(expr);
7205 }
7206 if (p != NULL)
7207 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007208 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007209 vim_free(p);
7210 }
7211 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007212 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007213#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007214 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007215#endif
7216 }
7217 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007218 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01007219
7220#ifdef FEAT_VTP
7221 else if (has_vtp_working())
7222 {
7223 // background color change requires clear + redraw
7224 update_screen(CLEAR);
7225 redrawcmd();
7226 }
7227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228}
7229
7230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007231ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007234 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 do_highlight(eap->arg, eap->forceit, FALSE);
7236}
7237
7238
7239/*
7240 * Call this function if we thought we were going to exit, but we won't
7241 * (because of an error). May need to restore the terminal mode.
7242 */
7243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007244not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
7246 exiting = FALSE;
7247 settmode(TMODE_RAW);
7248}
7249
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007250 static int
7251before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7252{
7253 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7254
7255 /* Bail out when autocommands closed the window.
7256 * Refuse to quit when the buffer in the last window is being closed (can
7257 * only happen in autocommands). */
7258 if (!win_valid(wp)
7259 || curbuf_locked()
7260 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7261 return TRUE;
7262
7263 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7264 {
7265 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7266 /* Refuse to quit when locked or when the buffer in the last window is
7267 * being closed (can only happen in autocommands). */
7268 if (curbuf_locked()
7269 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7270 return TRUE;
7271 }
7272
7273 return FALSE;
7274}
7275
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007277 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007278 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 */
7280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007281ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007283 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285#ifdef FEAT_CMDWIN
7286 if (cmdwin_type != 0)
7287 {
7288 cmdwin_result = Ctrl_C;
7289 return;
7290 }
7291#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007292 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007293 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007294 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007295 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007296 return;
7297 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007298 if (eap->addr_count > 0)
7299 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007300 int wnr = eap->line2;
7301
7302 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7303 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007304 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007305 }
7306 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007307 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007308
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007309 /* Refuse to quit when locked. */
7310 if (curbuf_locked())
7311 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007312
7313 /* Trigger QuitPre and maybe ExitPre */
7314 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007315 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
7317#ifdef FEAT_NETBEANS_INTG
7318 netbeansForcedQuit = eap->forceit;
7319#endif
7320
7321 /*
7322 * If there are more files or windows we won't exit.
7323 */
7324 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7325 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007326 if ((!buf_hide(wp->w_buffer)
7327 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007328 | (eap->forceit ? CCGD_FORCEIT : 0)
7329 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007331 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
7333 not_exiting();
7334 }
7335 else
7336 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007337 /* quit last window
7338 * Note: only_one_window() returns true, even so a help window is
7339 * still open. In that case only quit, if no address has been
7340 * specified. Example:
7341 * :h|wincmd w|1q - don't quit
7342 * :h|wincmd w|q - quit
7343 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007344 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007346 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007347#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007351 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 }
7353}
7354
7355/*
7356 * ":cquit".
7357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007359ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
7361 getout(1); /* this does not always pass on the exit code to the Manx
7362 compiler. why? */
7363}
7364
7365/*
7366 * ":qall": try to quit all windows
7367 */
7368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007369ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
7371# ifdef FEAT_CMDWIN
7372 if (cmdwin_type != 0)
7373 {
7374 if (eap->forceit)
7375 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7376 else
7377 cmdwin_result = K_XF2;
7378 return;
7379 }
7380# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007381
7382 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007383 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007384 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007385 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007386 return;
7387 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007388
7389 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007390 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007391
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007393 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 getout(0);
7395 not_exiting();
7396}
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398/*
7399 * ":close": close current window, unless it is the last one
7400 */
7401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007402ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007404 win_T *win;
7405 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007406#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007408 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007410#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007411 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007412 {
7413 if (eap->addr_count == 0)
7414 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007415 else
7416 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007417 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007418 {
7419 winnr++;
7420 if (winnr == eap->line2)
7421 break;
7422 }
7423 if (win == NULL)
7424 win = lastwin;
7425 ex_win_close(eap->forceit, win, NULL);
7426 }
7427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
Bram Moolenaar4033c552017-09-16 20:54:51 +02007430#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431/*
7432 * ":pclose": Close any preview window.
7433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007435ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007436{
7437 win_T *win;
7438
Bram Moolenaar29323592016-07-24 22:04:11 +02007439 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007440 if (win->w_p_pvw)
7441 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007442 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443 break;
7444 }
7445}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007446#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447
Bram Moolenaarf740b292006-02-16 22:11:02 +00007448/*
7449 * Close window "win" and take care of handling closing the last window for a
7450 * modified buffer.
7451 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007453ex_win_close(
7454 int forceit,
7455 win_T *win,
7456 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
7458 int need_hide;
7459 buf_T *buf = win->w_buffer;
7460
7461 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007462 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007464#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if ((p_confirm || cmdmod.confirm) && p_write)
7466 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007467 bufref_T bufref;
7468
7469 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007471 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 return;
7473 need_hide = FALSE;
7474 }
7475 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007478 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 return;
7480 }
7481 }
7482
Bram Moolenaar4033c552017-09-16 20:54:51 +02007483#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007485#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007488 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007489 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007490 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007491 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492}
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007495 * Handle the argument for a tabpage related ex command.
7496 * Returns a tabpage number.
7497 * When an error is encountered then eap->errmsg is set.
7498 */
7499 static int
7500get_tabpage_arg(exarg_T *eap)
7501{
7502 int tab_number;
7503 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7504
7505 if (eap->arg && *eap->arg != NUL)
7506 {
7507 char_u *p = eap->arg;
7508 char_u *p_save;
7509 int relative = 0; /* argument +N/-N means: go to N places to the
7510 * right/left relative to the current position. */
7511
7512 if (*p == '-')
7513 {
7514 relative = -1;
7515 p++;
7516 }
7517 else if (*p == '+')
7518 {
7519 relative = 1;
7520 p++;
7521 }
7522
7523 p_save = p;
7524 tab_number = getdigits(&p);
7525
7526 if (relative == 0)
7527 {
7528 if (STRCMP(p, "$") == 0)
7529 tab_number = LAST_TAB_NR;
7530 else if (p == p_save || *p_save == '-' || *p != NUL
7531 || tab_number > LAST_TAB_NR)
7532 {
7533 /* No numbers as argument. */
7534 eap->errmsg = e_invarg;
7535 goto theend;
7536 }
7537 }
7538 else
7539 {
7540 if (*p_save == NUL)
7541 tab_number = 1;
7542 else if (p == p_save || *p_save == '-' || *p != NUL
7543 || tab_number == 0)
7544 {
7545 /* No numbers as argument. */
7546 eap->errmsg = e_invarg;
7547 goto theend;
7548 }
7549 tab_number = tab_number * relative + tabpage_index(curtab);
7550 if (!unaccept_arg0 && relative == -1)
7551 --tab_number;
7552 }
7553 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7554 eap->errmsg = e_invarg;
7555 }
7556 else if (eap->addr_count > 0)
7557 {
7558 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007559 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007560 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007561 tab_number = 0;
7562 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007563 else
7564 {
7565 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01007566 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01007567 {
7568 --tab_number;
7569 if (tab_number < unaccept_arg0)
7570 eap->errmsg = e_invarg;
7571 }
7572 }
7573 }
7574 else
7575 {
7576 switch (eap->cmdidx)
7577 {
7578 case CMD_tabnext:
7579 tab_number = tabpage_index(curtab) + 1;
7580 if (tab_number > LAST_TAB_NR)
7581 tab_number = 1;
7582 break;
7583 case CMD_tabmove:
7584 tab_number = LAST_TAB_NR;
7585 break;
7586 default:
7587 tab_number = tabpage_index(curtab);
7588 }
7589 }
7590
7591theend:
7592 return tab_number;
7593}
7594
7595/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007596 * ":tabclose": close current tab page, unless it is the last one.
7597 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 */
7599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007600ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007602 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007603 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007604
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007605# ifdef FEAT_CMDWIN
7606 if (cmdwin_type != 0)
7607 cmdwin_result = K_IGNORE;
7608 else
7609# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007610 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007611 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007612 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007614 tab_number = get_tabpage_arg(eap);
7615 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007616 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007617 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007618 if (tp == NULL)
7619 {
7620 beep_flush();
7621 return;
7622 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007623 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007624 {
7625 tabpage_close_other(tp, eap->forceit);
7626 return;
7627 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007628 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007629 tabpage_close(eap->forceit);
7630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007632}
7633
7634/*
7635 * ":tabonly": close all tab pages except the current one
7636 */
7637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007638ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007639{
7640 tabpage_T *tp;
7641 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007642 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007643
7644# ifdef FEAT_CMDWIN
7645 if (cmdwin_type != 0)
7646 cmdwin_result = K_IGNORE;
7647 else
7648# endif
7649 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007650 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007651 else
7652 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007653 tab_number = get_tabpage_arg(eap);
7654 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007655 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007656 goto_tabpage(tab_number);
7657 /* Repeat this up to a 1000 times, because autocommands may
7658 * mess up the lists. */
7659 for (done = 0; done < 1000; ++done)
7660 {
7661 FOR_ALL_TABPAGES(tp)
7662 if (tp->tp_topframe != topframe)
7663 {
7664 tabpage_close_other(tp, eap->forceit);
7665 /* if we failed to close it quit */
7666 if (valid_tabpage(tp))
7667 done = 1000;
7668 /* start over, "tp" is now invalid */
7669 break;
7670 }
7671 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007672 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007673 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007674 }
7675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007679 * Close the current tab page.
7680 */
7681 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007682tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007683{
7684 /* First close all the windows but the current one. If that worked then
7685 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007686 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007687 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007688 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007689 ex_win_close(forceit, curwin, NULL);
7690# ifdef FEAT_GUI
7691 need_mouse_correct = TRUE;
7692# endif
7693}
7694
7695/*
7696 * Close tab page "tp", which is not the current tab page.
7697 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007698 * Also takes care of the tab pages line disappearing when closing the
7699 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007700 */
7701 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007702tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007703{
7704 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007705 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007706 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007707
7708 /* Limit to 1000 windows, autocommands may add a window while we close
7709 * one. OK, so I'm paranoid... */
7710 while (++done < 1000)
7711 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007712 wp = tp->tp_firstwin;
7713 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007714
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007715 /* Autocommands may delete the tab page under our fingers and we may
7716 * fail to close a window with a modified buffer. */
7717 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007718 break;
7719 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007720
Bram Moolenaar29323592016-07-24 22:04:11 +02007721 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007722
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007723 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007724 if (h != tabline_height())
7725 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007726}
7727
7728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 * ":only".
7730 */
7731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007732ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007734 win_T *wp;
7735 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736# ifdef FEAT_GUI
7737 need_mouse_correct = TRUE;
7738# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007739 if (eap->addr_count > 0)
7740 {
7741 wnr = eap->line2;
7742 for (wp = firstwin; --wnr > 0; )
7743 {
7744 if (wp->w_next == NULL)
7745 break;
7746 else
7747 wp = wp->w_next;
7748 }
7749 win_goto(wp);
7750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 close_others(TRUE, eap->forceit);
7752}
7753
7754/*
7755 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007756 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007758 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007759ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 if (eap->addr_count == 0)
7762 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007763 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765
7766 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007767ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007769 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007770 if (!eap->skip)
7771 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007772#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007773 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007774#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007775 if (eap->addr_count == 0)
7776 win_close(curwin, FALSE); /* don't free buffer */
7777 else
7778 {
7779 int winnr = 0;
7780 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007781
Bram Moolenaar2256c992016-11-15 21:17:07 +01007782 FOR_ALL_WINDOWS(win)
7783 {
7784 winnr++;
7785 if (winnr == eap->line2)
7786 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007787 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007788 if (win == NULL)
7789 win = lastwin;
7790 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793}
7794
7795/*
7796 * ":stop" and ":suspend": Suspend Vim.
7797 */
7798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 /*
7802 * Disallow suspending for "rvim".
7803 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007804 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
7806 if (!eap->forceit)
7807 autowrite_all();
7808 windgoto((int)Rows - 1, 0);
7809 out_char('\n');
7810 out_flush();
7811 stoptermcap();
7812 out_flush(); /* needed for SUN to restore xterm buffer */
7813#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007814 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815#endif
7816 ui_suspend(); /* call machine specific function */
7817#ifdef FEAT_TITLE
7818 maketitle();
7819 resettitle(); /* force updating the title */
7820#endif
7821 starttermcap();
7822 scroll_start(); /* scroll screen before redrawing */
7823 redraw_later_clear();
7824 shell_resized(); /* may have resized window */
7825 }
7826}
7827
7828/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007829 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 */
7831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007832ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834#ifdef FEAT_CMDWIN
7835 if (cmdwin_type != 0)
7836 {
7837 cmdwin_result = Ctrl_C;
7838 return;
7839 }
7840#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007841 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007842 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007843 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007844 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007845 return;
7846 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007847
7848 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007849 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850
7851 /*
7852 * if more files or windows we won't exit
7853 */
7854 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7855 exiting = TRUE;
7856 if ( ((eap->cmdidx == CMD_wq
7857 || curbufIsChanged())
7858 && do_write(eap) == FAIL)
7859 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007860 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
7862 not_exiting();
7863 }
7864 else
7865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007868 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869# ifdef FEAT_GUI
7870 need_mouse_correct = TRUE;
7871# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007872 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007873 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875}
7876
7877/*
7878 * ":print", ":list", ":number".
7879 */
7880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007881ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007883 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007884 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007887 for ( ;!got_int; ui_breakcheck())
7888 {
7889 print_line(eap->line1,
7890 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7891 || (eap->flags & EXFLAG_NR)),
7892 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7893 if (++eap->line1 > eap->line2)
7894 break;
7895 out_flush(); /* show one line at a time */
7896 }
7897 setpcmark();
7898 /* put cursor at last line */
7899 curwin->w_cursor.lnum = eap->line2;
7900 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904}
7905
7906#ifdef FEAT_BYTEOFF
7907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007908ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 goto_byte(eap->line2);
7911}
7912#endif
7913
7914/*
7915 * ":shell".
7916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007918ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
7920 do_shell(NULL, 0);
7921}
7922
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007923#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007925static int drop_busy = FALSE;
7926static int drop_filec;
7927static char_u **drop_filev = NULL;
7928static int drop_split;
7929static void (*drop_callback)(void *);
7930static void *drop_cookie;
7931
7932 static void
7933handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 exarg_T ea;
7936 int save_msg_scroll = msg_scroll;
7937
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007938 // Setting the argument list may cause screen updates and being called
7939 // recursively. Avoid that by setting drop_busy.
7940 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
7942 /* Check whether the current buffer is changed. If so, we will need
7943 * to split the current window or data could be lost.
7944 * We don't need to check if the 'hidden' option is set, as in this
7945 * case the buffer won't be lost.
7946 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007947 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
7949 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007950 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 --emsg_off;
7952 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007953 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (win_split(0, 0) == FAIL)
7956 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007957 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
7959 /* When splitting the window, create a new alist. Otherwise the
7960 * existing one is overwritten. */
7961 alist_unlink(curwin->w_alist);
7962 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964
7965 /*
7966 * Set up the new argument list.
7967 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007968 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
7970 /*
7971 * Move to the first file.
7972 */
7973 /* Fake up a minimal "next" command for do_argfile() */
7974 vim_memset(&ea, 0, sizeof(ea));
7975 ea.cmd = (char_u *)"next";
7976 do_argfile(&ea, 0);
7977
7978 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7979 * mode that is not needed here. */
7980 need_start_insertmode = FALSE;
7981
7982 /* Restore msg_scroll, otherwise a following command may cause scrolling
7983 * unexpectedly. The screen will be redrawn by the caller, thus
7984 * msg_scroll being set by displaying a message is irrelevant. */
7985 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007986
7987 if (drop_callback != NULL)
7988 drop_callback(drop_cookie);
7989
7990 drop_filev = NULL;
7991 drop_busy = FALSE;
7992}
7993
7994/*
7995 * Handle a file drop. The code is here because a drop is *nearly* like an
7996 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01007997 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007998 * code is very similar to :args and hence needs access to a lot of the static
7999 * functions in this file.
8000 *
8001 * The "filev" list must have been allocated using alloc(), as should each item
8002 * in the list. This function takes over responsibility for freeing the "filev"
8003 * list.
8004 */
8005 void
8006handle_drop(
8007 int filec, // the number of files dropped
8008 char_u **filev, // the list of files dropped
8009 int split, // force splitting the window
8010 void (*callback)(void *), // to be called after setting the argument
8011 // list
8012 void *cookie) // argument for "callback" (allocated)
8013{
8014 // Cannot handle recursive drops, finish the pending one.
8015 if (drop_busy)
8016 {
8017 FreeWild(filec, filev);
8018 vim_free(cookie);
8019 return;
8020 }
8021
8022 // When calling handle_drop() more than once in a row we only use the last
8023 // one.
8024 if (drop_filev != NULL)
8025 {
8026 FreeWild(drop_filec, drop_filev);
8027 vim_free(drop_cookie);
8028 }
8029
8030 drop_filec = filec;
8031 drop_filev = filev;
8032 drop_split = split;
8033 drop_callback = callback;
8034 drop_cookie = cookie;
8035
8036 // Postpone this when:
8037 // - editing the command line
8038 // - not possible to change the current buffer
8039 // - updating the screen
8040 // As it may change buffers and window structures that are in use and cause
8041 // freed memory to be used.
8042 if (text_locked() || curbuf_locked() || updating_screen)
8043 return;
8044
8045 handle_drop_internal();
8046}
8047
8048/*
8049 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8050 * reset: Handle a postponed drop.
8051 */
8052 void
8053handle_any_postponed_drop(void)
8054{
8055 if (!drop_busy && drop_filev != NULL
8056 && !text_locked() && !curbuf_locked() && !updating_screen)
8057 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058}
8059#endif
8060
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061/*
8062 * Clear an argument list: free all file names and reset it to zero entries.
8063 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008065alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
8067 while (--al->al_ga.ga_len >= 0)
8068 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8069 ga_clear(&al->al_ga);
8070}
8071
8072/*
8073 * Init an argument list.
8074 */
8075 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008076alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8079}
8080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081/*
8082 * Remove a reference from an argument list.
8083 * Ignored when the argument list is the global one.
8084 * If the argument list is no longer used by any window, free it.
8085 */
8086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008087alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 if (al != &global_alist && --al->al_refcount <= 0)
8090 {
8091 alist_clear(al);
8092 vim_free(al);
8093 }
8094}
8095
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096/*
8097 * Create a new argument list and use it for the current window.
8098 */
8099 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008100alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
8102 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8103 if (curwin->w_alist == NULL)
8104 {
8105 curwin->w_alist = &global_alist;
8106 ++global_alist.al_refcount;
8107 }
8108 else
8109 {
8110 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008111 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 alist_init(curwin->w_alist);
8113 }
8114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008116#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117/*
8118 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008119 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8120 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 */
8122 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008123alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008126 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 char_u **new_arg_files;
8128 int new_arg_file_count;
8129 char_u *save_p_su = p_su;
8130 int i;
8131
8132 /* Don't use 'suffixes' here. This should work like the shell did the
8133 * expansion. Also, the vimrc file isn't read yet, thus the user
8134 * can't set the options. */
8135 p_su = empty_option;
8136 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8137 if (old_arg_files != NULL)
8138 {
8139 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008140 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8141 old_arg_count = GARGCOUNT;
8142 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008144 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 && new_arg_file_count > 0)
8146 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008147 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8148 TRUE, fnum_list, fnum_len);
8149 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 }
8152 p_su = save_p_su;
8153}
8154#endif
8155
8156/*
8157 * Set the argument list for the current window.
8158 * Takes over the allocated files[] and the allocated fnames in it.
8159 */
8160 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008161alist_set(
8162 alist_T *al,
8163 int count,
8164 char_u **files,
8165 int use_curbuf,
8166 int *fnum_list,
8167 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008170 static int recursive = 0;
8171
8172 if (recursive)
8173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008174 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008175 return;
8176 }
8177 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
8179 alist_clear(al);
8180 if (ga_grow(&al->al_ga, count) == OK)
8181 {
8182 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008183 {
8184 if (got_int)
8185 {
8186 /* When adding many buffers this can take a long time. Allow
8187 * interrupting here. */
8188 while (i < count)
8189 vim_free(files[i++]);
8190 break;
8191 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008192
8193 /* May set buffer name of a buffer previously used for the
8194 * argument list, so that it's re-used by alist_add. */
8195 if (fnum_list != NULL && i < fnum_len)
8196 buf_set_name(fnum_list[i], files[i]);
8197
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008199 ui_breakcheck();
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 vim_free(files);
8202 }
8203 else
8204 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008207
8208 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209}
8210
8211/*
8212 * Add file "fname" to argument list "al".
8213 * "fname" must have been allocated and "al" must have been checked for room.
8214 */
8215 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008216alist_add(
8217 alist_T *al,
8218 char_u *fname,
8219 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
8221 if (fname == NULL) /* don't add NULL file names */
8222 return;
8223#ifdef BACKSLASH_IN_FILENAME
8224 slash_adjust(fname);
8225#endif
8226 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8227 if (set_fnum > 0)
8228 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8229 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8230 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231}
8232
8233#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8234/*
8235 * Adjust slashes in file names. Called after 'shellslash' was set.
8236 */
8237 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008238alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
8240 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008242 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243
8244 for (i = 0; i < GARGCOUNT; ++i)
8245 if (GARGLIST[i].ae_fname != NULL)
8246 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008247 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 if (wp->w_alist != &global_alist)
8249 for (i = 0; i < WARGCOUNT(wp); ++i)
8250 if (WARGLIST(wp)[i].ae_fname != NULL)
8251 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252}
8253#endif
8254
8255/*
8256 * ":preserve".
8257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008261 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 ml_preserve(curbuf, TRUE);
8263}
8264
8265/*
8266 * ":recover".
8267 */
8268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008269ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270{
8271 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8272 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008273 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8274 | CCGD_MULTWIN
8275 | (eap->forceit ? CCGD_FORCEIT : 0)
8276 | CCGD_EXCMD)
8277
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 && (*eap->arg == NUL
8279 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8280 ml_recover();
8281 recoverymode = FALSE;
8282}
8283
8284/*
8285 * Command modifier used in a wrong way.
8286 */
8287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008288ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 eap->errmsg = e_invcmd;
8291}
8292
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293/*
8294 * :sview [+command] file split window with new file, read-only
8295 * :split [[+command] file] split window with current or new file
8296 * :vsplit [[+command] file] split window vertically with current or new file
8297 * :new [[+command] file] split window with no or new file
8298 * :vnew [[+command] file] split vertically window with no or new file
8299 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008300 *
8301 * :tabedit open new Tab page with empty window
8302 * :tabedit [+command] file open new Tab page and edit "file"
8303 * :tabnew [[+command] file] just like :tabedit
8304 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 */
8306 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008307ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008309 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008310#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008312#endif
8313#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008315#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008316 int use_tab = eap->cmdidx == CMD_tabedit
8317 || eap->cmdidx == CMD_tabfind
8318 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
Bram Moolenaar4033c552017-09-16 20:54:51 +02008320#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323
Bram Moolenaar4033c552017-09-16 20:54:51 +02008324#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008326 * quickfix windows. But it's OK when doing ":tab split". */
8327 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
8329 if (eap->cmdidx == CMD_split)
8330 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 if (eap->cmdidx == CMD_vsplit)
8332 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
Bram Moolenaar4033c552017-09-16 20:54:51 +02008336#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008337 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {
8339 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8340 FNAME_MESS, TRUE, curbuf->b_ffname);
8341 if (fname == NULL)
8342 goto theend;
8343 eap->arg = fname;
8344 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008345# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008346 else
8347# endif
8348#endif
8349#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 && eap->cmdidx != CMD_new)
8353 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008354 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008355# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008356 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008357# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008358 au_has_group((char_u *)"FileExplorer"))
8359 {
8360 /* No browsing supported but we do have the file explorer:
8361 * Edit the directory. */
8362 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8363 eap->arg = (char_u *)".";
8364 }
8365 else
8366 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008367 fname = do_browse(0, (char_u *)(use_tab
8368 ? _("Edit File in new tab page")
8369 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008371 if (fname == NULL)
8372 goto theend;
8373 eap->arg = fname;
8374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 }
8376 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008379 /*
8380 * Either open new tab page or split the window.
8381 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008382 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008383 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008384 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8385 : eap->addr_count == 0 ? 0
8386 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008387 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008388 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008389
8390 /* set the alternate buffer for the window we came from */
8391 if (curwin != old_curwin
8392 && win_valid(old_curwin)
8393 && old_curwin->w_buffer != curbuf
8394 && !cmdmod.keepalt)
8395 old_curwin->w_alt_fnum = curbuf->b_fnum;
8396 }
8397 }
8398 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 /* Reset 'scrollbind' when editing another file, but keep it when
8402 * doing ":split" without arguments. */
8403 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008404# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008408 {
8409 RESET_BINDING(curwin);
8410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 else
8412 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 do_exedit(eap, old_curwin);
8414 }
8415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008416# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008420# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421theend:
8422 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008425
8426/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008427 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008428 */
8429 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008430tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008431{
8432 exarg_T ea;
8433
8434 vim_memset(&ea, 0, sizeof(ea));
8435 ea.cmdidx = CMD_tabnew;
8436 ea.cmd = (char_u *)"tabn";
8437 ea.arg = (char_u *)"";
8438 ex_splitview(&ea);
8439}
8440
8441/*
8442 * :tabnext command
8443 */
8444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008445ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008446{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008447 int tab_number;
8448
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008449 switch (eap->cmdidx)
8450 {
8451 case CMD_tabfirst:
8452 case CMD_tabrewind:
8453 goto_tabpage(1);
8454 break;
8455 case CMD_tablast:
8456 goto_tabpage(9999);
8457 break;
8458 case CMD_tabprevious:
8459 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008460 if (eap->arg && *eap->arg != NUL)
8461 {
8462 char_u *p = eap->arg;
8463 char_u *p_save = p;
8464
8465 tab_number = getdigits(&p);
8466 if (p == p_save || *p_save == '-' || *p != NUL
8467 || tab_number == 0)
8468 {
8469 /* No numbers as argument. */
8470 eap->errmsg = e_invarg;
8471 return;
8472 }
8473 }
8474 else
8475 {
8476 if (eap->addr_count == 0)
8477 tab_number = 1;
8478 else
8479 {
8480 tab_number = eap->line2;
8481 if (tab_number < 1)
8482 {
8483 eap->errmsg = e_invrange;
8484 return;
8485 }
8486 }
8487 }
8488 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008489 break;
8490 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008491 tab_number = get_tabpage_arg(eap);
8492 if (eap->errmsg == NULL)
8493 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008494 break;
8495 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008496}
8497
8498/*
8499 * :tabmove command
8500 */
8501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008502ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008503{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008504 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008505
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008506 tab_number = get_tabpage_arg(eap);
8507 if (eap->errmsg == NULL)
8508 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008509}
8510
8511/*
8512 * :tabs command: List tabs and their contents.
8513 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008516{
8517 tabpage_T *tp;
8518 win_T *wp;
8519 int tabcount = 1;
8520
8521 msg_start();
8522 msg_scroll = TRUE;
8523 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8524 {
8525 msg_putchar('\n');
8526 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008527 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008528 out_flush(); /* output one line at a time */
8529 ui_breakcheck();
8530
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008531 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008532 wp = firstwin;
8533 else
8534 wp = tp->tp_firstwin;
8535 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8536 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008537 msg_putchar('\n');
8538 msg_putchar(wp == curwin ? '>' : ' ');
8539 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008540 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8541 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008542 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008543 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008544 else
8545 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8546 IObuff, IOSIZE, TRUE);
8547 msg_outtrans(IObuff);
8548 out_flush(); /* output one line at a time */
8549 ui_breakcheck();
8550 }
8551 }
8552}
8553
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554/*
8555 * ":mode": Set screen mode.
8556 * If no argument given, just get the screen size and redraw.
8557 */
8558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008559ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 if (*eap->arg == NUL)
8562 shell_resized();
8563 else
8564 mch_screenmode(eap->arg);
8565}
8566
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567/*
8568 * ":resize".
8569 * set, increment or decrement current window height
8570 */
8571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008572ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 int n;
8575 win_T *wp = curwin;
8576
8577 if (eap->addr_count > 0)
8578 {
8579 n = eap->line2;
8580 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8581 ;
8582 }
8583
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008584# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (cmdmod.split & WSP_VERT)
8589 {
8590 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008591 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8593 n = 9999;
8594 win_setwidth_win((int)n, wp);
8595 }
8596 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
8598 if (*eap->arg == '-' || *eap->arg == '+')
8599 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008600 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 n = 9999;
8602 win_setheight_win((int)n, wp);
8603 }
8604}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
8606/*
8607 * ":find [+command] <file>" command.
8608 */
8609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008610ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612#ifdef FEAT_SEARCHPATH
8613 char_u *fname;
8614 int count;
8615
8616 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8617 TRUE, curbuf->b_ffname);
8618 if (eap->addr_count > 0)
8619 {
8620 /* Repeat finding the file "count" times. This matters when it
8621 * appears several times in the path. */
8622 count = eap->line2;
8623 while (fname != NULL && --count > 0)
8624 {
8625 vim_free(fname);
8626 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8627 FALSE, curbuf->b_ffname);
8628 }
8629 }
8630
8631 if (fname != NULL)
8632 {
8633 eap->arg = fname;
8634#endif
8635 do_exedit(eap, NULL);
8636#ifdef FEAT_SEARCHPATH
8637 vim_free(fname);
8638 }
8639#endif
8640}
8641
8642/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008643 * ":open" simulation: for now just work like ":visual".
8644 */
8645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008646ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008647{
8648 regmatch_T regmatch;
8649 char_u *p;
8650
8651 curwin->w_cursor.lnum = eap->line2;
8652 beginline(BL_SOL | BL_FIX);
8653 if (*eap->arg == '/')
8654 {
8655 /* ":open /pattern/": put cursor in column found with pattern */
8656 ++eap->arg;
8657 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8658 *p = NUL;
8659 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8660 if (regmatch.regprog != NULL)
8661 {
8662 regmatch.rm_ic = p_ic;
8663 p = ml_get_curline();
8664 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008665 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008667 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008668 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 }
8670 /* Move to the NUL, ignore any other arguments. */
8671 eap->arg += STRLEN(eap->arg);
8672 }
8673 check_cursor();
8674
8675 eap->cmdidx = CMD_visual;
8676 do_exedit(eap, NULL);
8677}
8678
8679/*
8680 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 */
8682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008683ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 do_exedit(eap, NULL);
8686}
8687
8688/*
8689 * ":edit <file>" command and alikes.
8690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008692do_exedit(
8693 exarg_T *eap,
8694 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008698 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
8700 /*
8701 * ":vi" command ends Ex mode.
8702 */
8703 if (exmode_active && (eap->cmdidx == CMD_visual
8704 || eap->cmdidx == CMD_view))
8705 {
8706 exmode_active = FALSE;
8707 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008708 {
8709 /* Special case: ":global/pat/visual\NLvi-commands" */
8710 if (global_busy)
8711 {
8712 int rd = RedrawingDisabled;
8713 int nwr = no_wait_return;
8714 int ms = msg_scroll;
8715#ifdef FEAT_GUI
8716 int he = hold_gui_events;
8717#endif
8718
8719 if (eap->nextcmd != NULL)
8720 {
8721 stuffReadbuff(eap->nextcmd);
8722 eap->nextcmd = NULL;
8723 }
8724
8725 if (exmode_was != EXMODE_VIM)
8726 settmode(TMODE_RAW);
8727 RedrawingDisabled = 0;
8728 no_wait_return = 0;
8729 need_wait_return = FALSE;
8730 msg_scroll = 0;
8731#ifdef FEAT_GUI
8732 hold_gui_events = 0;
8733#endif
8734 must_redraw = CLEAR;
8735
8736 main_loop(FALSE, TRUE);
8737
8738 RedrawingDisabled = rd;
8739 no_wait_return = nwr;
8740 msg_scroll = ms;
8741#ifdef FEAT_GUI
8742 hold_gui_events = he;
8743#endif
8744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 }
8748
8749 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008750 || eap->cmdidx == CMD_tabnew
8751 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008752 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008754 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 setpcmark();
8756 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008757 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8758 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008760 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 || *eap->arg != NUL
8762#ifdef FEAT_BROWSE
8763 || cmdmod.browse
8764#endif
8765 )
8766 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008767 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8768 * can bring us here, others are stopped earlier. */
8769 if (*eap->arg != NUL && curbuf_locked())
8770 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008771
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 n = readonlymode;
8773 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8774 readonlymode = TRUE;
8775 else if (eap->cmdidx == CMD_enew)
8776 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8777 empty buffer */
8778 setpcmark();
8779 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8780 NULL, eap,
8781 /* ":edit" goes to first line if Vi compatible */
8782 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8783 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8784 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008785 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008787 /* after a split we can use an existing buffer */
8788 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008790 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 {
8792 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 if (old_curwin != NULL)
8794 {
8795 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008796 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008798#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008799 cleanup_T cs;
8800
8801 /* Reset the error/interrupt/exception state here so that
8802 * aborting() returns FALSE when closing a window. */
8803 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008804#endif
8805#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008807#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008808 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008809
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008810#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008811 /* Restore the error/interrupt/exception state if not
8812 * discarded by a new aborting error, interrupt, or
8813 * uncaught exception. */
8814 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 }
8819 else if (readonlymode && curbuf->b_nwindows == 1)
8820 {
8821 /* When editing an already visited buffer, 'readonly' won't be set
8822 * but the previous value is kept. With ":view" and ":sview" we
8823 * want the file to be readonly, except when another window is
8824 * editing the same buffer. */
8825 curbuf->b_p_ro = TRUE;
8826 }
8827 readonlymode = n;
8828 }
8829 else
8830 {
8831 if (eap->do_ecmd_cmd != NULL)
8832 do_cmdline_cmd(eap->do_ecmd_cmd);
8833#ifdef FEAT_TITLE
8834 n = curwin->w_arg_idx_invalid;
8835#endif
8836 check_arg_idx(curwin);
8837#ifdef FEAT_TITLE
8838 if (n != curwin->w_arg_idx_invalid)
8839 maketitle();
8840#endif
8841 }
8842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 /*
8844 * if ":split file" worked, set alternate file name in old window to new
8845 * file
8846 */
8847 if (old_curwin != NULL
8848 && *eap->arg != NUL
8849 && curwin != old_curwin
8850 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008851 && old_curwin->w_buffer != curbuf
8852 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854
8855 ex_no_reprint = TRUE;
8856}
8857
8858#ifndef FEAT_GUI
8859/*
8860 * ":gui" and ":gvim" when there is no GUI.
8861 */
8862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008863ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 eap->errmsg = e_nogvim;
8866}
8867#endif
8868
Bram Moolenaar4f974752019-02-17 17:44:42 +01008869#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008871ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 gui_make_tearoff(eap->arg);
8874}
8875#endif
8876
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008877#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8878 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008880ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008882# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8883 if (gui.in_use)
8884 gui_make_popup(eap->arg, eap->forceit);
8885# ifdef FEAT_TERM_POPUP_MENU
8886 else
8887# endif
8888# endif
8889# ifdef FEAT_TERM_POPUP_MENU
8890 pum_make_popup(eap->arg, eap->forceit);
8891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892}
8893#endif
8894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008896ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008899 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008901 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902}
8903
8904/*
8905 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8906 * offset.
8907 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008910ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008913 win_T *save_curwin = curwin;
8914 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 long topline;
8916 long y;
8917 linenr_T old_linenr = curwin->w_cursor.lnum;
8918
8919 setpcmark();
8920
8921 /*
8922 * determine max topline
8923 */
8924 if (curwin->w_p_scb)
8925 {
8926 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008927 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 {
8929 if (wp->w_p_scb && wp->w_buffer)
8930 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01008931 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (topline > y)
8933 topline = y;
8934 }
8935 }
8936 if (topline < 1)
8937 topline = 1;
8938 }
8939 else
8940 {
8941 topline = 1;
8942 }
8943
8944
8945 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008946 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008948 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
8950 if (curwin->w_p_scb)
8951 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008952 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 y = topline - curwin->w_topline;
8954 if (y > 0)
8955 scrollup(y, TRUE);
8956 else
8957 scrolldown(-y, TRUE);
8958 curwin->w_scbind_pos = topline;
8959 redraw_later(VALID);
8960 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008964 curwin = save_curwin;
8965 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 if (curwin->w_p_scb)
8967 {
8968 did_syncbind = TRUE;
8969 checkpcmark();
8970 if (old_linenr != curwin->w_cursor.lnum)
8971 {
8972 char_u ctrl_o[2];
8973
8974 ctrl_o[0] = Ctrl_O;
8975 ctrl_o[1] = 0;
8976 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8977 }
8978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979}
8980
8981
8982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008983ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008985 int i;
8986 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8987 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988
8989 if (eap->usefilter) /* :r!cmd */
8990 do_bang(1, eap, FALSE, FALSE, TRUE);
8991 else
8992 {
8993 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8994 return;
8995
8996#ifdef FEAT_BROWSE
8997 if (cmdmod.browse)
8998 {
8999 char_u *browseFile;
9000
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009001 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 NULL, NULL, NULL, curbuf);
9003 if (browseFile != NULL)
9004 {
9005 i = readfile(browseFile, NULL,
9006 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9007 vim_free(browseFile);
9008 }
9009 else
9010 i = OK;
9011 }
9012 else
9013#endif
9014 if (*eap->arg == NUL)
9015 {
9016 if (check_fname() == FAIL) /* check for no file name */
9017 return;
9018 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9019 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9020 }
9021 else
9022 {
9023 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9024 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9025 i = readfile(eap->arg, NULL,
9026 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9027
9028 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009029 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009031#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 if (!aborting())
9033#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009034 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 }
9036 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009037 {
9038 if (empty && exmode_active)
9039 {
9040 /* Delete the empty line that remains. Historically ex does
9041 * this but vi doesn't. */
9042 if (eap->line2 == 0)
9043 lnum = curbuf->b_ml.ml_line_count;
9044 else
9045 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009046 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009047 {
9048 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009049 if (curwin->w_cursor.lnum > 1
9050 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009051 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009052 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009053 }
9054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 }
9058}
9059
Bram Moolenaarea408852005-06-25 22:49:46 +00009060static char_u *prev_dir = NULL;
9061
9062#if defined(EXITFREE) || defined(PROTO)
9063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009064free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009065{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009066 VIM_CLEAR(prev_dir);
9067 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009068}
9069#endif
9070
Bram Moolenaarf4258302013-06-02 18:20:17 +02009071/*
9072 * Deal with the side effects of changing the current directory.
9073 * When "local" is TRUE then this was after an ":lcd" command.
9074 */
9075 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009077{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009078 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009079 if (local)
9080 {
9081 /* If still in global directory, need to remember current
9082 * directory as global directory. */
9083 if (globaldir == NULL && prev_dir != NULL)
9084 globaldir = vim_strsave(prev_dir);
9085 /* Remember this local directory for the window. */
9086 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9087 curwin->w_localdir = vim_strsave(NameBuff);
9088 }
9089 else
9090 {
9091 /* We are now in the global directory, no need to remember its
9092 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009093 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009094 }
9095
9096 shorten_fnames(TRUE);
9097}
9098
Bram Moolenaarea408852005-06-25 22:49:46 +00009099
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100/*
9101 * ":cd", ":lcd", ":chdir" and ":lchdir".
9102 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009104ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009106 char_u *new_dir;
9107 char_u *tofree;
9108 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
9110 new_dir = eap->arg;
9111#if !defined(UNIX) && !defined(VMS)
9112 /* for non-UNIX ":cd" means: print current directory */
9113 if (*new_dir == NUL)
9114 ex_pwd(NULL);
9115 else
9116#endif
9117 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009118 if (allbuf_locked())
9119 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009120 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9121 && !eap->forceit)
9122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009123 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009124 return;
9125 }
9126
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 /* ":cd -": Change to previous directory */
9128 if (STRCMP(new_dir, "-") == 0)
9129 {
9130 if (prev_dir == NULL)
9131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009132 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 return;
9134 }
9135 new_dir = prev_dir;
9136 }
9137
9138 /* Save current directory for next ":cd -" */
9139 tofree = prev_dir;
9140 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9141 prev_dir = vim_strsave(NameBuff);
9142 else
9143 prev_dir = NULL;
9144
9145#if defined(UNIX) || defined(VMS)
9146 /* for UNIX ":cd" means: go to home directory */
9147 if (*new_dir == NUL)
9148 {
9149 /* use NameBuff for home directory name */
9150# ifdef VMS
9151 char_u *p;
9152
9153 p = mch_getenv((char_u *)"SYS$LOGIN");
9154 if (p == NULL || *p == NUL) /* empty is the same as not set */
9155 NameBuff[0] = NUL;
9156 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009157 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158# else
9159 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9160# endif
9161 new_dir = NameBuff;
9162 }
9163#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009164 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009165 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009166 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009167 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 else
9169 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009170 int is_local_chdir = eap->cmdidx == CMD_lcd
9171 || eap->cmdidx == CMD_lchdir;
9172
9173 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174
9175 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009176 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009178
9179 if (dir_differs)
9180 apply_autocmds(EVENT_DIRCHANGED,
9181 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9182 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 }
9184 vim_free(tofree);
9185 }
9186}
9187
9188/*
9189 * ":pwd".
9190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009192ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9195 {
9196#ifdef BACKSLASH_IN_FILENAME
9197 slash_adjust(NameBuff);
9198#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009199 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 }
9201 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009202 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203}
9204
9205/*
9206 * ":=".
9207 */
9208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009209ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009211 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009212 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213}
9214
9215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009216ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009218 int n;
9219 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220
9221 if (cursor_valid())
9222 {
9223 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9224 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009225 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009227
9228 len = eap->line2;
9229 switch (*eap->arg)
9230 {
9231 case 'm': break;
9232 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009233 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009234 }
9235 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236}
9237
9238/*
9239 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9240 */
9241 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009242do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009245 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
9247 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009248 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009249 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009251 wait_now = msec - done > 1000L ? 1000L : msec - done;
9252#ifdef FEAT_TIMERS
9253 {
9254 long due_time = check_due_timer();
9255
9256 if (due_time > 0 && due_time < wait_now)
9257 wait_now = due_time;
9258 }
9259#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009260#ifdef FEAT_JOB_CHANNEL
9261 if (has_any_channel() && wait_now > 100L)
9262 wait_now = 100L;
9263#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009264 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009265#ifdef FEAT_JOB_CHANNEL
9266 if (has_any_channel())
9267 ui_breakcheck_force(TRUE);
9268 else
9269#endif
9270 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009271#ifdef MESSAGE_QUEUE
9272 /* Process the netbeans and clientserver messages that may have been
9273 * received in the call to ui_breakcheck() when the GUI is in use. This
9274 * may occur when running a test case. */
9275 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009278
9279 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9280 // input buffer, otherwise a following call to input() fails.
9281 if (got_int)
9282 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283}
9284
9285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009286do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 int mode;
9289 char_u *cmdp;
9290
9291 cmdp = eap->cmd;
9292 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9293
9294 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9295 eap->arg, mode, isabbrev))
9296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009297 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009299 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 break;
9301 }
9302}
9303
9304/*
9305 * ":winsize" command (obsolete).
9306 */
9307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009308ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
9310 int w, h;
9311 char_u *arg = eap->arg;
9312 char_u *p;
9313
9314 w = getdigits(&arg);
9315 arg = skipwhite(arg);
9316 p = arg;
9317 h = getdigits(&arg);
9318 if (*p != NUL && *arg == NUL)
9319 set_shellsize(w, h, TRUE);
9320 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009321 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322}
9323
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009325ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326{
9327 int xchar = NUL;
9328 char_u *p;
9329
9330 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9331 {
9332 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9333 if (eap->arg[1] == NUL)
9334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009335 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 return;
9337 }
9338 xchar = eap->arg[1];
9339 p = eap->arg + 2;
9340 }
9341 else
9342 p = eap->arg + 1;
9343
9344 eap->nextcmd = check_nextcmd(p);
9345 p = skipwhite(p);
9346 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009347 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009348 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 {
9350 /* Pass flags on for ":vertical wincmd ]". */
9351 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009352 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9354 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009355 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 }
9357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358
Bram Moolenaar843ee412004-06-30 16:16:41 +00009359#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360/*
9361 * ":winpos".
9362 */
9363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009364ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 int x, y;
9367 char_u *arg = eap->arg;
9368 char_u *p;
9369
9370 if (*arg == NUL)
9371 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009372# if defined(FEAT_GUI) || defined(MSWIN)
9373# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009375# else
9376 if (mch_get_winpos(&x, &y) != FAIL)
9377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 {
9379 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009380 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 }
9382 else
9383# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009384 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 }
9386 else
9387 {
9388 x = getdigits(&arg);
9389 arg = skipwhite(arg);
9390 p = arg;
9391 y = getdigits(&arg);
9392 if (*p == NUL || *arg != NUL)
9393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009394 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 return;
9396 }
9397# ifdef FEAT_GUI
9398 if (gui.in_use)
9399 gui_mch_set_winpos(x, y);
9400 else if (gui.starting)
9401 {
9402 /* Remember the coordinates for when the window is opened. */
9403 gui_win_x = x;
9404 gui_win_y = y;
9405 }
9406# ifdef HAVE_TGETENT
9407 else
9408# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009409# else
9410# ifdef MSWIN
9411 mch_set_winpos(x, y);
9412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413# endif
9414# ifdef HAVE_TGETENT
9415 if (*T_CWP)
9416 term_set_winpos(x, y);
9417# endif
9418 }
9419}
9420#endif
9421
9422/*
9423 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9424 */
9425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009426ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427{
9428 oparg_T oa;
9429
9430 clear_oparg(&oa);
9431 oa.regname = eap->regname;
9432 oa.start.lnum = eap->line1;
9433 oa.end.lnum = eap->line2;
9434 oa.line_count = eap->line2 - eap->line1 + 1;
9435 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9438 {
9439 setpcmark();
9440 curwin->w_cursor.lnum = eap->line1;
9441 beginline(BL_SOL | BL_FIX);
9442 }
9443
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009444 if (VIsual_active)
9445 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009446
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 switch (eap->cmdidx)
9448 {
9449 case CMD_delete:
9450 oa.op_type = OP_DELETE;
9451 op_delete(&oa);
9452 break;
9453
9454 case CMD_yank:
9455 oa.op_type = OP_YANK;
9456 (void)op_yank(&oa, FALSE, TRUE);
9457 break;
9458
9459 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009460 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009462 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9463#else
9464 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009466 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 oa.op_type = OP_RSHIFT;
9468 else
9469 oa.op_type = OP_LSHIFT;
9470 op_shift(&oa, FALSE, eap->amount);
9471 break;
9472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009474 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475}
9476
9477/*
9478 * ":put".
9479 */
9480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009481ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
9483 /* ":0put" works like ":1put!". */
9484 if (eap->line2 == 0)
9485 {
9486 eap->line2 = 1;
9487 eap->forceit = TRUE;
9488 }
9489 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009490 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9491 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492}
9493
9494/*
9495 * Handle ":copy" and ":move".
9496 */
9497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009498ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
9500 long n;
9501
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009502 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 if (eap->arg == NULL) /* error detected */
9504 {
9505 eap->nextcmd = NULL;
9506 return;
9507 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009508 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509
9510 /*
9511 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9512 */
9513 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009515 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 return;
9517 }
9518
9519 if (eap->cmdidx == CMD_move)
9520 {
9521 if (do_move(eap->line1, eap->line2, n) == FAIL)
9522 return;
9523 }
9524 else
9525 ex_copy(eap->line1, eap->line2, n);
9526 u_clearline();
9527 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009528 ex_may_print(eap);
9529}
9530
9531/*
9532 * Print the current line if flags were given to the Ex command.
9533 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009535ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009536{
9537 if (eap->flags != 0)
9538 {
9539 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9540 (eap->flags & EXFLAG_LIST));
9541 ex_no_reprint = TRUE;
9542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543}
9544
9545/*
9546 * ":smagic" and ":snomagic".
9547 */
9548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009549ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 int magic_save = p_magic;
9552
9553 p_magic = (eap->cmdidx == CMD_smagic);
9554 do_sub(eap);
9555 p_magic = magic_save;
9556}
9557
9558/*
9559 * ":join".
9560 */
9561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009562ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 curwin->w_cursor.lnum = eap->line1;
9565 if (eap->line1 == eap->line2)
9566 {
9567 if (eap->addr_count >= 2) /* :2,2join does nothing */
9568 return;
9569 if (eap->line2 == curbuf->b_ml.ml_line_count)
9570 {
9571 beep_flush();
9572 return;
9573 }
9574 ++eap->line2;
9575 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009576 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009578 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579}
9580
9581/*
9582 * ":[addr]@r" or ":[addr]*r": execute register
9583 */
9584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009585ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009588 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589
9590 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009591 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592
9593#ifdef USE_ON_FLY_SCROLL
9594 dont_scroll = TRUE; /* disallow scrolling here */
9595#endif
9596
9597 /* get the register name. No name means to use the previous one */
9598 c = *eap->arg;
9599 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9600 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009601 /* Put the register in the typeahead buffer with the "silent" flag. */
9602 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9603 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 else
9608 {
9609 int save_efr = exec_from_reg;
9610
9611 exec_from_reg = TRUE;
9612
9613 /*
9614 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009615 * Continue until the stuff buffer is empty and all added characters
9616 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009618 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9620
9621 exec_from_reg = save_efr;
9622 }
9623}
9624
9625/*
9626 * ":!".
9627 */
9628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009629ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
9631 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9632}
9633
9634/*
9635 * ":undo".
9636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009638ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009640 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009641 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009642 else
9643 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644}
9645
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009646#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009648ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009649{
9650 char_u hash[UNDO_HASH_SIZE];
9651
9652 u_compute_hash(hash);
9653 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9654}
9655
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009657ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009658{
9659 char_u hash[UNDO_HASH_SIZE];
9660
9661 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009662 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009663}
9664#endif
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666/*
9667 * ":redo".
9668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009670ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
9672 u_redo(1);
9673}
9674
9675/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009676 * ":earlier" and ":later".
9677 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009679ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009680{
9681 long count = 0;
9682 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009683 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009684 char_u *p = eap->arg;
9685
9686 if (*p == NUL)
9687 count = 1;
9688 else if (isdigit(*p))
9689 {
9690 count = getdigits(&p);
9691 switch (*p)
9692 {
9693 case 's': ++p; sec = TRUE; break;
9694 case 'm': ++p; sec = TRUE; count *= 60; break;
9695 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009696 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9697 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009698 }
9699 }
9700
9701 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009702 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009703 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009704 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9705 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009706}
9707
9708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 * ":redir": start/stop redirection.
9710 */
9711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009712ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
9714 char *mode;
9715 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009716 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717
Bram Moolenaarba768492016-07-08 15:32:54 +02009718#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009719 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009721 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009722 return;
9723 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009724#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009725
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 if (STRICMP(eap->arg, "END") == 0)
9727 close_redir();
9728 else
9729 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009730 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009732 ++arg;
9733 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009735 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 mode = "a";
9737 }
9738 else
9739 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009740 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741
9742 close_redir();
9743
9744 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009745 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 if (fname == NULL)
9747 return;
9748#ifdef FEAT_BROWSE
9749 if (cmdmod.browse)
9750 {
9751 char_u *browseFile;
9752
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009753 browseFile = do_browse(BROWSE_SAVE,
9754 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009755 fname, NULL, NULL,
9756 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (browseFile == NULL)
9758 return; /* operation cancelled */
9759 vim_free(fname);
9760 fname = browseFile;
9761 eap->forceit = TRUE; /* since dialog already asked */
9762 }
9763#endif
9764
9765 redir_fd = open_exfile(fname, eap->forceit, mode);
9766 vim_free(fname);
9767 }
9768#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009769 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 {
9771 /* redirect to a register a-z (resp. A-Z for appending) */
9772 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009773 ++arg;
9774 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009776 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009777 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009779 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009781 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009782 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009783 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009784 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009786 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009787 if (*arg == '>')
9788 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009789 /* Make register empty when not using @A-@Z and the
9790 * command is valid. */
9791 if (*arg == NUL && !isupper(redir_reg))
9792 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009794 }
9795 if (*arg != NUL)
9796 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009797 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009798 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009799 }
9800 }
9801 else if (*arg == '=' && arg[1] == '>')
9802 {
9803 int append;
9804
9805 /* redirect to a variable */
9806 close_redir();
9807 arg += 2;
9808
9809 if (*arg == '>')
9810 {
9811 ++arg;
9812 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
9814 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009815 append = FALSE;
9816
9817 if (var_redir_start(skipwhite(arg), append) == OK)
9818 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 }
9820#endif
9821
9822 /* TODO: redirect to a buffer */
9823
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009825 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009827
9828 /* Make sure redirection is not off. Can happen for cmdline completion
9829 * that indirectly invokes a command to catch its output. */
9830 if (redir_fd != NULL
9831#ifdef FEAT_EVAL
9832 || redir_reg || redir_vname
9833#endif
9834 )
9835 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836}
9837
9838/*
9839 * ":redraw": force redraw
9840 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009841 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009842ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843{
9844 int r = RedrawingDisabled;
9845 int p = p_lz;
9846
9847 RedrawingDisabled = 0;
9848 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009849 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009851 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852#ifdef FEAT_TITLE
9853 if (need_maketitle)
9854 maketitle();
9855#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01009856#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01009857 resize_console_buf();
9858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 RedrawingDisabled = r;
9860 p_lz = p;
9861
9862 /* Reset msg_didout, so that a message that's there is overwritten. */
9863 msg_didout = FALSE;
9864 msg_col = 0;
9865
Bram Moolenaar56667a52013-07-17 11:54:28 +02009866 /* No need to wait after an intentional redraw. */
9867 need_wait_return = FALSE;
9868
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 out_flush();
9870}
9871
9872/*
9873 * ":redrawstatus": force redraw of status line(s)
9874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009876ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 int r = RedrawingDisabled;
9879 int p = p_lz;
9880
9881 RedrawingDisabled = 0;
9882 p_lz = FALSE;
9883 if (eap->forceit)
9884 status_redraw_all();
9885 else
9886 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009887 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 RedrawingDisabled = r;
9889 p_lz = p;
9890 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
Bram Moolenaare12bab32019-01-08 22:02:56 +01009893/*
9894 * ":redrawtabline": force redraw of the tabline
9895 */
9896 static void
9897ex_redrawtabline(exarg_T *eap UNUSED)
9898{
9899 int r = RedrawingDisabled;
9900 int p = p_lz;
9901
9902 RedrawingDisabled = 0;
9903 p_lz = FALSE;
9904
9905 draw_tabline();
9906
9907 RedrawingDisabled = r;
9908 p_lz = p;
9909 out_flush();
9910}
9911
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009913close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
9915 if (redir_fd != NULL)
9916 {
9917 fclose(redir_fd);
9918 redir_fd = NULL;
9919 }
9920#ifdef FEAT_EVAL
9921 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009922 if (redir_vname)
9923 {
9924 var_redir_stop();
9925 redir_vname = 0;
9926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#endif
9928}
9929
9930#if defined(FEAT_SESSION) && defined(USE_CRNL)
9931# define MKSESSION_NL
9932static int mksession_nl = FALSE; /* use NL only in put_eol() */
9933#endif
9934
9935/*
9936 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9937 */
9938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009939ex_mkrc(
9940 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941{
9942 FILE *fd;
9943 int failed = FALSE;
9944 char_u *fname;
9945#ifdef FEAT_BROWSE
9946 char_u *browseFile = NULL;
9947#endif
9948#ifdef FEAT_SESSION
9949 int view_session = FALSE;
9950 int using_vdir = FALSE; /* using 'viewdir'? */
9951 char_u *viewFile = NULL;
9952 unsigned *flagp;
9953#endif
9954
9955 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9956 {
9957#ifdef FEAT_SESSION
9958 view_session = TRUE;
9959#else
9960 ex_ni(eap);
9961 return;
9962#endif
9963 }
9964
9965#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009966 /* Use the short file name until ":lcd" is used. We also don't use the
9967 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009968 did_lcd = FALSE;
9969
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9971 if (eap->cmdidx == CMD_mkview
9972 && (*eap->arg == NUL
9973 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9974 {
9975 eap->forceit = TRUE;
9976 fname = get_view_file(*eap->arg);
9977 if (fname == NULL)
9978 return;
9979 viewFile = fname;
9980 using_vdir = TRUE;
9981 }
9982 else
9983#endif
9984 if (*eap->arg != NUL)
9985 fname = eap->arg;
9986 else if (eap->cmdidx == CMD_mkvimrc)
9987 fname = (char_u *)VIMRC_FILE;
9988#ifdef FEAT_SESSION
9989 else if (eap->cmdidx == CMD_mksession)
9990 fname = (char_u *)SESSION_FILE;
9991#endif
9992 else
9993 fname = (char_u *)EXRC_FILE;
9994
9995#ifdef FEAT_BROWSE
9996 if (cmdmod.browse)
9997 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009998 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999# ifdef FEAT_SESSION
10000 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10001 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10002# endif
10003 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010004 fname, (char_u *)"vim", NULL,
10005 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (browseFile == NULL)
10007 goto theend;
10008 fname = browseFile;
10009 eap->forceit = TRUE; /* since dialog already asked */
10010 }
10011#endif
10012
10013#if defined(FEAT_SESSION) && defined(vim_mkdir)
10014 /* When using 'viewdir' may have to create the directory. */
10015 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010016 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017#endif
10018
10019 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10020 if (fd != NULL)
10021 {
10022#ifdef FEAT_SESSION
10023 if (eap->cmdidx == CMD_mkview)
10024 flagp = &vop_flags;
10025 else
10026 flagp = &ssop_flags;
10027#endif
10028
10029#ifdef MKSESSION_NL
10030 /* "unix" in 'sessionoptions': use NL line separator */
10031 if (view_session && (*flagp & SSOP_UNIX))
10032 mksession_nl = TRUE;
10033#endif
10034
10035 /* Write the version command for :mkvimrc */
10036 if (eap->cmdidx == CMD_mkvimrc)
10037 (void)put_line(fd, "version 6.0");
10038
10039#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010040 if (eap->cmdidx == CMD_mksession)
10041 {
10042 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10043 failed = TRUE;
10044 }
10045
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (eap->cmdidx != CMD_mkview)
10047#endif
10048 {
10049 /* Write setting 'compatible' first, because it has side effects.
10050 * For that same reason only do it when needed. */
10051 if (p_cp)
10052 (void)put_line(fd, "if !&cp | set cp | endif");
10053 else
10054 (void)put_line(fd, "if &cp | set nocp | endif");
10055 }
10056
10057#ifdef FEAT_SESSION
10058 if (!view_session
10059 || (eap->cmdidx == CMD_mksession
10060 && (*flagp & SSOP_OPTIONS)))
10061#endif
10062 failed |= (makemap(fd, NULL) == FAIL
10063 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10064
10065#ifdef FEAT_SESSION
10066 if (!failed && view_session)
10067 {
10068 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10069 failed = TRUE;
10070 if (eap->cmdidx == CMD_mksession)
10071 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010072 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
Bram Moolenaard9462e32011-04-11 21:35:11 +020010074 dirnow = alloc(MAXPATHL);
10075 if (dirnow == NULL)
10076 failed = TRUE;
10077 else
10078 {
10079 /*
10080 * Change to session file's dir.
10081 */
10082 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010084 *dirnow = NUL;
10085 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10086 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010087 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010088 shorten_fnames(TRUE);
10089 }
10090 else if (*dirnow != NUL
10091 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10092 {
10093 if (mch_chdir((char *)globaldir) == 0)
10094 shorten_fnames(TRUE);
10095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096
Bram Moolenaard9462e32011-04-11 21:35:11 +020010097 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
Bram Moolenaard9462e32011-04-11 21:35:11 +020010099 /* restore original dir */
10100 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010102 {
10103 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010104 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010105 shorten_fnames(TRUE);
10106 }
10107 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 }
10109 }
10110 else
10111 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010112 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10113 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 }
10115 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10116 == FAIL)
10117 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010118# ifdef FEAT_SEARCH_EXTRA
10119 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10120 failed = TRUE;
10121# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010122 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10123 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010124 if (eap->cmdidx == CMD_mksession)
10125 {
10126 if (put_line(fd, "unlet SessionLoad") == FAIL)
10127 failed = TRUE;
10128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 }
10130#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010131 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10132 failed = TRUE;
10133
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 failed |= fclose(fd);
10135
10136 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010137 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10139 else if (eap->cmdidx == CMD_mksession)
10140 {
10141 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010142 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143
Bram Moolenaard9462e32011-04-11 21:35:11 +020010144 tbuf = alloc(MAXPATHL);
10145 if (tbuf != NULL)
10146 {
10147 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10148 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10149 vim_free(tbuf);
10150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 }
10152#endif
10153#ifdef MKSESSION_NL
10154 mksession_nl = FALSE;
10155#endif
10156 }
10157
10158#ifdef FEAT_BROWSE
10159theend:
10160 vim_free(browseFile);
10161#endif
10162#ifdef FEAT_SESSION
10163 vim_free(viewFile);
10164#endif
10165}
10166
Bram Moolenaardf177f62005-02-22 08:39:57 +000010167#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10168 || defined(PROTO)
10169 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010170vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010171{
10172 if (vim_mkdir(name, prot) != 0)
10173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010174 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010175 return FAIL;
10176 }
10177 return OK;
10178}
10179#endif
10180
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181/*
10182 * Open a file for writing for an Ex command, with some checks.
10183 * Return file descriptor, or NULL on failure.
10184 */
10185 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010186open_exfile(
10187 char_u *fname,
10188 int forceit,
10189 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 FILE *fd;
10192
10193#ifdef UNIX
10194 /* with Unix it is possible to open a directory */
10195 if (mch_isdir(fname))
10196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010197 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 return NULL;
10199 }
10200#endif
10201 if (!forceit && *mode != 'a' && vim_fexists(fname))
10202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010203 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 return NULL;
10205 }
10206
10207 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010208 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209
10210 return fd;
10211}
10212
10213/*
10214 * ":mark" and ":k".
10215 */
10216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010217ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218{
10219 pos_T pos;
10220
10221 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010222 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010224 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 else
10226 {
10227 pos = curwin->w_cursor; /* save curwin->w_cursor */
10228 curwin->w_cursor.lnum = eap->line2;
10229 beginline(BL_WHITE | BL_FIX);
10230 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010231 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10233 }
10234}
10235
10236/*
10237 * Update w_topline, w_leftcol and the cursor position.
10238 */
10239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010240update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241{
10242 check_cursor(); /* put cursor on valid line */
10243 update_topline();
10244 if (!curwin->w_p_wrap)
10245 validate_cursor();
10246 update_curswant();
10247}
10248
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010250 * Save the current State and go to Normal mode.
10251 * Return TRUE if the typeahead could be saved.
10252 */
10253 int
10254save_current_state(save_state_T *sst)
10255{
10256 sst->save_msg_scroll = msg_scroll;
10257 sst->save_restart_edit = restart_edit;
10258 sst->save_msg_didout = msg_didout;
10259 sst->save_State = State;
10260 sst->save_insertmode = p_im;
10261 sst->save_finish_op = finish_op;
10262 sst->save_opcount = opcount;
10263
10264 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10265 restart_edit = 0; /* don't go to Insert mode */
10266 p_im = FALSE; /* don't use 'insertmode' */
10267
10268 /*
10269 * Save the current typeahead. This is required to allow using ":normal"
10270 * from an event handler and makes sure we don't hang when the argument
10271 * ends with half a command.
10272 */
10273 save_typeahead(&sst->tabuf);
10274 return sst->tabuf.typebuf_valid;
10275}
10276
10277 void
10278restore_current_state(save_state_T *sst)
10279{
10280 /* Restore the previous typeahead. */
10281 restore_typeahead(&sst->tabuf);
10282
10283 msg_scroll = sst->save_msg_scroll;
10284 restart_edit = sst->save_restart_edit;
10285 p_im = sst->save_insertmode;
10286 finish_op = sst->save_finish_op;
10287 opcount = sst->save_opcount;
10288 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10289
10290 /* Restore the state (needed when called from a function executed for
10291 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10292 State = sst->save_State;
10293#ifdef CURSOR_SHAPE
10294 ui_cursor_shape(); /* may show different cursor shape */
10295#endif
10296}
10297
10298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 * ":normal[!] {commands}": Execute normal mode commands.
10300 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010301 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010302ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010304 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 char_u *arg = NULL;
10306 int l;
10307 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010309 if (ex_normal_lock > 0)
10310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010311 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010312 return;
10313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 if (ex_normal_busy >= p_mmd)
10315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010316 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 return;
10318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 /*
10321 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10322 * this for the K_SPECIAL leading byte, otherwise special keys will not
10323 * work.
10324 */
10325 if (has_mbyte)
10326 {
10327 int len = 0;
10328
10329 /* Count the number of characters to be escaped. */
10330 for (p = eap->arg; *p != NUL; ++p)
10331 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010332#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 if (*p == CSI) /* leadbyte CSI */
10334 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +010010335#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010336 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +010010338#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +010010340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 )
10342 len += 2;
10343 }
10344 if (len > 0)
10345 {
10346 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10347 if (arg != NULL)
10348 {
10349 len = 0;
10350 for (p = eap->arg; *p != NUL; ++p)
10351 {
10352 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +010010353#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 if (*p == CSI)
10355 {
10356 arg[len++] = KS_EXTRA;
10357 arg[len++] = (int)KE_CSI;
10358 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010359#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010360 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 {
10362 arg[len++] = *++p;
10363 if (*p == K_SPECIAL)
10364 {
10365 arg[len++] = KS_SPECIAL;
10366 arg[len++] = KE_FILLER;
10367 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010368#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 else if (*p == CSI)
10370 {
10371 arg[len++] = KS_EXTRA;
10372 arg[len++] = (int)KE_CSI;
10373 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376 arg[len] = NUL;
10377 }
10378 }
10379 }
10380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010382 ++ex_normal_busy;
10383 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 {
10385 /*
10386 * Repeat the :normal command for each line in the range. When no
10387 * range given, execute it just once, without positioning the cursor
10388 * first.
10389 */
10390 do
10391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 if (eap->addr_count != 0)
10393 {
10394 curwin->w_cursor.lnum = eap->line1++;
10395 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010396 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 }
10398
Bram Moolenaar13505972019-01-24 15:04:48 +010010399 exec_normal_cmd(arg != NULL
10400 ? arg
10401 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 }
10403 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10404 }
10405
10406 /* Might not return to the main loop when in an event handler. */
10407 update_topline_cursor();
10408
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010409 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010411#ifdef FEAT_MOUSE
10412 setmouse();
10413#endif
10414#ifdef CURSOR_SHAPE
10415 ui_cursor_shape(); /* may show different cursor shape */
10416#endif
10417
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419}
10420
10421/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010422 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 */
10424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010425ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010427 if (eap->forceit)
10428 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010429 /* cursor line can be zero on startup */
10430 if (!curwin->w_cursor.lnum)
10431 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010432 coladvance((colnr_T)MAXCOL);
10433 curwin->w_curswant = MAXCOL;
10434 curwin->w_set_curswant = FALSE;
10435 }
10436
Bram Moolenaara40c5002005-01-09 21:16:21 +000010437 /* Ignore the command when already in Insert mode. Inserting an
10438 * expression register that invokes a function can do this. */
10439 if (State & INSERT)
10440 return;
10441
Bram Moolenaar64969662005-12-14 21:59:55 +000010442 if (eap->cmdidx == CMD_startinsert)
10443 restart_edit = 'a';
10444 else if (eap->cmdidx == CMD_startreplace)
10445 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010447 restart_edit = 'V';
10448
10449 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010451 if (eap->cmdidx == CMD_startinsert)
10452 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 curwin->w_curswant = 0; /* avoid MAXCOL */
10454 }
10455}
10456
10457/*
10458 * ":stopinsert"
10459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010461ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 restart_edit = 0;
10464 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010465 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010468/*
10469 * Execute normal mode command "cmd".
10470 * "remap" can be REMAP_NONE or REMAP_YES.
10471 */
10472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010473exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010474{
Bram Moolenaar25281632016-01-21 23:32:32 +010010475 /* Stuff the argument into the typeahead buffer. */
10476 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010477 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010478}
Bram Moolenaar25281632016-01-21 23:32:32 +010010479
Bram Moolenaar25281632016-01-21 23:32:32 +010010480/*
10481 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010482 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010483 */
10484 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010485exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010486{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010487 oparg_T oa;
10488
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010489 clear_oparg(&oa);
10490 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010491 while ((!stuff_empty()
10492 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10493 || (use_vpeekc && vpeekc() != NUL))
10494 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010495 {
10496 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010497#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010498 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010499 && oa.op_type == OP_NOP && oa.regname == NUL
10500 && !VIsual_active)
10501 {
10502 /* If terminal_loop() returns OK we got a key that is handled
10503 * in Normal model. With FAIL we first need to position the
10504 * cursor and the screen needs to be redrawn. */
10505 if (terminal_loop(TRUE) == OK)
10506 normal_cmd(&oa, TRUE);
10507 }
10508 else
10509#endif
10510 /* execute a Normal mode cmd */
10511 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010512 }
10513}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010514
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515#ifdef FEAT_FIND_ID
10516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010517ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518{
10519 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10520 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10521 (linenr_T)1, (linenr_T)MAXLNUM);
10522}
10523
Bram Moolenaar4033c552017-09-16 20:54:51 +020010524#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525/*
10526 * ":psearch"
10527 */
10528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010529ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530{
10531 g_do_tagpreview = p_pvh;
10532 ex_findpat(eap);
10533 g_do_tagpreview = 0;
10534}
10535#endif
10536
10537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010538ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540 int whole = TRUE;
10541 long n;
10542 char_u *p;
10543 int action;
10544
10545 switch (cmdnames[eap->cmdidx].cmd_name[2])
10546 {
10547 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10548 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10549 action = ACTION_GOTO;
10550 else
10551 action = ACTION_SHOW;
10552 break;
10553 case 'i': /* ":ilist" and ":dlist" */
10554 action = ACTION_SHOW_ALL;
10555 break;
10556 case 'u': /* ":ijump" and ":djump" */
10557 action = ACTION_GOTO;
10558 break;
10559 default: /* ":isplit" and ":dsplit" */
10560 action = ACTION_SPLIT;
10561 break;
10562 }
10563
10564 n = 1;
10565 if (vim_isdigit(*eap->arg)) /* get count */
10566 {
10567 n = getdigits(&eap->arg);
10568 eap->arg = skipwhite(eap->arg);
10569 }
10570 if (*eap->arg == '/') /* Match regexp, not just whole words */
10571 {
10572 whole = FALSE;
10573 ++eap->arg;
10574 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10575 if (*p)
10576 {
10577 *p++ = NUL;
10578 p = skipwhite(p);
10579
10580 /* Check for trailing illegal characters */
10581 if (!ends_excmd(*p))
10582 eap->errmsg = e_trailing;
10583 else
10584 eap->nextcmd = check_nextcmd(p);
10585 }
10586 }
10587 if (!eap->skip)
10588 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10589 whole, !eap->forceit,
10590 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10591 n, action, eap->line1, eap->line2);
10592}
10593#endif
10594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595
Bram Moolenaar4033c552017-09-16 20:54:51 +020010596#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597/*
10598 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10599 */
10600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010601ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010603 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10605}
10606
10607/*
10608 * ":pedit"
10609 */
10610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010611ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
10613 win_T *curwin_save = curwin;
10614
10615 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010616 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010617 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 do_exedit(eap, NULL);
10619 keep_help_flag = FALSE;
10620 if (curwin != curwin_save && win_valid(curwin_save))
10621 {
10622 /* Return cursor to where we were */
10623 validate_cursor();
10624 redraw_later(VALID);
10625 win_enter(curwin_save, TRUE);
10626 }
10627 g_do_tagpreview = 0;
10628}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630
10631/*
10632 * ":stag", ":stselect" and ":stjump".
10633 */
10634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010635ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636{
10637 postponed_split = -1;
10638 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010639 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10641 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010642 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644
10645/*
10646 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10647 */
10648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010649ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
10651 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10652}
10653
10654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010655ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656{
10657 int cmd;
10658
10659 switch (name[1])
10660 {
10661 case 'j': cmd = DT_JUMP; /* ":tjump" */
10662 break;
10663 case 's': cmd = DT_SELECT; /* ":tselect" */
10664 break;
10665 case 'p': cmd = DT_PREV; /* ":tprevious" */
10666 break;
10667 case 'N': cmd = DT_PREV; /* ":tNext" */
10668 break;
10669 case 'n': cmd = DT_NEXT; /* ":tnext" */
10670 break;
10671 case 'o': cmd = DT_POP; /* ":pop" */
10672 break;
10673 case 'f': /* ":tfirst" */
10674 case 'r': cmd = DT_FIRST; /* ":trewind" */
10675 break;
10676 case 'l': cmd = DT_LAST; /* ":tlast" */
10677 break;
10678 default: /* ":tag" */
10679#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010680 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010682 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 return;
10684 }
10685#endif
10686 cmd = DT_TAG;
10687 break;
10688 }
10689
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010690 if (name[0] == 'l')
10691 {
10692#ifndef FEAT_QUICKFIX
10693 ex_ni(eap);
10694 return;
10695#else
10696 cmd = DT_LTAG;
10697#endif
10698 }
10699
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10701 eap->forceit, TRUE);
10702}
10703
10704/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010705 * Check "str" for starting with a special cmdline variable.
10706 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10707 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10708 */
10709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010710find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010711{
10712 int len;
10713 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010714 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010715 "%",
10716#define SPEC_PERC 0
10717 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010718#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010719 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010720#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010721 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010722#define SPEC_CCWORD (SPEC_CWORD + 1)
10723 "<cexpr>", /* expr under cursor */
10724#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010725 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010726#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010727 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010728#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010729 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010730#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010731 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010732#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010733 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010734#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010735 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010736#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010737 "<sflnum>", /* script file line number */
10738#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010739#ifdef FEAT_CLIENTSERVER
10740 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010741# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010742#endif
10743 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010744
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010745 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010746 {
10747 len = (int)STRLEN(spec_str[i]);
10748 if (STRNCMP(src, spec_str[i], len) == 0)
10749 {
10750 *usedlen = len;
10751 return i;
10752 }
10753 }
10754 return -1;
10755}
10756
10757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 * Evaluate cmdline variables.
10759 *
10760 * change '%' to curbuf->b_ffname
10761 * '#' to curwin->w_altfile
10762 * '<cword>' to word under the cursor
10763 * '<cWORD>' to WORD under the cursor
10764 * '<cfile>' to path name under the cursor
10765 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010766 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 * '<afile>' to file name for autocommand
10768 * '<abuf>' to buffer number for autocommand
10769 * '<amatch>' to matching name for autocommand
10770 *
10771 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10772 * "" for error without a message) and NULL is returned.
10773 * Returns an allocated string if a valid match was found.
10774 * Returns NULL if no match was found. "usedlen" then still contains the
10775 * number of characters to skip.
10776 */
10777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010778eval_vars(
10779 char_u *src, /* pointer into commandline */
10780 char_u *srcstart, /* beginning of valid memory for src */
10781 int *usedlen, /* characters after src that are used */
10782 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010783 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010784 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010785 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786{
10787 int i;
10788 char_u *s;
10789 char_u *result;
10790 char_u *resultbuf = NULL;
10791 int resultlen;
10792 buf_T *buf;
10793 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10794 int spec_idx;
10795#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010796 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 int skip_mod = FALSE;
10798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
10801 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010802 if (escaped != NULL)
10803 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804
10805 /*
10806 * Check if there is something to do.
10807 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010808 spec_idx = find_cmdline_var(src, usedlen);
10809 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 {
10811 *usedlen = 1;
10812 return NULL;
10813 }
10814
10815 /*
10816 * Skip when preceded with a backslash "\%" and "\#".
10817 * Note: In "\\%" the % is also not recognized!
10818 */
10819 if (src > srcstart && src[-1] == '\\')
10820 {
10821 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010822 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 return NULL;
10824 }
10825
10826 /*
10827 * word or WORD under cursor
10828 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010829 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10830 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010832 resultlen = find_ident_under_cursor(&result,
10833 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10834 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10835 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 if (resultlen == 0)
10837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010838 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 return NULL;
10840 }
10841 }
10842
10843 /*
10844 * '#': Alternate file name
10845 * '%': Current file name
10846 * File name under the cursor
10847 * File name for autocommand
10848 * and following modifiers
10849 */
10850 else
10851 {
10852 switch (spec_idx)
10853 {
10854 case SPEC_PERC: /* '%': current file */
10855 if (curbuf->b_fname == NULL)
10856 {
10857 result = (char_u *)"";
10858 valid = 0; /* Must have ":p:h" to be valid */
10859 }
10860 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010863#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010864 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010865#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 break;
10868
10869 case SPEC_HASH: /* '#' or "#99": alternate file */
10870 if (src[1] == '#') /* "##": the argument list */
10871 {
10872 result = arg_all();
10873 resultbuf = result;
10874 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010875 if (escaped != NULL)
10876 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#ifdef FEAT_MODIFY_FNAME
10878 skip_mod = TRUE;
10879#endif
10880 break;
10881 }
10882 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010883 if (*s == '<') /* "#<99" uses v:oldfiles */
10884 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010886 if (s == src + 2 && src[1] == '-')
10887 /* just a minus sign, don't skip over it */
10888 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 *usedlen = (int)(s - src); /* length of what we expand */
10890
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010891 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010893 if (*usedlen < 2)
10894 {
10895 /* Should we give an error message for #<text? */
10896 *usedlen = 1;
10897 return NULL;
10898 }
10899#ifdef FEAT_EVAL
10900 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10901 (long)i);
10902 if (result == NULL)
10903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010904 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010905 return NULL;
10906 }
10907#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010908 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
10912 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010913 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010914 if (i == 0 && src[1] == '<' && *usedlen > 1)
10915 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010916 buf = buflist_findnr(i);
10917 if (buf == NULL)
10918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010919 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010920 return NULL;
10921 }
10922 if (lnump != NULL)
10923 *lnump = ECMD_LAST;
10924 if (buf->b_fname == NULL)
10925 {
10926 result = (char_u *)"";
10927 valid = 0; /* Must have ":p:h" to be valid */
10928 }
10929 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010930 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010931 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010932#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010933 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010934#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010935 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 break;
10938
10939#ifdef FEAT_SEARCHPATH
10940 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010941 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (result == NULL)
10943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010944 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 return NULL;
10946 }
10947 resultbuf = result; /* remember allocated string */
10948 break;
10949#endif
10950
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 case SPEC_AFILE: /* file name for autocommand */
10952 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010953 if (result != NULL && !autocmd_fname_full)
10954 {
10955 /* Still need to turn the fname into a full path. It is
10956 * postponed to avoid a delay when <afile> is not used. */
10957 autocmd_fname_full = TRUE;
10958 result = FullName_save(autocmd_fname, FALSE);
10959 vim_free(autocmd_fname);
10960 autocmd_fname = result;
10961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 if (result == NULL)
10963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010964 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 return NULL;
10966 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010967 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 break;
10969
10970 case SPEC_ABUF: /* buffer number for autocommand */
10971 if (autocmd_bufnr <= 0)
10972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010973 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 return NULL;
10975 }
10976 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10977 result = strbuf;
10978 break;
10979
10980 case SPEC_AMATCH: /* match name for autocommand */
10981 result = autocmd_match;
10982 if (result == NULL)
10983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010984 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 return NULL;
10986 }
10987 break;
10988
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 case SPEC_SFILE: /* file name for ":so" command */
10990 result = sourcing_name;
10991 if (result == NULL)
10992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010993 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 return NULL;
10995 }
10996 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010997
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010998 case SPEC_SLNUM: /* line in file for ":so" command */
10999 if (sourcing_name == NULL || sourcing_lnum == 0)
11000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011001 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011002 return NULL;
11003 }
11004 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11005 result = strbuf;
11006 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011007
11008#ifdef FEAT_EVAL
11009 case SPEC_SFLNUM: /* line in script file */
11010 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011012 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011013 return NULL;
11014 }
11015 sprintf((char *)strbuf, "%ld",
11016 (long)(current_sctx.sc_lnum + sourcing_lnum));
11017 result = strbuf;
11018 break;
11019#endif
11020
11021#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011023 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11024 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 result = strbuf;
11026 break;
11027#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011028
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011029 default:
11030 result = (char_u *)""; /* avoid gcc warning */
11031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033
11034 resultlen = (int)STRLEN(result); /* length of new string */
11035 if (src[*usedlen] == '<') /* remove the file name extension */
11036 {
11037 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 resultlen = (int)(s - result);
11040 }
11041#ifdef FEAT_MODIFY_FNAME
11042 else if (!skip_mod)
11043 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011044 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 &resultlen);
11046 if (result == NULL)
11047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011048 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 return NULL;
11050 }
11051 }
11052#endif
11053 }
11054
11055 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11056 {
11057 if (valid != VALID_HEAD + VALID_PATH)
11058 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011059 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011061 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 result = NULL;
11063 }
11064 else
11065 result = vim_strnsave(result, resultlen);
11066 vim_free(resultbuf);
11067 return result;
11068}
11069
11070/*
11071 * Concatenate all files in the argument list, separated by spaces, and return
11072 * it in one allocated string.
11073 * Spaces and backslashes in the file names are escaped with a backslash.
11074 * Returns NULL when out of memory.
11075 */
11076 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011077arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 int len;
11080 int idx;
11081 char_u *retval = NULL;
11082 char_u *p;
11083
11084 /*
11085 * Do this loop two times:
11086 * first time: compute the total length
11087 * second time: concatenate the names
11088 */
11089 for (;;)
11090 {
11091 len = 0;
11092 for (idx = 0; idx < ARGCOUNT; ++idx)
11093 {
11094 p = alist_name(&ARGLIST[idx]);
11095 if (p != NULL)
11096 {
11097 if (len > 0)
11098 {
11099 /* insert a space in between names */
11100 if (retval != NULL)
11101 retval[len] = ' ';
11102 ++len;
11103 }
11104 for ( ; *p != NUL; ++p)
11105 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011106 if (*p == ' '
11107#ifndef BACKSLASH_IN_FILENAME
11108 || *p == '\\'
11109#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011110 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 {
11112 /* insert a backslash */
11113 if (retval != NULL)
11114 retval[len] = '\\';
11115 ++len;
11116 }
11117 if (retval != NULL)
11118 retval[len] = *p;
11119 ++len;
11120 }
11121 }
11122 }
11123
11124 /* second time: break here */
11125 if (retval != NULL)
11126 {
11127 retval[len] = NUL;
11128 break;
11129 }
11130
11131 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011132 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 if (retval == NULL)
11134 break;
11135 }
11136
11137 return retval;
11138}
11139
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140/*
11141 * Expand the <sfile> string in "arg".
11142 *
11143 * Returns an allocated string, or NULL for any error.
11144 */
11145 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011146expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011148 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 int len;
11150 char_u *result;
11151 char_u *newres;
11152 char_u *repl;
11153 int srclen;
11154 char_u *p;
11155
11156 result = vim_strsave(arg);
11157 if (result == NULL)
11158 return NULL;
11159
11160 for (p = result; *p; )
11161 {
11162 if (STRNCMP(p, "<sfile>", 7) != 0)
11163 ++p;
11164 else
11165 {
11166 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011167 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 if (errormsg != NULL)
11169 {
11170 if (*errormsg)
11171 emsg(errormsg);
11172 vim_free(result);
11173 return NULL;
11174 }
11175 if (repl == NULL) /* no match (cannot happen) */
11176 {
11177 p += srclen;
11178 continue;
11179 }
11180 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11181 newres = alloc(len);
11182 if (newres == NULL)
11183 {
11184 vim_free(repl);
11185 vim_free(result);
11186 return NULL;
11187 }
11188 mch_memmove(newres, result, (size_t)(p - result));
11189 STRCPY(newres + (p - result), repl);
11190 len = (int)STRLEN(newres);
11191 STRCAT(newres, p + srclen);
11192 vim_free(repl);
11193 vim_free(result);
11194 result = newres;
11195 p = newres + len; /* continue after the match */
11196 }
11197 }
11198
11199 return result;
11200}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201
11202#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011203static int ses_winsizes(FILE *fd, int restore_size,
11204 win_T *tab_firstwin);
11205static int ses_win_rec(FILE *fd, frame_T *fr);
11206static frame_T *ses_skipframe(frame_T *fr);
11207static int ses_do_frame(frame_T *fr);
11208static int ses_do_win(win_T *wp);
11209static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11210static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011211static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212
11213/*
11214 * Write openfile commands for the current buffers to an .exrc file.
11215 * Return FAIL on error, OK otherwise.
11216 */
11217 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011218makeopens(
11219 FILE *fd,
11220 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222 buf_T *buf;
11223 int only_save_windows = TRUE;
11224 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 int restore_size = TRUE;
11226 win_T *wp;
11227 char_u *sname;
11228 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011229 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011230 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011231 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011232 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011233 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011234 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235
11236 if (ssop_flags & SSOP_BUFFERS)
11237 only_save_windows = FALSE; /* Save ALL buffers */
11238
11239 /*
11240 * Begin by setting the this_session variable, and then other
11241 * sessionable variables.
11242 */
11243#ifdef FEAT_EVAL
11244 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11245 return FAIL;
11246 if (ssop_flags & SSOP_GLOBALS)
11247 if (store_session_globals(fd) == FAIL)
11248 return FAIL;
11249#endif
11250
11251 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011252 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 */
11254 if (put_line(fd, "silent only") == FAIL)
11255 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011256 if ((ssop_flags & SSOP_TABPAGES)
11257 && put_line(fd, "silent tabonly") == FAIL)
11258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259
11260 /*
11261 * Now a :cd command to the session directory or the current directory
11262 */
11263 if (ssop_flags & SSOP_SESDIR)
11264 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011265 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11266 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 return FAIL;
11268 }
11269 else if (ssop_flags & SSOP_CURDIR)
11270 {
11271 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11272 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011273 || fputs("cd ", fd) < 0
11274 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11275 || put_eol(fd) == FAIL)
11276 {
11277 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 vim_free(sname);
11281 }
11282
11283 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011284 * If there is an empty, unnamed buffer we will wipe it out later.
11285 * Remember the buffer number.
11286 */
11287 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11288 return FAIL;
11289 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11290 return FAIL;
11291 if (put_line(fd, "endif") == FAIL)
11292 return FAIL;
11293
11294 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 * Now save the current files, current buffer first.
11296 */
11297 if (put_line(fd, "set shortmess=aoO") == FAIL)
11298 return FAIL;
11299
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011301 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11303 return FAIL;
11304
11305 if (ssop_flags & SSOP_RESIZE)
11306 {
11307 /* Note: after the restore we still check it worked!*/
11308 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11309 || put_eol(fd) == FAIL)
11310 return FAIL;
11311 }
11312
11313#ifdef FEAT_GUI
11314 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11315 {
11316 int x, y;
11317
11318 if (gui_mch_get_winpos(&x, &y) == OK)
11319 {
11320 /* Note: after the restore we still check it worked!*/
11321 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11322 return FAIL;
11323 }
11324 }
11325#endif
11326
11327 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011328 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11329 * will be displayed when creating the next tab. That resizes the windows
11330 * in the first tab, which may cause problems. Set 'showtabline' to 2
11331 * temporarily to avoid that.
11332 */
11333 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11334 {
11335 if (put_line(fd, "set stal=2") == FAIL)
11336 return FAIL;
11337 restore_stal = TRUE;
11338 }
11339
11340 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011341 * May repeat putting Windows for each tab, when "tabpages" is in
11342 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011343 * Don't use goto_tabpage(), it may change directory and trigger
11344 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011346 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011347 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011348 if ((ssop_flags & SSOP_TABPAGES))
11349 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011350 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011351
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011352 // Similar to ses_win_rec() below, populate the tab pages first so
11353 // later local options won't be copied to the new tabs.
11354 FOR_ALL_TABPAGES(tp)
11355 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011356 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011357 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011358 return FAIL;
11359 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011360 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011362 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011363 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011364
Bram Moolenaar18144c82006-04-12 21:52:12 +000011365 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011367 tabpage_T *tp = find_tabpage(tabnr);
11368
11369 if (tp == NULL)
11370 break; /* done all tab pages */
11371 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011372 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011373 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011374 tab_topframe = topframe;
11375 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011376 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011377 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011378 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011379 tab_topframe = tp->tp_topframe;
11380 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011381 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011382 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384
Bram Moolenaar18144c82006-04-12 21:52:12 +000011385 /*
11386 * Before creating the window layout, try loading one file. If this
11387 * is aborted we don't end up with a number of useless windows.
11388 * This may have side effects! (e.g., compressed or network file).
11389 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011390 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011391 {
11392 if (ses_do_win(wp)
11393 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011394 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011395#ifdef FEAT_QUICKFIX
11396 && !bt_nofile(wp->w_buffer)
11397#endif
11398 )
11399 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011400 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11401 return FAIL;
11402 need_tabnext = FALSE;
11403
11404 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011405 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11406 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011407 return FAIL;
11408 if (!wp->w_arg_idx_invalid)
11409 edited_win = wp;
11410 break;
11411 }
11412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011414 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011415 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011416 return FAIL;
11417
Bram Moolenaar18144c82006-04-12 21:52:12 +000011418 /*
11419 * Save current window layout.
11420 */
11421 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011423 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011425 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11426 return FAIL;
11427 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429
Bram Moolenaar18144c82006-04-12 21:52:12 +000011430 /*
11431 * Check if window sizes can be restored (no windows omitted).
11432 * Remember the window number of the current window after restoring.
11433 */
11434 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011435 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011436 {
11437 if (ses_do_win(wp))
11438 ++nr;
11439 else
11440 restore_size = FALSE;
11441 if (curwin == wp)
11442 cnr = nr;
11443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444
Bram Moolenaar18144c82006-04-12 21:52:12 +000011445 /* Go to the first window. */
11446 if (put_line(fd, "wincmd t") == FAIL)
11447 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011448
Bram Moolenaar18144c82006-04-12 21:52:12 +000011449 /*
11450 * If more than one window, see if sizes can be restored.
11451 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11452 * resized when moving between windows.
11453 * Do this before restoring the view, so that the topline and the
11454 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011455 * winminheight and winminwidth need to be set to avoid an error if the
11456 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011457 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011458 if (put_line(fd, "set winminheight=0") == FAIL
11459 || put_line(fd, "set winheight=1") == FAIL
11460 || put_line(fd, "set winminwidth=0") == FAIL
11461 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011462 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011463 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465
Bram Moolenaar18144c82006-04-12 21:52:12 +000011466 /*
11467 * Restore the view of the window (options, file, cursor, etc.).
11468 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011469 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011470 {
11471 if (!ses_do_win(wp))
11472 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011473 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11474 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011475 return FAIL;
11476 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11477 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011478 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011479 }
11480
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011481 /* The argument index in the first tab page is zero, need to set it in
11482 * each window. For further tab pages it's the window where we do
11483 * "tabedit". */
11484 cur_arg_idx = next_arg_idx;
11485
Bram Moolenaar18144c82006-04-12 21:52:12 +000011486 /*
11487 * Restore cursor to the current window if it's not the first one.
11488 */
11489 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11490 || put_eol(fd) == FAIL))
11491 return FAIL;
11492
11493 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011494 * Restore window sizes again after jumping around in windows, because
11495 * the current window has a minimum size while others may not.
11496 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011497 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011498 return FAIL;
11499
Bram Moolenaar18144c82006-04-12 21:52:12 +000011500 /* Don't continue in another tab page when doing only the current one
11501 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011502 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011503 break;
11504 }
11505
11506 if (ssop_flags & SSOP_TABPAGES)
11507 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011508 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11509 || put_eol(fd) == FAIL)
11510 return FAIL;
11511 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011512 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11513 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011514
Bram Moolenaard39e2752019-01-26 20:07:38 +010011515 // Now put the remaining buffers into the buffer list.
11516 // This is near the end, so that when 'hidden' is set we don't create extra
11517 // buffers. If the buffer was already created with another command the
11518 // ":badd" will have no effect.
11519 FOR_ALL_BUFFERS(buf)
11520 {
11521 if (!(only_save_windows && buf->b_nwindows == 0)
11522 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11523#ifdef FEAT_TERMINAL
11524 // Skip terminal buffers: finished ones are not useful, others
11525 // will be resurrected and result in a new buffer.
11526 && !bt_terminal(buf)
11527#endif
11528 && buf->b_fname != NULL
11529 && buf->b_p_bl)
11530 {
11531 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11532 : buf->b_wininfo->wi_fpos.lnum) < 0
11533 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
11534 return FAIL;
11535 }
11536 }
11537
Bram Moolenaar9c102382006-05-03 21:26:49 +000011538 /*
11539 * Wipe out an empty unnamed buffer we started in.
11540 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011541 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011542 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011543 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011544 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011545 return FAIL;
11546 if (put_line(fd, "endif") == FAIL)
11547 return FAIL;
11548 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11549 return FAIL;
11550
11551 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11552 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11553 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11554 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011555 /* Re-apply 'winminheight' and 'winminwidth'. */
11556 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11557 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11558 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559
11560 /*
11561 * Lastly, execute the x.vim file if it exists.
11562 */
11563 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11564 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011565 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 || put_line(fd, "endif") == FAIL)
11567 return FAIL;
11568
11569 return OK;
11570}
11571
11572 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011573ses_winsizes(
11574 FILE *fd,
11575 int restore_size,
11576 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577{
11578 int n = 0;
11579 win_T *wp;
11580
11581 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11582 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011583 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 {
11585 if (!ses_do_win(wp))
11586 continue;
11587 ++n;
11588
11589 /* restore height when not full height */
11590 if (wp->w_height + wp->w_status_height < topframe->fr_height
11591 && (fprintf(fd,
11592 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11593 n, (long)wp->w_height, Rows / 2, Rows) < 0
11594 || put_eol(fd) == FAIL))
11595 return FAIL;
11596
11597 /* restore width when not full width */
11598 if (wp->w_width < Columns && (fprintf(fd,
11599 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11600 n, (long)wp->w_width, Columns / 2, Columns) < 0
11601 || put_eol(fd) == FAIL))
11602 return FAIL;
11603 }
11604 }
11605 else
11606 {
11607 /* Just equalise window sizes */
11608 if (put_line(fd, "wincmd =") == FAIL)
11609 return FAIL;
11610 }
11611 return OK;
11612}
11613
11614/*
11615 * Write commands to "fd" to recursively create windows for frame "fr",
11616 * horizontally and vertically split.
11617 * After the commands the last window in the frame is the current window.
11618 * Returns FAIL when writing the commands to "fd" fails.
11619 */
11620 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011621ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622{
11623 frame_T *frc;
11624 int count = 0;
11625
11626 if (fr->fr_layout != FR_LEAF)
11627 {
11628 /* Find first frame that's not skipped and then create a window for
11629 * each following one (first frame is already there). */
11630 frc = ses_skipframe(fr->fr_child);
11631 if (frc != NULL)
11632 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11633 {
11634 /* Make window as big as possible so that we have lots of room
11635 * to split. */
11636 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11637 || put_line(fd, fr->fr_layout == FR_COL
11638 ? "split" : "vsplit") == FAIL)
11639 return FAIL;
11640 ++count;
11641 }
11642
11643 /* Go back to the first window. */
11644 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11645 ? "%dwincmd k" : "%dwincmd h", count) < 0
11646 || put_eol(fd) == FAIL))
11647 return FAIL;
11648
11649 /* Recursively create frames/windows in each window of this column or
11650 * row. */
11651 frc = ses_skipframe(fr->fr_child);
11652 while (frc != NULL)
11653 {
11654 ses_win_rec(fd, frc);
11655 frc = ses_skipframe(frc->fr_next);
11656 /* Go to next window. */
11657 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11658 return FAIL;
11659 }
11660 }
11661 return OK;
11662}
11663
11664/*
11665 * Skip frames that don't contain windows we want to save in the Session.
11666 * Returns NULL when there none.
11667 */
11668 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011669ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670{
11671 frame_T *frc;
11672
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011673 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 if (ses_do_frame(frc))
11675 break;
11676 return frc;
11677}
11678
11679/*
11680 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11681 * the Session.
11682 */
11683 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011684ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686 frame_T *frc;
11687
11688 if (fr->fr_layout == FR_LEAF)
11689 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011690 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 if (ses_do_frame(frc))
11692 return TRUE;
11693 return FALSE;
11694}
11695
11696/*
11697 * Return non-zero if window "wp" is to be stored in the Session.
11698 */
11699 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011700ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011702#ifdef FEAT_TERMINAL
11703 if (bt_terminal(wp->w_buffer))
11704 return !term_is_finished(wp->w_buffer)
11705 && (ssop_flags & SSOP_TERMINAL)
11706 && term_should_restore(wp->w_buffer);
11707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 if (wp->w_buffer->b_fname == NULL
11709#ifdef FEAT_QUICKFIX
11710 /* When 'buftype' is "nofile" can't restore the window contents. */
11711 || bt_nofile(wp->w_buffer)
11712#endif
11713 )
11714 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011715 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 return (ssop_flags & SSOP_HELP);
11717 return TRUE;
11718}
11719
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011720 static int
11721put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11722{
11723 int r;
11724
11725 if (wp->w_curswant == MAXCOL)
11726 r = fprintf(fd, "%snormal! $", spaces);
11727 else
11728 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11729 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11730}
11731
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732/*
11733 * Write commands to "fd" to restore the view of a window.
11734 * Caller must make sure 'scrolloff' is zero.
11735 */
11736 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011737put_view(
11738 FILE *fd,
11739 win_T *wp,
11740 int add_edit, /* add ":edit" command to view */
11741 unsigned *flagp, /* vop_flags or ssop_flags */
11742 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011743 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744{
11745 win_T *save_curwin;
11746 int f;
11747 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011748 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749
11750 /* Always restore cursor position for ":mksession". For ":mkview" only
11751 * when 'viewoptions' contains "cursor". */
11752 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11753
11754 /*
11755 * Local argument list.
11756 */
11757 if (wp->w_alist == &global_alist)
11758 {
11759 if (put_line(fd, "argglobal") == FAIL)
11760 return FAIL;
11761 }
11762 else
11763 {
11764 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11765 flagp == &vop_flags
11766 || !(*flagp & SSOP_CURDIR)
11767 || wp->w_localdir != NULL, flagp) == FAIL)
11768 return FAIL;
11769 }
11770
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011771 /* Only when part of a session: restore the argument index. Some
11772 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011773 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011774 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011776 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 || put_eol(fd) == FAIL)
11778 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011779 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 }
11781
11782 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011783 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011785# ifdef FEAT_TERMINAL
11786 if (bt_terminal(wp->w_buffer))
11787 {
11788 if (term_write_session(fd, wp) == FAIL)
11789 return FAIL;
11790 }
11791 else
11792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 /*
11794 * Load the file.
11795 */
11796 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011797# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 )
11801 {
11802 /*
11803 * Editing a file in this buffer: use ":edit file".
11804 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011805 *
11806 * Note, if a buffer for that file already exists, use :badd to
11807 * edit that buffer, to not lose folding information (:edit resets
11808 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010011810 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011811 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010011812 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011813 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11814 || fputs(" | else | edit ", fd) < 0
11815 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11816 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011817 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 return FAIL;
11819 }
11820 else
11821 {
11822 /* No file in this buffer, just make it empty. */
11823 if (put_line(fd, "enew") == FAIL)
11824 return FAIL;
11825#ifdef FEAT_QUICKFIX
11826 if (wp->w_buffer->b_ffname != NULL)
11827 {
11828 /* The buffer does have a name, but it's not a file name. */
11829 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011830 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 return FAIL;
11832 }
11833#endif
11834 do_cursor = FALSE;
11835 }
11836 }
11837
11838 /*
11839 * Local mappings and abbreviations.
11840 */
11841 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11842 && makemap(fd, wp->w_buffer) == FAIL)
11843 return FAIL;
11844
11845 /*
11846 * Local options. Need to go to the window temporarily.
11847 * Store only local values when using ":mkview" and when ":mksession" is
11848 * used and 'sessionoptions' doesn't include "options".
11849 * Some folding options are always stored when "folds" is included,
11850 * otherwise the folds would not be restored correctly.
11851 */
11852 save_curwin = curwin;
11853 curwin = wp;
11854 curbuf = curwin->w_buffer;
11855 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11856 f = makeset(fd, OPT_LOCAL,
11857 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11858#ifdef FEAT_FOLDING
11859 else if (*flagp & SSOP_FOLDS)
11860 f = makefoldset(fd);
11861#endif
11862 else
11863 f = OK;
11864 curwin = save_curwin;
11865 curbuf = curwin->w_buffer;
11866 if (f == FAIL)
11867 return FAIL;
11868
11869#ifdef FEAT_FOLDING
11870 /*
11871 * Save Folds when 'buftype' is empty and for help files.
11872 */
11873 if ((*flagp & SSOP_FOLDS)
11874 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011875 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 {
11877 if (put_folds(fd, wp) == FAIL)
11878 return FAIL;
11879 }
11880#endif
11881
11882 /*
11883 * Set the cursor after creating folds, since that moves the cursor.
11884 */
11885 if (do_cursor)
11886 {
11887
11888 /* Restore the cursor line in the file and relatively in the
11889 * window. Don't use "G", it changes the jumplist. */
11890 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11891 (long)wp->w_cursor.lnum,
11892 (long)(wp->w_cursor.lnum - wp->w_topline),
11893 (long)wp->w_height / 2, (long)wp->w_height) < 0
11894 || put_eol(fd) == FAIL
11895 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11896 || put_line(fd, "exe s:l") == FAIL
11897 || put_line(fd, "normal! zt") == FAIL
11898 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11899 || put_eol(fd) == FAIL)
11900 return FAIL;
11901 /* Restore the cursor column and left offset when not wrapping. */
11902 if (wp->w_cursor.col == 0)
11903 {
11904 if (put_line(fd, "normal! 0") == FAIL)
11905 return FAIL;
11906 }
11907 else
11908 {
11909 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11910 {
11911 if (fprintf(fd,
11912 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011913 (long)wp->w_virtcol + 1,
11914 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 (long)wp->w_width / 2, (long)wp->w_width) < 0
11916 || put_eol(fd) == FAIL
11917 || put_line(fd, "if s:c > 0") == FAIL
11918 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011919 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11920 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 || put_eol(fd) == FAIL
11922 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011923 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 || put_line(fd, "endif") == FAIL)
11925 return FAIL;
11926 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011927 else if (put_view_curpos(fd, wp, "") == FAIL)
11928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 }
11930 }
11931
11932 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011933 * Local directory, if the current flag is not view options or the "curdir"
11934 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011936 if (wp->w_localdir != NULL
11937 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 {
11939 if (fputs("lcd ", fd) < 0
11940 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11941 || put_eol(fd) == FAIL)
11942 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011943 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 }
11945
11946 return OK;
11947}
11948
11949/*
11950 * Write an argument list to the session file.
11951 * Returns FAIL if writing fails.
11952 */
11953 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011954ses_arglist(
11955 FILE *fd,
11956 char *cmd,
11957 garray_T *gap,
11958 int fullname, /* TRUE: use full path name */
11959 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960{
11961 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011962 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 char_u *s;
11964
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011965 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11966 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011967 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 return FAIL;
11969 for (i = 0; i < gap->ga_len; ++i)
11970 {
11971 /* NULL file names are skipped (only happens when out of memory). */
11972 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11973 if (s != NULL)
11974 {
11975 if (fullname)
11976 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011977 buf = alloc(MAXPATHL);
11978 if (buf != NULL)
11979 {
11980 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11981 s = buf;
11982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011984 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011985 || ses_put_fname(fd, s, flagp) == FAIL
11986 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011987 {
11988 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011990 }
11991 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 }
11993 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011994 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995}
11996
11997/*
11998 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011999 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 * Returns FAIL if writing fails.
12001 */
12002 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012003ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
12005 char_u *name;
12006
12007 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012008 * the session file will be sourced.
12009 * Don't do this for ":mkview", we don't know the current directory.
12010 * Don't do this after ":lcd", we don't keep track of what the current
12011 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 if (buf->b_sfname != NULL
12013 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012014 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012015#ifdef FEAT_AUTOCHDIR
12016 && !p_acd
12017#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012018 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 name = buf->b_sfname;
12020 else
12021 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012022 if (ses_put_fname(fd, name, flagp) == FAIL
12023 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 return FAIL;
12025 return OK;
12026}
12027
12028/*
12029 * Write a file name to the session file.
12030 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12031 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012032 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 */
12034 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012035ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
12037 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012038 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040
12041 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012042 if (sname == NULL)
12043 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012045 if (*flagp & SSOP_SLASH)
12046 {
12047 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012048 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012049 if (*p == '\\')
12050 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012052
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012053 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012054 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012056 if (p == NULL)
12057 return FAIL;
12058
12059 /* write the result */
12060 if (fputs((char *)p, fd) < 0)
12061 retval = FAIL;
12062
12063 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 return retval;
12065}
12066
12067/*
12068 * ":loadview [nr]"
12069 */
12070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012071ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072{
12073 char_u *fname;
12074
12075 fname = get_view_file(*eap->arg);
12076 if (fname != NULL)
12077 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012078 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 vim_free(fname);
12080 }
12081}
12082
12083/*
12084 * Get the name of the view file for the current buffer.
12085 */
12086 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012087get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088{
12089 int len = 0;
12090 char_u *p, *s;
12091 char_u *retval;
12092 char_u *sname;
12093
12094 if (curbuf->b_ffname == NULL)
12095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012096 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 return NULL;
12098 }
12099 sname = home_replace_save(NULL, curbuf->b_ffname);
12100 if (sname == NULL)
12101 return NULL;
12102
12103 /*
12104 * We want a file name without separators, because we're not going to make
12105 * a directory.
12106 * "normal" path separator -> "=+"
12107 * "=" -> "=="
12108 * ":" path separator -> "=-"
12109 */
12110 for (p = sname; *p; ++p)
12111 if (*p == '=' || vim_ispathsep(*p))
12112 ++len;
12113 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12114 if (retval != NULL)
12115 {
12116 STRCPY(retval, p_vdir);
12117 add_pathsep(retval);
12118 s = retval + STRLEN(retval);
12119 for (p = sname; *p; ++p)
12120 {
12121 if (*p == '=')
12122 {
12123 *s++ = '=';
12124 *s++ = '=';
12125 }
12126 else if (vim_ispathsep(*p))
12127 {
12128 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012129#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 if (*p == ':')
12131 *s++ = '-';
12132 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012134 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 }
12136 else
12137 *s++ = *p;
12138 }
12139 *s++ = '=';
12140 *s++ = c;
12141 STRCPY(s, ".vim");
12142 }
12143
12144 vim_free(sname);
12145 return retval;
12146}
12147
12148#endif /* FEAT_SESSION */
12149
12150/*
12151 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12152 * Return FAIL for a write error.
12153 */
12154 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012155put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156{
12157 if (
12158#ifdef USE_CRNL
12159 (
12160# ifdef MKSESSION_NL
12161 !mksession_nl &&
12162# endif
12163 (putc('\r', fd) < 0)) ||
12164#endif
12165 (putc('\n', fd) < 0))
12166 return FAIL;
12167 return OK;
12168}
12169
12170/*
12171 * Write a line to "fd".
12172 * Return FAIL for a write error.
12173 */
12174 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012175put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176{
12177 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12178 return FAIL;
12179 return OK;
12180}
12181
12182#ifdef FEAT_VIMINFO
12183/*
12184 * ":rviminfo" and ":wviminfo".
12185 */
12186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012187ex_viminfo(
12188 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189{
12190 char_u *save_viminfo;
12191
12192 save_viminfo = p_viminfo;
12193 if (*p_viminfo == NUL)
12194 p_viminfo = (char_u *)"'100";
12195 if (eap->cmdidx == CMD_rviminfo)
12196 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012197 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12198 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012199 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 }
12201 else
12202 write_viminfo(eap->arg, eap->forceit);
12203 p_viminfo = save_viminfo;
12204}
12205#endif
12206
12207#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012208/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012209 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012210 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012213dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 if (fname == NULL)
12216 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012217 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218}
12219#endif
12220
12221/*
12222 * ":behave {mswin,xterm}"
12223 */
12224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012225ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226{
12227 if (STRCMP(eap->arg, "mswin") == 0)
12228 {
12229 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12230 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12231 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12232 set_option_value((char_u *)"keymodel", 0L,
12233 (char_u *)"startsel,stopsel", 0);
12234 }
12235 else if (STRCMP(eap->arg, "xterm") == 0)
12236 {
12237 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12238 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12239 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12240 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12241 }
12242 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012243 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244}
12245
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012246#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12247/*
12248 * Function given to ExpandGeneric() to obtain the possible arguments of the
12249 * ":behave {mswin,xterm}" command.
12250 */
12251 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012252get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012253{
12254 if (idx == 0)
12255 return (char_u *)"mswin";
12256 if (idx == 1)
12257 return (char_u *)"xterm";
12258 return NULL;
12259}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012260
12261/*
12262 * Function given to ExpandGeneric() to obtain the possible arguments of the
12263 * ":messages {clear}" command.
12264 */
12265 char_u *
12266get_messages_arg(expand_T *xp UNUSED, int idx)
12267{
12268 if (idx == 0)
12269 return (char_u *)"clear";
12270 return NULL;
12271}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012272#endif
12273
Bram Moolenaar113e1072019-01-20 15:30:40 +010012274#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012275 char_u *
12276get_mapclear_arg(expand_T *xp UNUSED, int idx)
12277{
12278 if (idx == 0)
12279 return (char_u *)"<buffer>";
12280 return NULL;
12281}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012282#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012283
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284static int filetype_detect = FALSE;
12285static int filetype_plugin = FALSE;
12286static int filetype_indent = FALSE;
12287
12288/*
12289 * ":filetype [plugin] [indent] {on,off,detect}"
12290 * on: Load the filetype.vim file to install autocommands for file types.
12291 * off: Load the ftoff.vim file to remove all autocommands for file types.
12292 * plugin on: load filetype.vim and ftplugin.vim
12293 * plugin off: load ftplugof.vim
12294 * indent on: load filetype.vim and indent.vim
12295 * indent off: load indoff.vim
12296 */
12297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012298ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
12300 char_u *arg = eap->arg;
12301 int plugin = FALSE;
12302 int indent = FALSE;
12303
12304 if (*eap->arg == NUL)
12305 {
12306 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012307 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 filetype_detect ? "ON" : "OFF",
12309 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12310 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12311 return;
12312 }
12313
12314 /* Accept "plugin" and "indent" in any order. */
12315 for (;;)
12316 {
12317 if (STRNCMP(arg, "plugin", 6) == 0)
12318 {
12319 plugin = TRUE;
12320 arg = skipwhite(arg + 6);
12321 continue;
12322 }
12323 if (STRNCMP(arg, "indent", 6) == 0)
12324 {
12325 indent = TRUE;
12326 arg = skipwhite(arg + 6);
12327 continue;
12328 }
12329 break;
12330 }
12331 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12332 {
12333 if (*arg == 'o' || !filetype_detect)
12334 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012335 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 filetype_detect = TRUE;
12337 if (plugin)
12338 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012339 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 filetype_plugin = TRUE;
12341 }
12342 if (indent)
12343 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012344 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 filetype_indent = TRUE;
12346 }
12347 }
12348 if (*arg == 'd')
12349 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012350 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012351 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 }
12353 }
12354 else if (STRCMP(arg, "off") == 0)
12355 {
12356 if (plugin || indent)
12357 {
12358 if (plugin)
12359 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012360 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 filetype_plugin = FALSE;
12362 }
12363 if (indent)
12364 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012365 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 filetype_indent = FALSE;
12367 }
12368 }
12369 else
12370 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012371 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 filetype_detect = FALSE;
12373 }
12374 }
12375 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012376 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377}
12378
12379/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012380 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 */
12382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012383ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384{
12385 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012386 {
12387 char_u *arg = eap->arg;
12388
12389 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12390 arg += 9;
12391
12392 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12393 if (arg != eap->arg)
12394 did_filetype = FALSE;
12395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012399ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
12401#ifdef FEAT_DIGRAPHS
12402 if (*eap->arg != NUL)
12403 putdigraph(eap->arg);
12404 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012405 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012407 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#endif
12409}
12410
12411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012412ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413{
12414 int flags = 0;
12415
12416 if (eap->cmdidx == CMD_setlocal)
12417 flags = OPT_LOCAL;
12418 else if (eap->cmdidx == CMD_setglobal)
12419 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012420#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 if (cmdmod.browse && flags == 0)
12422 ex_options(eap);
12423 else
12424#endif
12425 (void)do_set(eap->arg, flags);
12426}
12427
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012428#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12429 void
12430set_no_hlsearch(int flag)
12431{
12432 no_hlsearch = flag;
12433# ifdef FEAT_EVAL
12434 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12435# endif
12436}
12437
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438/*
12439 * ":nohlsearch"
12440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012442ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012444 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012445 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446}
12447
12448/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012449 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 * Sets nextcmd to the start of the next command, if any. Also called when
12451 * skipping commands to find the next command.
12452 */
12453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012454ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455{
12456 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012457 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 char_u *end;
12459 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012460 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012461
12462 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012463 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012464 else
12465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012466 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012467 return;
12468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469
12470 /* First clear any old pattern. */
12471 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012472 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473
12474 if (ends_excmd(*eap->arg))
12475 end = eap->arg;
12476 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012477 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 end = eap->arg + 4;
12479 else
12480 {
12481 p = skiptowhite(eap->arg);
12482 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012483 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 p = skipwhite(p);
12485 if (*p == NUL)
12486 {
12487 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012488 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012489 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 return;
12491 }
12492 end = skip_regexp(p + 1, *p, TRUE, NULL);
12493 if (!eap->skip)
12494 {
12495 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12496 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012497 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 eap->errmsg = e_trailing;
12499 return;
12500 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012501 if (*end != *p)
12502 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012503 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012504 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012505 return;
12506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507
12508 c = *end;
12509 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012510 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012511 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012512 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 }
12514 }
12515 eap->nextcmd = find_nextcmd(end);
12516}
12517#endif
12518
12519#ifdef FEAT_CRYPT
12520/*
12521 * ":X": Get crypt key
12522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012524ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012526 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012527 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528}
12529#endif
12530
12531#ifdef FEAT_FOLDING
12532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012533ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534{
12535 if (foldManualAllowed(TRUE))
12536 foldCreate(eap->line1, eap->line2);
12537}
12538
12539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012540ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541{
12542 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12543 eap->forceit, FALSE);
12544}
12545
12546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012547ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548{
12549 linenr_T lnum;
12550
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012551#ifdef FEAT_CLIPBOARD
12552 start_global_changes();
12553#endif
12554
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 /* First set the marks for all lines closed/open. */
12556 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12557 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12558 ml_setmarked(lnum);
12559
12560 /* Execute the command on the marked lines. */
12561 global_exe(eap->arg);
12562 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012563#ifdef FEAT_CLIPBOARD
12564 end_global_changes();
12565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566}
12567#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012568
Bram Moolenaar39665952018-08-15 20:59:48 +020012569#ifdef FEAT_QUICKFIX
12570/*
12571 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12572 * instead of a quickfix command.
12573 */
12574 int
12575is_loclist_cmd(int cmdidx)
12576{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012577 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012578 return FALSE;
12579 return cmdnames[cmdidx].cmd_name[0] == 'l';
12580}
12581#endif
12582
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012583# if defined(FEAT_TIMERS) || defined(PROTO)
12584 int
12585get_pressedreturn(void)
12586{
12587 return ex_pressedreturn;
12588}
12589
12590 void
12591set_pressedreturn(int val)
12592{
12593 ex_pressedreturn = val;
12594}
12595#endif