blob: 1646a22139bbd8b1986b62d44f95cc163c8afc49 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020032 sctx_T uc_script_ctx; /* SCTX where the command was defined */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010033# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
64#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010065static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010067static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static int if_level = 0; /* depth in :if */
69#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020070static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void ex_autocmd(exarg_T *eap);
85static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_bunload(exarg_T *eap);
87static void ex_buffer(exarg_T *eap);
88static void ex_bmodified(exarg_T *eap);
89static void ex_bnext(exarg_T *eap);
90static void ex_bprevious(exarg_T *eap);
91static void ex_brewind(exarg_T *eap);
92static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *getargcmd(char_u **);
94static char_u *skip_cmd_arg(char_u *p, int rembs);
95static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifndef FEAT_QUICKFIX
97# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000098# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# define ex_cc ex_ni
100# define ex_cnext ex_ni
101# define ex_cfile ex_ni
102# define qf_list ex_ni
103# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200104# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000106# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109# define ex_cclose ex_ni
110# define ex_copen ex_ni
111# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200112# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000114#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
115# define ex_cexpr ex_ni
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200118static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200120#if !defined(FEAT_PERL) \
121 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
122 || !defined(FEAT_TCL) \
123 || !defined(FEAT_RUBY) \
124 || !defined(FEAT_LUA) \
125 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000126# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100129static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000131#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000133#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100134static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
135static void ex_highlight(exarg_T *eap);
136static void ex_colorscheme(exarg_T *eap);
137static void ex_quit(exarg_T *eap);
138static void ex_cquit(exarg_T *eap);
139static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_close(exarg_T *eap);
141static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
142static void ex_only(exarg_T *eap);
143static void ex_resize(exarg_T *eap);
144static void ex_stag(exarg_T *eap);
145static void ex_tabclose(exarg_T *eap);
146static void ex_tabonly(exarg_T *eap);
147static void ex_tabnext(exarg_T *eap);
148static void ex_tabmove(exarg_T *eap);
149static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200150#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static void ex_pclose(exarg_T *eap);
152static void ex_ptag(exarg_T *eap);
153static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#else
155# define ex_pclose ex_ni
156# define ex_ptag ex_ni
157# define ex_pedit ex_ni
158#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ex_hide(exarg_T *eap);
160static void ex_stop(exarg_T *eap);
161static void ex_exit(exarg_T *eap);
162static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100164static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#else
166# define ex_goto ex_ni
167#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100168static void ex_shell(exarg_T *eap);
169static void ex_preserve(exarg_T *eap);
170static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100171static void ex_mode(exarg_T *eap);
172static void ex_wrongmodifier(exarg_T *eap);
173static void ex_find(exarg_T *eap);
174static void ex_open(exarg_T *eap);
175static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#ifndef FEAT_GUI
177# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100178static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
180#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
183# define ex_tearoff ex_ni
184#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100185#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
186 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
189# define ex_popup ex_ni
190#endif
191#ifndef FEAT_GUI_MSWIN
192# define ex_simalt ex_ni
193#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000194#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# define gui_mch_find_dialog ex_ni
196# define gui_mch_replace_dialog ex_ni
197#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000198#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# define ex_helpfind ex_ni
200#endif
201#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100202# define ex_cscope ex_ni
203# define ex_scscope ex_ni
204# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
206#ifndef FEAT_SYN_HL
207# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200208# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000209#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100210#ifndef FEAT_EVAL
211# define ex_packadd ex_ni
212# define ex_packloadall ex_ni
213#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200214#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200215# define ex_syntime ex_ni
216#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000217#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000218# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000219# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000220# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000221# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000222# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000223#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200224#ifndef FEAT_PERSISTENT_UNDO
225# define ex_rundo ex_ni
226# define ex_wundo ex_ni
227#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200228#ifndef FEAT_LUA
229# define ex_lua ex_script_ni
230# define ex_luado ex_ni
231# define ex_luafile ex_ni
232#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000233#ifndef FEAT_MZSCHEME
234# define ex_mzscheme ex_script_ni
235# define ex_mzfile ex_ni
236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifndef FEAT_PERL
238# define ex_perl ex_script_ni
239# define ex_perldo ex_ni
240#endif
241#ifndef FEAT_PYTHON
242# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200243# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# define ex_pyfile ex_ni
245#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200246#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200247# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200248# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200249# define ex_py3file ex_ni
250#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100251#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
252# define ex_pyx ex_script_ni
253# define ex_pyxdo ex_ni
254# define ex_pyxfile ex_ni
255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifndef FEAT_TCL
257# define ex_tcl ex_script_ni
258# define ex_tcldo ex_ni
259# define ex_tclfile ex_ni
260#endif
261#ifndef FEAT_RUBY
262# define ex_ruby ex_script_ni
263# define ex_rubydo ex_ni
264# define ex_rubyfile ex_ni
265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifndef FEAT_KEYMAP
267# define ex_loadkeymap ex_ni
268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static void ex_swapname(exarg_T *eap);
270static void ex_syncbind(exarg_T *eap);
271static void ex_read(exarg_T *eap);
272static void ex_pwd(exarg_T *eap);
273static void ex_equal(exarg_T *eap);
274static void ex_sleep(exarg_T *eap);
275static void do_exmap(exarg_T *eap, int isabbrev);
276static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000278#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100279static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280#else
281# define ex_winpos ex_ni
282#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100283static void ex_operators(exarg_T *eap);
284static void ex_put(exarg_T *eap);
285static void ex_copymove(exarg_T *eap);
286static void ex_submagic(exarg_T *eap);
287static void ex_join(exarg_T *eap);
288static void ex_at(exarg_T *eap);
289static void ex_bang(exarg_T *eap);
290static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200291#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100292static void ex_wundo(exarg_T *eap);
293static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200294#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100295static void ex_redo(exarg_T *eap);
296static void ex_later(exarg_T *eap);
297static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100298static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100299static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100300static void close_redir(void);
301static void ex_mkrc(exarg_T *eap);
302static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#ifdef FEAT_USR_CMDS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100304static char *uc_fun_cmd(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static void ex_startinsert(exarg_T *eap);
308static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100310static void ex_checkpath(exarg_T *eap);
311static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#else
313# define ex_findpat ex_ni
314# define ex_checkpath ex_ni
315#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200316#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#else
319# define ex_psearch ex_ni
320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_tag(exarg_T *eap);
322static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifndef FEAT_EVAL
324# define ex_scriptnames ex_ni
325# define ex_finish ex_ni
326# define ex_echo ex_ni
327# define ex_echohl ex_ni
328# define ex_execute ex_ni
329# define ex_call ex_ni
330# define ex_if ex_ni
331# define ex_endif ex_ni
332# define ex_else ex_ni
333# define ex_while ex_ni
334# define ex_continue ex_ni
335# define ex_break ex_ni
336# define ex_endwhile ex_ni
337# define ex_throw ex_ni
338# define ex_try ex_ni
339# define ex_catch ex_ni
340# define ex_finally ex_ni
341# define ex_endtry ex_ni
342# define ex_endfunction ex_ni
343# define ex_let ex_ni
344# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000345# define ex_lockvar ex_ni
346# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_function ex_ni
348# define ex_delfunction ex_ni
349# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000350# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100352static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100354static int makeopens(FILE *fd, char_u *dirnow);
355static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
356static void ex_loadview(exarg_T *eap);
357static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000358static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#else
360# define ex_loadview ex_ni
361#endif
362#ifndef FEAT_EVAL
363# define ex_compiler ex_ni
364#endif
365#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100366static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#else
368# define ex_viminfo ex_ni
369#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100370static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100371static void ex_filetype(exarg_T *eap);
372static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000374# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375# define ex_diffpatch ex_ni
376# define ex_diffgetput ex_ni
377# define ex_diffsplit ex_ni
378# define ex_diffthis ex_ni
379# define ex_diffupdate ex_ni
380#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100381static void ex_digraphs(exarg_T *eap);
382static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100383#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# define ex_options ex_ni
385#endif
386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100387static void ex_nohlsearch(exarg_T *eap);
388static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#else
390# define ex_nohlsearch ex_ni
391# define ex_match ex_ni
392#endif
393#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100394static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#else
396# define ex_X ex_ni
397#endif
398#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100399static void ex_fold(exarg_T *eap);
400static void ex_foldopen(exarg_T *eap);
401static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_fold ex_ni
404# define ex_foldopen ex_ni
405# define ex_folddo ex_ni
406#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100407#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# define ex_language ex_ni
409#endif
410#ifndef FEAT_SIGNS
411# define ex_sign ex_ni
412#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000413#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200414# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000415# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200416# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419#ifndef FEAT_EVAL
420# define ex_debug ex_ni
421# define ex_breakadd ex_ni
422# define ex_debuggreedy ex_ni
423# define ex_breakdel ex_ni
424# define ex_breaklist ex_ni
425#endif
426
427#ifndef FEAT_CMDHIST
428# define ex_history ex_ni
429#endif
430#ifndef FEAT_JUMPLIST
431# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200432# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433# define ex_changes ex_ni
434#endif
435
Bram Moolenaar05159a02005-02-26 23:04:13 +0000436#ifndef FEAT_PROFILE
437# define ex_profile ex_ni
438#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200439#ifndef FEAT_TERMINAL
440# define ex_terminal ex_ni
441#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443/*
444 * Declare cmdnames[].
445 */
446#define DO_DECLARE_EXCMD
447#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200448#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100449
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450static char_u dollar_command[2] = {'$', 0};
451
452
453#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000454/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455typedef struct
456{
457 char_u *line; /* command line */
458 linenr_T lnum; /* sourcing_lnum of the line */
459} wcmd_T;
460
461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000462 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000464 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000466struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 garray_T *lines_gap; /* growarray with line info */
469 int current_line; /* last read line from growarray */
470 int repeating; /* TRUE when looping a second time */
471 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100472 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 void *cookie;
474};
475
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100476static char_u *get_loop_line(int c, void *cookie, int indent);
477static int store_loop_line(garray_T *gap, char_u *line);
478static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000479
480/* Struct to save a few things while debugging. Used in do_cmdline() only. */
481struct dbg_stuff
482{
483 int trylevel;
484 int force_abort;
485 except_T *caught_stack;
486 char_u *vv_exception;
487 char_u *vv_throwpoint;
488 int did_emsg;
489 int got_int;
490 int did_throw;
491 int need_rethrow;
492 int check_cstack;
493 except_T *current_exception;
494};
495
Bram Moolenaared203462004-06-16 11:19:22 +0000496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100497save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000498{
499 dsp->trylevel = trylevel; trylevel = 0;
500 dsp->force_abort = force_abort; force_abort = FALSE;
501 dsp->caught_stack = caught_stack; caught_stack = NULL;
502 dsp->vv_exception = v_exception(NULL);
503 dsp->vv_throwpoint = v_throwpoint(NULL);
504
505 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
506 dsp->did_emsg = did_emsg; did_emsg = FALSE;
507 dsp->got_int = got_int; got_int = FALSE;
508 dsp->did_throw = did_throw; did_throw = FALSE;
509 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
510 dsp->check_cstack = check_cstack; check_cstack = FALSE;
511 dsp->current_exception = current_exception; current_exception = NULL;
512}
513
514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100515restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000516{
517 suppress_errthrow = FALSE;
518 trylevel = dsp->trylevel;
519 force_abort = dsp->force_abort;
520 caught_stack = dsp->caught_stack;
521 (void)v_exception(dsp->vv_exception);
522 (void)v_throwpoint(dsp->vv_throwpoint);
523 did_emsg = dsp->did_emsg;
524 got_int = dsp->got_int;
525 did_throw = dsp->did_throw;
526 need_rethrow = dsp->need_rethrow;
527 check_cstack = dsp->check_cstack;
528 current_exception = dsp->current_exception;
529}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
533 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
534 * command is given.
535 */
536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100537do_exmode(
538 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 int save_msg_scroll;
541 int prev_msg_row;
542 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100543 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544
545 if (improved)
546 exmode_active = EXMODE_VIM;
547 else
548 exmode_active = EXMODE_NORMAL;
549 State = NORMAL;
550
551 /* When using ":global /pat/ visual" and then "Q" we return to continue
552 * the :global command. */
553 if (global_busy)
554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 save_msg_scroll = msg_scroll;
557 ++RedrawingDisabled; /* don't redisplay the window */
558 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_GUI
560 /* Ignore scrollbar and mouse events in Ex mode */
561 ++hold_gui_events;
562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar32526b32019-01-19 17:43:09 +0100564 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 while (exmode_active)
566 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000567 /* Check for a ":normal" command and no more characters left. */
568 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
569 {
570 exmode_active = FALSE;
571 break;
572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 msg_scroll = TRUE;
574 need_wait_return = FALSE;
575 ex_pressedreturn = FALSE;
576 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100577 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 prev_msg_row = msg_row;
579 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (improved)
581 {
582 cmdline_row = msg_row;
583 do_cmdline(NULL, getexline, NULL, 0);
584 }
585 else
586 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
587 lines_left = Rows - 1;
588
Bram Moolenaardf177f62005-02-22 08:39:57 +0000589 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100590 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000592 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100593 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000596 if (ex_pressedreturn)
597 {
598 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000599 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000600 msg_row = prev_msg_row;
601 if (prev_msg_row == Rows - 1)
602 msg_row--;
603 }
604 msg_col = 0;
605 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
606 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000609 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
610 {
611 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100612 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100614 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
617
618#ifdef FEAT_GUI
619 --hold_gui_events;
620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 --RedrawingDisabled;
622 --no_wait_return;
623 update_screen(CLEAR);
624 need_wait_return = FALSE;
625 msg_scroll = save_msg_scroll;
626}
627
628/*
629 * Execute a simple command line. Used for translated commands like "*".
630 */
631 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100632do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 return do_cmdline(cmd, NULL, NULL,
635 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
636}
637
638/*
639 * do_cmdline(): execute one Ex command line
640 *
641 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100642 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 * 2. Split up in parts separated with '|'.
644 *
645 * This function can be called recursively!
646 *
647 * flags:
648 * DOCMD_VERBOSE - The command will be included in the error message.
649 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100650 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 * DOCMD_KEYTYPED - Don't reset KeyTyped.
652 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
653 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
654 *
655 * return FAIL if cmdline could not be executed, OK otherwise
656 */
657 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100658do_cmdline(
659 char_u *cmdline,
660 char_u *(*fgetline)(int, void *, int),
661 void *cookie, /* argument for fgetline() */
662 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 char_u *next_cmdline; /* next cmd to execute */
665 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100666 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 static int recursive = 0; /* recursive depth */
668 int msg_didout_before_start = 0;
669 int count = 0; /* line number count */
670 int did_inc = FALSE; /* incremented RedrawingDisabled */
671 int retval = OK;
672#ifdef FEAT_EVAL
673 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000674 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int current_line = 0; /* active line in lines_ga */
676 char_u *fname = NULL; /* function or script name */
677 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
678 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000679 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int initial_trylevel;
681 struct msglist **saved_msg_list = NULL;
682 struct msglist *private_msg_list;
683
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100684 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100685 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# define cmd_cookie cookie
693#endif
694 static int call_depth = 0; /* recursiveness */
695
696#ifdef FEAT_EVAL
697 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
698 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000699 * This ensures that the do_errthrow() call in do_one_cmd() does not
700 * combine the messages stored by an earlier invocation of do_one_cmd()
701 * with the command name of the later one. This would happen when
702 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 saved_msg_list = msg_list;
704 msg_list = &private_msg_list;
705 private_msg_list = NULL;
706#endif
707
708 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100709 * here. The value of 200 allows nested function calls, ":source", etc.
710 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100711 if (call_depth >= 200
712#ifdef FEAT_EVAL
713 && call_depth >= p_mfd
714#endif
715 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_EVAL
719 /* When converting to an exception, we do not include the command name
720 * since this is not an error of the specific command. */
721 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
722 msg_list = saved_msg_list;
723#endif
724 return FAIL;
725 }
726 ++call_depth;
727
728#ifdef FEAT_EVAL
729 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000730 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 cstack.cs_trylevel = 0;
732 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000733 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
735
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ++ex_nesting_level;
742
743 /* Get the function or script name and the address where the next breakpoint
744 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000745 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 fname = func_name(real_cookie);
748 breakpoint = func_breakpoint(real_cookie);
749 dbg_tick = func_dbg_tick(real_cookie);
750 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
753 fname = sourcing_name;
754 breakpoint = source_breakpoint(real_cookie);
755 dbg_tick = source_dbg_tick(real_cookie);
756 }
757
758 /*
759 * Initialize "force_abort" and "suppress_errthrow" at the top level.
760 */
761 if (!recursive)
762 {
763 force_abort = FALSE;
764 suppress_errthrow = FALSE;
765 }
766
767 /*
768 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000769 * exception handling (used when debugging). Otherwise clear it to avoid
770 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000772 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000773 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000774 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200775 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 initial_trylevel = trylevel;
778
779 /*
780 * "did_throw" will be set to TRUE when an exception is being thrown.
781 */
782 did_throw = FALSE;
783#endif
784 /*
785 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000786 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * If force_abort is set, we cancel everything.
788 */
789 did_emsg = FALSE;
790
791 /*
792 * KeyTyped is only set when calling vgetc(). Reset it here when not
793 * calling vgetc() (sourced command lines).
794 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100795 if (!(flags & DOCMD_KEYTYPED)
796 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 KeyTyped = FALSE;
798
799 /*
800 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000801 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * - for multiple commands on one line, separated with '|'
803 * - when repeating until there are no more lines (for ":source")
804 */
805 next_cmdline = cmdline;
806 do
807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#endif
811
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000812 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if (next_cmdline == NULL
814#ifdef FEAT_EVAL
815 && !force_abort
816 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#endif
819 )
820 did_emsg = FALSE;
821
822 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 * 3. If a line is given: Make a copy, so we can mess with it.
826 */
827
828#ifdef FEAT_EVAL
829 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000830 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {
832 /* Each '|' separated command is stored separately in lines_ga, to
833 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100834 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836 /* Check if a function has returned or, unless it has an unclosed
837 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000841 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 func_line_end(real_cookie);
843# endif
844 if (func_has_ended(real_cookie))
845 {
846 retval = FAIL;
847 break;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000851 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 script_line_end();
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100857 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 retval = FAIL;
860 break;
861 }
862
863 /* If breakpoints have been added/deleted need to check for it. */
864 if (breakpoint != NULL && dbg_tick != NULL
865 && *dbg_tick != debug_tick)
866 {
867 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 fname, sourcing_lnum);
870 *dbg_tick = debug_tick;
871 }
872
873 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
874 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
875
876 /* Did we encounter a breakpoint? */
877 if (breakpoint != NULL && *breakpoint != 0
878 && *breakpoint <= sourcing_lnum)
879 {
880 dbg_breakpoint(fname, sourcing_lnum);
881 /* Find next breakpoint. */
882 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 fname, sourcing_lnum);
885 *dbg_tick = debug_tick;
886 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000888 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000889 {
890 if (getline_is_func)
891 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100892 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 script_line_start();
894 }
895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100901 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 * below, so that it stores lines in or reads them from
903 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 * while/for loop. */
905 cmd_getline = get_loop_line;
906 cmd_cookie = (void *)&cmd_loop_cookie;
907 cmd_loop_cookie.lines_gap = &lines_ga;
908 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000910 cmd_loop_cookie.cookie = cookie;
911 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 else
914 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100915 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 cmd_cookie = cookie;
917 }
918#endif
919
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100920 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (next_cmdline == NULL)
922 {
923 /*
924 * Need to set msg_didout for the first line after an ":if",
925 * otherwise the ":if" will be overwritten.
926 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100927 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#ifdef FEAT_EVAL
931 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
932#else
933 0
934#endif
935 )) == NULL)
936 {
937 /* Don't call wait_return for aborted command line. The NULL
938 * returned for the end of a sourced file or executed function
939 * doesn't do this. */
940 if (KeyTyped && !(flags & DOCMD_REPEAT))
941 need_wait_return = FALSE;
942 retval = FAIL;
943 break;
944 }
945 used_getline = TRUE;
946
947 /*
948 * Keep the first typed line. Clear it when more lines are typed.
949 */
950 if (flags & DOCMD_KEEPLINE)
951 {
952 vim_free(repeat_cmdline);
953 if (count == 0)
954 repeat_cmdline = vim_strsave(next_cmdline);
955 else
956 repeat_cmdline = NULL;
957 }
958 }
959
960 /* 3. Make a copy of the command so we can mess with it. */
961 else if (cmdline_copy == NULL)
962 {
963 next_cmdline = vim_strsave(next_cmdline);
964 if (next_cmdline == NULL)
965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100966 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 retval = FAIL;
968 break;
969 }
970 }
971 cmdline_copy = next_cmdline;
972
973#ifdef FEAT_EVAL
974 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 * Save the current line when inside a ":while" or ":for", and when
976 * the command looks like a ":while" or ":for", because we may need it
977 * later. When there is a '|' and another command, it is stored
978 * separately, because we need to be able to jump back to it from an
979 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 if (current_line == lines_ga.ga_len
982 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 retval = FAIL;
987 break;
988 }
989 }
990 did_endif = FALSE;
991#endif
992
993 if (count++ == 0)
994 {
995 /*
996 * All output from the commands is put below each other, without
997 * waiting for a return. Don't do this when executing commands
998 * from a script or when being called recursive (e.g. for ":e
999 * +command file").
1000 */
1001 if (!(flags & DOCMD_NOWAIT) && !recursive)
1002 {
1003 msg_didout_before_start = msg_didout;
1004 msg_didany = FALSE; /* no output yet */
1005 msg_start();
1006 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001007 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 ++RedrawingDisabled;
1009 did_inc = TRUE;
1010 }
1011 }
1012
1013 if (p_verbose >= 15 && sourcing_name != NULL)
1014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001016 verbose_enter_scroll();
1017
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001020 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001021 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001022
1023 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 --no_wait_return;
1025 }
1026
1027 /*
1028 * 2. Execute one '|' separated command.
1029 * do_one_cmd() will return NULL if there is no trailing '|'.
1030 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1031 */
1032 ++recursive;
1033 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1034#ifdef FEAT_EVAL
1035 &cstack,
1036#endif
1037 cmd_getline, cmd_cookie);
1038 --recursive;
1039
1040#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cmd_cookie == (void *)&cmd_loop_cookie)
1042 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045#endif
1046
1047 if (next_cmdline == NULL)
1048 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001049 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#ifdef FEAT_CMDHIST
1051 /*
1052 * If the command was typed, remember it for the ':' register.
1053 * Do this AFTER executing the command to make :@: work.
1054 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001055 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && new_last_cmdline != NULL)
1057 {
1058 vim_free(last_cmdline);
1059 last_cmdline = new_last_cmdline;
1060 new_last_cmdline = NULL;
1061 }
1062#endif
1063 }
1064 else
1065 {
1066 /* need to copy the command after the '|' to cmdline_copy, for the
1067 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001068 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 next_cmdline = cmdline_copy;
1070 }
1071
1072
1073#ifdef FEAT_EVAL
1074 /* reset did_emsg for a function that is not aborted by an error */
1075 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 && !func_has_abort(real_cookie))
1078 did_emsg = FALSE;
1079
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 ++current_line;
1083
1084 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 * An ":endwhile", ":endfor" and ":continue" is handled here.
1086 * If we were executing commands, jump back to the ":while" or
1087 * ":for".
1088 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 /* Jump back to the matching ":while" or ":for". Be careful
1095 * not to use a cs_line[] from an entry that isn't a ":while"
1096 * or ":for": It would make "current_line" invalid and can
1097 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (!did_emsg && !got_int && !did_throw
1099 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 && (cstack.cs_flags[cstack.cs_idx]
1101 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && cstack.cs_line[cstack.cs_idx] >= 0
1103 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1104 {
1105 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 /* remember we jumped there */
1107 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 line_breakcheck(); /* check if CTRL-C typed */
1109
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 /* Check for the next breakpoint at or after the ":while"
1111 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (breakpoint != NULL)
1113 {
1114 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001115 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 fname,
1117 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1118 *dbg_tick = debug_tick;
1119 }
1120 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001125 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1126 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1137 }
1138 }
1139
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001140 /* Check for the next breakpoint after a watchexpression */
1141 if (breakpoint != NULL && has_watchexpr())
1142 {
1143 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1144 *dbg_tick = debug_tick;
1145 }
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /*
1148 * When not inside any ":while" loop, clear remembered lines.
1149 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 if (lines_ga.ga_len > 0)
1153 {
1154 sourcing_lnum =
1155 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1156 free_cmdlines(&lines_ga);
1157 }
1158 current_line = 0;
1159 }
1160
1161 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1163 * being restored at the ":endtry". Reset them here and set the
1164 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1165 * This includes the case where a missing ":endif", ":endwhile" or
1166 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 cstack.cs_lflags &= ~CSL_HAD_FINA;
1171 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1172 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 did_throw ? (void *)current_exception : NULL);
1174 did_emsg = got_int = did_throw = FALSE;
1175 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1176 }
1177
1178 /* Update global "trylevel" for recursive calls to do_cmdline() from
1179 * within this loop. */
1180 trylevel = initial_trylevel + cstack.cs_trylevel;
1181
1182 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001183 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 * files) is aborted because of an error, an interrupt, or an uncaught
1185 * exception, cancel everything. If it is left normally, reset
1186 * force_abort to get the non-EH compatible abortion behavior for
1187 * the rest of the script.
1188 */
1189 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1190 force_abort = FALSE;
1191
1192 /* Convert an interrupt to an exception if appropriate. */
1193 (void)do_intthrow(&cstack);
1194#endif /* FEAT_EVAL */
1195
1196 }
1197 /*
1198 * Continue executing command lines when:
1199 * - no CTRL-C typed, no aborting error, no exception thrown or try
1200 * conditionals need to be checked for executing finally clauses or
1201 * catching an interrupt exception
1202 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 * looping for ":source" command or function call.
1205 */
1206 while (!((got_int
1207#ifdef FEAT_EVAL
1208 || (did_emsg && force_abort) || did_throw
1209#endif
1210 )
1211#ifdef FEAT_EVAL
1212 && cstack.cs_trylevel == 0
1213#endif
1214 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001215 && !(did_emsg
1216#ifdef FEAT_EVAL
1217 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001218 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001219 * the :endtry to be missed. */
1220 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1221#endif
1222 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001223 && (getline_equal(fgetline, cookie, getexmodeline)
1224 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 && (next_cmdline != NULL
1226#ifdef FEAT_EVAL
1227 || cstack.cs_idx >= 0
1228#endif
1229 || (flags & DOCMD_REPEAT)));
1230
1231 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001232 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#ifdef FEAT_EVAL
1234 free_cmdlines(&lines_ga);
1235 ga_clear(&lines_ga);
1236
1237 if (cstack.cs_idx >= 0)
1238 {
1239 /*
1240 * If a sourced file or executed function ran to its end, report the
1241 * unclosed conditional.
1242 */
1243 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001244 && ((getline_equal(fgetline, cookie, getsourceline)
1245 && !source_finished(fgetline, cookie))
1246 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 && !func_has_ended(real_cookie))))
1248 {
1249 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001256 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
1259 /*
1260 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1261 * ":endtry" in a sourced file or executed function. If the try
1262 * conditional is in its finally clause, ignore anything pending.
1263 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001264 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 */
1266 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001267 {
1268 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1269
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001270 if (idx >= 0)
1271 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001272 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1273 &cstack.cs_looplevel);
1274 }
1275 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 trylevel = initial_trylevel;
1277 }
1278
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001279 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1280 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001282 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 ? (char_u *)"endfunction" : (char_u *)NULL);
1284
1285 if (trylevel == 0)
1286 {
1287 /*
1288 * When an exception is being thrown out of the outermost try
1289 * conditional, discard the uncaught exception, disable the conversion
1290 * of interrupts or errors to exceptions, and ensure that no more
1291 * commands are executed.
1292 */
1293 if (did_throw)
1294 {
1295 void *p = NULL;
1296 char_u *saved_sourcing_name;
1297 int saved_sourcing_lnum;
1298 struct msglist *messages = NULL, *next;
1299
1300 /*
1301 * If the uncaught exception is a user exception, report it as an
1302 * error. If it is an error exception, display the saved error
1303 * message now. For an interrupt exception, do nothing; the
1304 * interrupt message is given elsewhere.
1305 */
1306 switch (current_exception->type)
1307 {
1308 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001309 vim_snprintf((char *)IObuff, IOSIZE,
1310 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 current_exception->value);
1312 p = vim_strsave(IObuff);
1313 break;
1314 case ET_ERROR:
1315 messages = current_exception->messages;
1316 current_exception->messages = NULL;
1317 break;
1318 case ET_INTERRUPT:
1319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321
1322 saved_sourcing_name = sourcing_name;
1323 saved_sourcing_lnum = sourcing_lnum;
1324 sourcing_name = current_exception->throw_name;
1325 sourcing_lnum = current_exception->throw_lnum;
1326 current_exception->throw_name = NULL;
1327
1328 discard_current_exception(); /* uses IObuff if 'verbose' */
1329 suppress_errthrow = TRUE;
1330 force_abort = TRUE;
1331
1332 if (messages != NULL)
1333 {
1334 do
1335 {
1336 next = messages->next;
1337 emsg(messages->msg);
1338 vim_free(messages->msg);
1339 vim_free(messages);
1340 messages = next;
1341 }
1342 while (messages != NULL);
1343 }
1344 else if (p != NULL)
1345 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001346 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 vim_free(p);
1348 }
1349 vim_free(sourcing_name);
1350 sourcing_name = saved_sourcing_name;
1351 sourcing_lnum = saved_sourcing_lnum;
1352 }
1353
1354 /*
1355 * On an interrupt or an aborting error not converted to an exception,
1356 * disable the conversion of errors to exceptions. (Interrupts are not
1357 * converted any more, here.) This enables also the interrupt message
1358 * when force_abort is set and did_emsg unset in case of an interrupt
1359 * from a finally clause after an error.
1360 */
1361 else if (got_int || (did_emsg && force_abort))
1362 suppress_errthrow = TRUE;
1363 }
1364
1365 /*
1366 * The current cstack will be freed when do_cmdline() returns. An uncaught
1367 * exception will have to be rethrown in the previous cstack. If a function
1368 * has just returned or a script file was just finished and the previous
1369 * cstack belongs to the same function or, respectively, script file, it
1370 * will have to be checked for finally clauses to be executed due to the
1371 * ":return" or ":finish". This is done in do_one_cmd().
1372 */
1373 if (did_throw)
1374 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001375 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001377 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 && ex_nesting_level > func_level(real_cookie) + 1))
1379 {
1380 if (!did_throw)
1381 check_cstack = TRUE;
1382 }
1383 else
1384 {
1385 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001386 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 --ex_nesting_level;
1388 /*
1389 * Go to debug mode when returning from a function in which we are
1390 * single-stepping.
1391 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001392 if ((getline_equal(fgetline, cookie, getsourceline)
1393 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001395 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ? (char_u *)_("End of sourced file")
1397 : (char_u *)_("End of function"));
1398 }
1399
1400 /*
1401 * Restore the exception environment (done after returning from the
1402 * debugger).
1403 */
1404 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001405 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
1407 msg_list = saved_msg_list;
1408#endif /* FEAT_EVAL */
1409
1410 /*
1411 * If there was too much output to fit on the command line, ask the user to
1412 * hit return before redrawing the screen. With the ":global" command we do
1413 * this only once after the command is finished.
1414 */
1415 if (did_inc)
1416 {
1417 --RedrawingDisabled;
1418 --no_wait_return;
1419 msg_scroll = FALSE;
1420
1421 /*
1422 * When just finished an ":if"-":else" which was typed, no need to
1423 * wait for hit-return. Also for an error situation.
1424 */
1425 if (retval == FAIL
1426#ifdef FEAT_EVAL
1427 || (did_endif && KeyTyped && !did_emsg)
1428#endif
1429 )
1430 {
1431 need_wait_return = FALSE;
1432 msg_didany = FALSE; /* don't wait when restarting edit */
1433 }
1434 else if (need_wait_return)
1435 {
1436 /*
1437 * The msg_start() above clears msg_didout. The wait_return we do
1438 * here should not overwrite the command that may be shown before
1439 * doing that.
1440 */
1441 msg_didout |= msg_didout_before_start;
1442 wait_return(FALSE);
1443 }
1444 }
1445
Bram Moolenaar2a942252012-11-28 23:03:07 +01001446#ifdef FEAT_EVAL
1447 did_endif = FALSE; /* in case do_cmdline used recursively */
1448#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 /*
1450 * Reset if_level, in case a sourced script file contains more ":if" than
1451 * ":endif" (could be ":if x | foo | endif").
1452 */
1453 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001454#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 --call_depth;
1457 return retval;
1458}
1459
1460#ifdef FEAT_EVAL
1461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001462 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001467 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 wcmd_T *wp;
1469 char_u *line;
1470
1471 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1472 {
1473 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001474 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001476 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (cp->getline == NULL)
1478 line = getcmdline(c, 0L, indent);
1479 else
1480 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001481 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 ++cp->current_line;
1483
1484 return line;
1485 }
1486
1487 KeyTyped = FALSE;
1488 ++cp->current_line;
1489 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1490 sourcing_lnum = wp->lnum;
1491 return vim_strsave(wp->line);
1492}
1493
1494/*
1495 * Store a line in "gap" so that a ":while" loop can execute it again.
1496 */
1497 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 if (ga_grow(gap, 1) == FAIL)
1501 return FAIL;
1502 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1503 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1504 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 return OK;
1506}
1507
1508/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 while (gap->ga_len > 0)
1515 {
1516 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1517 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519}
1520#endif
1521
1522/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1524 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001527getline_equal(
1528 char_u *(*fgetline)(int, void *, int),
1529 void *cookie UNUSED, /* argument for fgetline() */
1530 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001533 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001537 * function that's originally used to obtain the lines. This may be
1538 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540 cp = (struct loop_cookie *)cookie;
1541 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 gp = cp->getline;
1544 cp = cp->cookie;
1545 }
1546 return gp == func;
1547#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001548 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
1550}
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001553 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 * getline function. Otherwise return "cookie".
1555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001557getline_cookie(
1558 char_u *(*fgetline)(int, void *, int) UNUSED,
1559 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Bram Moolenaar13505972019-01-24 15:04:48 +01001561#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001562 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaar89d40322006-08-29 15:30:07 +00001565 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001566 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001568 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001569 cp = (struct loop_cookie *)cookie;
1570 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572 gp = cp->getline;
1573 cp = cp->cookie;
1574 }
1575 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001576#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001579}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581
1582/*
1583 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1584 * ":bwipeout", etc.
1585 * Returns the buffer number.
1586 */
1587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001589{
1590 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001592 int count = offset;
1593
1594 buf = firstbuf;
1595 while (buf->b_next != NULL && buf->b_fnum < lnum)
1596 buf = buf->b_next;
1597 while (count != 0)
1598 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001599 count += (offset < 0) ? 1 : -1;
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001603 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001604 if (addr_type == ADDR_LOADED_BUFFERS)
1605 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001606 while (buf->b_ml.ml_mfp == NULL)
1607 {
1608 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1609 if (nextbuf == NULL)
1610 break;
1611 buf = nextbuf;
1612 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001614 /* we might have gone too far, last buffer is not loadedd */
1615 if (addr_type == ADDR_LOADED_BUFFERS)
1616 while (buf->b_ml.ml_mfp == NULL)
1617 {
1618 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
1620 break;
1621 buf = nextbuf;
1622 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 return buf->b_fnum;
1624}
1625
Bram Moolenaarf240e182014-11-27 18:33:02 +01001626 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001627current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001628{
1629 win_T *wp;
1630 int nr = 0;
1631
Bram Moolenaar29323592016-07-24 22:04:11 +02001632 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001633 {
1634 ++nr;
1635 if (wp == win)
1636 break;
1637 }
1638 return nr;
1639}
1640
1641 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001643{
1644 tabpage_T *tp;
1645 int nr = 0;
1646
Bram Moolenaar29323592016-07-24 22:04:11 +02001647 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648 {
1649 ++nr;
1650 if (tp == tab)
1651 break;
1652 }
1653 return nr;
1654}
1655
1656# define CURRENT_WIN_NR current_win_nr(curwin)
1657# define LAST_WIN_NR current_win_nr(NULL)
1658# define CURRENT_TAB_NR current_tab_nr(curtab)
1659# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
1662 * Execute one Ex command.
1663 *
1664 * If 'sourcing' is TRUE, the command will be included in the error message.
1665 *
1666 * 1. skip comment lines and leading space
1667 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001668 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001669 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001670 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001671 * 6. parse arguments
1672 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001674 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 *
1676 * This function may be called recursively!
1677 */
1678#if (_MSC_VER == 1200)
1679/*
Bram Moolenaared203462004-06-16 11:19:22 +00001680 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001682 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683#endif
1684 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001685do_one_cmd(
1686 char_u **cmdlinep,
1687 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001689 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001691 char_u *(*fgetline)(int, void *, int),
1692 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 char_u *p;
1695 linenr_T lnum;
1696 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001697 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001698 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001700 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 cmdmod_T save_cmdmod;
1702 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001703 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
1705 vim_memset(&ea, 0, sizeof(ea));
1706 ea.line1 = 1;
1707 ea.line2 = 1;
1708#ifdef FEAT_EVAL
1709 ++ex_nesting_level;
1710#endif
1711
Bram Moolenaar21691f82012-12-05 19:13:18 +01001712 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (quitmore
1714#ifdef FEAT_EVAL
1715 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001717#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001718 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001719 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 --quitmore;
1721
1722 /*
1723 * Reset browse, confirm, etc.. They are restored when returning, for
1724 * recursive calls.
1725 */
1726 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001728 /* "#!anything" is handled like a comment. */
1729 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1730 goto doend;
1731
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001732/*
1733 * 1. Skip comment lines and leading white space and colons.
1734 * 2. Handle command modifiers.
1735 */
1736 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001738 ea.cmdlinep = cmdlinep;
1739 ea.getline = fgetline;
1740 ea.cookie = cookie;
1741#ifdef FEAT_EVAL
1742 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001744 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001745 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001747 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749#ifdef FEAT_EVAL
1750 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1751 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1752#else
1753 ea.skip = (if_level > 0);
1754#endif
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001757 * 3. Skip over the range to find the command. Let "p" point to after it.
1758 *
1759 * We need the command to know what kind of range it uses.
1760 */
1761 cmd = ea.cmd;
1762 ea.cmd = skip_range(ea.cmd, NULL);
1763 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1764 ea.cmd = skipwhite(ea.cmd + 1);
1765 p = find_command(&ea, NULL);
1766
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001767#ifdef FEAT_EVAL
1768# ifdef FEAT_PROFILE
1769 // Count this line for profiling if skip is TRUE.
1770 if (do_profiling == PROF_YES
1771 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1772 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1773 {
1774 int skip = did_emsg || got_int || did_throw;
1775
1776 if (ea.cmdidx == CMD_catch)
1777 skip = !skip && !(cstack->cs_idx >= 0
1778 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1779 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1780 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1781 skip = skip || !(cstack->cs_idx >= 0
1782 && !(cstack->cs_flags[cstack->cs_idx]
1783 & (CSF_ACTIVE | CSF_TRUE)));
1784 else if (ea.cmdidx == CMD_finally)
1785 skip = FALSE;
1786 else if (ea.cmdidx != CMD_endif
1787 && ea.cmdidx != CMD_endfor
1788 && ea.cmdidx != CMD_endtry
1789 && ea.cmdidx != CMD_endwhile)
1790 skip = ea.skip;
1791
1792 if (!skip)
1793 {
1794 if (getline_equal(fgetline, cookie, get_func_line))
1795 func_line_exec(getline_cookie(fgetline, cookie));
1796 else if (getline_equal(fgetline, cookie, getsourceline))
1797 script_line_exec();
1798 }
1799 }
1800# endif
1801
1802 /* May go to debug mode. If this happens and the ">quit" debug command is
1803 * used, throw an interrupt exception and skip the next command. */
1804 dbg_check_breakpoint(&ea);
1805 if (!ea.skip && got_int)
1806 {
1807 ea.skip = TRUE;
1808 (void)do_intthrow(cstack);
1809 }
1810#endif
1811
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001812/*
1813 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 *
1815 * where 'addr' is:
1816 *
1817 * % (entire file)
1818 * $ [+-NUM]
1819 * 'x [+-NUM] (where x denotes a currently defined mark)
1820 * . [+-NUM]
1821 * [+-NUM]..
1822 * NUM
1823 *
1824 * The ea.cmd pointer is updated to point to the first character following the
1825 * range spec. If an initial address is found, but no second, the upper bound
1826 * is equal to the lower.
1827 */
1828
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001829 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001830 if (!IS_USER_CMDIDX(ea.cmdidx))
1831 {
1832 if (ea.cmdidx != CMD_SIZE)
1833 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1834 else
1835 ea.addr_type = ADDR_LINES;
1836
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001837 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001838 if (ea.cmdidx == CMD_wincmd && p != NULL)
1839 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001840 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001841
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001842 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001843 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001844 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001847 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849
1850 /*
1851 * Skip ':' and any white space
1852 */
1853 ea.cmd = skipwhite(ea.cmd);
1854 while (*ea.cmd == ':')
1855 ea.cmd = skipwhite(ea.cmd + 1);
1856
1857 /*
1858 * If we got a line, but no command, then go to the line.
1859 * If we find a '|' or '\n' we set ea.nextcmd.
1860 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001861 if (*ea.cmd == NUL || *ea.cmd == '"'
1862 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 /*
1865 * strange vi behaviour:
1866 * ":3" jumps to line 3
1867 * ":3|..." prints line 3
1868 * ":|" prints current line
1869 */
1870 if (ea.skip) /* skip this if inside :if */
1871 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001872 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
1874 ea.cmdidx = CMD_print;
1875 ea.argt = RANGE+COUNT+TRLBAR;
1876 if ((errormsg = invalid_range(&ea)) == NULL)
1877 {
1878 correct_range(&ea);
1879 ex_print(&ea);
1880 }
1881 }
1882 else if (ea.addr_count != 0)
1883 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001884 if (ea.line2 > curbuf->b_ml.ml_line_count)
1885 {
1886 /* With '-' in 'cpoptions' a line number past the file is an
1887 * error, otherwise put it at the end of the file. */
1888 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1889 ea.line2 = -1;
1890 else
1891 ea.line2 = curbuf->b_ml.ml_line_count;
1892 }
1893
1894 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001895 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
1897 {
1898 if (ea.line2 == 0)
1899 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 else
1901 curwin->w_cursor.lnum = ea.line2;
1902 beginline(BL_SOL | BL_FIX);
1903 }
1904 }
1905 goto doend;
1906 }
1907
Bram Moolenaard5005162014-08-22 23:05:54 +02001908 /* If this looks like an undefined user command and there are CmdUndefined
1909 * autocommands defined, trigger the matching autocommands. */
1910 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1911 && ASCII_ISUPPER(*ea.cmd)
1912 && has_cmdundefined())
1913 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001914 int ret;
1915
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001916 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001917 while (ASCII_ISALNUM(*p))
1918 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001919 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001920 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1921 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001922 /* If the autocommands did something and didn't cause an error, try
1923 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001924 p = (ret
1925#ifdef FEAT_EVAL
1926 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001927#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001928 ) ? find_command(&ea, NULL) : ea.cmd;
1929 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931#ifdef FEAT_USR_CMDS
1932 if (p == NULL)
1933 {
1934 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001935 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 goto doend;
1937 }
1938 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001939 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1940 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 errormsg = uc_fun_cmd();
1943 goto doend;
1944 }
1945#endif
1946 if (ea.cmdidx == CMD_SIZE)
1947 {
1948 if (!ea.skip)
1949 {
1950 STRCPY(IObuff, _("E492: Not an editor command"));
1951 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001952 {
1953 /* If the modifier was parsed OK the error must be in the
1954 * following command */
1955 if (after_modifier != NULL)
1956 append_command(after_modifier);
1957 else
1958 append_command(*cmdlinep);
1959 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001960 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001961 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 goto doend;
1964 }
1965
Bram Moolenaar958636c2014-10-21 20:01:58 +02001966 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1967 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001968#ifdef HAVE_EX_SCRIPT_NI
1969 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1970#endif
1971 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973#ifndef FEAT_EVAL
1974 /*
1975 * When the expression evaluation is disabled, recognize the ":if" and
1976 * ":endif" commands and ignore everything in between it.
1977 */
1978 if (ea.cmdidx == CMD_if)
1979 ++if_level;
1980 if (if_level)
1981 {
1982 if (ea.cmdidx == CMD_endif)
1983 --if_level;
1984 goto doend;
1985 }
1986
1987#endif
1988
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001989 /* forced commands */
1990 if (*p == '!' && ea.cmdidx != CMD_substitute
1991 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 ++p;
1994 ea.forceit = TRUE;
1995 }
1996 else
1997 ea.forceit = FALSE;
1998
1999/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002000 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002002 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002003 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 if (!ea.skip)
2006 {
2007#ifdef HAVE_SANDBOX
2008 if (sandbox != 0 && !(ea.argt & SBOXOK))
2009 {
2010 /* Command not allowed in sandbox. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
2014#endif
2015 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2016 {
2017 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002018 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 goto doend;
2020 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002021
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002022 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002023 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002025 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 goto doend;
2028 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002029
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002030 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002031 * Do allow ":checktime" (it is postponed).
2032 * Do allow ":edit" (check for an argument later).
2033 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002034 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002035 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002036 && ea.cmdidx != CMD_edit
2037 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002038 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039 && curbuf_locked())
2040 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2043 {
2044 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 goto doend;
2047 }
2048 }
2049
2050 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 goto doend;
2054 }
2055
2056 /*
2057 * Don't complain about the range if it is not used
2058 * (could happen if line_count is accidentally set to 0).
2059 */
2060 if (!ea.skip && !ni)
2061 {
2062 /*
2063 * If the range is backwards, ask for confirmation and, if given, swap
2064 * ea.line1 & ea.line2 so it's forwards again.
2065 * When global command is busy, don't ask, will fail below.
2066 */
2067 if (!global_busy && ea.line1 > ea.line2)
2068 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002069 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002071 if (sourcing || exmode_active)
2072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002074 goto doend;
2075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 if (ask_yesno((char_u *)
2077 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002078 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 lnum = ea.line1;
2081 ea.line1 = ea.line2;
2082 ea.line2 = lnum;
2083 }
2084 if ((errormsg = invalid_range(&ea)) != NULL)
2085 goto doend;
2086 }
2087
2088 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2089 ea.line2 = 1;
2090
2091 correct_range(&ea);
2092
2093#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002094 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2095 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 /* Put the first line at the start of a closed fold, put the last line
2098 * at the end of a closed fold. */
2099 (void)hasFolding(ea.line1, &ea.line1, NULL);
2100 (void)hasFolding(ea.line2, NULL, &ea.line2);
2101 }
2102#endif
2103
2104#ifdef FEAT_QUICKFIX
2105 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002106 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 * option here, so things like % get expanded.
2108 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002109 p = replace_makeprg(&ea, p, cmdlinep);
2110 if (p == NULL)
2111 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112#endif
2113
2114 /*
2115 * Skip to start of argument.
2116 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2117 */
2118 if (ea.cmdidx == CMD_bang)
2119 ea.arg = p;
2120 else
2121 ea.arg = skipwhite(p);
2122
Bram Moolenaar379fb762018-08-30 15:58:28 +02002123 // ":file" cannot be run with an argument when "curbuf_lock" is set
2124 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2125 goto doend;
2126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 /*
2128 * Check for "++opt=val" argument.
2129 * Must be first, allow ":w ++enc=utf8 !cmd"
2130 */
2131 if (ea.argt & ARGOPT)
2132 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2133 if (getargopt(&ea) == FAIL && !ni)
2134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002135 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 goto doend;
2137 }
2138
2139 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2140 {
2141 if (*ea.arg == '>') /* append */
2142 {
2143 if (*++ea.arg != '>') /* typed wrong */
2144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 goto doend;
2147 }
2148 ea.arg = skipwhite(ea.arg + 1);
2149 ea.append = TRUE;
2150 }
2151 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2152 {
2153 ++ea.arg;
2154 ea.usefilter = TRUE;
2155 }
2156 }
2157
2158 if (ea.cmdidx == CMD_read)
2159 {
2160 if (ea.forceit)
2161 {
2162 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2163 ea.forceit = FALSE;
2164 }
2165 else if (*ea.arg == '!') /* :r !filter */
2166 {
2167 ++ea.arg;
2168 ea.usefilter = TRUE;
2169 }
2170 }
2171
2172 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2173 {
2174 ea.amount = 1;
2175 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2176 {
2177 ++ea.arg;
2178 ++ea.amount;
2179 }
2180 ea.arg = skipwhite(ea.arg);
2181 }
2182
2183 /*
2184 * Check for "+command" argument, before checking for next command.
2185 * Don't do this for ":read !cmd" and ":write !cmd".
2186 */
2187 if ((ea.argt & EDITCMD) && !ea.usefilter)
2188 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2189
2190 /*
2191 * Check for '|' to separate commands and '"' to start comments.
2192 * Don't do this for ":read !cmd" and ":write !cmd".
2193 */
2194 if ((ea.argt & TRLBAR) && !ea.usefilter)
2195 separate_nextcmd(&ea);
2196
2197 /*
2198 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002199 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2200 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002202 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002203 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002204 || ea.cmdidx == CMD_global
2205 || ea.cmdidx == CMD_vglobal
2206 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
2208 for (p = ea.arg; *p; ++p)
2209 {
2210 /* Remove one backslash before a newline, so that it's possible to
2211 * pass a newline to the shell and also a newline that is preceded
2212 * with a backslash. This makes it impossible to end a shell
2213 * command in a backslash, but that doesn't appear useful.
2214 * Halving the number of backslashes is incompatible with previous
2215 * versions. */
2216 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002217 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 else if (*p == '\n')
2219 {
2220 ea.nextcmd = p + 1;
2221 *p = NUL;
2222 break;
2223 }
2224 }
2225 }
2226
2227 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2228 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002229 buf_T *buf;
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002232 switch (ea.addr_type)
2233 {
2234 case ADDR_LINES:
2235 ea.line2 = curbuf->b_ml.ml_line_count;
2236 break;
2237 case ADDR_LOADED_BUFFERS:
2238 buf = firstbuf;
2239 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2240 buf = buf->b_next;
2241 ea.line1 = buf->b_fnum;
2242 buf = lastbuf;
2243 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2244 buf = buf->b_prev;
2245 ea.line2 = buf->b_fnum;
2246 break;
2247 case ADDR_BUFFERS:
2248 ea.line1 = firstbuf->b_fnum;
2249 ea.line2 = lastbuf->b_fnum;
2250 break;
2251 case ADDR_WINDOWS:
2252 ea.line2 = LAST_WIN_NR;
2253 break;
2254 case ADDR_TABS:
2255 ea.line2 = LAST_TAB_NR;
2256 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002257 case ADDR_TABS_RELATIVE:
2258 ea.line2 = 1;
2259 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002260 case ADDR_ARGUMENTS:
2261 if (ARGCOUNT == 0)
2262 ea.line1 = ea.line2 = 0;
2263 else
2264 ea.line2 = ARGCOUNT;
2265 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002266#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002267 case ADDR_QUICKFIX:
2268 ea.line2 = qf_get_size(&ea);
2269 if (ea.line2 == 0)
2270 ea.line2 = 1;
2271 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002272#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275
2276 /* accept numbered register only when no count allowed (:put) */
2277 if ( (ea.argt & REGSTR)
2278 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002279 /* Do not allow register = for user commands */
2280 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2282 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002283#ifndef FEAT_CLIPBOARD
2284 /* check these explicitly for a more specific error message */
2285 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002287 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002288 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002291 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2292 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002293 {
2294 ea.regname = *ea.arg++;
2295#ifdef FEAT_EVAL
2296 /* for '=' register: accept the rest of the line as an expression */
2297 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2298 {
2299 set_expr_line(vim_strsave(ea.arg));
2300 ea.arg += STRLEN(ea.arg);
2301 }
2302#endif
2303 ea.arg = skipwhite(ea.arg);
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306
2307 /*
2308 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2309 * count, it's a buffer name.
2310 */
2311 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2312 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002313 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 n = getdigits(&ea.arg);
2316 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002317 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002319 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 goto doend;
2321 }
2322 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2323 {
2324 ea.line2 = n;
2325 if (ea.addr_count == 0)
2326 ea.addr_count = 1;
2327 }
2328 else
2329 {
2330 ea.line1 = ea.line2;
2331 ea.line2 += n - 1;
2332 ++ea.addr_count;
2333 /*
2334 * Be vi compatible: no error message for out of range.
2335 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002336 if (ea.addr_type == ADDR_LINES
2337 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 ea.line2 = curbuf->b_ml.ml_line_count;
2339 }
2340 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002341
2342 /*
2343 * Check for flags: 'l', 'p' and '#'.
2344 */
2345 if (ea.argt & EXFLAGS)
2346 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002348 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002349 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002351 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 goto doend;
2353 }
2354
2355 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002357 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 goto doend;
2359 }
2360
2361#ifdef FEAT_EVAL
2362 /*
2363 * Skip the command when it's not going to be executed.
2364 * The commands like :if, :endif, etc. always need to be executed.
2365 * Also make an exception for commands that handle a trailing command
2366 * themselves.
2367 */
2368 if (ea.skip)
2369 {
2370 switch (ea.cmdidx)
2371 {
2372 /* commands that need evaluation */
2373 case CMD_while:
2374 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002375 case CMD_for:
2376 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 case CMD_if:
2378 case CMD_elseif:
2379 case CMD_else:
2380 case CMD_endif:
2381 case CMD_try:
2382 case CMD_catch:
2383 case CMD_finally:
2384 case CMD_endtry:
2385 case CMD_function:
2386 break;
2387
2388 /* Commands that handle '|' themselves. Check: A command should
2389 * either have the TRLBAR flag, appear in this list or appear in
2390 * the list at ":help :bar". */
2391 case CMD_aboveleft:
2392 case CMD_and:
2393 case CMD_belowright:
2394 case CMD_botright:
2395 case CMD_browse:
2396 case CMD_call:
2397 case CMD_confirm:
2398 case CMD_delfunction:
2399 case CMD_djump:
2400 case CMD_dlist:
2401 case CMD_dsearch:
2402 case CMD_dsplit:
2403 case CMD_echo:
2404 case CMD_echoerr:
2405 case CMD_echomsg:
2406 case CMD_echon:
2407 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002408 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_help:
2410 case CMD_hide:
2411 case CMD_ijump:
2412 case CMD_ilist:
2413 case CMD_isearch:
2414 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002415 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 case CMD_keepjumps:
2417 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002418 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_leftabove:
2420 case CMD_let:
2421 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002422 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002424 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002425 case CMD_noautocmd:
2426 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 case CMD_perl:
2428 case CMD_psearch:
2429 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002430 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002431 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_return:
2433 case CMD_rightbelow:
2434 case CMD_ruby:
2435 case CMD_silent:
2436 case CMD_smagic:
2437 case CMD_snomagic:
2438 case CMD_substitute:
2439 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002440 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_tcl:
2442 case CMD_throw:
2443 case CMD_tilde:
2444 case CMD_topleft:
2445 case CMD_unlet:
2446 case CMD_verbose:
2447 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002448 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 break;
2450
2451 default: goto doend;
2452 }
2453 }
2454#endif
2455
2456 if (ea.argt & XFILE)
2457 {
2458 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2459 goto doend;
2460 }
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /*
2463 * Accept buffer name. Cannot be used at the same time with a buffer
2464 * number. Don't do this for a user command.
2465 */
2466 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002467 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 /*
2470 * :bdelete, :bwipeout and :bunload take several arguments, separated
2471 * by spaces: find next space (skipping over escaped characters).
2472 * The others take one argument: ignore trailing spaces.
2473 */
2474 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2475 || ea.cmdidx == CMD_bunload)
2476 p = skiptowhite_esc(ea.arg);
2477 else
2478 {
2479 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002480 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 --p;
2482 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002483 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2484 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (ea.line2 < 0) /* failed */
2486 goto doend;
2487 ea.addr_count = 1;
2488 ea.arg = skipwhite(p);
2489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar2be57332018-02-13 18:05:18 +01002491 /* The :try command saves the emsg_silent flag, reset it here when
2492 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002493 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002494 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002495 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002496 if (emsg_silent < 0)
2497 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002498 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002499 }
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002502 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002506 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 /*
2509 * Execute a user-defined command.
2510 */
2511 do_ucmd(&ea);
2512 }
2513 else
2514#endif
2515 {
2516 /*
2517 * Call the function to execute the command.
2518 */
2519 ea.errmsg = NULL;
2520 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2521 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002522 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
2524
2525#ifdef FEAT_EVAL
2526 /*
2527 * If the command just executed called do_cmdline(), any throw or ":return"
2528 * or ":finish" encountered there must also check the cstack of the still
2529 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2530 * exception, or reanimate a returned function or finished script file and
2531 * return or finish it again.
2532 */
2533 if (need_rethrow)
2534 do_throw(cstack);
2535 else if (check_cstack)
2536 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002539 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 && current_func_returned())
2541 do_return(&ea, TRUE, FALSE, NULL);
2542 }
2543 need_rethrow = check_cstack = FALSE;
2544#endif
2545
2546doend:
2547 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002550 curwin->w_cursor.col = 0;
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2554 {
2555 if (sourcing)
2556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002557 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002560 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002562 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 emsg(errormsg);
2565 }
2566#ifdef FEAT_EVAL
2567 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002568 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2569 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570#endif
2571
Bram Moolenaareffed932018-08-14 13:38:17 +02002572 if (ea.verbose_save >= 0)
2573 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002574
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002575 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 cmdmod = save_cmdmod;
2577
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 /* messages could be enabled for a serious error, need to check if the
2581 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002582 if (!did_emsg || msg_silent > ea.save_msg_silent)
2583 msg_silent = ea.save_msg_silent;
2584 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (emsg_silent < 0)
2586 emsg_silent = 0;
2587 /* Restore msg_scroll, it's set by file I/O commands, even when no
2588 * message is actually displayed. */
2589 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002590
2591 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2592 * somewhere in the line. Put it back in the first column. */
2593 if (redirecting())
2594 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002597#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002598 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002599 --sandbox;
2600#endif
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2603 ea.nextcmd = NULL;
2604
2605#ifdef FEAT_EVAL
2606 --ex_nesting_level;
2607#endif
2608
2609 return ea.nextcmd;
2610}
2611#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002612 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614
2615/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 * Parse and skip over command modifiers:
2617 * - update eap->cmd
2618 * - store flags in "cmdmod".
2619 * - Set ex_pressedreturn for an empty command line.
2620 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002621 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 * - set p_verbose for ":verbose"
2623 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002624 * When "skip_only" is TRUE the global variables are not changed, except for
2625 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002626 * Return FAIL when the command is not to be executed.
2627 * May set "errormsg" to an error message.
2628 */
2629 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002631{
2632 char_u *p;
2633
2634 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2635 eap->verbose_save = -1;
2636 eap->save_msg_silent = -1;
2637
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 for (;;)
2640 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002641 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2642 ++eap->cmd;
2643
2644 /* in ex mode, an empty line works like :+ */
2645 if (*eap->cmd == NUL && exmode_active
2646 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2647 || getline_equal(eap->getline, eap->cookie, getexline))
2648 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2649 {
2650 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002651 if (!skip_only)
2652 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002653 }
2654
2655 /* ignore comment and empty lines */
2656 if (*eap->cmd == '"')
2657 return FAIL;
2658 if (*eap->cmd == NUL)
2659 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002660 if (!skip_only)
2661 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 return FAIL;
2663 }
2664
Bram Moolenaareffed932018-08-14 13:38:17 +02002665 p = skip_range(eap->cmd, NULL);
2666 switch (*p)
2667 {
2668 /* When adding an entry, also modify cmd_exists(). */
2669 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2670 break;
2671 cmdmod.split |= WSP_ABOVE;
2672 continue;
2673
2674 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2675 {
2676 cmdmod.split |= WSP_BELOW;
2677 continue;
2678 }
2679 if (checkforcmd(&eap->cmd, "browse", 3))
2680 {
2681#ifdef FEAT_BROWSE_CMD
2682 cmdmod.browse = TRUE;
2683#endif
2684 continue;
2685 }
2686 if (!checkforcmd(&eap->cmd, "botright", 2))
2687 break;
2688 cmdmod.split |= WSP_BOT;
2689 continue;
2690
2691 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2692 break;
2693#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2694 cmdmod.confirm = TRUE;
2695#endif
2696 continue;
2697
2698 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2699 {
2700 cmdmod.keepmarks = TRUE;
2701 continue;
2702 }
2703 if (checkforcmd(&eap->cmd, "keepalt", 5))
2704 {
2705 cmdmod.keepalt = TRUE;
2706 continue;
2707 }
2708 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2709 {
2710 cmdmod.keeppatterns = TRUE;
2711 continue;
2712 }
2713 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2714 break;
2715 cmdmod.keepjumps = TRUE;
2716 continue;
2717
2718 case 'f': /* only accept ":filter {pat} cmd" */
2719 {
2720 char_u *reg_pat;
2721
2722 if (!checkforcmd(&p, "filter", 4)
2723 || *p == NUL || ends_excmd(*p))
2724 break;
2725 if (*p == '!')
2726 {
2727 cmdmod.filter_force = TRUE;
2728 p = skipwhite(p + 1);
2729 if (*p == NUL || ends_excmd(*p))
2730 break;
2731 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002732 if (skip_only)
2733 p = skip_vimgrep_pat(p, NULL, NULL);
2734 else
2735 // NOTE: This puts a NUL after the pattern.
2736 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002737 if (p == NULL || *p == NUL)
2738 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002739 if (!skip_only)
2740 {
2741 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002742 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002743 if (cmdmod.filter_regmatch.regprog == NULL)
2744 break;
2745 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 eap->cmd = p;
2747 continue;
2748 }
2749
2750 /* ":hide" and ":hide | cmd" are not modifiers */
2751 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2752 || *p == NUL || ends_excmd(*p))
2753 break;
2754 eap->cmd = p;
2755 cmdmod.hide = TRUE;
2756 continue;
2757
2758 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2759 {
2760 cmdmod.lockmarks = TRUE;
2761 continue;
2762 }
2763
2764 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2765 break;
2766 cmdmod.split |= WSP_ABOVE;
2767 continue;
2768
2769 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2770 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002771 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 {
2773 /* Set 'eventignore' to "all". Restore the
2774 * existing option value later. */
2775 cmdmod.save_ei = vim_strsave(p_ei);
2776 set_string_option_direct((char_u *)"ei", -1,
2777 (char_u *)"all", OPT_FREE, SID_NONE);
2778 }
2779 continue;
2780 }
2781 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2782 break;
2783 cmdmod.noswapfile = TRUE;
2784 continue;
2785
2786 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2787 break;
2788 cmdmod.split |= WSP_BELOW;
2789 continue;
2790
2791 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2792 {
2793#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002794 if (!skip_only)
2795 {
2796 if (!eap->did_sandbox)
2797 ++sandbox;
2798 eap->did_sandbox = TRUE;
2799 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002800#endif
2801 continue;
2802 }
2803 if (!checkforcmd(&eap->cmd, "silent", 3))
2804 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002805 if (!skip_only)
2806 {
2807 if (eap->save_msg_silent == -1)
2808 eap->save_msg_silent = msg_silent;
2809 ++msg_silent;
2810 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2812 {
2813 /* ":silent!", but not "silent !cmd" */
2814 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 {
2817 ++emsg_silent;
2818 ++eap->did_esilent;
2819 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 }
2821 continue;
2822
2823 case 't': if (checkforcmd(&p, "tab", 3))
2824 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002825 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002827 long tabnr = get_address(eap, &eap->cmd,
2828 ADDR_TABS, eap->skip,
2829 skip_only, FALSE, 1);
2830 if (tabnr == MAXLNUM)
2831 cmdmod.tab = tabpage_index(curtab) + 1;
2832 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002834 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002836 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002837 return FAIL;
2838 }
2839 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 }
2842 eap->cmd = p;
2843 continue;
2844 }
2845 if (!checkforcmd(&eap->cmd, "topleft", 2))
2846 break;
2847 cmdmod.split |= WSP_TOP;
2848 continue;
2849
2850 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2851 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002852 if (!skip_only)
2853 {
2854 if (eap->save_msg_silent == -1)
2855 eap->save_msg_silent = msg_silent;
2856 msg_silent = 0;
2857 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 continue;
2859
2860 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2861 {
2862 cmdmod.split |= WSP_VERT;
2863 continue;
2864 }
2865 if (!checkforcmd(&p, "verbose", 4))
2866 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002867 if (!skip_only)
2868 {
2869 if (eap->verbose_save < 0)
2870 eap->verbose_save = p_verbose;
2871 if (vim_isdigit(*eap->cmd))
2872 p_verbose = atoi((char *)eap->cmd);
2873 else
2874 p_verbose = 1;
2875 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 eap->cmd = p;
2877 continue;
2878 }
2879 break;
2880 }
2881
2882 return OK;
2883}
2884
2885/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002886 * Free contents of "cmdmod".
2887 */
2888 static void
2889free_cmdmod(void)
2890{
2891 if (cmdmod.save_ei != NULL)
2892 {
2893 /* Restore 'eventignore' to the value before ":noautocmd". */
2894 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2895 OPT_FREE, SID_NONE);
2896 free_string_option(cmdmod.save_ei);
2897 }
2898
2899 if (cmdmod.filter_regmatch.regprog != NULL)
2900 vim_regfree(cmdmod.filter_regmatch.regprog);
2901}
2902
2903/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002904 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002905 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002906 * Return FAIL and set "errormsg" or return OK.
2907 */
2908 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002910{
2911 int address_count = 1;
2912 linenr_T lnum;
2913
2914 // Repeat for all ',' or ';' separated addresses.
2915 for (;;)
2916 {
2917 eap->line1 = eap->line2;
2918 switch (eap->addr_type)
2919 {
2920 case ADDR_LINES:
2921 // default is current line number
2922 eap->line2 = curwin->w_cursor.lnum;
2923 break;
2924 case ADDR_WINDOWS:
2925 eap->line2 = CURRENT_WIN_NR;
2926 break;
2927 case ADDR_ARGUMENTS:
2928 eap->line2 = curwin->w_arg_idx + 1;
2929 if (eap->line2 > ARGCOUNT)
2930 eap->line2 = ARGCOUNT;
2931 break;
2932 case ADDR_LOADED_BUFFERS:
2933 case ADDR_BUFFERS:
2934 eap->line2 = curbuf->b_fnum;
2935 break;
2936 case ADDR_TABS:
2937 eap->line2 = CURRENT_TAB_NR;
2938 break;
2939 case ADDR_TABS_RELATIVE:
2940 eap->line2 = 1;
2941 break;
2942#ifdef FEAT_QUICKFIX
2943 case ADDR_QUICKFIX:
2944 eap->line2 = qf_get_cur_valid_idx(eap);
2945 break;
2946#endif
2947 }
2948 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002949 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 eap->addr_count == 0, address_count++);
2951 if (eap->cmd == NULL) // error detected
2952 return FAIL;
2953 if (lnum == MAXLNUM)
2954 {
2955 if (*eap->cmd == '%') // '%' - all lines
2956 {
2957 ++eap->cmd;
2958 switch (eap->addr_type)
2959 {
2960 case ADDR_LINES:
2961 eap->line1 = 1;
2962 eap->line2 = curbuf->b_ml.ml_line_count;
2963 break;
2964 case ADDR_LOADED_BUFFERS:
2965 {
2966 buf_T *buf = firstbuf;
2967
2968 while (buf->b_next != NULL
2969 && buf->b_ml.ml_mfp == NULL)
2970 buf = buf->b_next;
2971 eap->line1 = buf->b_fnum;
2972 buf = lastbuf;
2973 while (buf->b_prev != NULL
2974 && buf->b_ml.ml_mfp == NULL)
2975 buf = buf->b_prev;
2976 eap->line2 = buf->b_fnum;
2977 break;
2978 }
2979 case ADDR_BUFFERS:
2980 eap->line1 = firstbuf->b_fnum;
2981 eap->line2 = lastbuf->b_fnum;
2982 break;
2983 case ADDR_WINDOWS:
2984 case ADDR_TABS:
2985 if (IS_USER_CMDIDX(eap->cmdidx))
2986 {
2987 eap->line1 = 1;
2988 eap->line2 = eap->addr_type == ADDR_WINDOWS
2989 ? LAST_WIN_NR : LAST_TAB_NR;
2990 }
2991 else
2992 {
2993 // there is no Vim command which uses '%' and
2994 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002995 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002996 return FAIL;
2997 }
2998 break;
2999 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003000 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002 return FAIL;
3003 case ADDR_ARGUMENTS:
3004 if (ARGCOUNT == 0)
3005 eap->line1 = eap->line2 = 0;
3006 else
3007 {
3008 eap->line1 = 1;
3009 eap->line2 = ARGCOUNT;
3010 }
3011 break;
3012#ifdef FEAT_QUICKFIX
3013 case ADDR_QUICKFIX:
3014 eap->line1 = 1;
3015 eap->line2 = qf_get_size(eap);
3016 if (eap->line2 == 0)
3017 eap->line2 = 1;
3018 break;
3019#endif
3020 }
3021 ++eap->addr_count;
3022 }
3023 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3024 {
3025 pos_T *fp;
3026
3027 // '*' - visual area
3028 if (eap->addr_type != ADDR_LINES)
3029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003030 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003031 return FAIL;
3032 }
3033
3034 ++eap->cmd;
3035 if (!eap->skip)
3036 {
3037 fp = getmark('<', FALSE);
3038 if (check_mark(fp) == FAIL)
3039 return FAIL;
3040 eap->line1 = fp->lnum;
3041 fp = getmark('>', FALSE);
3042 if (check_mark(fp) == FAIL)
3043 return FAIL;
3044 eap->line2 = fp->lnum;
3045 ++eap->addr_count;
3046 }
3047 }
3048 }
3049 else
3050 eap->line2 = lnum;
3051 eap->addr_count++;
3052
3053 if (*eap->cmd == ';')
3054 {
3055 if (!eap->skip)
3056 {
3057 curwin->w_cursor.lnum = eap->line2;
3058 // don't leave the cursor on an illegal line or column
3059 check_cursor();
3060 }
3061 }
3062 else if (*eap->cmd != ',')
3063 break;
3064 ++eap->cmd;
3065 }
3066
3067 // One address given: set start and end lines.
3068 if (eap->addr_count == 1)
3069 {
3070 eap->line1 = eap->line2;
3071 // ... but only implicit: really no address given
3072 if (lnum == MAXLNUM)
3073 eap->addr_count = 0;
3074 }
3075 return OK;
3076}
3077
3078/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003079 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 * If there is a match advance "pp" to the argument and return TRUE.
3081 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003082 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003083checkforcmd(
3084 char_u **pp, /* start of command */
3085 char *cmd, /* name of command */
3086 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 int i;
3089
3090 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003091 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 break;
3093 if (i >= len && !isalpha((*pp)[i]))
3094 {
3095 *pp = skipwhite(*pp + i);
3096 return TRUE;
3097 }
3098 return FALSE;
3099}
3100
3101/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003102 * Append "cmd" to the error message in IObuff.
3103 * Takes care of limiting the length and handling 0xa0, which would be
3104 * invisible otherwise.
3105 */
3106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003108{
3109 char_u *s = cmd;
3110 char_u *d;
3111
3112 STRCAT(IObuff, ": ");
3113 d = IObuff + STRLEN(IObuff);
3114 while (*s != NUL && d - IObuff < IOSIZE - 7)
3115 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003116 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003117 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003118 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003119 STRCPY(d, "<a0>");
3120 d += 4;
3121 }
3122 else
3123 MB_COPY_CHAR(s, d);
3124 }
3125 *d = NUL;
3126}
3127
3128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003130 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003132 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 * Returns NULL for an ambiguous user command.
3134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003136find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 int len;
3139 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003140 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 /*
3143 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003144 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * - the 'k' command can directly be followed by any character.
3146 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003147 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003149 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
3151 p = eap->cmd;
3152 if (*p == 'k')
3153 {
3154 eap->cmdidx = CMD_k;
3155 ++p;
3156 }
3157 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003158 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3159 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 || p[1] == 'g'
3161 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3162 || p[1] == 'I'
3163 || (p[1] == 'r' && p[2] != 'e')))
3164 {
3165 eap->cmdidx = CMD_substitute;
3166 ++p;
3167 }
3168 else
3169 {
3170 while (ASCII_ISALPHA(*p))
3171 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003172 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003173 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003174 while (ASCII_ISALNUM(*p))
3175 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 /* check for non-alpha command */
3178 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3179 ++p;
3180 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003181 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3182 {
3183 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3184 * :delete with the 'l' flag. Same for 'p'. */
3185 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003186 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003187 break;
3188 if (i == len - 1)
3189 {
3190 --len;
3191 if (p[-1] == 'l')
3192 eap->flags |= EXFLAG_LIST;
3193 else
3194 eap->flags |= EXFLAG_PRINT;
3195 }
3196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003198 if (ASCII_ISLOWER(eap->cmd[0]))
3199 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003200 int c1 = eap->cmd[0];
3201 int c2 = eap->cmd[1];
3202
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003203 if (command_count != (int)CMD_SIZE)
3204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003205 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003206 getout(1);
3207 }
3208
3209 /* Use a precomputed index for fast look-up in cmdnames[]
3210 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003211 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3212 if (ASCII_ISLOWER(c2))
3213 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003216 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3219 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3220 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3221 (size_t)len) == 0)
3222 {
3223#ifdef FEAT_EVAL
3224 if (full != NULL
3225 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3226 *full = TRUE;
3227#endif
3228 break;
3229 }
3230
3231#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003232 /* Look for a user defined command as a last resort. Let ":Print" be
3233 * overruled by a user defined command. */
3234 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3235 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 while (ASCII_ISALNUM(*p))
3239 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 eap->cmdidx = CMD_SIZE;
3245 }
3246
3247 return p;
3248}
3249
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250#ifdef FEAT_USR_CMDS
3251/*
3252 * Search for a user command that matches "eap->cmd".
3253 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3254 * Return a pointer to just after the command.
3255 * Return NULL if there is no matching command.
3256 */
3257 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003258find_ucmd(
3259 exarg_T *eap,
3260 char_u *p, /* end of the command (possibly including count) */
3261 int *full, /* set to TRUE for a full match */
3262 expand_T *xp, /* used for completion, NULL otherwise */
3263 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003264{
3265 int len = (int)(p - eap->cmd);
3266 int j, k, matchlen = 0;
3267 ucmd_T *uc;
3268 int found = FALSE;
3269 int possible = FALSE;
3270 char_u *cp, *np; /* Point into typed cmd and test name */
3271 garray_T *gap;
3272 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3273 only full match global is accepted. */
3274
3275 /*
3276 * Look for buffer-local user commands first, then global ones.
3277 */
3278 gap = &curbuf->b_ucmds;
3279 for (;;)
3280 {
3281 for (j = 0; j < gap->ga_len; ++j)
3282 {
3283 uc = USER_CMD_GA(gap, j);
3284 cp = eap->cmd;
3285 np = uc->uc_name;
3286 k = 0;
3287 while (k < len && *np != NUL && *cp++ == *np++)
3288 k++;
3289 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3290 {
3291 /* If finding a second match, the command is ambiguous. But
3292 * not if a buffer-local command wasn't a full match and a
3293 * global command is a full match. */
3294 if (k == len && found && *np != NUL)
3295 {
3296 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003297 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003298 amb_local = TRUE;
3299 }
3300
3301 if (!found || (k == len && *np == NUL))
3302 {
3303 /* If we matched up to a digit, then there could
3304 * be another command including the digit that we
3305 * should use instead.
3306 */
3307 if (k == len)
3308 found = TRUE;
3309 else
3310 possible = TRUE;
3311
3312 if (gap == &ucmds)
3313 eap->cmdidx = CMD_USER;
3314 else
3315 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003316 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003318 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319
3320# ifdef FEAT_CMDL_COMPL
3321 if (compl != NULL)
3322 *compl = uc->uc_compl;
3323# ifdef FEAT_EVAL
3324 if (xp != NULL)
3325 {
3326 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003327 xp->xp_script_ctx = uc->uc_script_ctx;
3328 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 }
3330# endif
3331# endif
3332 /* Do not search for further abbreviations
3333 * if this is an exact match. */
3334 matchlen = k;
3335 if (k == len && *np == NUL)
3336 {
3337 if (full != NULL)
3338 *full = TRUE;
3339 amb_local = FALSE;
3340 break;
3341 }
3342 }
3343 }
3344 }
3345
3346 /* Stop if we found a full match or searched all. */
3347 if (j < gap->ga_len || gap == &ucmds)
3348 break;
3349 gap = &ucmds;
3350 }
3351
3352 /* Only found ambiguous matches. */
3353 if (amb_local)
3354 {
3355 if (xp != NULL)
3356 xp->xp_context = EXPAND_UNSUCCESSFUL;
3357 return NULL;
3358 }
3359
3360 /* The match we found may be followed immediately by a number. Move "p"
3361 * back to point to it. */
3362 if (found || possible)
3363 return p + (matchlen - len);
3364 return p;
3365}
3366#endif
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369static struct cmdmod
3370{
3371 char *name;
3372 int minlen;
3373 int has_count; /* :123verbose :3tab */
3374} cmdmods[] = {
3375 {"aboveleft", 3, FALSE},
3376 {"belowright", 3, FALSE},
3377 {"botright", 2, FALSE},
3378 {"browse", 3, FALSE},
3379 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003380 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003381 {"hide", 3, FALSE},
3382 {"keepalt", 5, FALSE},
3383 {"keepjumps", 5, FALSE},
3384 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003385 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003386 {"leftabove", 5, FALSE},
3387 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003388 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003389 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003390 {"rightbelow", 6, FALSE},
3391 {"sandbox", 3, FALSE},
3392 {"silent", 3, FALSE},
3393 {"tab", 3, TRUE},
3394 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003395 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003396 {"verbose", 4, TRUE},
3397 {"vertical", 4, FALSE},
3398};
3399
3400/*
3401 * Return length of a command modifier (including optional count).
3402 * Return zero when it's not a modifier.
3403 */
3404 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003405modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003406{
3407 int i, j;
3408 char_u *p = cmd;
3409
3410 if (VIM_ISDIGIT(*cmd))
3411 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003412 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003413 {
3414 for (j = 0; p[j] != NUL; ++j)
3415 if (p[j] != cmdmods[i].name[j])
3416 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003417 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003418 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003419 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003420 }
3421 return 0;
3422}
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424/*
3425 * Return > 0 if an Ex command "name" exists.
3426 * Return 2 if there is an exact match.
3427 * Return 3 if there is an ambiguous match.
3428 */
3429 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003430cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
3432 exarg_T ea;
3433 int full = FALSE;
3434 int i;
3435 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003436 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003439 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
3441 for (j = 0; name[j] != NUL; ++j)
3442 if (name[j] != cmdmods[i].name[j])
3443 break;
3444 if (name[j] == NUL && j >= cmdmods[i].minlen)
3445 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3446 }
3447
Bram Moolenaara9587612006-05-04 21:47:50 +00003448 /* Check built-in commands and user defined commands.
3449 * For ":2match" and ":3match" we need to skip the number. */
3450 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003452 p = find_command(&ea, &full);
3453 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003455 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3456 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003457 if (*skipwhite(p) != NUL)
3458 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3460}
3461#endif
3462
3463/*
3464 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3465 * we don't need/want deleted. Maybe this could be done better if we didn't
3466 * repeat all this stuff. The only problem is that they may not stay
3467 * perfectly compatible with each other, but then the command line syntax
3468 * probably won't change that much -- webb.
3469 */
3470 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471set_one_cmd_context(
3472 expand_T *xp,
3473 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475 char_u *p;
3476 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 int len = 0;
3478 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3480 int compl = EXPAND_NOTHING;
3481#endif
3482#ifdef FEAT_CMDL_COMPL
3483 int delim;
3484#endif
3485 int forceit = FALSE;
3486 int usefilter = FALSE; /* filter instead of file name */
3487
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003488 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 xp->xp_pattern = buff;
3490 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003494 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 */
3496 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3497 ;
3498 xp->xp_pattern = cmd;
3499
3500 if (*cmd == NUL)
3501 return NULL;
3502 if (*cmd == '"') /* ignore comment lines */
3503 {
3504 xp->xp_context = EXPAND_NOTHING;
3505 return NULL;
3506 }
3507
3508/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003509 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 */
3511 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 xp->xp_pattern = cmd;
3513 if (*cmd == NUL)
3514 return NULL;
3515 if (*cmd == '"')
3516 {
3517 xp->xp_context = EXPAND_NOTHING;
3518 return NULL;
3519 }
3520
3521 if (*cmd == '|' || *cmd == '\n')
3522 return cmd + 1; /* There's another command */
3523
3524 /*
3525 * Isolate the command and search for it in the command table.
3526 * Exceptions:
3527 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003528 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3530 */
3531 if (*cmd == 'k' && cmd[1] != 'e')
3532 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003533 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 p = cmd + 1;
3535 }
3536 else
3537 {
3538 p = cmd;
3539 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3540 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003541 /* a user command may contain digits */
3542 if (ASCII_ISUPPER(cmd[0]))
3543 while (ASCII_ISALNUM(*p) || *p == '*')
3544 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003545 /* for python 3.x: ":py3*" commands completion */
3546 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003547 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003548 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003549 while (ASCII_ISALPHA(*p) || *p == '*')
3550 ++p;
3551 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003552 /* check for non-alpha command */
3553 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3554 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003557 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559 xp->xp_context = EXPAND_UNSUCCESSFUL;
3560 return NULL;
3561 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003563 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3564 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3565 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 break;
3567
3568#ifdef FEAT_USR_CMDS
3569 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3571 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#endif
3573 }
3574
3575 /*
3576 * If the cursor is touching the command, and it ends in an alpha-numeric
3577 * character, complete the command name.
3578 */
3579 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3580 return NULL;
3581
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3585 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 p = cmd + 1;
3588 }
3589#ifdef FEAT_USR_CMDS
3590 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3591 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 ea.cmd = cmd;
3593 p = find_ucmd(&ea, p, NULL, xp,
3594# if defined(FEAT_CMDL_COMPL)
3595 &compl
3596# else
3597 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003600 if (p == NULL)
3601 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603#endif
3604 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 /* Not still touching the command and it was an illegal one */
3608 xp->xp_context = EXPAND_UNSUCCESSFUL;
3609 return NULL;
3610 }
3611
3612 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3613
3614 if (*p == '!') /* forced commands */
3615 {
3616 forceit = TRUE;
3617 ++p;
3618 }
3619
3620/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003621 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003623 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003624 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
3626 arg = skipwhite(p);
3627
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003628 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 if (*arg == '>') /* append */
3631 {
3632 if (*++arg == '>')
3633 ++arg;
3634 arg = skipwhite(arg);
3635 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
3638 ++arg;
3639 usefilter = TRUE;
3640 }
3641 }
3642
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 usefilter = forceit; /* :r! filter if forced */
3646 if (*arg == '!') /* :r !filter */
3647 {
3648 ++arg;
3649 usefilter = TRUE;
3650 }
3651 }
3652
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003653 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 while (*arg == *cmd) /* allow any number of '>' or '<' */
3656 ++arg;
3657 arg = skipwhite(arg);
3658 }
3659
3660 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 /* Check if we're in the +command */
3664 p = arg + 1;
3665 arg = skip_cmd_arg(arg, FALSE);
3666
3667 /* Still touching the command after '+'? */
3668 if (*arg == NUL)
3669 return p;
3670
3671 /* Skip space(s) after +command to get to the real argument */
3672 arg = skipwhite(arg);
3673 }
3674
3675 /*
3676 * Check for '|' to separate commands and '"' to start comments.
3677 * Don't do this for ":read !cmd" and ":write !cmd".
3678 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003679 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681 p = arg;
3682 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003683 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 p += 2;
3685 while (*p)
3686 {
3687 if (*p == Ctrl_V)
3688 {
3689 if (p[1] != NUL)
3690 ++p;
3691 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003692 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 || *p == '|' || *p == '\n')
3694 {
3695 if (*(p - 1) != '\\')
3696 {
3697 if (*p == '|' || *p == '\n')
3698 return p + 1;
3699 return NULL; /* It's a comment */
3700 }
3701 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003702 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704 }
3705
3706 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003707 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 vim_strchr((char_u *)"|\"", *arg) == NULL)
3709 return NULL;
3710
3711 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003712 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003714 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003715 while (*p && p < buff + len)
3716 {
3717 if (*p == ' ' || *p == TAB)
3718 {
3719 /* argument starts after a space */
3720 xp->xp_pattern = ++p;
3721 }
3722 else
3723 {
3724 if (*p == '\\' && *(p + 1) != NUL)
3725 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003726 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003727 }
3728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003730 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003732 int c;
3733 int in_quote = FALSE;
3734 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 /*
3737 * Allow spaces within back-quotes to count as part of the argument
3738 * being expanded.
3739 */
3740 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003741 p = xp->xp_pattern;
3742 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003744 if (has_mbyte)
3745 c = mb_ptr2char(p);
3746 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003747 c = *p;
3748 if (c == '\\' && p[1] != NUL)
3749 ++p;
3750 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
3752 if (!in_quote)
3753 {
3754 xp->xp_pattern = p;
3755 bow = p + 1;
3756 }
3757 in_quote = !in_quote;
3758 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003759 /* An argument can contain just about everything, except
3760 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003761 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003762#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003763 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003764#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003765 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003766 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003767 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003768 while (*p != NUL)
3769 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003770 if (has_mbyte)
3771 c = mb_ptr2char(p);
3772 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003773 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003774 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003775 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003776 if (has_mbyte)
3777 len = (*mb_ptr2len)(p);
3778 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003779 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003780 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003781 }
3782 if (in_quote)
3783 bow = p;
3784 else
3785 xp->xp_pattern = p;
3786 p -= len;
3787 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003788 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790
3791 /*
3792 * If we are still inside the quotes, and we passed a space, just
3793 * expand from there.
3794 */
3795 if (bow != NULL && in_quote)
3796 xp->xp_pattern = bow;
3797 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003798
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003799 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003800 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003801 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003802#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003803 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003804#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003805 /* When still after the command name expand executables. */
3806 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003807 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 /* Check for environment variable */
3811 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003812#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 || *xp->xp_pattern == '%'
3814#endif
3815 )
3816 {
3817 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3818 if (!vim_isIDc(*p))
3819 break;
3820 if (*p == NUL)
3821 {
3822 xp->xp_context = EXPAND_ENV_VARS;
3823 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003824#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3825 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003826 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003827 compl = EXPAND_ENV_VARS;
3828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003831#if defined(FEAT_CMDL_COMPL)
3832 /* Check for user names */
3833 if (*xp->xp_pattern == '~')
3834 {
3835 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3836 ;
3837 /* Complete ~user only if it partially matches a user name.
3838 * A full match ~user<Tab> will be replaced by user's home
3839 * directory i.e. something like ~user<Tab> -> /home/user/ */
3840 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003841 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003842 {
3843 xp->xp_context = EXPAND_USER;
3844 ++xp->xp_pattern;
3845 }
3846 }
3847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849
3850/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003851 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003853 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003855 case CMD_find:
3856 case CMD_sfind:
3857 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003858 if (xp->xp_context == EXPAND_FILES)
3859 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 case CMD_cd:
3862 case CMD_chdir:
3863 case CMD_lcd:
3864 case CMD_lchdir:
3865 if (xp->xp_context == EXPAND_FILES)
3866 xp->xp_context = EXPAND_DIRECTORIES;
3867 break;
3868 case CMD_help:
3869 xp->xp_context = EXPAND_HELP;
3870 xp->xp_pattern = arg;
3871 break;
3872
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003873 /* Command modifiers: return the argument.
3874 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003876 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 case CMD_belowright:
3878 case CMD_botright:
3879 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003880 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 case CMD_cdo:
3882 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003884 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_folddoclosed:
3886 case CMD_folddoopen:
3887 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003888 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_keepjumps:
3890 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003891 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003892 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003894 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003896 case CMD_noautocmd:
3897 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003899 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003901 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003902 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_topleft:
3904 case CMD_verbose:
3905 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003906 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 return arg;
3908
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003909 case CMD_filter:
3910 if (*arg != NUL)
3911 arg = skip_vimgrep_pat(arg, NULL, NULL);
3912 if (arg == NULL || *arg == NUL)
3913 {
3914 xp->xp_context = EXPAND_NOTHING;
3915 return NULL;
3916 }
3917 return skipwhite(arg);
3918
Bram Moolenaar4f688582007-07-24 12:34:30 +00003919#ifdef FEAT_CMDL_COMPL
3920# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 case CMD_match:
3922 if (*arg == NUL || !ends_excmd(*arg))
3923 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003924 /* also complete "None" */
3925 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 arg = skipwhite(skiptowhite(arg));
3927 if (*arg != NUL)
3928 {
3929 xp->xp_context = EXPAND_NOTHING;
3930 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3931 }
3932 }
3933 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936/*
3937 * All completion for the +cmdline_compl feature goes here.
3938 */
3939
3940# ifdef FEAT_USR_CMDS
3941 case CMD_command:
3942 /* Check for attributes */
3943 while (*arg == '-')
3944 {
3945 arg++; /* Skip "-" */
3946 p = skiptowhite(arg);
3947 if (*p == NUL)
3948 {
3949 /* Cursor is still in the attribute */
3950 p = vim_strchr(arg, '=');
3951 if (p == NULL)
3952 {
3953 /* No "=", so complete attribute names */
3954 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3955 xp->xp_pattern = arg;
3956 return NULL;
3957 }
3958
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003959 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 * their arguments as well.
3961 */
3962 if (STRNICMP(arg, "complete", p - arg) == 0)
3963 {
3964 xp->xp_context = EXPAND_USER_COMPLETE;
3965 xp->xp_pattern = p + 1;
3966 return NULL;
3967 }
3968 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3969 {
3970 xp->xp_context = EXPAND_USER_NARGS;
3971 xp->xp_pattern = p + 1;
3972 return NULL;
3973 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003974 else if (STRNICMP(arg, "addr", p - arg) == 0)
3975 {
3976 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3977 xp->xp_pattern = p + 1;
3978 return NULL;
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 return NULL;
3981 }
3982 arg = skipwhite(p);
3983 }
3984
3985 /* After the attributes comes the new command name */
3986 p = skiptowhite(arg);
3987 if (*p == NUL)
3988 {
3989 xp->xp_context = EXPAND_USER_COMMANDS;
3990 xp->xp_pattern = arg;
3991 break;
3992 }
3993
3994 /* And finally comes a normal command */
3995 return skipwhite(p);
3996
3997 case CMD_delcommand:
3998 xp->xp_context = EXPAND_USER_COMMANDS;
3999 xp->xp_pattern = arg;
4000 break;
4001# endif
4002
4003 case CMD_global:
4004 case CMD_vglobal:
4005 delim = *arg; /* get the delimiter */
4006 if (delim)
4007 ++arg; /* skip delimiter if there is one */
4008
4009 while (arg[0] != NUL && arg[0] != delim)
4010 {
4011 if (arg[0] == '\\' && arg[1] != NUL)
4012 ++arg;
4013 ++arg;
4014 }
4015 if (arg[0] != NUL)
4016 return arg + 1;
4017 break;
4018 case CMD_and:
4019 case CMD_substitute:
4020 delim = *arg;
4021 if (delim)
4022 {
4023 /* skip "from" part */
4024 ++arg;
4025 arg = skip_regexp(arg, delim, p_magic, NULL);
4026 }
4027 /* skip "to" part */
4028 while (arg[0] != NUL && arg[0] != delim)
4029 {
4030 if (arg[0] == '\\' && arg[1] != NUL)
4031 ++arg;
4032 ++arg;
4033 }
4034 if (arg[0] != NUL) /* skip delimiter */
4035 ++arg;
4036 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4037 ++arg;
4038 if (arg[0] != NUL)
4039 return arg;
4040 break;
4041 case CMD_isearch:
4042 case CMD_dsearch:
4043 case CMD_ilist:
4044 case CMD_dlist:
4045 case CMD_ijump:
4046 case CMD_psearch:
4047 case CMD_djump:
4048 case CMD_isplit:
4049 case CMD_dsplit:
4050 arg = skipwhite(skipdigits(arg)); /* skip count */
4051 if (*arg == '/') /* Match regexp, not just whole words */
4052 {
4053 for (++arg; *arg && *arg != '/'; arg++)
4054 if (*arg == '\\' && arg[1] != NUL)
4055 arg++;
4056 if (*arg)
4057 {
4058 arg = skipwhite(arg + 1);
4059
4060 /* Check for trailing illegal characters */
4061 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4062 xp->xp_context = EXPAND_NOTHING;
4063 else
4064 return arg;
4065 }
4066 }
4067 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 case CMD_autocmd:
4070 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004072 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 case CMD_set:
4075 set_context_in_set_cmd(xp, arg, 0);
4076 break;
4077 case CMD_setglobal:
4078 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4079 break;
4080 case CMD_setlocal:
4081 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4082 break;
4083 case CMD_tag:
4084 case CMD_stag:
4085 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004086 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 case CMD_tselect:
4088 case CMD_stselect:
4089 case CMD_ptselect:
4090 case CMD_tjump:
4091 case CMD_stjump:
4092 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004093 if (*p_wop != NUL)
4094 xp->xp_context = EXPAND_TAGS_LISTFILES;
4095 else
4096 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 xp->xp_pattern = arg;
4098 break;
4099 case CMD_augroup:
4100 xp->xp_context = EXPAND_AUGROUP;
4101 xp->xp_pattern = arg;
4102 break;
4103#ifdef FEAT_SYN_HL
4104 case CMD_syntax:
4105 set_context_in_syntax_cmd(xp, arg);
4106 break;
4107#endif
4108#ifdef FEAT_EVAL
4109 case CMD_let:
4110 case CMD_if:
4111 case CMD_elseif:
4112 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004113 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 case CMD_echo:
4115 case CMD_echon:
4116 case CMD_execute:
4117 case CMD_echomsg:
4118 case CMD_echoerr:
4119 case CMD_call:
4120 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004121 case CMD_cexpr:
4122 case CMD_caddexpr:
4123 case CMD_cgetexpr:
4124 case CMD_lexpr:
4125 case CMD_laddexpr:
4126 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004127 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 break;
4129
4130 case CMD_unlet:
4131 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4132 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 xp->xp_context = EXPAND_USER_VARS;
4135 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004136
4137 if (*xp->xp_pattern == '$')
4138 {
4139 xp->xp_context = EXPAND_ENV_VARS;
4140 ++xp->xp_pattern;
4141 }
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 break;
4144
4145 case CMD_function:
4146 case CMD_delfunction:
4147 xp->xp_context = EXPAND_USER_FUNC;
4148 xp->xp_pattern = arg;
4149 break;
4150
4151 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004152 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 break;
4154#endif
4155 case CMD_highlight:
4156 set_context_in_highlight_cmd(xp, arg);
4157 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004158#ifdef FEAT_CSCOPE
4159 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004160 case CMD_lcscope:
4161 case CMD_scscope:
4162 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004163 break;
4164#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004165#ifdef FEAT_SIGNS
4166 case CMD_sign:
4167 set_context_in_sign_cmd(xp, arg);
4168 break;
4169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 case CMD_bdelete:
4171 case CMD_bwipeout:
4172 case CMD_bunload:
4173 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4174 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004175 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 case CMD_buffer:
4177 case CMD_sbuffer:
4178 case CMD_checktime:
4179 xp->xp_context = EXPAND_BUFFERS;
4180 xp->xp_pattern = arg;
4181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_USR_CMDS
4183 case CMD_USER:
4184 case CMD_USER_BUF:
4185 if (compl != EXPAND_NOTHING)
4186 {
4187 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004188 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
4190# ifdef FEAT_MENU
4191 if (compl == EXPAND_MENUS)
4192 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4193# endif
4194 if (compl == EXPAND_COMMANDS)
4195 return arg;
4196 if (compl == EXPAND_MAPPINGS)
4197 return set_context_in_map_cmd(xp, (char_u *)"map",
4198 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004199 /* Find start of last argument. */
4200 p = arg;
4201 while (*p)
4202 {
4203 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004204 /* argument starts after a space */
4205 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004206 else if (*p == '\\' && *(p + 1) != NUL)
4207 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004208 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 xp->xp_pattern = arg;
4211 }
4212 xp->xp_context = compl;
4213 }
4214 break;
4215#endif
4216 case CMD_map: case CMD_noremap:
4217 case CMD_nmap: case CMD_nnoremap:
4218 case CMD_vmap: case CMD_vnoremap:
4219 case CMD_omap: case CMD_onoremap:
4220 case CMD_imap: case CMD_inoremap:
4221 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004222 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004223 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004224 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004225 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004227 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 case CMD_unmap:
4229 case CMD_nunmap:
4230 case CMD_vunmap:
4231 case CMD_ounmap:
4232 case CMD_iunmap:
4233 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004234 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004235 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004236 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004237 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004239 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004240 case CMD_mapclear:
4241 case CMD_nmapclear:
4242 case CMD_vmapclear:
4243 case CMD_omapclear:
4244 case CMD_imapclear:
4245 case CMD_cmapclear:
4246 case CMD_lmapclear:
4247 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004248 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004249 case CMD_xmapclear:
4250 xp->xp_context = EXPAND_MAPCLEAR;
4251 xp->xp_pattern = arg;
4252 break;
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 case CMD_abbreviate: case CMD_noreabbrev:
4255 case CMD_cabbrev: case CMD_cnoreabbrev:
4256 case CMD_iabbrev: case CMD_inoreabbrev:
4257 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004258 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 case CMD_unabbreviate:
4260 case CMD_cunabbrev:
4261 case CMD_iunabbrev:
4262 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004263 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264#ifdef FEAT_MENU
4265 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4266 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4267 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4268 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4269 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4270 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4271 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004272 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 case CMD_tmenu: case CMD_tunmenu:
4274 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4275 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4276#endif
4277
4278 case CMD_colorscheme:
4279 xp->xp_context = EXPAND_COLORS;
4280 xp->xp_pattern = arg;
4281 break;
4282
4283 case CMD_compiler:
4284 xp->xp_context = EXPAND_COMPILER;
4285 xp->xp_pattern = arg;
4286 break;
4287
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004288 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004289 xp->xp_context = EXPAND_OWNSYNTAX;
4290 xp->xp_pattern = arg;
4291 break;
4292
4293 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004294 xp->xp_context = EXPAND_FILETYPE;
4295 xp->xp_pattern = arg;
4296 break;
4297
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004298 case CMD_packadd:
4299 xp->xp_context = EXPAND_PACKADD;
4300 xp->xp_pattern = arg;
4301 break;
4302
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004303#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004305 p = skiptowhite(arg);
4306 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 xp->xp_context = EXPAND_LANGUAGE;
4309 xp->xp_pattern = arg;
4310 }
4311 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004312 {
4313 if ( STRNCMP(arg, "messages", p - arg) == 0
4314 || STRNCMP(arg, "ctype", p - arg) == 0
4315 || STRNCMP(arg, "time", p - arg) == 0)
4316 {
4317 xp->xp_context = EXPAND_LOCALES;
4318 xp->xp_pattern = skipwhite(p);
4319 }
4320 else
4321 xp->xp_context = EXPAND_NOTHING;
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 break;
4324#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004325#if defined(FEAT_PROFILE)
4326 case CMD_profile:
4327 set_context_in_profile_cmd(xp, arg);
4328 break;
4329#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004330 case CMD_behave:
4331 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004332 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004335 case CMD_messages:
4336 xp->xp_context = EXPAND_MESSAGES;
4337 xp->xp_pattern = arg;
4338 break;
4339
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004340#if defined(FEAT_CMDHIST)
4341 case CMD_history:
4342 xp->xp_context = EXPAND_HISTORY;
4343 xp->xp_pattern = arg;
4344 break;
4345#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004346#if defined(FEAT_PROFILE)
4347 case CMD_syntime:
4348 xp->xp_context = EXPAND_SYNTIME;
4349 xp->xp_pattern = arg;
4350 break;
4351#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004352
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004353 case CMD_argdelete:
4354 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4355 arg = xp->xp_pattern + 1;
4356 xp->xp_context = EXPAND_ARGLIST;
4357 xp->xp_pattern = arg;
4358 break;
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#endif /* FEAT_CMDL_COMPL */
4361
4362 default:
4363 break;
4364 }
4365 return NULL;
4366}
4367
4368/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004369 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * Backslashed delimiters after / or ? will be skipped, and commands will
4372 * not be expanded between /'s and ?'s or after "'".
4373 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004374 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 * Returns the "cmd" pointer advanced to beyond the range.
4376 */
4377 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004378skip_range(
4379 char_u *cmd,
4380 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004382 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004384 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004386 if (*cmd == '\\')
4387 {
4388 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4389 ++cmd;
4390 else
4391 break;
4392 }
4393 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 if (*++cmd == NUL && ctx != NULL)
4396 *ctx = EXPAND_NOTHING;
4397 }
4398 else if (*cmd == '/' || *cmd == '?')
4399 {
4400 delim = *cmd++;
4401 while (*cmd != NUL && *cmd != delim)
4402 if (*cmd++ == '\\' && *cmd != NUL)
4403 ++cmd;
4404 if (*cmd == NUL && ctx != NULL)
4405 *ctx = EXPAND_NOTHING;
4406 }
4407 if (*cmd != NUL)
4408 ++cmd;
4409 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004410
4411 /* Skip ":" and white space. */
4412 while (*cmd == ':')
4413 cmd = skipwhite(cmd + 1);
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return cmd;
4416}
4417
4418/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004419 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 *
4421 * Set ptr to the next character after the part that was interpreted.
4422 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004423 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 *
4425 * Return MAXLNUM when no Ex address was found.
4426 */
4427 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004428get_address(
4429 exarg_T *eap UNUSED,
4430 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004431 int addr_type, // flag: one of ADDR_LINES, ...
4432 int skip, // only skip the address, don't use it
4433 int silent, // no errors or side effects
4434 int to_other_file, // flag: may jump to other file
4435 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436{
4437 int c;
4438 int i;
4439 long n;
4440 char_u *cmd;
4441 pos_T pos;
4442 pos_T *fp;
4443 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004444 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 cmd = skipwhite(*ptr);
4447 lnum = MAXLNUM;
4448 do
4449 {
4450 switch (*cmd)
4451 {
4452 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 ++cmd;
4454 switch (addr_type)
4455 {
4456 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 lnum = curwin->w_cursor.lnum;
4458 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004460 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 break;
4462 case ADDR_ARGUMENTS:
4463 lnum = curwin->w_arg_idx + 1;
4464 break;
4465 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004466 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 lnum = curbuf->b_fnum;
4468 break;
4469 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004470 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004472 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004474 cmd = NULL;
4475 goto error;
4476 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004477#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004478 case ADDR_QUICKFIX:
4479 lnum = qf_get_cur_valid_idx(eap);
4480 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004481#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 }
4483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 ++cmd;
4487 switch (addr_type)
4488 {
4489 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 lnum = curbuf->b_ml.ml_line_count;
4491 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004493 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 break;
4495 case ADDR_ARGUMENTS:
4496 lnum = ARGCOUNT;
4497 break;
4498 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004499 buf = lastbuf;
4500 while (buf->b_ml.ml_mfp == NULL)
4501 {
4502 if (buf->b_prev == NULL)
4503 break;
4504 buf = buf->b_prev;
4505 }
4506 lnum = buf->b_fnum;
4507 break;
4508 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 lnum = lastbuf->b_fnum;
4510 break;
4511 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004512 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004514 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004515 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004516 cmd = NULL;
4517 goto error;
4518 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004519#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004520 case ADDR_QUICKFIX:
4521 lnum = qf_get_size(eap);
4522 if (lnum == 0)
4523 lnum = 1;
4524 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004525#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 }
4527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 if (*++cmd == NUL)
4531 {
4532 cmd = NULL;
4533 goto error;
4534 }
4535 if (addr_type != ADDR_LINES)
4536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004537 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004538 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 goto error;
4540 }
4541 if (skip)
4542 ++cmd;
4543 else
4544 {
4545 /* Only accept a mark in another file when it is
4546 * used by itself: ":'M". */
4547 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4548 ++cmd;
4549 if (fp == (pos_T *)-1)
4550 /* Jumped to another file. */
4551 lnum = curwin->w_cursor.lnum;
4552 else
4553 {
4554 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
4556 cmd = NULL;
4557 goto error;
4558 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 lnum = fp->lnum;
4560 }
4561 }
4562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 case '/':
4565 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 c = *cmd++;
4567 if (addr_type != ADDR_LINES)
4568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004569 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004570 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 goto error;
4572 }
4573 if (skip) /* skip "/pat/" */
4574 {
4575 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4576 if (*cmd == c)
4577 ++cmd;
4578 }
4579 else
4580 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004581 int flags;
4582
4583 pos = curwin->w_cursor; // save curwin->w_cursor
4584
4585 // When '/' or '?' follows another address, start from
4586 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 if (lnum != MAXLNUM)
4588 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004589
4590 // Start a forward search at the end of the line (unless
4591 // before the first line).
4592 // Start a backward search at the start of the line.
4593 // This makes sure we never match in the current
4594 // line, and can match anywhere in the
4595 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004596 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 curwin->w_cursor.col = MAXCOL;
4598 else
4599 curwin->w_cursor.col = 0;
4600 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004601 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4602 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 {
4604 curwin->w_cursor = pos;
4605 cmd = NULL;
4606 goto error;
4607 }
4608 lnum = curwin->w_cursor.lnum;
4609 curwin->w_cursor = pos;
4610 /* adjust command string pointer */
4611 cmd += searchcmdlen;
4612 }
4613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 ++cmd;
4617 if (addr_type != ADDR_LINES)
4618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004619 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004620 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 goto error;
4622 }
4623 if (*cmd == '&')
4624 i = RE_SUBST;
4625 else if (*cmd == '?' || *cmd == '/')
4626 i = RE_SEARCH;
4627 else
4628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004629 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 cmd = NULL;
4631 goto error;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 if (!skip)
4635 {
4636 /*
4637 * When search follows another address, start from
4638 * there.
4639 */
4640 if (lnum != MAXLNUM)
4641 pos.lnum = lnum;
4642 else
4643 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004645 /*
4646 * Start the search just like for the above
4647 * do_search().
4648 */
4649 if (*cmd != '?')
4650 pos.col = MAXCOL;
4651 else
4652 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004653 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004654 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004655 *cmd == '?' ? BACKWARD : FORWARD,
4656 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004657 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004658 lnum = pos.lnum;
4659 else
4660 {
4661 cmd = NULL;
4662 goto error;
4663 }
4664 }
4665 ++cmd;
4666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004669 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4670 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672
4673 for (;;)
4674 {
4675 cmd = skipwhite(cmd);
4676 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4677 break;
4678
4679 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004680 {
4681 switch (addr_type)
4682 {
4683 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004684 /* "+1" is same as ".+1" */
4685 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004686 break;
4687 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004688 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004689 break;
4690 case ADDR_ARGUMENTS:
4691 lnum = curwin->w_arg_idx + 1;
4692 break;
4693 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004694 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004695 lnum = curbuf->b_fnum;
4696 break;
4697 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004698 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004699 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004700 case ADDR_TABS_RELATIVE:
4701 lnum = 1;
4702 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004703#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004704 case ADDR_QUICKFIX:
4705 lnum = qf_get_cur_valid_idx(eap);
4706 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004707#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004708 }
4709 }
4710
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (VIM_ISDIGIT(*cmd))
4712 i = '+'; /* "number" is same as "+number" */
4713 else
4714 i = *cmd++;
4715 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4716 n = 1;
4717 else
4718 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004719
4720 if (addr_type == ADDR_TABS_RELATIVE)
4721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004722 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004723 cmd = NULL;
4724 goto error;
4725 }
4726 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004727 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004728 lnum = compute_buffer_local_count(
4729 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004731 {
4732#ifdef FEAT_FOLDING
4733 /* Relative line addressing, need to adjust for folded lines
4734 * now, but only do it after the first address. */
4735 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4736 && address_count >= 2)
4737 (void)hasFolding(lnum, NULL, &lnum);
4738#endif
4739 if (i == '-')
4740 lnum -= n;
4741 else
4742 lnum += n;
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 } while (*cmd == '/' || *cmd == '?');
4746
4747error:
4748 *ptr = cmd;
4749 return lnum;
4750}
4751
4752/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004753 * Get flags from an Ex command argument.
4754 */
4755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004757{
4758 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4759 {
4760 if (*eap->arg == 'l')
4761 eap->flags |= EXFLAG_LIST;
4762 else if (*eap->arg == 'p')
4763 eap->flags |= EXFLAG_PRINT;
4764 else
4765 eap->flags |= EXFLAG_NR;
4766 eap->arg = skipwhite(eap->arg + 1);
4767 }
4768}
4769
4770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 * Function called for command which is Not Implemented. NI!
4772 */
4773 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004777 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778}
4779
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004780#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781/*
4782 * Function called for script command which is Not Implemented. NI!
4783 * Skips over ":perl <<EOF" constructs.
4784 */
4785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
4788 if (!eap->skip)
4789 ex_ni(eap);
4790 else
4791 vim_free(script_get(eap, eap->arg));
4792}
4793#endif
4794
4795/*
4796 * Check range in Ex command for validity.
4797 * Return NULL when valid, error message when invalid.
4798 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004799 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004800invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004802 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if ( eap->line1 < 0
4804 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004805 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004806 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004807
4808 if (eap->argt & RANGE)
4809 {
4810 switch(eap->addr_type)
4811 {
4812 case ADDR_LINES:
4813 if (!(eap->argt & NOTADR)
4814 && eap->line2 > curbuf->b_ml.ml_line_count
4815#ifdef FEAT_DIFF
4816 + (eap->cmdidx == CMD_diffget)
4817#endif
4818 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004820 break;
4821 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004822 /* add 1 if ARGCOUNT is 0 */
4823 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004824 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 break;
4826 case ADDR_BUFFERS:
4827 if (eap->line1 < firstbuf->b_fnum
4828 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004829 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830 break;
4831 case ADDR_LOADED_BUFFERS:
4832 buf = firstbuf;
4833 while (buf->b_ml.ml_mfp == NULL)
4834 {
4835 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837 buf = buf->b_next;
4838 }
4839 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004840 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004841 buf = lastbuf;
4842 while (buf->b_ml.ml_mfp == NULL)
4843 {
4844 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004845 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004846 buf = buf->b_prev;
4847 }
4848 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004849 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004850 break;
4851 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004852 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004853 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004854 break;
4855 case ADDR_TABS:
4856 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004857 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004859 case ADDR_TABS_RELATIVE:
4860 /* Do nothing */
4861 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004862#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004863 case ADDR_QUICKFIX:
4864 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004865 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004866 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004867#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004868 }
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 return NULL;
4871}
4872
4873/*
4874 * Correct the range for zero line number, if required.
4875 */
4876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004877correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
4879 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4880 {
4881 if (eap->line1 == 0)
4882 eap->line1 = 1;
4883 if (eap->line2 == 0)
4884 eap->line2 = 1;
4885 }
4886}
4887
Bram Moolenaar748bf032005-02-02 23:04:36 +00004888#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004889/*
4890 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4891 * pattern. Otherwise return eap->arg.
4892 */
4893 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004894skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004895{
4896 char_u *p = eap->arg;
4897
Bram Moolenaara37420f2006-02-04 22:37:47 +00004898 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4899 || eap->cmdidx == CMD_vimgrepadd
4900 || eap->cmdidx == CMD_lvimgrepadd
4901 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004902 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004903 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004904 if (p == NULL)
4905 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004906 }
4907 return p;
4908}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004909
4910/*
4911 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4912 * in the command line, so that things like % get expanded.
4913 */
4914 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004916{
4917 char_u *new_cmdline;
4918 char_u *program;
4919 char_u *pos;
4920 char_u *ptr;
4921 int len;
4922 int i;
4923
4924 /*
4925 * Don't do it when ":vimgrep" is used for ":grep".
4926 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004927 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4928 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4929 || eap->cmdidx == CMD_grepadd
4930 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004931 && !grep_internal(eap->cmdidx))
4932 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004933 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4934 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004935 {
4936 if (*curbuf->b_p_gp == NUL)
4937 program = p_gp;
4938 else
4939 program = curbuf->b_p_gp;
4940 }
4941 else
4942 {
4943 if (*curbuf->b_p_mp == NUL)
4944 program = p_mp;
4945 else
4946 program = curbuf->b_p_mp;
4947 }
4948
4949 p = skipwhite(p);
4950
4951 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4952 {
4953 /* replace $* by given arguments */
4954 i = 1;
4955 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4956 ++i;
4957 len = (int)STRLEN(p);
4958 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4959 if (new_cmdline == NULL)
4960 return NULL; /* out of memory */
4961 ptr = new_cmdline;
4962 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4963 {
4964 i = (int)(pos - program);
4965 STRNCPY(ptr, program, i);
4966 STRCPY(ptr += i, p);
4967 ptr += len;
4968 program = pos + 2;
4969 }
4970 STRCPY(ptr, program);
4971 }
4972 else
4973 {
4974 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4975 if (new_cmdline == NULL)
4976 return NULL; /* out of memory */
4977 STRCPY(new_cmdline, program);
4978 STRCAT(new_cmdline, " ");
4979 STRCAT(new_cmdline, p);
4980 }
4981 msg_make(p);
4982
4983 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4984 vim_free(*cmdlinep);
4985 *cmdlinep = new_cmdline;
4986 p = new_cmdline;
4987 }
4988 return p;
4989}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004990#endif
4991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992/*
4993 * Expand file name in Ex command argument.
4994 * Return FAIL for failure, OK otherwise.
4995 */
4996 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997expand_filename(
4998 exarg_T *eap,
4999 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005000 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 int has_wildcards; /* need to expand wildcards */
5003 char_u *repl;
5004 int srclen;
5005 char_u *p;
5006 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005007 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
Bram Moolenaar748bf032005-02-02 23:04:36 +00005009#ifdef FEAT_QUICKFIX
5010 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5011 p = skip_grep_pat(eap);
5012#else
5013 p = eap->arg;
5014#endif
5015
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 /*
5017 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5018 * the file name contains a wildcard it should not cause expanding.
5019 * (it will be expanded anyway if there is a wildcard before replacing).
5020 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005021 has_wildcards = mch_has_wildcard(p);
5022 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005024#ifdef FEAT_EVAL
5025 /* Skip over `=expr`, wildcards in it are not expanded. */
5026 if (p[0] == '`' && p[1] == '=')
5027 {
5028 p += 2;
5029 (void)skip_expr(&p);
5030 if (*p == '`')
5031 ++p;
5032 continue;
5033 }
5034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /*
5036 * Quick check if this cannot be the start of a special string.
5037 * Also removes backslash before '%', '#' and '<'.
5038 */
5039 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5040 {
5041 ++p;
5042 continue;
5043 }
5044
5045 /*
5046 * Try to find a match at this position.
5047 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005048 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5049 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (*errormsgp != NULL) /* error detected */
5051 return FAIL;
5052 if (repl == NULL) /* no match found */
5053 {
5054 p += srclen;
5055 continue;
5056 }
5057
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005058 /* Wildcards won't be expanded below, the replacement is taken
5059 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5060 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5061 {
5062 char_u *l = repl;
5063
5064 repl = expand_env_save(repl);
5065 vim_free(l);
5066 }
5067
Bram Moolenaar63b92542007-03-27 14:57:09 +00005068 /* Need to escape white space et al. with a backslash.
5069 * Don't do this for:
5070 * - replacement that already has been escaped: "##"
5071 * - shell commands (may have to use quotes instead).
5072 * - non-unix systems when there is a single argument (spaces don't
5073 * separate arguments then).
5074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005076 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 && eap->cmdidx != CMD_grep
5079 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005080 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005081 && eap->cmdidx != CMD_lgrep
5082 && eap->cmdidx != CMD_lgrepadd
5083 && eap->cmdidx != CMD_lmake
5084 && eap->cmdidx != CMD_make
5085 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#ifndef UNIX
5087 && !(eap->argt & NOSPC)
5088#endif
5089 )
5090 {
5091 char_u *l;
5092#ifdef BACKSLASH_IN_FILENAME
5093 /* Don't escape a backslash here, because rem_backslash() doesn't
5094 * remove it later. */
5095 static char_u *nobslash = (char_u *)" \t\"|";
5096# define ESCAPE_CHARS nobslash
5097#else
5098# define ESCAPE_CHARS escape_chars
5099#endif
5100
5101 for (l = repl; *l; ++l)
5102 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5103 {
5104 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5105 if (l != NULL)
5106 {
5107 vim_free(repl);
5108 repl = l;
5109 }
5110 break;
5111 }
5112 }
5113
5114 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005115 if ((eap->usefilter || eap->cmdidx == CMD_bang
5116 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005117 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 char_u *l;
5120
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005121 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (l != NULL)
5123 {
5124 vim_free(repl);
5125 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 }
5128
5129 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5130 vim_free(repl);
5131 if (p == NULL)
5132 return FAIL;
5133 }
5134
5135 /*
5136 * One file argument: Expand wildcards.
5137 * Don't do this with ":r !command" or ":w !command".
5138 */
5139 if ((eap->argt & NOSPC) && !eap->usefilter)
5140 {
5141 /*
5142 * May do this twice:
5143 * 1. Replace environment variables.
5144 * 2. Replace any other wildcards, remove backslashes.
5145 */
5146 for (n = 1; n <= 2; ++n)
5147 {
5148 if (n == 2)
5149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 /*
5151 * Halve the number of backslashes (this is Vi compatible).
5152 * For Unix and OS/2, when wildcards are expanded, this is
5153 * done by ExpandOne() below.
5154 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005155#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (!has_wildcards)
5157#endif
5158 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160
5161 if (has_wildcards)
5162 {
5163 if (n == 1)
5164 {
5165 /*
5166 * First loop: May expand environment variables. This
5167 * can be done much faster with expand_env() than with
5168 * something else (e.g., calling a shell).
5169 * After expanding environment variables, check again
5170 * if there are still wildcards present.
5171 */
5172 if (vim_strchr(eap->arg, '$') != NULL
5173 || vim_strchr(eap->arg, '~') != NULL)
5174 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005175 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005176 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 has_wildcards = mch_has_wildcard(NameBuff);
5178 p = NameBuff;
5179 }
5180 else
5181 p = NULL;
5182 }
5183 else /* n == 2 */
5184 {
5185 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005186 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188 ExpandInit(&xpc);
5189 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005190 if (p_wic)
5191 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005193 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (p == NULL)
5195 return FAIL;
5196 }
5197 if (p != NULL)
5198 {
5199 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5200 p, cmdlinep);
5201 if (n == 2) /* p came from ExpandOne() */
5202 vim_free(p);
5203 }
5204 }
5205 }
5206 }
5207 return OK;
5208}
5209
5210/*
5211 * Replace part of the command line, keeping eap->cmd, eap->arg and
5212 * eap->nextcmd correct.
5213 * "src" points to the part that is to be replaced, of length "srclen".
5214 * "repl" is the replacement string.
5215 * Returns a pointer to the character after the replaced string.
5216 * Returns NULL for failure.
5217 */
5218 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005219repl_cmdline(
5220 exarg_T *eap,
5221 char_u *src,
5222 int srclen,
5223 char_u *repl,
5224 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 int len;
5227 int i;
5228 char_u *new_cmdline;
5229
5230 /*
5231 * The new command line is build in new_cmdline[].
5232 * First allocate it.
5233 * Careful: a "+cmd" argument may have been NUL terminated.
5234 */
5235 len = (int)STRLEN(repl);
5236 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005237 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5239 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5240 return NULL; /* out of memory! */
5241
5242 /*
5243 * Copy the stuff before the expanded part.
5244 * Copy the expanded stuff.
5245 * Copy what came after the expanded part.
5246 * Copy the next commands, if there are any.
5247 */
5248 i = (int)(src - *cmdlinep); /* length of part before match */
5249 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 mch_memmove(new_cmdline + i, repl, (size_t)len);
5252 i += len; /* remember the end of the string */
5253 STRCPY(new_cmdline + i, src + srclen);
5254 src = new_cmdline + i; /* remember where to continue */
5255
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005256 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 i = (int)STRLEN(new_cmdline) + 1;
5259 STRCPY(new_cmdline + i, eap->nextcmd);
5260 eap->nextcmd = new_cmdline + i;
5261 }
5262 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5263 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5264 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5265 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5266 vim_free(*cmdlinep);
5267 *cmdlinep = new_cmdline;
5268
5269 return src;
5270}
5271
5272/*
5273 * Check for '|' to separate commands and '"' to start comments.
5274 */
5275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 char_u *p;
5279
Bram Moolenaar86b68352004-12-27 21:59:20 +00005280#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005281 p = skip_grep_pat(eap);
5282#else
5283 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005284#endif
5285
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005286 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
5288 if (*p == Ctrl_V)
5289 {
5290 if (eap->argt & (USECTRLV | XFILE))
5291 ++p; /* skip CTRL-V and next char */
5292 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005293 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005294 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (*p == NUL) /* stop at NUL after CTRL-V */
5296 break;
5297 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005298
5299#ifdef FEAT_EVAL
5300 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005301 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005302 {
5303 p += 2;
5304 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005305 }
5306#endif
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 /* Check for '"': start of comment or '|': next command */
5309 /* :@" and :*" do not start a comment!
5310 * :redir @" doesn't either. */
5311 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5312 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5313 || p != eap->arg)
5314 && (eap->cmdidx != CMD_redir
5315 || p != eap->arg + 1 || p[-1] != '@'))
5316 || *p == '|' || *p == '\n')
5317 {
5318 /*
5319 * We remove the '\' before the '|', unless USECTRLV is used
5320 * AND 'b' is present in 'cpoptions'.
5321 */
5322 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5323 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5324 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005325 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 --p;
5327 }
5328 else
5329 {
5330 eap->nextcmd = check_nextcmd(p);
5331 *p = NUL;
5332 break;
5333 }
5334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5338 del_trailing_spaces(eap->arg);
5339}
5340
5341/*
5342 * get + command from ex argument
5343 */
5344 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 char_u *arg = *argp;
5348 char_u *command = NULL;
5349
5350 if (*arg == '+') /* +[command] */
5351 {
5352 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005353 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 command = dollar_command;
5355 else
5356 {
5357 command = arg;
5358 arg = skip_cmd_arg(command, TRUE);
5359 if (*arg != NUL)
5360 *arg++ = NUL; /* terminate command with NUL */
5361 }
5362
5363 arg = skipwhite(arg); /* skip over spaces */
5364 *argp = arg;
5365 }
5366 return command;
5367}
5368
5369/*
5370 * Find end of "+command" argument. Skip over "\ " and "\\".
5371 */
5372 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373skip_cmd_arg(
5374 char_u *p,
5375 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 while (*p && !vim_isspace(*p))
5378 {
5379 if (*p == '\\' && p[1] != NUL)
5380 {
5381 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005382 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else
5384 ++p;
5385 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005386 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 return p;
5389}
5390
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005391 int
5392get_bad_opt(char_u *p, exarg_T *eap)
5393{
5394 if (STRICMP(p, "keep") == 0)
5395 eap->bad_char = BAD_KEEP;
5396 else if (STRICMP(p, "drop") == 0)
5397 eap->bad_char = BAD_DROP;
5398 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5399 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005400 else
5401 return FAIL;
5402 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005403}
5404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405/*
5406 * Get "++opt=arg" argument.
5407 * Return FAIL or OK.
5408 */
5409 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 char_u *arg = eap->arg + 2;
5413 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005414 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 /* ":edit ++[no]bin[ary] file" */
5418 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5419 {
5420 if (*arg == 'n')
5421 {
5422 arg += 2;
5423 eap->force_bin = FORCE_NOBIN;
5424 }
5425 else
5426 eap->force_bin = FORCE_BIN;
5427 if (!checkforcmd(&arg, "binary", 3))
5428 return FAIL;
5429 eap->arg = skipwhite(arg);
5430 return OK;
5431 }
5432
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005433 /* ":read ++edit file" */
5434 if (STRNCMP(arg, "edit", 4) == 0)
5435 {
5436 eap->read_edit = TRUE;
5437 eap->arg = skipwhite(arg + 4);
5438 return OK;
5439 }
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (STRNCMP(arg, "ff", 2) == 0)
5442 {
5443 arg += 2;
5444 pp = &eap->force_ff;
5445 }
5446 else if (STRNCMP(arg, "fileformat", 10) == 0)
5447 {
5448 arg += 10;
5449 pp = &eap->force_ff;
5450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 else if (STRNCMP(arg, "enc", 3) == 0)
5452 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005453 if (STRNCMP(arg, "encoding", 8) == 0)
5454 arg += 8;
5455 else
5456 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 pp = &eap->force_enc;
5458 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005459 else if (STRNCMP(arg, "bad", 3) == 0)
5460 {
5461 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005462 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 if (pp == NULL || *arg != '=')
5466 return FAIL;
5467
5468 ++arg;
5469 *pp = (int)(arg - eap->cmd);
5470 arg = skip_cmd_arg(arg, FALSE);
5471 eap->arg = skipwhite(arg);
5472 *arg = NUL;
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (pp == &eap->force_ff)
5475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5477 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005478 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005480 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 /* Make 'fileencoding' lower case. */
5483 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5484 *p = TOLOWER_ASC(*p);
5485 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005486 else
5487 {
5488 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5489 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005490 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005491 return FAIL;
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
5494 return OK;
5495}
5496
5497/*
5498 * ":abbreviate" and friends.
5499 */
5500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005501ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 do_exmap(eap, TRUE); /* almost the same as mapping */
5504}
5505
5506/*
5507 * ":map" and friends.
5508 */
5509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 /*
5513 * If we are sourcing .exrc or .vimrc in current directory we
5514 * print the mappings for security reasons.
5515 */
5516 if (secure)
5517 {
5518 secure = 2;
5519 msg_outtrans(eap->cmd);
5520 msg_putchar('\n');
5521 }
5522 do_exmap(eap, FALSE);
5523}
5524
5525/*
5526 * ":unmap" and friends.
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 do_exmap(eap, FALSE);
5532}
5533
5534/*
5535 * ":mapclear" and friends.
5536 */
5537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005538ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5541}
5542
5543/*
5544 * ":abclear" and friends.
5545 */
5546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
5549 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5550}
5551
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005556 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 * directory for security reasons.
5558 */
5559 if (secure)
5560 {
5561 secure = 2;
5562 eap->errmsg = e_curdir;
5563 }
5564 else if (eap->cmdidx == CMD_autocmd)
5565 do_autocmd(eap->arg, eap->forceit);
5566 else
5567 do_augroup(eap->arg, eap->forceit);
5568}
5569
5570/*
5571 * ":doautocmd": Apply the automatic commands to the current buffer.
5572 */
5573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005574ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005576 char_u *arg = eap->arg;
5577 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005578 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005579
Bram Moolenaar1610d052016-06-09 22:53:01 +02005580 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5581 /* Only when there is no <nomodeline>. */
5582 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005583 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586/*
5587 * :[N]bunload[!] [N] [bufname] unload buffer
5588 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5589 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5590 */
5591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 eap->errmsg = do_bufdel(
5595 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5596 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5597 : DOBUF_UNLOAD, eap->arg,
5598 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5599}
5600
5601/*
5602 * :[N]buffer [N] to buffer N
5603 * :[N]sbuffer [N] to buffer N
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 if (*eap->arg)
5609 eap->errmsg = e_trailing;
5610 else
5611 {
5612 if (eap->addr_count == 0) /* default is current buffer */
5613 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5614 else
5615 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005616 if (eap->do_ecmd_cmd != NULL)
5617 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619}
5620
5621/*
5622 * :[N]bmodified [N] to next mod. buffer
5623 * :[N]sbmodified [N] to next mod. buffer
5624 */
5625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005629 if (eap->do_ecmd_cmd != NULL)
5630 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631}
5632
5633/*
5634 * :[N]bnext [N] to next buffer
5635 * :[N]sbnext [N] split and to next buffer
5636 */
5637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005641 if (eap->do_ecmd_cmd != NULL)
5642 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
5646 * :[N]bNext [N] to previous buffer
5647 * :[N]bprevious [N] to previous buffer
5648 * :[N]sbNext [N] split and to previous buffer
5649 * :[N]sbprevious [N] split and to previous buffer
5650 */
5651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005655 if (eap->do_ecmd_cmd != NULL)
5656 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}
5658
5659/*
5660 * :brewind to first buffer
5661 * :bfirst to first buffer
5662 * :sbrewind split and to first buffer
5663 * :sbfirst split and to first buffer
5664 */
5665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005669 if (eap->do_ecmd_cmd != NULL)
5670 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671}
5672
5673/*
5674 * :blast to last buffer
5675 * :sblast split and to last buffer
5676 */
5677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005681 if (eap->do_ecmd_cmd != NULL)
5682 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
5685 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005686ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 return (c == NUL || c == '|' || c == '"' || c == '\n');
5689}
5690
5691#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5692 || defined(PROTO)
5693/*
5694 * Return the next command, after the first '|' or '\n'.
5695 * Return NULL if not found.
5696 */
5697 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 while (*p != '|' && *p != '\n')
5701 {
5702 if (*p == NUL)
5703 return NULL;
5704 ++p;
5705 }
5706 return (p + 1);
5707}
5708#endif
5709
5710/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005711 * Check if *p is a separator between Ex commands, skipping over white space.
5712 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 */
5714 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005717 char_u *s = skipwhite(p);
5718
5719 if (*s == '|' || *s == '\n')
5720 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 else
5722 return NULL;
5723}
5724
5725/*
5726 * - if there are more files to edit
5727 * - and this is the last window
5728 * - and forceit not used
5729 * - and not repeated twice on a row
5730 * return FAIL and give error message if 'message' TRUE
5731 * return OK otherwise
5732 */
5733 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005734check_more(
5735 int message, /* when FALSE check only, no messages */
5736 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5739
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005740 if (!forceit && only_one_window()
5741 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
5743 if (message)
5744 {
5745#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5746 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5747 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005748 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005750 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5751 NGETTEXT("%d more file to edit. Quit anyway?",
5752 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5754 return OK;
5755 return FAIL;
5756 }
5757#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005758 semsg(NGETTEXT("E173: %d more file to edit",
5759 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 quitmore = 2; /* next try to quit is allowed */
5761 }
5762 return FAIL;
5763 }
5764 return OK;
5765}
5766
5767#ifdef FEAT_CMDL_COMPL
5768/*
5769 * Function given to ExpandGeneric() to obtain the list of command names.
5770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005772get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 if (idx >= (int)CMD_SIZE)
5775# ifdef FEAT_USR_CMDS
5776 return get_user_command_name(idx);
5777# else
5778 return NULL;
5779# endif
5780 return cmdnames[idx].cmd_name;
5781}
5782#endif
5783
5784#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005786uc_add_command(
5787 char_u *name,
5788 size_t name_len,
5789 char_u *rep,
5790 long argt,
5791 long def,
5792 int flags,
5793 int compl,
5794 char_u *compl_arg,
5795 int addr_type,
5796 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 ucmd_T *cmd = NULL;
5799 char_u *p;
5800 int i;
5801 int cmp = 1;
5802 char_u *rep_buf = NULL;
5803 garray_T *gap;
5804
Bram Moolenaar9c102382006-05-03 21:26:49 +00005805 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (rep_buf == NULL)
5807 {
5808 /* Can't replace termcodes - try using the string as is */
5809 rep_buf = vim_strsave(rep);
5810
5811 /* Give up if out of memory */
5812 if (rep_buf == NULL)
5813 return FAIL;
5814 }
5815
5816 /* get address of growarray: global or in curbuf */
5817 if (flags & UC_BUFFER)
5818 {
5819 gap = &curbuf->b_ucmds;
5820 if (gap->ga_itemsize == 0)
5821 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5822 }
5823 else
5824 gap = &ucmds;
5825
5826 /* Search for the command in the already defined commands. */
5827 for (i = 0; i < gap->ga_len; ++i)
5828 {
5829 size_t len;
5830
5831 cmd = USER_CMD_GA(gap, i);
5832 len = STRLEN(cmd->uc_name);
5833 cmp = STRNCMP(name, cmd->uc_name, name_len);
5834 if (cmp == 0)
5835 {
5836 if (name_len < len)
5837 cmp = -1;
5838 else if (name_len > len)
5839 cmp = 1;
5840 }
5841
5842 if (cmp == 0)
5843 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005844 // Command can be replaced with "command!" and when sourcing the
5845 // same script again, but only once.
5846 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5847 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005849 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005850 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 goto fail;
5852 }
5853
Bram Moolenaard23a8232018-02-10 18:45:26 +01005854 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005855#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005856 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 break;
5859 }
5860
5861 /* Stop as soon as we pass the name to add */
5862 if (cmp < 0)
5863 break;
5864 }
5865
5866 /* Extend the array unless we're replacing an existing command */
5867 if (cmp != 0)
5868 {
5869 if (ga_grow(gap, 1) != OK)
5870 goto fail;
5871 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5872 goto fail;
5873
5874 cmd = USER_CMD_GA(gap, i);
5875 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5876
5877 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879 cmd->uc_name = p;
5880 }
5881
5882 cmd->uc_rep = rep_buf;
5883 cmd->uc_argt = argt;
5884 cmd->uc_def = def;
5885 cmd->uc_compl = compl;
5886#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005887 cmd->uc_script_ctx = current_sctx;
5888 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889# ifdef FEAT_CMDL_COMPL
5890 cmd->uc_compl_arg = compl_arg;
5891# endif
5892#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005893 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
5895 return OK;
5896
5897fail:
5898 vim_free(rep_buf);
5899#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5900 vim_free(compl_arg);
5901#endif
5902 return FAIL;
5903}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005906#if defined(FEAT_USR_CMDS)
5907static struct
5908{
5909 int expand;
5910 char *name;
5911} addr_type_complete[] =
5912{
5913 {ADDR_ARGUMENTS, "arguments"},
5914 {ADDR_LINES, "lines"},
5915 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5916 {ADDR_TABS, "tabs"},
5917 {ADDR_BUFFERS, "buffers"},
5918 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005919 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005920 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005921 {-1, NULL}
5922};
5923#endif
5924
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005925#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926/*
5927 * List of names for completion for ":command" with the EXPAND_ flag.
5928 * Must be alphabetical for completion.
5929 */
5930static struct
5931{
5932 int expand;
5933 char *name;
5934} command_complete[] =
5935{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005936 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005938 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005940 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005942 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005943#if defined(FEAT_CSCOPE)
5944 {EXPAND_CSCOPE, "cscope"},
5945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5947 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005948 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949#endif
5950 {EXPAND_DIRECTORIES, "dir"},
5951 {EXPAND_ENV_VARS, "environment"},
5952 {EXPAND_EVENTS, "event"},
5953 {EXPAND_EXPRESSION, "expression"},
5954 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005955 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005956 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {EXPAND_FUNCTIONS, "function"},
5958 {EXPAND_HELP, "help"},
5959 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005960#if defined(FEAT_CMDHIST)
5961 {EXPAND_HISTORY, "history"},
5962#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005963#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005964 {EXPAND_LOCALES, "locale"},
5965#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005966 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {EXPAND_MAPPINGS, "mapping"},
5968 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005969 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005970 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005971#if defined(FEAT_PROFILE)
5972 {EXPAND_SYNTIME, "syntime"},
5973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005975 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005976 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005977#if defined(FEAT_SIGNS)
5978 {EXPAND_SIGN, "sign"},
5979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {EXPAND_TAGS, "tag"},
5981 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005982 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {EXPAND_USER_VARS, "var"},
5984 {0, NULL}
5985};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005988#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005990uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 int i, j;
5993 int found = FALSE;
5994 ucmd_T *cmd;
5995 int len;
5996 long a;
5997 garray_T *gap;
5998
5999 gap = &curbuf->b_ucmds;
6000 for (;;)
6001 {
6002 for (i = 0; i < gap->ga_len; ++i)
6003 {
6004 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006005 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
Bram Moolenaard29459b2016-08-26 22:29:11 +02006007 /* Skip commands which don't match the requested prefix and
6008 * commands filtered out. */
6009 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6010 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 continue;
6012
6013 /* Put out the title first time */
6014 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006015 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 found = TRUE;
6017 msg_putchar('\n');
6018 if (got_int)
6019 break;
6020
6021 /* Special cases */
6022 msg_putchar(a & BANG ? '!' : ' ');
6023 msg_putchar(a & REGSTR ? '"' : ' ');
6024 msg_putchar(gap != &ucmds ? 'b' : ' ');
6025 msg_putchar(' ');
6026
Bram Moolenaar8820b482017-03-16 17:23:31 +01006027 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 len = (int)STRLEN(cmd->uc_name) + 4;
6029
6030 do {
6031 msg_putchar(' ');
6032 ++len;
6033 } while (len < 16);
6034
6035 len = 0;
6036
6037 /* Arguments */
6038 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6039 {
6040 case 0: IObuff[len++] = '0'; break;
6041 case (EXTRA): IObuff[len++] = '*'; break;
6042 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6043 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6044 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6045 }
6046
6047 do {
6048 IObuff[len++] = ' ';
6049 } while (len < 5);
6050
6051 /* Range */
6052 if (a & (RANGE|COUNT))
6053 {
6054 if (a & COUNT)
6055 {
6056 /* -count=N */
6057 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6058 len += (int)STRLEN(IObuff + len);
6059 }
6060 else if (a & DFLALL)
6061 IObuff[len++] = '%';
6062 else if (cmd->uc_def >= 0)
6063 {
6064 /* -range=N */
6065 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6066 len += (int)STRLEN(IObuff + len);
6067 }
6068 else
6069 IObuff[len++] = '.';
6070 }
6071
6072 do {
6073 IObuff[len++] = ' ';
6074 } while (len < 11);
6075
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006076 /* Address Type */
6077 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6078 if (addr_type_complete[j].expand != ADDR_LINES
6079 && addr_type_complete[j].expand == cmd->uc_addr_type)
6080 {
6081 STRCPY(IObuff + len, addr_type_complete[j].name);
6082 len += (int)STRLEN(IObuff + len);
6083 break;
6084 }
6085
6086 do {
6087 IObuff[len++] = ' ';
6088 } while (len < 21);
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 /* Completion */
6091 for (j = 0; command_complete[j].expand != 0; ++j)
6092 if (command_complete[j].expand == cmd->uc_compl)
6093 {
6094 STRCPY(IObuff + len, command_complete[j].name);
6095 len += (int)STRLEN(IObuff + len);
6096 break;
6097 }
6098
6099 do {
6100 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006101 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
6103 IObuff[len] = '\0';
6104 msg_outtrans(IObuff);
6105
6106 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006107#ifdef FEAT_EVAL
6108 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006109 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 out_flush();
6112 ui_breakcheck();
6113 if (got_int)
6114 break;
6115 }
6116 if (gap == &ucmds || i < gap->ga_len)
6117 break;
6118 gap = &ucmds;
6119 }
6120
6121 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006122 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123}
6124
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006125 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006126uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6129 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6130 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6131 0xb9, 0x7f, 0};
6132 int i;
6133
6134 for (i = 0; fcmd[i]; ++i)
6135 IObuff[i] = fcmd[i] - 0x40;
6136 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006137 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138}
6139
6140 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006141uc_scan_attr(
6142 char_u *attr,
6143 size_t len,
6144 long *argt,
6145 long *def,
6146 int *flags,
6147 int *compl,
6148 char_u **compl_arg,
6149 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 char_u *p;
6152
6153 if (len == 0)
6154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006155 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 return FAIL;
6157 }
6158
6159 /* First, try the simple attributes (no arguments) */
6160 if (STRNICMP(attr, "bang", len) == 0)
6161 *argt |= BANG;
6162 else if (STRNICMP(attr, "buffer", len) == 0)
6163 *flags |= UC_BUFFER;
6164 else if (STRNICMP(attr, "register", len) == 0)
6165 *argt |= REGSTR;
6166 else if (STRNICMP(attr, "bar", len) == 0)
6167 *argt |= TRLBAR;
6168 else
6169 {
6170 int i;
6171 char_u *val = NULL;
6172 size_t vallen = 0;
6173 size_t attrlen = len;
6174
6175 /* Look for the attribute name - which is the part before any '=' */
6176 for (i = 0; i < (int)len; ++i)
6177 {
6178 if (attr[i] == '=')
6179 {
6180 val = &attr[i + 1];
6181 vallen = len - i - 1;
6182 attrlen = i;
6183 break;
6184 }
6185 }
6186
6187 if (STRNICMP(attr, "nargs", attrlen) == 0)
6188 {
6189 if (vallen == 1)
6190 {
6191 if (*val == '0')
6192 /* Do nothing - this is the default */;
6193 else if (*val == '1')
6194 *argt |= (EXTRA | NOSPC | NEEDARG);
6195 else if (*val == '*')
6196 *argt |= EXTRA;
6197 else if (*val == '?')
6198 *argt |= (EXTRA | NOSPC);
6199 else if (*val == '+')
6200 *argt |= (EXTRA | NEEDARG);
6201 else
6202 goto wrong_nargs;
6203 }
6204 else
6205 {
6206wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006207 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 return FAIL;
6209 }
6210 }
6211 else if (STRNICMP(attr, "range", attrlen) == 0)
6212 {
6213 *argt |= RANGE;
6214 if (vallen == 1 && *val == '%')
6215 *argt |= DFLALL;
6216 else if (val != NULL)
6217 {
6218 p = val;
6219 if (*def >= 0)
6220 {
6221two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006222 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 return FAIL;
6224 }
6225
6226 *def = getdigits(&p);
6227 *argt |= (ZEROR | NOTADR);
6228
6229 if (p != val + vallen || vallen == 0)
6230 {
6231invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006232 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 return FAIL;
6234 }
6235 }
6236 }
6237 else if (STRNICMP(attr, "count", attrlen) == 0)
6238 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006239 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241 if (val != NULL)
6242 {
6243 p = val;
6244 if (*def >= 0)
6245 goto two_count;
6246
6247 *def = getdigits(&p);
6248
6249 if (p != val + vallen)
6250 goto invalid_count;
6251 }
6252
6253 if (*def < 0)
6254 *def = 0;
6255 }
6256 else if (STRNICMP(attr, "complete", attrlen) == 0)
6257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (val == NULL)
6259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006260 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 return FAIL;
6262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006264 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6265 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006268 else if (STRNICMP(attr, "addr", attrlen) == 0)
6269 {
6270 *argt |= RANGE;
6271 if (val == NULL)
6272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006273 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006274 return FAIL;
6275 }
6276 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6277 == FAIL)
6278 return FAIL;
6279 if (addr_type_arg != ADDR_LINES)
6280 *argt |= (ZEROR | NOTADR) ;
6281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 else
6283 {
6284 char_u ch = attr[len];
6285 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006286 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 attr[len] = ch;
6288 return FAIL;
6289 }
6290 }
6291
6292 return OK;
6293}
6294
Bram Moolenaara850a712009-01-28 14:42:59 +00006295/*
6296 * ":command ..."
6297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006299ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 char_u *name;
6302 char_u *end;
6303 char_u *p;
6304 long argt = 0;
6305 long def = -1;
6306 int flags = 0;
6307 int compl = EXPAND_NOTHING;
6308 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006309 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006311 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 p = eap->arg;
6314
6315 /* Check for attributes */
6316 while (*p == '-')
6317 {
6318 ++p;
6319 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006320 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6321 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 == FAIL)
6323 return;
6324 p = skipwhite(end);
6325 }
6326
6327 /* Get the name (if any) and skip to the following argument */
6328 name = p;
6329 if (ASCII_ISALPHA(*p))
6330 while (ASCII_ISALNUM(*p))
6331 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006332 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006334 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 return;
6336 }
6337 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006338 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
6340 /* If there is nothing after the name, and no attributes were specified,
6341 * we are listing commands
6342 */
6343 p = skipwhite(end);
6344 if (!has_attr && ends_excmd(*p))
6345 {
6346 uc_list(name, end - name);
6347 }
6348 else if (!ASCII_ISUPPER(*name))
6349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006350 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 return;
6352 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006353 else if ((name_len == 1 && *name == 'X')
6354 || (name_len <= 4
6355 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006357 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006358 return;
6359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 else
6361 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006362 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
6365/*
6366 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 * Clear all user commands, global and for current buffer.
6368 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006370ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 uc_clear(&ucmds);
6373 uc_clear(&curbuf->b_ucmds);
6374}
6375
6376/*
6377 * Clear all user commands for "gap".
6378 */
6379 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006380uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 int i;
6383 ucmd_T *cmd;
6384
6385 for (i = 0; i < gap->ga_len; ++i)
6386 {
6387 cmd = USER_CMD_GA(gap, i);
6388 vim_free(cmd->uc_name);
6389 vim_free(cmd->uc_rep);
6390# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6391 vim_free(cmd->uc_compl_arg);
6392# endif
6393 }
6394 ga_clear(gap);
6395}
6396
6397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006398ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
6400 int i = 0;
6401 ucmd_T *cmd = NULL;
6402 int cmp = -1;
6403 garray_T *gap;
6404
6405 gap = &curbuf->b_ucmds;
6406 for (;;)
6407 {
6408 for (i = 0; i < gap->ga_len; ++i)
6409 {
6410 cmd = USER_CMD_GA(gap, i);
6411 cmp = STRCMP(eap->arg, cmd->uc_name);
6412 if (cmp <= 0)
6413 break;
6414 }
6415 if (gap == &ucmds || cmp == 0)
6416 break;
6417 gap = &ucmds;
6418 }
6419
6420 if (cmp != 0)
6421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006422 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 return;
6424 }
6425
6426 vim_free(cmd->uc_name);
6427 vim_free(cmd->uc_rep);
6428# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6429 vim_free(cmd->uc_compl_arg);
6430# endif
6431
6432 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434 if (i < gap->ga_len)
6435 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6436}
6437
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006438/*
6439 * split and quote args for <f-args>
6440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 char_u *buf;
6445 char_u *p;
6446 char_u *q;
6447 int len;
6448
6449 /* Precalculate length */
6450 p = arg;
6451 len = 2; /* Initial and final quotes */
6452
6453 while (*p)
6454 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006455 if (p[0] == '\\' && p[1] == '\\')
6456 {
6457 len += 2;
6458 p += 2;
6459 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006460 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
6462 len += 1;
6463 p += 2;
6464 }
6465 else if (*p == '\\' || *p == '"')
6466 {
6467 len += 2;
6468 p += 1;
6469 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006470 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
6472 p = skipwhite(p);
6473 if (*p == NUL)
6474 break;
6475 len += 3; /* "," */
6476 }
6477 else
6478 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006479 int charlen = (*mb_ptr2len)(p);
Bram Moolenaar13505972019-01-24 15:04:48 +01006480
Bram Moolenaardfef1542012-07-10 19:25:10 +02006481 len += charlen;
6482 p += charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
6484 }
6485
6486 buf = alloc(len + 1);
6487 if (buf == NULL)
6488 {
6489 *lenp = 0;
6490 return buf;
6491 }
6492
6493 p = arg;
6494 q = buf;
6495 *q++ = '"';
6496 while (*p)
6497 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006498 if (p[0] == '\\' && p[1] == '\\')
6499 {
6500 *q++ = '\\';
6501 *q++ = '\\';
6502 p += 2;
6503 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006504 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 *q++ = p[1];
6507 p += 2;
6508 }
6509 else if (*p == '\\' || *p == '"')
6510 {
6511 *q++ = '\\';
6512 *q++ = *p++;
6513 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006514 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 p = skipwhite(p);
6517 if (*p == NUL)
6518 break;
6519 *q++ = '"';
6520 *q++ = ',';
6521 *q++ = '"';
6522 }
6523 else
6524 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006525 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527 }
6528 *q++ = '"';
6529 *q = 0;
6530
6531 *lenp = len;
6532 return buf;
6533}
6534
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006535 static size_t
6536add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6537{
6538 size_t result;
6539
6540 result = STRLEN(mod_str);
6541 if (*multi_mods)
6542 result += 1;
6543 if (buf != NULL)
6544 {
6545 if (*multi_mods)
6546 STRCAT(buf, " ");
6547 STRCAT(buf, mod_str);
6548 }
6549
6550 *multi_mods = 1;
6551
6552 return result;
6553}
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
6556 * Check for a <> code in a user command.
6557 * "code" points to the '<'. "len" the length of the <> (inclusive).
6558 * "buf" is where the result is to be added.
6559 * "split_buf" points to a buffer used for splitting, caller should free it.
6560 * "split_len" is the length of what "split_buf" contains.
6561 * Returns the length of the replacement, which has been added to "buf".
6562 * Returns -1 if there was no match, and only the "<" has been copied.
6563 */
6564 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565uc_check_code(
6566 char_u *code,
6567 size_t len,
6568 char_u *buf,
6569 ucmd_T *cmd, /* the user command we're expanding */
6570 exarg_T *eap, /* ex arguments */
6571 char_u **split_buf,
6572 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 size_t result = 0;
6575 char_u *p = code + 1;
6576 size_t l = len - 2;
6577 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006578 enum {
6579 ct_ARGS,
6580 ct_BANG,
6581 ct_COUNT,
6582 ct_LINE1,
6583 ct_LINE2,
6584 ct_RANGE,
6585 ct_MODS,
6586 ct_REGISTER,
6587 ct_LT,
6588 ct_NONE
6589 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6592 {
6593 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6594 p += 2;
6595 l -= 2;
6596 }
6597
Bram Moolenaar371d5402006-03-20 21:47:49 +00006598 ++l;
6599 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006601 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006603 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006605 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006607 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006609 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006611 else if (STRNICMP(p, "range>", l) == 0)
6612 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006613 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006615 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006617 else if (STRNICMP(p, "mods>", l) == 0)
6618 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 switch (type)
6621 {
6622 case ct_ARGS:
6623 /* Simple case first */
6624 if (*eap->arg == NUL)
6625 {
6626 if (quote == 1)
6627 {
6628 result = 2;
6629 if (buf != NULL)
6630 STRCPY(buf, "''");
6631 }
6632 else
6633 result = 0;
6634 break;
6635 }
6636
6637 /* When specified there is a single argument don't split it.
6638 * Works for ":Cmd %" when % is "a b c". */
6639 if ((eap->argt & NOSPC) && quote == 2)
6640 quote = 1;
6641
6642 switch (quote)
6643 {
6644 case 0: /* No quoting, no splitting */
6645 result = STRLEN(eap->arg);
6646 if (buf != NULL)
6647 STRCPY(buf, eap->arg);
6648 break;
6649 case 1: /* Quote, but don't split */
6650 result = STRLEN(eap->arg) + 2;
6651 for (p = eap->arg; *p; ++p)
6652 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006653 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6654 /* DBCS can contain \ in a trail byte, skip the
6655 * double-byte character. */
6656 ++p;
6657 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006658 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 ++result;
6660 }
6661
6662 if (buf != NULL)
6663 {
6664 *buf++ = '"';
6665 for (p = eap->arg; *p; ++p)
6666 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006667 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6668 /* DBCS can contain \ in a trail byte, copy the
6669 * double-byte character to avoid escaping. */
6670 *buf++ = *p++;
6671 else
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006672 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 *buf++ = '\\';
6674 *buf++ = *p;
6675 }
6676 *buf = '"';
6677 }
6678
6679 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006680 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 /* This is hard, so only do it once, and cache the result */
6682 if (*split_buf == NULL)
6683 *split_buf = uc_split_args(eap->arg, split_len);
6684
6685 result = *split_len;
6686 if (buf != NULL && result != 0)
6687 STRCPY(buf, *split_buf);
6688
6689 break;
6690 }
6691 break;
6692
6693 case ct_BANG:
6694 result = eap->forceit ? 1 : 0;
6695 if (quote)
6696 result += 2;
6697 if (buf != NULL)
6698 {
6699 if (quote)
6700 *buf++ = '"';
6701 if (eap->forceit)
6702 *buf++ = '!';
6703 if (quote)
6704 *buf = '"';
6705 }
6706 break;
6707
6708 case ct_LINE1:
6709 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006710 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 case ct_COUNT:
6712 {
6713 char num_buf[20];
6714 long num = (type == ct_LINE1) ? eap->line1 :
6715 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006716 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6718 size_t num_len;
6719
6720 sprintf(num_buf, "%ld", num);
6721 num_len = STRLEN(num_buf);
6722 result = num_len;
6723
6724 if (quote)
6725 result += 2;
6726
6727 if (buf != NULL)
6728 {
6729 if (quote)
6730 *buf++ = '"';
6731 STRCPY(buf, num_buf);
6732 buf += num_len;
6733 if (quote)
6734 *buf = '"';
6735 }
6736
6737 break;
6738 }
6739
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006740 case ct_MODS:
6741 {
6742 int multi_mods = 0;
6743 typedef struct {
6744 int *varp;
6745 char *name;
6746 } mod_entry_T;
6747 static mod_entry_T mod_entries[] = {
6748#ifdef FEAT_BROWSE_CMD
6749 {&cmdmod.browse, "browse"},
6750#endif
6751#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6752 {&cmdmod.confirm, "confirm"},
6753#endif
6754 {&cmdmod.hide, "hide"},
6755 {&cmdmod.keepalt, "keepalt"},
6756 {&cmdmod.keepjumps, "keepjumps"},
6757 {&cmdmod.keepmarks, "keepmarks"},
6758 {&cmdmod.keeppatterns, "keeppatterns"},
6759 {&cmdmod.lockmarks, "lockmarks"},
6760 {&cmdmod.noswapfile, "noswapfile"},
6761 {NULL, NULL}
6762 };
6763 int i;
6764
6765 result = quote ? 2 : 0;
6766 if (buf != NULL)
6767 {
6768 if (quote)
6769 *buf++ = '"';
6770 *buf = '\0';
6771 }
6772
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006773 /* :aboveleft and :leftabove */
6774 if (cmdmod.split & WSP_ABOVE)
6775 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6776 /* :belowright and :rightbelow */
6777 if (cmdmod.split & WSP_BELOW)
6778 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6779 /* :botright */
6780 if (cmdmod.split & WSP_BOT)
6781 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006782
6783 /* the modifiers that are simple flags */
6784 for (i = 0; mod_entries[i].varp != NULL; ++i)
6785 if (*mod_entries[i].varp)
6786 result += add_cmd_modifier(buf, mod_entries[i].name,
6787 &multi_mods);
6788
6789 /* TODO: How to support :noautocmd? */
6790#ifdef HAVE_SANDBOX
6791 /* TODO: How to support :sandbox?*/
6792#endif
6793 /* :silent */
6794 if (msg_silent > 0)
6795 result += add_cmd_modifier(buf,
6796 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006797 /* :tab */
6798 if (cmdmod.tab > 0)
6799 result += add_cmd_modifier(buf, "tab", &multi_mods);
6800 /* :topleft */
6801 if (cmdmod.split & WSP_TOP)
6802 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006803 /* TODO: How to support :unsilent?*/
6804 /* :verbose */
6805 if (p_verbose > 0)
6806 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006807 /* :vertical */
6808 if (cmdmod.split & WSP_VERT)
6809 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006810 if (quote && buf != NULL)
6811 {
6812 buf += result - 2;
6813 *buf = '"';
6814 }
6815 break;
6816 }
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 case ct_REGISTER:
6819 result = eap->regname ? 1 : 0;
6820 if (quote)
6821 result += 2;
6822 if (buf != NULL)
6823 {
6824 if (quote)
6825 *buf++ = '\'';
6826 if (eap->regname)
6827 *buf++ = eap->regname;
6828 if (quote)
6829 *buf = '\'';
6830 }
6831 break;
6832
6833 case ct_LT:
6834 result = 1;
6835 if (buf != NULL)
6836 *buf = '<';
6837 break;
6838
6839 default:
6840 /* Not recognized: just copy the '<' and return -1. */
6841 result = (size_t)-1;
6842 if (buf != NULL)
6843 *buf = '<';
6844 break;
6845 }
6846
6847 return result;
6848}
6849
6850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006851do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 char_u *buf;
6854 char_u *p;
6855 char_u *q;
6856
6857 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006858 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006859 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 size_t len, totlen;
6861
6862 size_t split_len = 0;
6863 char_u *split_buf = NULL;
6864 ucmd_T *cmd;
6865#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006866 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867#endif
6868
6869 if (eap->cmdidx == CMD_USER)
6870 cmd = USER_CMD(eap->useridx);
6871 else
6872 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6873
6874 /*
6875 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006876 * First round: "buf" is NULL, compute length, allocate "buf".
6877 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 */
6879 buf = NULL;
6880 for (;;)
6881 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006882 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006883 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006885
6886 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006888 start = vim_strchr(p, '<');
6889 if (start != NULL)
6890 end = vim_strchr(start + 1, '>');
6891 if (buf != NULL)
6892 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006893 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6894 ;
6895 if (*ksp == K_SPECIAL
6896 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006897 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6898# ifdef FEAT_GUI
6899 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6900# endif
6901 ))
6902 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006903 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006904 * KS_SPECIAL KE_FILLER, like for mappings, but
6905 * do_cmdline() doesn't handle that, so convert it back.
6906 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6907 len = ksp - p;
6908 if (len > 0)
6909 {
6910 mch_memmove(q, p, len);
6911 q += len;
6912 }
6913 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6914 p = ksp + 3;
6915 continue;
6916 }
6917 }
6918
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006919 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006920 if (start == NULL || end == NULL)
6921 break;
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /* Include the '>' */
6924 ++end;
6925
6926 /* Take everything up to the '<' */
6927 len = start - p;
6928 if (buf == NULL)
6929 totlen += len;
6930 else
6931 {
6932 mch_memmove(q, p, len);
6933 q += len;
6934 }
6935
6936 len = uc_check_code(start, end - start, q, cmd, eap,
6937 &split_buf, &split_len);
6938 if (len == (size_t)-1)
6939 {
6940 /* no match, continue after '<' */
6941 p = start + 1;
6942 len = 1;
6943 }
6944 else
6945 p = end;
6946 if (buf == NULL)
6947 totlen += len;
6948 else
6949 q += len;
6950 }
6951 if (buf != NULL) /* second time here, finished */
6952 {
6953 STRCPY(q, p);
6954 break;
6955 }
6956
6957 totlen += STRLEN(p); /* Add on the trailing characters */
6958 buf = alloc((unsigned)(totlen + 1));
6959 if (buf == NULL)
6960 {
6961 vim_free(split_buf);
6962 return;
6963 }
6964 }
6965
6966#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006967 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#endif
6969 (void)do_cmdline(buf, eap->getline, eap->cookie,
6970 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6971#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006972 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#endif
6974 vim_free(buf);
6975 vim_free(split_buf);
6976}
6977
6978# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6979 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6983}
6984
6985/*
6986 * Function given to ExpandGeneric() to obtain the list of user command names.
6987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006989get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 if (idx < curbuf->b_ucmds.ga_len)
6992 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6993 idx -= curbuf->b_ucmds.ga_len;
6994 if (idx < ucmds.ga_len)
6995 return USER_CMD(idx)->uc_name;
6996 return NULL;
6997}
6998
6999/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007000 * Function given to ExpandGeneric() to obtain the list of user address type names.
7001 */
7002 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007003get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007004{
7005 return (char_u *)addr_type_complete[idx].name;
7006}
7007
7008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 * Function given to ExpandGeneric() to obtain the list of user command
7010 * attributes.
7011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007016 {"addr", "bang", "bar", "buffer", "complete",
7017 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007019 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 return NULL;
7021 return (char_u *)user_cmd_flags[idx];
7022}
7023
7024/*
7025 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007028get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7031
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007032 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 return NULL;
7034 return (char_u *)user_cmd_nargs[idx];
7035}
7036
7037/*
7038 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 return (char_u *)command_complete[idx].name;
7044}
7045# endif /* FEAT_CMDL_COMPL */
7046
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007047/*
7048 * Parse address type argument
7049 */
7050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051parse_addr_type_arg(
7052 char_u *value,
7053 int vallen,
7054 long *argt,
7055 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007056{
7057 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007058
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007059 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7060 {
7061 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7062 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7063 if (a && b)
7064 {
7065 *addr_type_arg = addr_type_complete[i].expand;
7066 break;
7067 }
7068 }
7069
7070 if (addr_type_complete[i].expand == -1)
7071 {
7072 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007073
Bram Moolenaar1c465442017-03-12 20:10:05 +01007074 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007075 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007076 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007077 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007078 return FAIL;
7079 }
7080
7081 if (*addr_type_arg != ADDR_LINES)
7082 *argt |= NOTADR;
7083
7084 return OK;
7085}
7086
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087#endif /* FEAT_USR_CMDS */
7088
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007089#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7090/*
7091 * Parse a completion argument "value[vallen]".
7092 * The detected completion goes in "*complp", argument type in "*argt".
7093 * When there is an argument, for function and user defined completion, it's
7094 * copied to allocated memory and stored in "*compl_arg".
7095 * Returns FAIL if something is wrong.
7096 */
7097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098parse_compl_arg(
7099 char_u *value,
7100 int vallen,
7101 int *complp,
7102 long *argt,
7103 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007104{
7105 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007106# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007107 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007108# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007109 int i;
7110 int valend = vallen;
7111
7112 /* Look for any argument part - which is the part after any ',' */
7113 for (i = 0; i < vallen; ++i)
7114 {
7115 if (value[i] == ',')
7116 {
7117 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007118# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007119 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007120# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121 valend = i;
7122 break;
7123 }
7124 }
7125
7126 for (i = 0; command_complete[i].expand != 0; ++i)
7127 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007128 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007129 && STRNCMP(value, command_complete[i].name, valend) == 0)
7130 {
7131 *complp = command_complete[i].expand;
7132 if (command_complete[i].expand == EXPAND_BUFFERS)
7133 *argt |= BUFNAME;
7134 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7135 || command_complete[i].expand == EXPAND_FILES)
7136 *argt |= XFILE;
7137 break;
7138 }
7139 }
7140
7141 if (command_complete[i].expand == 0)
7142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007143 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007144 return FAIL;
7145 }
7146
7147# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7148 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7149 && arg != NULL)
7150# else
7151 if (arg != NULL)
7152# endif
7153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007154 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007155 return FAIL;
7156 }
7157
7158# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7159 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7160 && arg == NULL)
7161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007162 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007163 return FAIL;
7164 }
7165
7166 if (arg != NULL)
7167 *compl_arg = vim_strnsave(arg, (int)arglen);
7168# endif
7169 return OK;
7170}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007171
7172 int
7173cmdcomplete_str_to_type(char_u *complete_str)
7174{
7175 int i;
7176
7177 for (i = 0; command_complete[i].expand != 0; ++i)
7178 if (STRCMP(complete_str, command_complete[i].name) == 0)
7179 return command_complete[i].expand;
7180
7181 return EXPAND_NOTHING;
7182}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007183#endif
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
Bram Moolenaare6850792010-05-14 15:28:44 +02007188 if (*eap->arg == NUL)
7189 {
7190#ifdef FEAT_EVAL
7191 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7192 char_u *p = NULL;
7193
7194 if (expr != NULL)
7195 {
7196 ++emsg_off;
7197 p = eval_to_string(expr, NULL, FALSE);
7198 --emsg_off;
7199 vim_free(expr);
7200 }
7201 if (p != NULL)
7202 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007203 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007204 vim_free(p);
7205 }
7206 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007207 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007208#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007209 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007210#endif
7211 }
7212 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007213 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01007214
7215#ifdef FEAT_VTP
7216 else if (has_vtp_working())
7217 {
7218 // background color change requires clear + redraw
7219 update_screen(CLEAR);
7220 redrawcmd();
7221 }
7222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223}
7224
7225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007226ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007229 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 do_highlight(eap->arg, eap->forceit, FALSE);
7231}
7232
7233
7234/*
7235 * Call this function if we thought we were going to exit, but we won't
7236 * (because of an error). May need to restore the terminal mode.
7237 */
7238 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007239not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
7241 exiting = FALSE;
7242 settmode(TMODE_RAW);
7243}
7244
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007245 static int
7246before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7247{
7248 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7249
7250 /* Bail out when autocommands closed the window.
7251 * Refuse to quit when the buffer in the last window is being closed (can
7252 * only happen in autocommands). */
7253 if (!win_valid(wp)
7254 || curbuf_locked()
7255 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7256 return TRUE;
7257
7258 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7259 {
7260 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7261 /* Refuse to quit when locked or when the buffer in the last window is
7262 * being closed (can only happen in autocommands). */
7263 if (curbuf_locked()
7264 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7265 return TRUE;
7266 }
7267
7268 return FALSE;
7269}
7270
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007272 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007273 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 */
7275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007276ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007278 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007279
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280#ifdef FEAT_CMDWIN
7281 if (cmdwin_type != 0)
7282 {
7283 cmdwin_result = Ctrl_C;
7284 return;
7285 }
7286#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007287 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007288 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007289 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007290 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007291 return;
7292 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007293 if (eap->addr_count > 0)
7294 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007295 int wnr = eap->line2;
7296
7297 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7298 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007299 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007300 }
7301 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007302 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007303
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007304 /* Refuse to quit when locked. */
7305 if (curbuf_locked())
7306 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007307
7308 /* Trigger QuitPre and maybe ExitPre */
7309 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007310 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312#ifdef FEAT_NETBEANS_INTG
7313 netbeansForcedQuit = eap->forceit;
7314#endif
7315
7316 /*
7317 * If there are more files or windows we won't exit.
7318 */
7319 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7320 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007321 if ((!buf_hide(wp->w_buffer)
7322 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007323 | (eap->forceit ? CCGD_FORCEIT : 0)
7324 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007326 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 {
7328 not_exiting();
7329 }
7330 else
7331 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007332 /* quit last window
7333 * Note: only_one_window() returns true, even so a help window is
7334 * still open. In that case only quit, if no address has been
7335 * specified. Example:
7336 * :h|wincmd w|1q - don't quit
7337 * :h|wincmd w|q - quit
7338 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007339 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007341 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007342#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007346 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 }
7348}
7349
7350/*
7351 * ":cquit".
7352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007354ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
7356 getout(1); /* this does not always pass on the exit code to the Manx
7357 compiler. why? */
7358}
7359
7360/*
7361 * ":qall": try to quit all windows
7362 */
7363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007364ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366# ifdef FEAT_CMDWIN
7367 if (cmdwin_type != 0)
7368 {
7369 if (eap->forceit)
7370 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7371 else
7372 cmdwin_result = K_XF2;
7373 return;
7374 }
7375# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007376
7377 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007378 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007379 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007380 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007381 return;
7382 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007383
7384 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007385 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007388 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 getout(0);
7390 not_exiting();
7391}
7392
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393/*
7394 * ":close": close current window, unless it is the last one
7395 */
7396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007397ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007399 win_T *win;
7400 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007401#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007403 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007405#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007406 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007407 {
7408 if (eap->addr_count == 0)
7409 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007410 else
7411 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007412 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007413 {
7414 winnr++;
7415 if (winnr == eap->line2)
7416 break;
7417 }
7418 if (win == NULL)
7419 win = lastwin;
7420 ex_win_close(eap->forceit, win, NULL);
7421 }
7422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423}
7424
Bram Moolenaar4033c552017-09-16 20:54:51 +02007425#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007426/*
7427 * ":pclose": Close any preview window.
7428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007430ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431{
7432 win_T *win;
7433
Bram Moolenaar29323592016-07-24 22:04:11 +02007434 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007435 if (win->w_p_pvw)
7436 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007437 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007438 break;
7439 }
7440}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007441#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007442
Bram Moolenaarf740b292006-02-16 22:11:02 +00007443/*
7444 * Close window "win" and take care of handling closing the last window for a
7445 * modified buffer.
7446 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007448ex_win_close(
7449 int forceit,
7450 win_T *win,
7451 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 int need_hide;
7454 buf_T *buf = win->w_buffer;
7455
7456 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007457 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007459#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if ((p_confirm || cmdmod.confirm) && p_write)
7461 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007462 bufref_T bufref;
7463
7464 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007466 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 return;
7468 need_hide = FALSE;
7469 }
7470 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007473 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 return;
7475 }
7476 }
7477
Bram Moolenaar4033c552017-09-16 20:54:51 +02007478#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007480#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007483 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007484 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007485 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007486 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487}
7488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007490 * Handle the argument for a tabpage related ex command.
7491 * Returns a tabpage number.
7492 * When an error is encountered then eap->errmsg is set.
7493 */
7494 static int
7495get_tabpage_arg(exarg_T *eap)
7496{
7497 int tab_number;
7498 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7499
7500 if (eap->arg && *eap->arg != NUL)
7501 {
7502 char_u *p = eap->arg;
7503 char_u *p_save;
7504 int relative = 0; /* argument +N/-N means: go to N places to the
7505 * right/left relative to the current position. */
7506
7507 if (*p == '-')
7508 {
7509 relative = -1;
7510 p++;
7511 }
7512 else if (*p == '+')
7513 {
7514 relative = 1;
7515 p++;
7516 }
7517
7518 p_save = p;
7519 tab_number = getdigits(&p);
7520
7521 if (relative == 0)
7522 {
7523 if (STRCMP(p, "$") == 0)
7524 tab_number = LAST_TAB_NR;
7525 else if (p == p_save || *p_save == '-' || *p != NUL
7526 || tab_number > LAST_TAB_NR)
7527 {
7528 /* No numbers as argument. */
7529 eap->errmsg = e_invarg;
7530 goto theend;
7531 }
7532 }
7533 else
7534 {
7535 if (*p_save == NUL)
7536 tab_number = 1;
7537 else if (p == p_save || *p_save == '-' || *p != NUL
7538 || tab_number == 0)
7539 {
7540 /* No numbers as argument. */
7541 eap->errmsg = e_invarg;
7542 goto theend;
7543 }
7544 tab_number = tab_number * relative + tabpage_index(curtab);
7545 if (!unaccept_arg0 && relative == -1)
7546 --tab_number;
7547 }
7548 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7549 eap->errmsg = e_invarg;
7550 }
7551 else if (eap->addr_count > 0)
7552 {
7553 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007554 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007555 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007556 tab_number = 0;
7557 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007558 else
7559 {
7560 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01007561 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01007562 {
7563 --tab_number;
7564 if (tab_number < unaccept_arg0)
7565 eap->errmsg = e_invarg;
7566 }
7567 }
7568 }
7569 else
7570 {
7571 switch (eap->cmdidx)
7572 {
7573 case CMD_tabnext:
7574 tab_number = tabpage_index(curtab) + 1;
7575 if (tab_number > LAST_TAB_NR)
7576 tab_number = 1;
7577 break;
7578 case CMD_tabmove:
7579 tab_number = LAST_TAB_NR;
7580 break;
7581 default:
7582 tab_number = tabpage_index(curtab);
7583 }
7584 }
7585
7586theend:
7587 return tab_number;
7588}
7589
7590/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007591 * ":tabclose": close current tab page, unless it is the last one.
7592 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 */
7594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007595ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007597 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007598 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007599
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007600# ifdef FEAT_CMDWIN
7601 if (cmdwin_type != 0)
7602 cmdwin_result = K_IGNORE;
7603 else
7604# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007605 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007606 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007609 tab_number = get_tabpage_arg(eap);
7610 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007611 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007612 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007613 if (tp == NULL)
7614 {
7615 beep_flush();
7616 return;
7617 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007618 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007619 {
7620 tabpage_close_other(tp, eap->forceit);
7621 return;
7622 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007623 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007624 tabpage_close(eap->forceit);
7625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007627}
7628
7629/*
7630 * ":tabonly": close all tab pages except the current one
7631 */
7632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007633ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007634{
7635 tabpage_T *tp;
7636 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007637 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007638
7639# ifdef FEAT_CMDWIN
7640 if (cmdwin_type != 0)
7641 cmdwin_result = K_IGNORE;
7642 else
7643# endif
7644 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007645 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007646 else
7647 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007648 tab_number = get_tabpage_arg(eap);
7649 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007650 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007651 goto_tabpage(tab_number);
7652 /* Repeat this up to a 1000 times, because autocommands may
7653 * mess up the lists. */
7654 for (done = 0; done < 1000; ++done)
7655 {
7656 FOR_ALL_TABPAGES(tp)
7657 if (tp->tp_topframe != topframe)
7658 {
7659 tabpage_close_other(tp, eap->forceit);
7660 /* if we failed to close it quit */
7661 if (valid_tabpage(tp))
7662 done = 1000;
7663 /* start over, "tp" is now invalid */
7664 break;
7665 }
7666 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007667 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007668 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007669 }
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
7673/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007674 * Close the current tab page.
7675 */
7676 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007677tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007678{
7679 /* First close all the windows but the current one. If that worked then
7680 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007681 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007682 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007683 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007684 ex_win_close(forceit, curwin, NULL);
7685# ifdef FEAT_GUI
7686 need_mouse_correct = TRUE;
7687# endif
7688}
7689
7690/*
7691 * Close tab page "tp", which is not the current tab page.
7692 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007693 * Also takes care of the tab pages line disappearing when closing the
7694 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007695 */
7696 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007698{
7699 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007700 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007701 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007702
7703 /* Limit to 1000 windows, autocommands may add a window while we close
7704 * one. OK, so I'm paranoid... */
7705 while (++done < 1000)
7706 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007707 wp = tp->tp_firstwin;
7708 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007709
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007710 /* Autocommands may delete the tab page under our fingers and we may
7711 * fail to close a window with a modified buffer. */
7712 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007713 break;
7714 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007715
Bram Moolenaar29323592016-07-24 22:04:11 +02007716 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007717
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007718 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007719 if (h != tabline_height())
7720 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007721}
7722
7723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 * ":only".
7725 */
7726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007727ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007729 win_T *wp;
7730 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731# ifdef FEAT_GUI
7732 need_mouse_correct = TRUE;
7733# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007734 if (eap->addr_count > 0)
7735 {
7736 wnr = eap->line2;
7737 for (wp = firstwin; --wnr > 0; )
7738 {
7739 if (wp->w_next == NULL)
7740 break;
7741 else
7742 wp = wp->w_next;
7743 }
7744 win_goto(wp);
7745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 close_others(TRUE, eap->forceit);
7747}
7748
7749/*
7750 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007751 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007753 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007754ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
7756 if (eap->addr_count == 0)
7757 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007758 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
7761 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007762ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007764 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007765 if (!eap->skip)
7766 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007767#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007768 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007769#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007770 if (eap->addr_count == 0)
7771 win_close(curwin, FALSE); /* don't free buffer */
7772 else
7773 {
7774 int winnr = 0;
7775 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007776
Bram Moolenaar2256c992016-11-15 21:17:07 +01007777 FOR_ALL_WINDOWS(win)
7778 {
7779 winnr++;
7780 if (winnr == eap->line2)
7781 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007782 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007783 if (win == NULL)
7784 win = lastwin;
7785 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 }
7788}
7789
7790/*
7791 * ":stop" and ":suspend": Suspend Vim.
7792 */
7793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007794ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 /*
7797 * Disallow suspending for "rvim".
7798 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007799 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {
7801 if (!eap->forceit)
7802 autowrite_all();
7803 windgoto((int)Rows - 1, 0);
7804 out_char('\n');
7805 out_flush();
7806 stoptermcap();
7807 out_flush(); /* needed for SUN to restore xterm buffer */
7808#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007809 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810#endif
7811 ui_suspend(); /* call machine specific function */
7812#ifdef FEAT_TITLE
7813 maketitle();
7814 resettitle(); /* force updating the title */
7815#endif
7816 starttermcap();
7817 scroll_start(); /* scroll screen before redrawing */
7818 redraw_later_clear();
7819 shell_resized(); /* may have resized window */
7820 }
7821}
7822
7823/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007824 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 */
7826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007827ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829#ifdef FEAT_CMDWIN
7830 if (cmdwin_type != 0)
7831 {
7832 cmdwin_result = Ctrl_C;
7833 return;
7834 }
7835#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007836 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007837 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007838 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007839 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007840 return;
7841 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007842
7843 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007844 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846 /*
7847 * if more files or windows we won't exit
7848 */
7849 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7850 exiting = TRUE;
7851 if ( ((eap->cmdidx == CMD_wq
7852 || curbufIsChanged())
7853 && do_write(eap) == FAIL)
7854 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007855 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {
7857 not_exiting();
7858 }
7859 else
7860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007863 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864# ifdef FEAT_GUI
7865 need_mouse_correct = TRUE;
7866# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007867 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007868 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870}
7871
7872/*
7873 * ":print", ":list", ":number".
7874 */
7875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007876ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007878 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007879 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007882 for ( ;!got_int; ui_breakcheck())
7883 {
7884 print_line(eap->line1,
7885 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7886 || (eap->flags & EXFLAG_NR)),
7887 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7888 if (++eap->line1 > eap->line2)
7889 break;
7890 out_flush(); /* show one line at a time */
7891 }
7892 setpcmark();
7893 /* put cursor at last line */
7894 curwin->w_cursor.lnum = eap->line2;
7895 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899}
7900
7901#ifdef FEAT_BYTEOFF
7902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007903ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 goto_byte(eap->line2);
7906}
7907#endif
7908
7909/*
7910 * ":shell".
7911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007913ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 do_shell(NULL, 0);
7916}
7917
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007918#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007920static int drop_busy = FALSE;
7921static int drop_filec;
7922static char_u **drop_filev = NULL;
7923static int drop_split;
7924static void (*drop_callback)(void *);
7925static void *drop_cookie;
7926
7927 static void
7928handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 exarg_T ea;
7931 int save_msg_scroll = msg_scroll;
7932
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007933 // Setting the argument list may cause screen updates and being called
7934 // recursively. Avoid that by setting drop_busy.
7935 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937 /* Check whether the current buffer is changed. If so, we will need
7938 * to split the current window or data could be lost.
7939 * We don't need to check if the 'hidden' option is set, as in this
7940 * case the buffer won't be lost.
7941 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007942 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {
7944 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007945 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 --emsg_off;
7947 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007948 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (win_split(0, 0) == FAIL)
7951 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007952 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953
7954 /* When splitting the window, create a new alist. Otherwise the
7955 * existing one is overwritten. */
7956 alist_unlink(curwin->w_alist);
7957 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959
7960 /*
7961 * Set up the new argument list.
7962 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007963 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964
7965 /*
7966 * Move to the first file.
7967 */
7968 /* Fake up a minimal "next" command for do_argfile() */
7969 vim_memset(&ea, 0, sizeof(ea));
7970 ea.cmd = (char_u *)"next";
7971 do_argfile(&ea, 0);
7972
7973 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7974 * mode that is not needed here. */
7975 need_start_insertmode = FALSE;
7976
7977 /* Restore msg_scroll, otherwise a following command may cause scrolling
7978 * unexpectedly. The screen will be redrawn by the caller, thus
7979 * msg_scroll being set by displaying a message is irrelevant. */
7980 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007981
7982 if (drop_callback != NULL)
7983 drop_callback(drop_cookie);
7984
7985 drop_filev = NULL;
7986 drop_busy = FALSE;
7987}
7988
7989/*
7990 * Handle a file drop. The code is here because a drop is *nearly* like an
7991 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01007992 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007993 * code is very similar to :args and hence needs access to a lot of the static
7994 * functions in this file.
7995 *
7996 * The "filev" list must have been allocated using alloc(), as should each item
7997 * in the list. This function takes over responsibility for freeing the "filev"
7998 * list.
7999 */
8000 void
8001handle_drop(
8002 int filec, // the number of files dropped
8003 char_u **filev, // the list of files dropped
8004 int split, // force splitting the window
8005 void (*callback)(void *), // to be called after setting the argument
8006 // list
8007 void *cookie) // argument for "callback" (allocated)
8008{
8009 // Cannot handle recursive drops, finish the pending one.
8010 if (drop_busy)
8011 {
8012 FreeWild(filec, filev);
8013 vim_free(cookie);
8014 return;
8015 }
8016
8017 // When calling handle_drop() more than once in a row we only use the last
8018 // one.
8019 if (drop_filev != NULL)
8020 {
8021 FreeWild(drop_filec, drop_filev);
8022 vim_free(drop_cookie);
8023 }
8024
8025 drop_filec = filec;
8026 drop_filev = filev;
8027 drop_split = split;
8028 drop_callback = callback;
8029 drop_cookie = cookie;
8030
8031 // Postpone this when:
8032 // - editing the command line
8033 // - not possible to change the current buffer
8034 // - updating the screen
8035 // As it may change buffers and window structures that are in use and cause
8036 // freed memory to be used.
8037 if (text_locked() || curbuf_locked() || updating_screen)
8038 return;
8039
8040 handle_drop_internal();
8041}
8042
8043/*
8044 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8045 * reset: Handle a postponed drop.
8046 */
8047 void
8048handle_any_postponed_drop(void)
8049{
8050 if (!drop_busy && drop_filev != NULL
8051 && !text_locked() && !curbuf_locked() && !updating_screen)
8052 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053}
8054#endif
8055
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056/*
8057 * Clear an argument list: free all file names and reset it to zero entries.
8058 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008059 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008060alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062 while (--al->al_ga.ga_len >= 0)
8063 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8064 ga_clear(&al->al_ga);
8065}
8066
8067/*
8068 * Init an argument list.
8069 */
8070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8074}
8075
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076/*
8077 * Remove a reference from an argument list.
8078 * Ignored when the argument list is the global one.
8079 * If the argument list is no longer used by any window, free it.
8080 */
8081 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008082alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 if (al != &global_alist && --al->al_refcount <= 0)
8085 {
8086 alist_clear(al);
8087 vim_free(al);
8088 }
8089}
8090
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091/*
8092 * Create a new argument list and use it for the current window.
8093 */
8094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008095alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8098 if (curwin->w_alist == NULL)
8099 {
8100 curwin->w_alist = &global_alist;
8101 ++global_alist.al_refcount;
8102 }
8103 else
8104 {
8105 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008106 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 alist_init(curwin->w_alist);
8108 }
8109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008111#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112/*
8113 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008114 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8115 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 */
8117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008118alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119{
8120 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008121 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 char_u **new_arg_files;
8123 int new_arg_file_count;
8124 char_u *save_p_su = p_su;
8125 int i;
8126
8127 /* Don't use 'suffixes' here. This should work like the shell did the
8128 * expansion. Also, the vimrc file isn't read yet, thus the user
8129 * can't set the options. */
8130 p_su = empty_option;
8131 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8132 if (old_arg_files != NULL)
8133 {
8134 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008135 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8136 old_arg_count = GARGCOUNT;
8137 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008139 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 && new_arg_file_count > 0)
8141 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008142 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8143 TRUE, fnum_list, fnum_len);
8144 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 }
8147 p_su = save_p_su;
8148}
8149#endif
8150
8151/*
8152 * Set the argument list for the current window.
8153 * Takes over the allocated files[] and the allocated fnames in it.
8154 */
8155 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156alist_set(
8157 alist_T *al,
8158 int count,
8159 char_u **files,
8160 int use_curbuf,
8161 int *fnum_list,
8162 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
8164 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008165 static int recursive = 0;
8166
8167 if (recursive)
8168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008169 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008170 return;
8171 }
8172 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173
8174 alist_clear(al);
8175 if (ga_grow(&al->al_ga, count) == OK)
8176 {
8177 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008178 {
8179 if (got_int)
8180 {
8181 /* When adding many buffers this can take a long time. Allow
8182 * interrupting here. */
8183 while (i < count)
8184 vim_free(files[i++]);
8185 break;
8186 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008187
8188 /* May set buffer name of a buffer previously used for the
8189 * argument list, so that it's re-used by alist_add. */
8190 if (fnum_list != NULL && i < fnum_len)
8191 buf_set_name(fnum_list[i], files[i]);
8192
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008194 ui_breakcheck();
8195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 vim_free(files);
8197 }
8198 else
8199 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008202
8203 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204}
8205
8206/*
8207 * Add file "fname" to argument list "al".
8208 * "fname" must have been allocated and "al" must have been checked for room.
8209 */
8210 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008211alist_add(
8212 alist_T *al,
8213 char_u *fname,
8214 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215{
8216 if (fname == NULL) /* don't add NULL file names */
8217 return;
8218#ifdef BACKSLASH_IN_FILENAME
8219 slash_adjust(fname);
8220#endif
8221 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8222 if (set_fnum > 0)
8223 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8224 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8225 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226}
8227
8228#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8229/*
8230 * Adjust slashes in file names. Called after 'shellslash' was set.
8231 */
8232 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008233alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008237 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239 for (i = 0; i < GARGCOUNT; ++i)
8240 if (GARGLIST[i].ae_fname != NULL)
8241 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008242 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (wp->w_alist != &global_alist)
8244 for (i = 0; i < WARGCOUNT(wp); ++i)
8245 if (WARGLIST(wp)[i].ae_fname != NULL)
8246 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247}
8248#endif
8249
8250/*
8251 * ":preserve".
8252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008256 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 ml_preserve(curbuf, TRUE);
8258}
8259
8260/*
8261 * ":recover".
8262 */
8263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008264ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8267 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008268 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8269 | CCGD_MULTWIN
8270 | (eap->forceit ? CCGD_FORCEIT : 0)
8271 | CCGD_EXCMD)
8272
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 && (*eap->arg == NUL
8274 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8275 ml_recover();
8276 recoverymode = FALSE;
8277}
8278
8279/*
8280 * Command modifier used in a wrong way.
8281 */
8282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008283ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
8285 eap->errmsg = e_invcmd;
8286}
8287
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288/*
8289 * :sview [+command] file split window with new file, read-only
8290 * :split [[+command] file] split window with current or new file
8291 * :vsplit [[+command] file] split window vertically with current or new file
8292 * :new [[+command] file] split window with no or new file
8293 * :vnew [[+command] file] split vertically window with no or new file
8294 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008295 *
8296 * :tabedit open new Tab page with empty window
8297 * :tabedit [+command] file open new Tab page and edit "file"
8298 * :tabnew [[+command] file] just like :tabedit
8299 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 */
8301 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008302ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008304 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008305#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008307#endif
8308#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008310#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008311 int use_tab = eap->cmdidx == CMD_tabedit
8312 || eap->cmdidx == CMD_tabfind
8313 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314
Bram Moolenaar4033c552017-09-16 20:54:51 +02008315#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
Bram Moolenaar4033c552017-09-16 20:54:51 +02008319#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008321 * quickfix windows. But it's OK when doing ":tab split". */
8322 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {
8324 if (eap->cmdidx == CMD_split)
8325 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 if (eap->cmdidx == CMD_vsplit)
8327 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330
Bram Moolenaar4033c552017-09-16 20:54:51 +02008331#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008332 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
8334 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8335 FNAME_MESS, TRUE, curbuf->b_ffname);
8336 if (fname == NULL)
8337 goto theend;
8338 eap->arg = fname;
8339 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008340# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008341 else
8342# endif
8343#endif
8344#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 && eap->cmdidx != CMD_new)
8348 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008349 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008350# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008351 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008352# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008353 au_has_group((char_u *)"FileExplorer"))
8354 {
8355 /* No browsing supported but we do have the file explorer:
8356 * Edit the directory. */
8357 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8358 eap->arg = (char_u *)".";
8359 }
8360 else
8361 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008362 fname = do_browse(0, (char_u *)(use_tab
8363 ? _("Edit File in new tab page")
8364 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008366 if (fname == NULL)
8367 goto theend;
8368 eap->arg = fname;
8369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 }
8371 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008374 /*
8375 * Either open new tab page or split the window.
8376 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008377 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008378 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008379 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8380 : eap->addr_count == 0 ? 0
8381 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008382 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008383 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008384
8385 /* set the alternate buffer for the window we came from */
8386 if (curwin != old_curwin
8387 && win_valid(old_curwin)
8388 && old_curwin->w_buffer != curbuf
8389 && !cmdmod.keepalt)
8390 old_curwin->w_alt_fnum = curbuf->b_fnum;
8391 }
8392 }
8393 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 /* Reset 'scrollbind' when editing another file, but keep it when
8397 * doing ":split" without arguments. */
8398 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008399# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008403 {
8404 RESET_BINDING(curwin);
8405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 else
8407 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 do_exedit(eap, old_curwin);
8409 }
8410
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008411# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008415# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416theend:
8417 vim_free(fname);
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
8421/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008422 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008423 */
8424 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008425tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008426{
8427 exarg_T ea;
8428
8429 vim_memset(&ea, 0, sizeof(ea));
8430 ea.cmdidx = CMD_tabnew;
8431 ea.cmd = (char_u *)"tabn";
8432 ea.arg = (char_u *)"";
8433 ex_splitview(&ea);
8434}
8435
8436/*
8437 * :tabnext command
8438 */
8439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008440ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008441{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008442 int tab_number;
8443
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008444 switch (eap->cmdidx)
8445 {
8446 case CMD_tabfirst:
8447 case CMD_tabrewind:
8448 goto_tabpage(1);
8449 break;
8450 case CMD_tablast:
8451 goto_tabpage(9999);
8452 break;
8453 case CMD_tabprevious:
8454 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008455 if (eap->arg && *eap->arg != NUL)
8456 {
8457 char_u *p = eap->arg;
8458 char_u *p_save = p;
8459
8460 tab_number = getdigits(&p);
8461 if (p == p_save || *p_save == '-' || *p != NUL
8462 || tab_number == 0)
8463 {
8464 /* No numbers as argument. */
8465 eap->errmsg = e_invarg;
8466 return;
8467 }
8468 }
8469 else
8470 {
8471 if (eap->addr_count == 0)
8472 tab_number = 1;
8473 else
8474 {
8475 tab_number = eap->line2;
8476 if (tab_number < 1)
8477 {
8478 eap->errmsg = e_invrange;
8479 return;
8480 }
8481 }
8482 }
8483 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008484 break;
8485 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008486 tab_number = get_tabpage_arg(eap);
8487 if (eap->errmsg == NULL)
8488 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008489 break;
8490 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008491}
8492
8493/*
8494 * :tabmove command
8495 */
8496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008497ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008498{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008499 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008500
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008501 tab_number = get_tabpage_arg(eap);
8502 if (eap->errmsg == NULL)
8503 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008504}
8505
8506/*
8507 * :tabs command: List tabs and their contents.
8508 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008510ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008511{
8512 tabpage_T *tp;
8513 win_T *wp;
8514 int tabcount = 1;
8515
8516 msg_start();
8517 msg_scroll = TRUE;
8518 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8519 {
8520 msg_putchar('\n');
8521 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008522 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008523 out_flush(); /* output one line at a time */
8524 ui_breakcheck();
8525
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008526 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008527 wp = firstwin;
8528 else
8529 wp = tp->tp_firstwin;
8530 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8531 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008532 msg_putchar('\n');
8533 msg_putchar(wp == curwin ? '>' : ' ');
8534 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008535 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8536 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008537 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008538 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008539 else
8540 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8541 IObuff, IOSIZE, TRUE);
8542 msg_outtrans(IObuff);
8543 out_flush(); /* output one line at a time */
8544 ui_breakcheck();
8545 }
8546 }
8547}
8548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549/*
8550 * ":mode": Set screen mode.
8551 * If no argument given, just get the screen size and redraw.
8552 */
8553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008554ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 if (*eap->arg == NUL)
8557 shell_resized();
8558 else
8559 mch_screenmode(eap->arg);
8560}
8561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562/*
8563 * ":resize".
8564 * set, increment or decrement current window height
8565 */
8566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008567ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568{
8569 int n;
8570 win_T *wp = curwin;
8571
8572 if (eap->addr_count > 0)
8573 {
8574 n = eap->line2;
8575 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8576 ;
8577 }
8578
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008579# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 if (cmdmod.split & WSP_VERT)
8584 {
8585 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008586 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8588 n = 9999;
8589 win_setwidth_win((int)n, wp);
8590 }
8591 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {
8593 if (*eap->arg == '-' || *eap->arg == '+')
8594 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008595 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 n = 9999;
8597 win_setheight_win((int)n, wp);
8598 }
8599}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
8601/*
8602 * ":find [+command] <file>" command.
8603 */
8604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008605ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607#ifdef FEAT_SEARCHPATH
8608 char_u *fname;
8609 int count;
8610
8611 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8612 TRUE, curbuf->b_ffname);
8613 if (eap->addr_count > 0)
8614 {
8615 /* Repeat finding the file "count" times. This matters when it
8616 * appears several times in the path. */
8617 count = eap->line2;
8618 while (fname != NULL && --count > 0)
8619 {
8620 vim_free(fname);
8621 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8622 FALSE, curbuf->b_ffname);
8623 }
8624 }
8625
8626 if (fname != NULL)
8627 {
8628 eap->arg = fname;
8629#endif
8630 do_exedit(eap, NULL);
8631#ifdef FEAT_SEARCHPATH
8632 vim_free(fname);
8633 }
8634#endif
8635}
8636
8637/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008638 * ":open" simulation: for now just work like ":visual".
8639 */
8640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008641ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008642{
8643 regmatch_T regmatch;
8644 char_u *p;
8645
8646 curwin->w_cursor.lnum = eap->line2;
8647 beginline(BL_SOL | BL_FIX);
8648 if (*eap->arg == '/')
8649 {
8650 /* ":open /pattern/": put cursor in column found with pattern */
8651 ++eap->arg;
8652 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8653 *p = NUL;
8654 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8655 if (regmatch.regprog != NULL)
8656 {
8657 regmatch.rm_ic = p_ic;
8658 p = ml_get_curline();
8659 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008660 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008661 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008662 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008663 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008664 }
8665 /* Move to the NUL, ignore any other arguments. */
8666 eap->arg += STRLEN(eap->arg);
8667 }
8668 check_cursor();
8669
8670 eap->cmdidx = CMD_visual;
8671 do_exedit(eap, NULL);
8672}
8673
8674/*
8675 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 */
8677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008678ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 do_exedit(eap, NULL);
8681}
8682
8683/*
8684 * ":edit <file>" command and alikes.
8685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008687do_exedit(
8688 exarg_T *eap,
8689 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
8691 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008693 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
8695 /*
8696 * ":vi" command ends Ex mode.
8697 */
8698 if (exmode_active && (eap->cmdidx == CMD_visual
8699 || eap->cmdidx == CMD_view))
8700 {
8701 exmode_active = FALSE;
8702 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008703 {
8704 /* Special case: ":global/pat/visual\NLvi-commands" */
8705 if (global_busy)
8706 {
8707 int rd = RedrawingDisabled;
8708 int nwr = no_wait_return;
8709 int ms = msg_scroll;
8710#ifdef FEAT_GUI
8711 int he = hold_gui_events;
8712#endif
8713
8714 if (eap->nextcmd != NULL)
8715 {
8716 stuffReadbuff(eap->nextcmd);
8717 eap->nextcmd = NULL;
8718 }
8719
8720 if (exmode_was != EXMODE_VIM)
8721 settmode(TMODE_RAW);
8722 RedrawingDisabled = 0;
8723 no_wait_return = 0;
8724 need_wait_return = FALSE;
8725 msg_scroll = 0;
8726#ifdef FEAT_GUI
8727 hold_gui_events = 0;
8728#endif
8729 must_redraw = CLEAR;
8730
8731 main_loop(FALSE, TRUE);
8732
8733 RedrawingDisabled = rd;
8734 no_wait_return = nwr;
8735 msg_scroll = ms;
8736#ifdef FEAT_GUI
8737 hold_gui_events = he;
8738#endif
8739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
8743
8744 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008745 || eap->cmdidx == CMD_tabnew
8746 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008747 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008749 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 setpcmark();
8751 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008752 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8753 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008755 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 || *eap->arg != NUL
8757#ifdef FEAT_BROWSE
8758 || cmdmod.browse
8759#endif
8760 )
8761 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008762 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8763 * can bring us here, others are stopped earlier. */
8764 if (*eap->arg != NUL && curbuf_locked())
8765 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 n = readonlymode;
8768 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8769 readonlymode = TRUE;
8770 else if (eap->cmdidx == CMD_enew)
8771 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8772 empty buffer */
8773 setpcmark();
8774 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8775 NULL, eap,
8776 /* ":edit" goes to first line if Vi compatible */
8777 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8778 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8779 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008780 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008782 /* after a split we can use an existing buffer */
8783 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008785 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 {
8787 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 if (old_curwin != NULL)
8789 {
8790 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008791 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008793#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008794 cleanup_T cs;
8795
8796 /* Reset the error/interrupt/exception state here so that
8797 * aborting() returns FALSE when closing a window. */
8798 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008799#endif
8800#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008802#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008803 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008804
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008805#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008806 /* Restore the error/interrupt/exception state if not
8807 * discarded by a new aborting error, interrupt, or
8808 * uncaught exception. */
8809 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 }
8812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 }
8814 else if (readonlymode && curbuf->b_nwindows == 1)
8815 {
8816 /* When editing an already visited buffer, 'readonly' won't be set
8817 * but the previous value is kept. With ":view" and ":sview" we
8818 * want the file to be readonly, except when another window is
8819 * editing the same buffer. */
8820 curbuf->b_p_ro = TRUE;
8821 }
8822 readonlymode = n;
8823 }
8824 else
8825 {
8826 if (eap->do_ecmd_cmd != NULL)
8827 do_cmdline_cmd(eap->do_ecmd_cmd);
8828#ifdef FEAT_TITLE
8829 n = curwin->w_arg_idx_invalid;
8830#endif
8831 check_arg_idx(curwin);
8832#ifdef FEAT_TITLE
8833 if (n != curwin->w_arg_idx_invalid)
8834 maketitle();
8835#endif
8836 }
8837
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 /*
8839 * if ":split file" worked, set alternate file name in old window to new
8840 * file
8841 */
8842 if (old_curwin != NULL
8843 && *eap->arg != NUL
8844 && curwin != old_curwin
8845 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008846 && old_curwin->w_buffer != curbuf
8847 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849
8850 ex_no_reprint = TRUE;
8851}
8852
8853#ifndef FEAT_GUI
8854/*
8855 * ":gui" and ":gvim" when there is no GUI.
8856 */
8857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008858ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 eap->errmsg = e_nogvim;
8861}
8862#endif
8863
8864#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008866ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868 gui_make_tearoff(eap->arg);
8869}
8870#endif
8871
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008872#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8873 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008875ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008877# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8878 if (gui.in_use)
8879 gui_make_popup(eap->arg, eap->forceit);
8880# ifdef FEAT_TERM_POPUP_MENU
8881 else
8882# endif
8883# endif
8884# ifdef FEAT_TERM_POPUP_MENU
8885 pum_make_popup(eap->arg, eap->forceit);
8886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887}
8888#endif
8889
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008891ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008894 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008896 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897}
8898
8899/*
8900 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8901 * offset.
8902 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008905ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008908 win_T *save_curwin = curwin;
8909 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 long topline;
8911 long y;
8912 linenr_T old_linenr = curwin->w_cursor.lnum;
8913
8914 setpcmark();
8915
8916 /*
8917 * determine max topline
8918 */
8919 if (curwin->w_p_scb)
8920 {
8921 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008922 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
8924 if (wp->w_p_scb && wp->w_buffer)
8925 {
8926 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8927 if (topline > y)
8928 topline = y;
8929 }
8930 }
8931 if (topline < 1)
8932 topline = 1;
8933 }
8934 else
8935 {
8936 topline = 1;
8937 }
8938
8939
8940 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008941 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008943 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 {
8945 if (curwin->w_p_scb)
8946 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008947 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 y = topline - curwin->w_topline;
8949 if (y > 0)
8950 scrollup(y, TRUE);
8951 else
8952 scrolldown(-y, TRUE);
8953 curwin->w_scbind_pos = topline;
8954 redraw_later(VALID);
8955 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008959 curwin = save_curwin;
8960 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 if (curwin->w_p_scb)
8962 {
8963 did_syncbind = TRUE;
8964 checkpcmark();
8965 if (old_linenr != curwin->w_cursor.lnum)
8966 {
8967 char_u ctrl_o[2];
8968
8969 ctrl_o[0] = Ctrl_O;
8970 ctrl_o[1] = 0;
8971 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8972 }
8973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974}
8975
8976
8977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008978ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008980 int i;
8981 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8982 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983
8984 if (eap->usefilter) /* :r!cmd */
8985 do_bang(1, eap, FALSE, FALSE, TRUE);
8986 else
8987 {
8988 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8989 return;
8990
8991#ifdef FEAT_BROWSE
8992 if (cmdmod.browse)
8993 {
8994 char_u *browseFile;
8995
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008996 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 NULL, NULL, NULL, curbuf);
8998 if (browseFile != NULL)
8999 {
9000 i = readfile(browseFile, NULL,
9001 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9002 vim_free(browseFile);
9003 }
9004 else
9005 i = OK;
9006 }
9007 else
9008#endif
9009 if (*eap->arg == NUL)
9010 {
9011 if (check_fname() == FAIL) /* check for no file name */
9012 return;
9013 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9014 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9015 }
9016 else
9017 {
9018 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9019 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9020 i = readfile(eap->arg, NULL,
9021 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9022
9023 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009024 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009026#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 if (!aborting())
9028#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009029 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
9031 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009032 {
9033 if (empty && exmode_active)
9034 {
9035 /* Delete the empty line that remains. Historically ex does
9036 * this but vi doesn't. */
9037 if (eap->line2 == 0)
9038 lnum = curbuf->b_ml.ml_line_count;
9039 else
9040 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009041 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009042 {
9043 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009044 if (curwin->w_cursor.lnum > 1
9045 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009046 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009047 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009048 }
9049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 }
9053}
9054
Bram Moolenaarea408852005-06-25 22:49:46 +00009055static char_u *prev_dir = NULL;
9056
9057#if defined(EXITFREE) || defined(PROTO)
9058 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009059free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009060{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009061 VIM_CLEAR(prev_dir);
9062 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009063}
9064#endif
9065
Bram Moolenaarf4258302013-06-02 18:20:17 +02009066/*
9067 * Deal with the side effects of changing the current directory.
9068 * When "local" is TRUE then this was after an ":lcd" command.
9069 */
9070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009071post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009072{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009073 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009074 if (local)
9075 {
9076 /* If still in global directory, need to remember current
9077 * directory as global directory. */
9078 if (globaldir == NULL && prev_dir != NULL)
9079 globaldir = vim_strsave(prev_dir);
9080 /* Remember this local directory for the window. */
9081 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9082 curwin->w_localdir = vim_strsave(NameBuff);
9083 }
9084 else
9085 {
9086 /* We are now in the global directory, no need to remember its
9087 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009088 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009089 }
9090
9091 shorten_fnames(TRUE);
9092}
9093
Bram Moolenaarea408852005-06-25 22:49:46 +00009094
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095/*
9096 * ":cd", ":lcd", ":chdir" and ":lchdir".
9097 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009099ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009101 char_u *new_dir;
9102 char_u *tofree;
9103 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 new_dir = eap->arg;
9106#if !defined(UNIX) && !defined(VMS)
9107 /* for non-UNIX ":cd" means: print current directory */
9108 if (*new_dir == NUL)
9109 ex_pwd(NULL);
9110 else
9111#endif
9112 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009113 if (allbuf_locked())
9114 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009115 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9116 && !eap->forceit)
9117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009118 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009119 return;
9120 }
9121
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 /* ":cd -": Change to previous directory */
9123 if (STRCMP(new_dir, "-") == 0)
9124 {
9125 if (prev_dir == NULL)
9126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009127 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 return;
9129 }
9130 new_dir = prev_dir;
9131 }
9132
9133 /* Save current directory for next ":cd -" */
9134 tofree = prev_dir;
9135 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9136 prev_dir = vim_strsave(NameBuff);
9137 else
9138 prev_dir = NULL;
9139
9140#if defined(UNIX) || defined(VMS)
9141 /* for UNIX ":cd" means: go to home directory */
9142 if (*new_dir == NUL)
9143 {
9144 /* use NameBuff for home directory name */
9145# ifdef VMS
9146 char_u *p;
9147
9148 p = mch_getenv((char_u *)"SYS$LOGIN");
9149 if (p == NULL || *p == NUL) /* empty is the same as not set */
9150 NameBuff[0] = NUL;
9151 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009152 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153# else
9154 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9155# endif
9156 new_dir = NameBuff;
9157 }
9158#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009159 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009160 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009161 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009162 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 else
9164 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009165 int is_local_chdir = eap->cmdidx == CMD_lcd
9166 || eap->cmdidx == CMD_lchdir;
9167
9168 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169
9170 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009171 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009173
9174 if (dir_differs)
9175 apply_autocmds(EVENT_DIRCHANGED,
9176 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9177 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 }
9179 vim_free(tofree);
9180 }
9181}
9182
9183/*
9184 * ":pwd".
9185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009187ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9190 {
9191#ifdef BACKSLASH_IN_FILENAME
9192 slash_adjust(NameBuff);
9193#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009194 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 }
9196 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009197 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198}
9199
9200/*
9201 * ":=".
9202 */
9203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009204ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009206 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009207 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208}
9209
9210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009211ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009213 int n;
9214 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215
9216 if (cursor_valid())
9217 {
9218 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9219 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009220 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009222
9223 len = eap->line2;
9224 switch (*eap->arg)
9225 {
9226 case 'm': break;
9227 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009228 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009229 }
9230 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
9233/*
9234 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9235 */
9236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009237do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
9239 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009240 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009243 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009244 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009246 wait_now = msec - done > 1000L ? 1000L : msec - done;
9247#ifdef FEAT_TIMERS
9248 {
9249 long due_time = check_due_timer();
9250
9251 if (due_time > 0 && due_time < wait_now)
9252 wait_now = due_time;
9253 }
9254#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009255#ifdef FEAT_JOB_CHANNEL
9256 if (has_any_channel() && wait_now > 100L)
9257 wait_now = 100L;
9258#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009259 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009260#ifdef FEAT_JOB_CHANNEL
9261 if (has_any_channel())
9262 ui_breakcheck_force(TRUE);
9263 else
9264#endif
9265 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009266#ifdef MESSAGE_QUEUE
9267 /* Process the netbeans and clientserver messages that may have been
9268 * received in the call to ui_breakcheck() when the GUI is in use. This
9269 * may occur when running a test case. */
9270 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009273
9274 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9275 // input buffer, otherwise a following call to input() fails.
9276 if (got_int)
9277 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009281do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283 int mode;
9284 char_u *cmdp;
9285
9286 cmdp = eap->cmd;
9287 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9288
9289 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9290 eap->arg, mode, isabbrev))
9291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009292 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009294 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 break;
9296 }
9297}
9298
9299/*
9300 * ":winsize" command (obsolete).
9301 */
9302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009303ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304{
9305 int w, h;
9306 char_u *arg = eap->arg;
9307 char_u *p;
9308
9309 w = getdigits(&arg);
9310 arg = skipwhite(arg);
9311 p = arg;
9312 h = getdigits(&arg);
9313 if (*p != NUL && *arg == NUL)
9314 set_shellsize(w, h, TRUE);
9315 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009316 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317}
9318
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009320ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322 int xchar = NUL;
9323 char_u *p;
9324
9325 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9326 {
9327 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9328 if (eap->arg[1] == NUL)
9329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009330 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 return;
9332 }
9333 xchar = eap->arg[1];
9334 p = eap->arg + 2;
9335 }
9336 else
9337 p = eap->arg + 1;
9338
9339 eap->nextcmd = check_nextcmd(p);
9340 p = skipwhite(p);
9341 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009342 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009343 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
9345 /* Pass flags on for ":vertical wincmd ]". */
9346 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009347 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9349 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009350 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 }
9352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353
Bram Moolenaar843ee412004-06-30 16:16:41 +00009354#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355/*
9356 * ":winpos".
9357 */
9358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009359ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360{
9361 int x, y;
9362 char_u *arg = eap->arg;
9363 char_u *p;
9364
9365 if (*arg == NUL)
9366 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009367# if defined(FEAT_GUI) || defined(MSWIN)
9368# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009370# else
9371 if (mch_get_winpos(&x, &y) != FAIL)
9372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 {
9374 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009375 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 }
9377 else
9378# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009379 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 }
9381 else
9382 {
9383 x = getdigits(&arg);
9384 arg = skipwhite(arg);
9385 p = arg;
9386 y = getdigits(&arg);
9387 if (*p == NUL || *arg != NUL)
9388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009389 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 return;
9391 }
9392# ifdef FEAT_GUI
9393 if (gui.in_use)
9394 gui_mch_set_winpos(x, y);
9395 else if (gui.starting)
9396 {
9397 /* Remember the coordinates for when the window is opened. */
9398 gui_win_x = x;
9399 gui_win_y = y;
9400 }
9401# ifdef HAVE_TGETENT
9402 else
9403# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009404# else
9405# ifdef MSWIN
9406 mch_set_winpos(x, y);
9407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408# endif
9409# ifdef HAVE_TGETENT
9410 if (*T_CWP)
9411 term_set_winpos(x, y);
9412# endif
9413 }
9414}
9415#endif
9416
9417/*
9418 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9419 */
9420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009421ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 oparg_T oa;
9424
9425 clear_oparg(&oa);
9426 oa.regname = eap->regname;
9427 oa.start.lnum = eap->line1;
9428 oa.end.lnum = eap->line2;
9429 oa.line_count = eap->line2 - eap->line1 + 1;
9430 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9433 {
9434 setpcmark();
9435 curwin->w_cursor.lnum = eap->line1;
9436 beginline(BL_SOL | BL_FIX);
9437 }
9438
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009439 if (VIsual_active)
9440 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009441
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 switch (eap->cmdidx)
9443 {
9444 case CMD_delete:
9445 oa.op_type = OP_DELETE;
9446 op_delete(&oa);
9447 break;
9448
9449 case CMD_yank:
9450 oa.op_type = OP_YANK;
9451 (void)op_yank(&oa, FALSE, TRUE);
9452 break;
9453
9454 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009455 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009457 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9458#else
9459 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009461 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 oa.op_type = OP_RSHIFT;
9463 else
9464 oa.op_type = OP_LSHIFT;
9465 op_shift(&oa, FALSE, eap->amount);
9466 break;
9467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009469 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470}
9471
9472/*
9473 * ":put".
9474 */
9475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009476ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477{
9478 /* ":0put" works like ":1put!". */
9479 if (eap->line2 == 0)
9480 {
9481 eap->line2 = 1;
9482 eap->forceit = TRUE;
9483 }
9484 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009485 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9486 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487}
9488
9489/*
9490 * Handle ":copy" and ":move".
9491 */
9492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009493ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 long n;
9496
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009497 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 if (eap->arg == NULL) /* error detected */
9499 {
9500 eap->nextcmd = NULL;
9501 return;
9502 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009503 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504
9505 /*
9506 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9507 */
9508 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009510 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 return;
9512 }
9513
9514 if (eap->cmdidx == CMD_move)
9515 {
9516 if (do_move(eap->line1, eap->line2, n) == FAIL)
9517 return;
9518 }
9519 else
9520 ex_copy(eap->line1, eap->line2, n);
9521 u_clearline();
9522 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009523 ex_may_print(eap);
9524}
9525
9526/*
9527 * Print the current line if flags were given to the Ex command.
9528 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009529 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009530ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009531{
9532 if (eap->flags != 0)
9533 {
9534 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9535 (eap->flags & EXFLAG_LIST));
9536 ex_no_reprint = TRUE;
9537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538}
9539
9540/*
9541 * ":smagic" and ":snomagic".
9542 */
9543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009544ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 int magic_save = p_magic;
9547
9548 p_magic = (eap->cmdidx == CMD_smagic);
9549 do_sub(eap);
9550 p_magic = magic_save;
9551}
9552
9553/*
9554 * ":join".
9555 */
9556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009557ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 curwin->w_cursor.lnum = eap->line1;
9560 if (eap->line1 == eap->line2)
9561 {
9562 if (eap->addr_count >= 2) /* :2,2join does nothing */
9563 return;
9564 if (eap->line2 == curbuf->b_ml.ml_line_count)
9565 {
9566 beep_flush();
9567 return;
9568 }
9569 ++eap->line2;
9570 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009571 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009573 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574}
9575
9576/*
9577 * ":[addr]@r" or ":[addr]*r": execute register
9578 */
9579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009580ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009583 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
9585 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009586 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587
9588#ifdef USE_ON_FLY_SCROLL
9589 dont_scroll = TRUE; /* disallow scrolling here */
9590#endif
9591
9592 /* get the register name. No name means to use the previous one */
9593 c = *eap->arg;
9594 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9595 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009596 /* Put the register in the typeahead buffer with the "silent" flag. */
9597 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9598 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 else
9603 {
9604 int save_efr = exec_from_reg;
9605
9606 exec_from_reg = TRUE;
9607
9608 /*
9609 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009610 * Continue until the stuff buffer is empty and all added characters
9611 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009613 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9615
9616 exec_from_reg = save_efr;
9617 }
9618}
9619
9620/*
9621 * ":!".
9622 */
9623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009624ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
9626 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9627}
9628
9629/*
9630 * ":undo".
9631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009633ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009635 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009636 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009637 else
9638 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639}
9640
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009641#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009643ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009644{
9645 char_u hash[UNDO_HASH_SIZE];
9646
9647 u_compute_hash(hash);
9648 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9649}
9650
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009652ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009653{
9654 char_u hash[UNDO_HASH_SIZE];
9655
9656 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009657 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009658}
9659#endif
9660
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661/*
9662 * ":redo".
9663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009665ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 u_redo(1);
9668}
9669
9670/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009671 * ":earlier" and ":later".
9672 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009674ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009675{
9676 long count = 0;
9677 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009678 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009679 char_u *p = eap->arg;
9680
9681 if (*p == NUL)
9682 count = 1;
9683 else if (isdigit(*p))
9684 {
9685 count = getdigits(&p);
9686 switch (*p)
9687 {
9688 case 's': ++p; sec = TRUE; break;
9689 case 'm': ++p; sec = TRUE; count *= 60; break;
9690 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009691 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9692 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009693 }
9694 }
9695
9696 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009697 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009698 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009699 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9700 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009701}
9702
9703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 * ":redir": start/stop redirection.
9705 */
9706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009707ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709 char *mode;
9710 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009711 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712
Bram Moolenaarba768492016-07-08 15:32:54 +02009713#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009714 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009716 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009717 return;
9718 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009719#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009720
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 if (STRICMP(eap->arg, "END") == 0)
9722 close_redir();
9723 else
9724 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009725 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009727 ++arg;
9728 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009730 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 mode = "a";
9732 }
9733 else
9734 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009735 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736
9737 close_redir();
9738
9739 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009740 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (fname == NULL)
9742 return;
9743#ifdef FEAT_BROWSE
9744 if (cmdmod.browse)
9745 {
9746 char_u *browseFile;
9747
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009748 browseFile = do_browse(BROWSE_SAVE,
9749 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009750 fname, NULL, NULL,
9751 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (browseFile == NULL)
9753 return; /* operation cancelled */
9754 vim_free(fname);
9755 fname = browseFile;
9756 eap->forceit = TRUE; /* since dialog already asked */
9757 }
9758#endif
9759
9760 redir_fd = open_exfile(fname, eap->forceit, mode);
9761 vim_free(fname);
9762 }
9763#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009764 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 {
9766 /* redirect to a register a-z (resp. A-Z for appending) */
9767 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009768 ++arg;
9769 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009771 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009772 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009774 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009776 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009777 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009778 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009779 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009781 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009782 if (*arg == '>')
9783 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009784 /* Make register empty when not using @A-@Z and the
9785 * command is valid. */
9786 if (*arg == NUL && !isupper(redir_reg))
9787 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009789 }
9790 if (*arg != NUL)
9791 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009792 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009793 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009794 }
9795 }
9796 else if (*arg == '=' && arg[1] == '>')
9797 {
9798 int append;
9799
9800 /* redirect to a variable */
9801 close_redir();
9802 arg += 2;
9803
9804 if (*arg == '>')
9805 {
9806 ++arg;
9807 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 }
9809 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009810 append = FALSE;
9811
9812 if (var_redir_start(skipwhite(arg), append) == OK)
9813 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 }
9815#endif
9816
9817 /* TODO: redirect to a buffer */
9818
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009820 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009822
9823 /* Make sure redirection is not off. Can happen for cmdline completion
9824 * that indirectly invokes a command to catch its output. */
9825 if (redir_fd != NULL
9826#ifdef FEAT_EVAL
9827 || redir_reg || redir_vname
9828#endif
9829 )
9830 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831}
9832
9833/*
9834 * ":redraw": force redraw
9835 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009837ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 int r = RedrawingDisabled;
9840 int p = p_lz;
9841
9842 RedrawingDisabled = 0;
9843 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009844 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009846 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847#ifdef FEAT_TITLE
9848 if (need_maketitle)
9849 maketitle();
9850#endif
9851 RedrawingDisabled = r;
9852 p_lz = p;
9853
9854 /* Reset msg_didout, so that a message that's there is overwritten. */
9855 msg_didout = FALSE;
9856 msg_col = 0;
9857
Bram Moolenaar56667a52013-07-17 11:54:28 +02009858 /* No need to wait after an intentional redraw. */
9859 need_wait_return = FALSE;
9860
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 out_flush();
9862}
9863
9864/*
9865 * ":redrawstatus": force redraw of status line(s)
9866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009868ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 int r = RedrawingDisabled;
9871 int p = p_lz;
9872
9873 RedrawingDisabled = 0;
9874 p_lz = FALSE;
9875 if (eap->forceit)
9876 status_redraw_all();
9877 else
9878 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009879 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 RedrawingDisabled = r;
9881 p_lz = p;
9882 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883}
9884
Bram Moolenaare12bab32019-01-08 22:02:56 +01009885/*
9886 * ":redrawtabline": force redraw of the tabline
9887 */
9888 static void
9889ex_redrawtabline(exarg_T *eap UNUSED)
9890{
9891 int r = RedrawingDisabled;
9892 int p = p_lz;
9893
9894 RedrawingDisabled = 0;
9895 p_lz = FALSE;
9896
9897 draw_tabline();
9898
9899 RedrawingDisabled = r;
9900 p_lz = p;
9901 out_flush();
9902}
9903
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009905close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
9907 if (redir_fd != NULL)
9908 {
9909 fclose(redir_fd);
9910 redir_fd = NULL;
9911 }
9912#ifdef FEAT_EVAL
9913 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009914 if (redir_vname)
9915 {
9916 var_redir_stop();
9917 redir_vname = 0;
9918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919#endif
9920}
9921
9922#if defined(FEAT_SESSION) && defined(USE_CRNL)
9923# define MKSESSION_NL
9924static int mksession_nl = FALSE; /* use NL only in put_eol() */
9925#endif
9926
9927/*
9928 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9929 */
9930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009931ex_mkrc(
9932 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933{
9934 FILE *fd;
9935 int failed = FALSE;
9936 char_u *fname;
9937#ifdef FEAT_BROWSE
9938 char_u *browseFile = NULL;
9939#endif
9940#ifdef FEAT_SESSION
9941 int view_session = FALSE;
9942 int using_vdir = FALSE; /* using 'viewdir'? */
9943 char_u *viewFile = NULL;
9944 unsigned *flagp;
9945#endif
9946
9947 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9948 {
9949#ifdef FEAT_SESSION
9950 view_session = TRUE;
9951#else
9952 ex_ni(eap);
9953 return;
9954#endif
9955 }
9956
9957#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009958 /* Use the short file name until ":lcd" is used. We also don't use the
9959 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009960 did_lcd = FALSE;
9961
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9963 if (eap->cmdidx == CMD_mkview
9964 && (*eap->arg == NUL
9965 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9966 {
9967 eap->forceit = TRUE;
9968 fname = get_view_file(*eap->arg);
9969 if (fname == NULL)
9970 return;
9971 viewFile = fname;
9972 using_vdir = TRUE;
9973 }
9974 else
9975#endif
9976 if (*eap->arg != NUL)
9977 fname = eap->arg;
9978 else if (eap->cmdidx == CMD_mkvimrc)
9979 fname = (char_u *)VIMRC_FILE;
9980#ifdef FEAT_SESSION
9981 else if (eap->cmdidx == CMD_mksession)
9982 fname = (char_u *)SESSION_FILE;
9983#endif
9984 else
9985 fname = (char_u *)EXRC_FILE;
9986
9987#ifdef FEAT_BROWSE
9988 if (cmdmod.browse)
9989 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009990 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991# ifdef FEAT_SESSION
9992 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9993 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9994# endif
9995 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009996 fname, (char_u *)"vim", NULL,
9997 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 if (browseFile == NULL)
9999 goto theend;
10000 fname = browseFile;
10001 eap->forceit = TRUE; /* since dialog already asked */
10002 }
10003#endif
10004
10005#if defined(FEAT_SESSION) && defined(vim_mkdir)
10006 /* When using 'viewdir' may have to create the directory. */
10007 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010008 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009#endif
10010
10011 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10012 if (fd != NULL)
10013 {
10014#ifdef FEAT_SESSION
10015 if (eap->cmdidx == CMD_mkview)
10016 flagp = &vop_flags;
10017 else
10018 flagp = &ssop_flags;
10019#endif
10020
10021#ifdef MKSESSION_NL
10022 /* "unix" in 'sessionoptions': use NL line separator */
10023 if (view_session && (*flagp & SSOP_UNIX))
10024 mksession_nl = TRUE;
10025#endif
10026
10027 /* Write the version command for :mkvimrc */
10028 if (eap->cmdidx == CMD_mkvimrc)
10029 (void)put_line(fd, "version 6.0");
10030
10031#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010032 if (eap->cmdidx == CMD_mksession)
10033 {
10034 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10035 failed = TRUE;
10036 }
10037
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 if (eap->cmdidx != CMD_mkview)
10039#endif
10040 {
10041 /* Write setting 'compatible' first, because it has side effects.
10042 * For that same reason only do it when needed. */
10043 if (p_cp)
10044 (void)put_line(fd, "if !&cp | set cp | endif");
10045 else
10046 (void)put_line(fd, "if &cp | set nocp | endif");
10047 }
10048
10049#ifdef FEAT_SESSION
10050 if (!view_session
10051 || (eap->cmdidx == CMD_mksession
10052 && (*flagp & SSOP_OPTIONS)))
10053#endif
10054 failed |= (makemap(fd, NULL) == FAIL
10055 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10056
10057#ifdef FEAT_SESSION
10058 if (!failed && view_session)
10059 {
10060 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10061 failed = TRUE;
10062 if (eap->cmdidx == CMD_mksession)
10063 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010064 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065
Bram Moolenaard9462e32011-04-11 21:35:11 +020010066 dirnow = alloc(MAXPATHL);
10067 if (dirnow == NULL)
10068 failed = TRUE;
10069 else
10070 {
10071 /*
10072 * Change to session file's dir.
10073 */
10074 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010076 *dirnow = NUL;
10077 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10078 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010079 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010080 shorten_fnames(TRUE);
10081 }
10082 else if (*dirnow != NUL
10083 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10084 {
10085 if (mch_chdir((char *)globaldir) == 0)
10086 shorten_fnames(TRUE);
10087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088
Bram Moolenaard9462e32011-04-11 21:35:11 +020010089 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090
Bram Moolenaard9462e32011-04-11 21:35:11 +020010091 /* restore original dir */
10092 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010094 {
10095 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010096 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010097 shorten_fnames(TRUE);
10098 }
10099 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 }
10101 }
10102 else
10103 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010104 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10105 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10108 == FAIL)
10109 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010110# ifdef FEAT_SEARCH_EXTRA
10111 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10112 failed = TRUE;
10113# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010114 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10115 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010116 if (eap->cmdidx == CMD_mksession)
10117 {
10118 if (put_line(fd, "unlet SessionLoad") == FAIL)
10119 failed = TRUE;
10120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 }
10122#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010123 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10124 failed = TRUE;
10125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 failed |= fclose(fd);
10127
10128 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010129 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10131 else if (eap->cmdidx == CMD_mksession)
10132 {
10133 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010134 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135
Bram Moolenaard9462e32011-04-11 21:35:11 +020010136 tbuf = alloc(MAXPATHL);
10137 if (tbuf != NULL)
10138 {
10139 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10140 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10141 vim_free(tbuf);
10142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 }
10144#endif
10145#ifdef MKSESSION_NL
10146 mksession_nl = FALSE;
10147#endif
10148 }
10149
10150#ifdef FEAT_BROWSE
10151theend:
10152 vim_free(browseFile);
10153#endif
10154#ifdef FEAT_SESSION
10155 vim_free(viewFile);
10156#endif
10157}
10158
Bram Moolenaardf177f62005-02-22 08:39:57 +000010159#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10160 || defined(PROTO)
10161 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010162vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010163{
10164 if (vim_mkdir(name, prot) != 0)
10165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010166 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010167 return FAIL;
10168 }
10169 return OK;
10170}
10171#endif
10172
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173/*
10174 * Open a file for writing for an Ex command, with some checks.
10175 * Return file descriptor, or NULL on failure.
10176 */
10177 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010178open_exfile(
10179 char_u *fname,
10180 int forceit,
10181 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182{
10183 FILE *fd;
10184
10185#ifdef UNIX
10186 /* with Unix it is possible to open a directory */
10187 if (mch_isdir(fname))
10188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010189 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 return NULL;
10191 }
10192#endif
10193 if (!forceit && *mode != 'a' && vim_fexists(fname))
10194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010195 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 return NULL;
10197 }
10198
10199 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010200 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
10202 return fd;
10203}
10204
10205/*
10206 * ":mark" and ":k".
10207 */
10208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010209ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211 pos_T pos;
10212
10213 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010214 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010216 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
10218 {
10219 pos = curwin->w_cursor; /* save curwin->w_cursor */
10220 curwin->w_cursor.lnum = eap->line2;
10221 beginline(BL_WHITE | BL_FIX);
10222 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010223 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10225 }
10226}
10227
10228/*
10229 * Update w_topline, w_leftcol and the cursor position.
10230 */
10231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010232update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233{
10234 check_cursor(); /* put cursor on valid line */
10235 update_topline();
10236 if (!curwin->w_p_wrap)
10237 validate_cursor();
10238 update_curswant();
10239}
10240
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010242 * Save the current State and go to Normal mode.
10243 * Return TRUE if the typeahead could be saved.
10244 */
10245 int
10246save_current_state(save_state_T *sst)
10247{
10248 sst->save_msg_scroll = msg_scroll;
10249 sst->save_restart_edit = restart_edit;
10250 sst->save_msg_didout = msg_didout;
10251 sst->save_State = State;
10252 sst->save_insertmode = p_im;
10253 sst->save_finish_op = finish_op;
10254 sst->save_opcount = opcount;
10255
10256 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10257 restart_edit = 0; /* don't go to Insert mode */
10258 p_im = FALSE; /* don't use 'insertmode' */
10259
10260 /*
10261 * Save the current typeahead. This is required to allow using ":normal"
10262 * from an event handler and makes sure we don't hang when the argument
10263 * ends with half a command.
10264 */
10265 save_typeahead(&sst->tabuf);
10266 return sst->tabuf.typebuf_valid;
10267}
10268
10269 void
10270restore_current_state(save_state_T *sst)
10271{
10272 /* Restore the previous typeahead. */
10273 restore_typeahead(&sst->tabuf);
10274
10275 msg_scroll = sst->save_msg_scroll;
10276 restart_edit = sst->save_restart_edit;
10277 p_im = sst->save_insertmode;
10278 finish_op = sst->save_finish_op;
10279 opcount = sst->save_opcount;
10280 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10281
10282 /* Restore the state (needed when called from a function executed for
10283 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10284 State = sst->save_State;
10285#ifdef CURSOR_SHAPE
10286 ui_cursor_shape(); /* may show different cursor shape */
10287#endif
10288}
10289
10290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 * ":normal[!] {commands}": Execute normal mode commands.
10292 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010293 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010294ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010296 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 char_u *arg = NULL;
10298 int l;
10299 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010301 if (ex_normal_lock > 0)
10302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010303 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010304 return;
10305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 if (ex_normal_busy >= p_mmd)
10307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010308 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 return;
10310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 /*
10313 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10314 * this for the K_SPECIAL leading byte, otherwise special keys will not
10315 * work.
10316 */
10317 if (has_mbyte)
10318 {
10319 int len = 0;
10320
10321 /* Count the number of characters to be escaped. */
10322 for (p = eap->arg; *p != NUL; ++p)
10323 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010324#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 if (*p == CSI) /* leadbyte CSI */
10326 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +010010327#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010328 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +010010330#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +010010332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 )
10334 len += 2;
10335 }
10336 if (len > 0)
10337 {
10338 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10339 if (arg != NULL)
10340 {
10341 len = 0;
10342 for (p = eap->arg; *p != NUL; ++p)
10343 {
10344 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +010010345#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 if (*p == CSI)
10347 {
10348 arg[len++] = KS_EXTRA;
10349 arg[len++] = (int)KE_CSI;
10350 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010351#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010352 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 {
10354 arg[len++] = *++p;
10355 if (*p == K_SPECIAL)
10356 {
10357 arg[len++] = KS_SPECIAL;
10358 arg[len++] = KE_FILLER;
10359 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010360#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 else if (*p == CSI)
10362 {
10363 arg[len++] = KS_EXTRA;
10364 arg[len++] = (int)KE_CSI;
10365 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 }
10368 arg[len] = NUL;
10369 }
10370 }
10371 }
10372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010374 ++ex_normal_busy;
10375 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 {
10377 /*
10378 * Repeat the :normal command for each line in the range. When no
10379 * range given, execute it just once, without positioning the cursor
10380 * first.
10381 */
10382 do
10383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 if (eap->addr_count != 0)
10385 {
10386 curwin->w_cursor.lnum = eap->line1++;
10387 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010388 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 }
10390
Bram Moolenaar13505972019-01-24 15:04:48 +010010391 exec_normal_cmd(arg != NULL
10392 ? arg
10393 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
10395 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10396 }
10397
10398 /* Might not return to the main loop when in an event handler. */
10399 update_topline_cursor();
10400
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010401 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010403#ifdef FEAT_MOUSE
10404 setmouse();
10405#endif
10406#ifdef CURSOR_SHAPE
10407 ui_cursor_shape(); /* may show different cursor shape */
10408#endif
10409
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411}
10412
10413/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010414 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 */
10416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010417ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010419 if (eap->forceit)
10420 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010421 /* cursor line can be zero on startup */
10422 if (!curwin->w_cursor.lnum)
10423 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010424 coladvance((colnr_T)MAXCOL);
10425 curwin->w_curswant = MAXCOL;
10426 curwin->w_set_curswant = FALSE;
10427 }
10428
Bram Moolenaara40c5002005-01-09 21:16:21 +000010429 /* Ignore the command when already in Insert mode. Inserting an
10430 * expression register that invokes a function can do this. */
10431 if (State & INSERT)
10432 return;
10433
Bram Moolenaar64969662005-12-14 21:59:55 +000010434 if (eap->cmdidx == CMD_startinsert)
10435 restart_edit = 'a';
10436 else if (eap->cmdidx == CMD_startreplace)
10437 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010439 restart_edit = 'V';
10440
10441 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010443 if (eap->cmdidx == CMD_startinsert)
10444 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 curwin->w_curswant = 0; /* avoid MAXCOL */
10446 }
10447}
10448
10449/*
10450 * ":stopinsert"
10451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010453ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454{
10455 restart_edit = 0;
10456 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010457 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010460/*
10461 * Execute normal mode command "cmd".
10462 * "remap" can be REMAP_NONE or REMAP_YES.
10463 */
10464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010465exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010466{
Bram Moolenaar25281632016-01-21 23:32:32 +010010467 /* Stuff the argument into the typeahead buffer. */
10468 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010469 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010470}
Bram Moolenaar25281632016-01-21 23:32:32 +010010471
Bram Moolenaar25281632016-01-21 23:32:32 +010010472/*
10473 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010474 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010475 */
10476 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010477exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010478{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010479 oparg_T oa;
10480
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010481 clear_oparg(&oa);
10482 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010483 while ((!stuff_empty()
10484 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10485 || (use_vpeekc && vpeekc() != NUL))
10486 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010487 {
10488 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010489#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010490 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010491 && oa.op_type == OP_NOP && oa.regname == NUL
10492 && !VIsual_active)
10493 {
10494 /* If terminal_loop() returns OK we got a key that is handled
10495 * in Normal model. With FAIL we first need to position the
10496 * cursor and the screen needs to be redrawn. */
10497 if (terminal_loop(TRUE) == OK)
10498 normal_cmd(&oa, TRUE);
10499 }
10500 else
10501#endif
10502 /* execute a Normal mode cmd */
10503 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010504 }
10505}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010506
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507#ifdef FEAT_FIND_ID
10508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010509ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510{
10511 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10512 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10513 (linenr_T)1, (linenr_T)MAXLNUM);
10514}
10515
Bram Moolenaar4033c552017-09-16 20:54:51 +020010516#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517/*
10518 * ":psearch"
10519 */
10520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010521ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522{
10523 g_do_tagpreview = p_pvh;
10524 ex_findpat(eap);
10525 g_do_tagpreview = 0;
10526}
10527#endif
10528
10529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010530ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 int whole = TRUE;
10533 long n;
10534 char_u *p;
10535 int action;
10536
10537 switch (cmdnames[eap->cmdidx].cmd_name[2])
10538 {
10539 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10540 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10541 action = ACTION_GOTO;
10542 else
10543 action = ACTION_SHOW;
10544 break;
10545 case 'i': /* ":ilist" and ":dlist" */
10546 action = ACTION_SHOW_ALL;
10547 break;
10548 case 'u': /* ":ijump" and ":djump" */
10549 action = ACTION_GOTO;
10550 break;
10551 default: /* ":isplit" and ":dsplit" */
10552 action = ACTION_SPLIT;
10553 break;
10554 }
10555
10556 n = 1;
10557 if (vim_isdigit(*eap->arg)) /* get count */
10558 {
10559 n = getdigits(&eap->arg);
10560 eap->arg = skipwhite(eap->arg);
10561 }
10562 if (*eap->arg == '/') /* Match regexp, not just whole words */
10563 {
10564 whole = FALSE;
10565 ++eap->arg;
10566 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10567 if (*p)
10568 {
10569 *p++ = NUL;
10570 p = skipwhite(p);
10571
10572 /* Check for trailing illegal characters */
10573 if (!ends_excmd(*p))
10574 eap->errmsg = e_trailing;
10575 else
10576 eap->nextcmd = check_nextcmd(p);
10577 }
10578 }
10579 if (!eap->skip)
10580 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10581 whole, !eap->forceit,
10582 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10583 n, action, eap->line1, eap->line2);
10584}
10585#endif
10586
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587
Bram Moolenaar4033c552017-09-16 20:54:51 +020010588#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589/*
10590 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10591 */
10592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010593ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010595 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10597}
10598
10599/*
10600 * ":pedit"
10601 */
10602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010603ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
10605 win_T *curwin_save = curwin;
10606
10607 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010608 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010609 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 do_exedit(eap, NULL);
10611 keep_help_flag = FALSE;
10612 if (curwin != curwin_save && win_valid(curwin_save))
10613 {
10614 /* Return cursor to where we were */
10615 validate_cursor();
10616 redraw_later(VALID);
10617 win_enter(curwin_save, TRUE);
10618 }
10619 g_do_tagpreview = 0;
10620}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622
10623/*
10624 * ":stag", ":stselect" and ":stjump".
10625 */
10626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010627ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628{
10629 postponed_split = -1;
10630 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010631 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10633 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010634 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636
10637/*
10638 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10639 */
10640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010641ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
10643 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10644}
10645
10646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010647ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648{
10649 int cmd;
10650
10651 switch (name[1])
10652 {
10653 case 'j': cmd = DT_JUMP; /* ":tjump" */
10654 break;
10655 case 's': cmd = DT_SELECT; /* ":tselect" */
10656 break;
10657 case 'p': cmd = DT_PREV; /* ":tprevious" */
10658 break;
10659 case 'N': cmd = DT_PREV; /* ":tNext" */
10660 break;
10661 case 'n': cmd = DT_NEXT; /* ":tnext" */
10662 break;
10663 case 'o': cmd = DT_POP; /* ":pop" */
10664 break;
10665 case 'f': /* ":tfirst" */
10666 case 'r': cmd = DT_FIRST; /* ":trewind" */
10667 break;
10668 case 'l': cmd = DT_LAST; /* ":tlast" */
10669 break;
10670 default: /* ":tag" */
10671#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010672 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010674 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 return;
10676 }
10677#endif
10678 cmd = DT_TAG;
10679 break;
10680 }
10681
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010682 if (name[0] == 'l')
10683 {
10684#ifndef FEAT_QUICKFIX
10685 ex_ni(eap);
10686 return;
10687#else
10688 cmd = DT_LTAG;
10689#endif
10690 }
10691
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10693 eap->forceit, TRUE);
10694}
10695
10696/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010697 * Check "str" for starting with a special cmdline variable.
10698 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10699 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10700 */
10701 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010702find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010703{
10704 int len;
10705 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010706 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010707 "%",
10708#define SPEC_PERC 0
10709 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010710#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010711 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010712#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010713 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010714#define SPEC_CCWORD (SPEC_CWORD + 1)
10715 "<cexpr>", /* expr under cursor */
10716#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010717 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010718#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010719 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010720#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010721 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010722#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010723 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010724#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010725 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010726#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010727 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010728#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010729 "<sflnum>", /* script file line number */
10730#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010731#ifdef FEAT_CLIENTSERVER
10732 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010733# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010734#endif
10735 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010736
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010737 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010738 {
10739 len = (int)STRLEN(spec_str[i]);
10740 if (STRNCMP(src, spec_str[i], len) == 0)
10741 {
10742 *usedlen = len;
10743 return i;
10744 }
10745 }
10746 return -1;
10747}
10748
10749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 * Evaluate cmdline variables.
10751 *
10752 * change '%' to curbuf->b_ffname
10753 * '#' to curwin->w_altfile
10754 * '<cword>' to word under the cursor
10755 * '<cWORD>' to WORD under the cursor
10756 * '<cfile>' to path name under the cursor
10757 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010758 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 * '<afile>' to file name for autocommand
10760 * '<abuf>' to buffer number for autocommand
10761 * '<amatch>' to matching name for autocommand
10762 *
10763 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10764 * "" for error without a message) and NULL is returned.
10765 * Returns an allocated string if a valid match was found.
10766 * Returns NULL if no match was found. "usedlen" then still contains the
10767 * number of characters to skip.
10768 */
10769 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010770eval_vars(
10771 char_u *src, /* pointer into commandline */
10772 char_u *srcstart, /* beginning of valid memory for src */
10773 int *usedlen, /* characters after src that are used */
10774 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010775 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010776 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010777 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778{
10779 int i;
10780 char_u *s;
10781 char_u *result;
10782 char_u *resultbuf = NULL;
10783 int resultlen;
10784 buf_T *buf;
10785 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10786 int spec_idx;
10787#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010788 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 int skip_mod = FALSE;
10790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792
10793 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010794 if (escaped != NULL)
10795 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796
10797 /*
10798 * Check if there is something to do.
10799 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010800 spec_idx = find_cmdline_var(src, usedlen);
10801 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 {
10803 *usedlen = 1;
10804 return NULL;
10805 }
10806
10807 /*
10808 * Skip when preceded with a backslash "\%" and "\#".
10809 * Note: In "\\%" the % is also not recognized!
10810 */
10811 if (src > srcstart && src[-1] == '\\')
10812 {
10813 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010814 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 return NULL;
10816 }
10817
10818 /*
10819 * word or WORD under cursor
10820 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010821 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10822 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010824 resultlen = find_ident_under_cursor(&result,
10825 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10826 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10827 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 if (resultlen == 0)
10829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010830 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 return NULL;
10832 }
10833 }
10834
10835 /*
10836 * '#': Alternate file name
10837 * '%': Current file name
10838 * File name under the cursor
10839 * File name for autocommand
10840 * and following modifiers
10841 */
10842 else
10843 {
10844 switch (spec_idx)
10845 {
10846 case SPEC_PERC: /* '%': current file */
10847 if (curbuf->b_fname == NULL)
10848 {
10849 result = (char_u *)"";
10850 valid = 0; /* Must have ":p:h" to be valid */
10851 }
10852 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010855#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010856 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010857#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 break;
10860
10861 case SPEC_HASH: /* '#' or "#99": alternate file */
10862 if (src[1] == '#') /* "##": the argument list */
10863 {
10864 result = arg_all();
10865 resultbuf = result;
10866 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010867 if (escaped != NULL)
10868 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869#ifdef FEAT_MODIFY_FNAME
10870 skip_mod = TRUE;
10871#endif
10872 break;
10873 }
10874 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010875 if (*s == '<') /* "#<99" uses v:oldfiles */
10876 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010878 if (s == src + 2 && src[1] == '-')
10879 /* just a minus sign, don't skip over it */
10880 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 *usedlen = (int)(s - src); /* length of what we expand */
10882
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010883 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010885 if (*usedlen < 2)
10886 {
10887 /* Should we give an error message for #<text? */
10888 *usedlen = 1;
10889 return NULL;
10890 }
10891#ifdef FEAT_EVAL
10892 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10893 (long)i);
10894 if (result == NULL)
10895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010896 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010897 return NULL;
10898 }
10899#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010900 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 }
10904 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010905 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010906 if (i == 0 && src[1] == '<' && *usedlen > 1)
10907 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010908 buf = buflist_findnr(i);
10909 if (buf == NULL)
10910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010911 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010912 return NULL;
10913 }
10914 if (lnump != NULL)
10915 *lnump = ECMD_LAST;
10916 if (buf->b_fname == NULL)
10917 {
10918 result = (char_u *)"";
10919 valid = 0; /* Must have ":p:h" to be valid */
10920 }
10921 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010922 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010923 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010924#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010925 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010926#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010927 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 break;
10930
10931#ifdef FEAT_SEARCHPATH
10932 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010933 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (result == NULL)
10935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010936 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 return NULL;
10938 }
10939 resultbuf = result; /* remember allocated string */
10940 break;
10941#endif
10942
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 case SPEC_AFILE: /* file name for autocommand */
10944 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010945 if (result != NULL && !autocmd_fname_full)
10946 {
10947 /* Still need to turn the fname into a full path. It is
10948 * postponed to avoid a delay when <afile> is not used. */
10949 autocmd_fname_full = TRUE;
10950 result = FullName_save(autocmd_fname, FALSE);
10951 vim_free(autocmd_fname);
10952 autocmd_fname = result;
10953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 if (result == NULL)
10955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010956 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 return NULL;
10958 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010959 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 break;
10961
10962 case SPEC_ABUF: /* buffer number for autocommand */
10963 if (autocmd_bufnr <= 0)
10964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010965 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 return NULL;
10967 }
10968 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10969 result = strbuf;
10970 break;
10971
10972 case SPEC_AMATCH: /* match name for autocommand */
10973 result = autocmd_match;
10974 if (result == NULL)
10975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010976 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 return NULL;
10978 }
10979 break;
10980
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 case SPEC_SFILE: /* file name for ":so" command */
10982 result = sourcing_name;
10983 if (result == NULL)
10984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010985 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 return NULL;
10987 }
10988 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010989
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010990 case SPEC_SLNUM: /* line in file for ":so" command */
10991 if (sourcing_name == NULL || sourcing_lnum == 0)
10992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010993 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010994 return NULL;
10995 }
10996 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10997 result = strbuf;
10998 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010999
11000#ifdef FEAT_EVAL
11001 case SPEC_SFLNUM: /* line in script file */
11002 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011004 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011005 return NULL;
11006 }
11007 sprintf((char *)strbuf, "%ld",
11008 (long)(current_sctx.sc_lnum + sourcing_lnum));
11009 result = strbuf;
11010 break;
11011#endif
11012
11013#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011015 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11016 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 result = strbuf;
11018 break;
11019#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011020
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011021 default:
11022 result = (char_u *)""; /* avoid gcc warning */
11023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 }
11025
11026 resultlen = (int)STRLEN(result); /* length of new string */
11027 if (src[*usedlen] == '<') /* remove the file name extension */
11028 {
11029 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 resultlen = (int)(s - result);
11032 }
11033#ifdef FEAT_MODIFY_FNAME
11034 else if (!skip_mod)
11035 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011036 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 &resultlen);
11038 if (result == NULL)
11039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011040 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 return NULL;
11042 }
11043 }
11044#endif
11045 }
11046
11047 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11048 {
11049 if (valid != VALID_HEAD + VALID_PATH)
11050 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011051 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011053 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 result = NULL;
11055 }
11056 else
11057 result = vim_strnsave(result, resultlen);
11058 vim_free(resultbuf);
11059 return result;
11060}
11061
11062/*
11063 * Concatenate all files in the argument list, separated by spaces, and return
11064 * it in one allocated string.
11065 * Spaces and backslashes in the file names are escaped with a backslash.
11066 * Returns NULL when out of memory.
11067 */
11068 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011069arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070{
11071 int len;
11072 int idx;
11073 char_u *retval = NULL;
11074 char_u *p;
11075
11076 /*
11077 * Do this loop two times:
11078 * first time: compute the total length
11079 * second time: concatenate the names
11080 */
11081 for (;;)
11082 {
11083 len = 0;
11084 for (idx = 0; idx < ARGCOUNT; ++idx)
11085 {
11086 p = alist_name(&ARGLIST[idx]);
11087 if (p != NULL)
11088 {
11089 if (len > 0)
11090 {
11091 /* insert a space in between names */
11092 if (retval != NULL)
11093 retval[len] = ' ';
11094 ++len;
11095 }
11096 for ( ; *p != NUL; ++p)
11097 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011098 if (*p == ' '
11099#ifndef BACKSLASH_IN_FILENAME
11100 || *p == '\\'
11101#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011102 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 {
11104 /* insert a backslash */
11105 if (retval != NULL)
11106 retval[len] = '\\';
11107 ++len;
11108 }
11109 if (retval != NULL)
11110 retval[len] = *p;
11111 ++len;
11112 }
11113 }
11114 }
11115
11116 /* second time: break here */
11117 if (retval != NULL)
11118 {
11119 retval[len] = NUL;
11120 break;
11121 }
11122
11123 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011124 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 if (retval == NULL)
11126 break;
11127 }
11128
11129 return retval;
11130}
11131
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132/*
11133 * Expand the <sfile> string in "arg".
11134 *
11135 * Returns an allocated string, or NULL for any error.
11136 */
11137 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011138expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011140 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 int len;
11142 char_u *result;
11143 char_u *newres;
11144 char_u *repl;
11145 int srclen;
11146 char_u *p;
11147
11148 result = vim_strsave(arg);
11149 if (result == NULL)
11150 return NULL;
11151
11152 for (p = result; *p; )
11153 {
11154 if (STRNCMP(p, "<sfile>", 7) != 0)
11155 ++p;
11156 else
11157 {
11158 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011159 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 if (errormsg != NULL)
11161 {
11162 if (*errormsg)
11163 emsg(errormsg);
11164 vim_free(result);
11165 return NULL;
11166 }
11167 if (repl == NULL) /* no match (cannot happen) */
11168 {
11169 p += srclen;
11170 continue;
11171 }
11172 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11173 newres = alloc(len);
11174 if (newres == NULL)
11175 {
11176 vim_free(repl);
11177 vim_free(result);
11178 return NULL;
11179 }
11180 mch_memmove(newres, result, (size_t)(p - result));
11181 STRCPY(newres + (p - result), repl);
11182 len = (int)STRLEN(newres);
11183 STRCAT(newres, p + srclen);
11184 vim_free(repl);
11185 vim_free(result);
11186 result = newres;
11187 p = newres + len; /* continue after the match */
11188 }
11189 }
11190
11191 return result;
11192}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193
11194#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011195static int ses_winsizes(FILE *fd, int restore_size,
11196 win_T *tab_firstwin);
11197static int ses_win_rec(FILE *fd, frame_T *fr);
11198static frame_T *ses_skipframe(frame_T *fr);
11199static int ses_do_frame(frame_T *fr);
11200static int ses_do_win(win_T *wp);
11201static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11202static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011203static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204
11205/*
11206 * Write openfile commands for the current buffers to an .exrc file.
11207 * Return FAIL on error, OK otherwise.
11208 */
11209 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011210makeopens(
11211 FILE *fd,
11212 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
11214 buf_T *buf;
11215 int only_save_windows = TRUE;
11216 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 int restore_size = TRUE;
11218 win_T *wp;
11219 char_u *sname;
11220 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011221 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011222 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011223 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011224 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011225 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011226 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227
11228 if (ssop_flags & SSOP_BUFFERS)
11229 only_save_windows = FALSE; /* Save ALL buffers */
11230
11231 /*
11232 * Begin by setting the this_session variable, and then other
11233 * sessionable variables.
11234 */
11235#ifdef FEAT_EVAL
11236 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11237 return FAIL;
11238 if (ssop_flags & SSOP_GLOBALS)
11239 if (store_session_globals(fd) == FAIL)
11240 return FAIL;
11241#endif
11242
11243 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011244 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 */
11246 if (put_line(fd, "silent only") == FAIL)
11247 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011248 if ((ssop_flags & SSOP_TABPAGES)
11249 && put_line(fd, "silent tabonly") == FAIL)
11250 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
11252 /*
11253 * Now a :cd command to the session directory or the current directory
11254 */
11255 if (ssop_flags & SSOP_SESDIR)
11256 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011257 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11258 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 return FAIL;
11260 }
11261 else if (ssop_flags & SSOP_CURDIR)
11262 {
11263 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11264 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011265 || fputs("cd ", fd) < 0
11266 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11267 || put_eol(fd) == FAIL)
11268 {
11269 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 vim_free(sname);
11273 }
11274
11275 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011276 * If there is an empty, unnamed buffer we will wipe it out later.
11277 * Remember the buffer number.
11278 */
11279 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11280 return FAIL;
11281 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11282 return FAIL;
11283 if (put_line(fd, "endif") == FAIL)
11284 return FAIL;
11285
11286 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 * Now save the current files, current buffer first.
11288 */
11289 if (put_line(fd, "set shortmess=aoO") == FAIL)
11290 return FAIL;
11291
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011293 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11295 return FAIL;
11296
11297 if (ssop_flags & SSOP_RESIZE)
11298 {
11299 /* Note: after the restore we still check it worked!*/
11300 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11301 || put_eol(fd) == FAIL)
11302 return FAIL;
11303 }
11304
11305#ifdef FEAT_GUI
11306 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11307 {
11308 int x, y;
11309
11310 if (gui_mch_get_winpos(&x, &y) == OK)
11311 {
11312 /* Note: after the restore we still check it worked!*/
11313 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11314 return FAIL;
11315 }
11316 }
11317#endif
11318
11319 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011320 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11321 * will be displayed when creating the next tab. That resizes the windows
11322 * in the first tab, which may cause problems. Set 'showtabline' to 2
11323 * temporarily to avoid that.
11324 */
11325 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11326 {
11327 if (put_line(fd, "set stal=2") == FAIL)
11328 return FAIL;
11329 restore_stal = TRUE;
11330 }
11331
11332 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011333 * May repeat putting Windows for each tab, when "tabpages" is in
11334 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011335 * Don't use goto_tabpage(), it may change directory and trigger
11336 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011338 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011339 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011340 if ((ssop_flags & SSOP_TABPAGES))
11341 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011342 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011343
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011344 // Similar to ses_win_rec() below, populate the tab pages first so
11345 // later local options won't be copied to the new tabs.
11346 FOR_ALL_TABPAGES(tp)
11347 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011348 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011349 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011350 return FAIL;
11351 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011352 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011354 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011355 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011356
Bram Moolenaar18144c82006-04-12 21:52:12 +000011357 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011359 tabpage_T *tp = find_tabpage(tabnr);
11360
11361 if (tp == NULL)
11362 break; /* done all tab pages */
11363 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011364 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011365 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011366 tab_topframe = topframe;
11367 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011368 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011369 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011370 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011371 tab_topframe = tp->tp_topframe;
11372 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011373 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011374 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376
Bram Moolenaar18144c82006-04-12 21:52:12 +000011377 /*
11378 * Before creating the window layout, try loading one file. If this
11379 * is aborted we don't end up with a number of useless windows.
11380 * This may have side effects! (e.g., compressed or network file).
11381 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011382 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011383 {
11384 if (ses_do_win(wp)
11385 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011386 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011387#ifdef FEAT_QUICKFIX
11388 && !bt_nofile(wp->w_buffer)
11389#endif
11390 )
11391 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011392 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11393 return FAIL;
11394 need_tabnext = FALSE;
11395
11396 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011397 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11398 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011399 return FAIL;
11400 if (!wp->w_arg_idx_invalid)
11401 edited_win = wp;
11402 break;
11403 }
11404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011406 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011407 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011408 return FAIL;
11409
Bram Moolenaar18144c82006-04-12 21:52:12 +000011410 /*
11411 * Save current window layout.
11412 */
11413 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011415 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011417 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11418 return FAIL;
11419 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11420 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421
Bram Moolenaar18144c82006-04-12 21:52:12 +000011422 /*
11423 * Check if window sizes can be restored (no windows omitted).
11424 * Remember the window number of the current window after restoring.
11425 */
11426 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011427 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011428 {
11429 if (ses_do_win(wp))
11430 ++nr;
11431 else
11432 restore_size = FALSE;
11433 if (curwin == wp)
11434 cnr = nr;
11435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436
Bram Moolenaar18144c82006-04-12 21:52:12 +000011437 /* Go to the first window. */
11438 if (put_line(fd, "wincmd t") == FAIL)
11439 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011440
Bram Moolenaar18144c82006-04-12 21:52:12 +000011441 /*
11442 * If more than one window, see if sizes can be restored.
11443 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11444 * resized when moving between windows.
11445 * Do this before restoring the view, so that the topline and the
11446 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011447 * winminheight and winminwidth need to be set to avoid an error if the
11448 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011449 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011450 if (put_line(fd, "set winminheight=0") == FAIL
11451 || put_line(fd, "set winheight=1") == FAIL
11452 || put_line(fd, "set winminwidth=0") == FAIL
11453 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011454 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011455 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011456 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457
Bram Moolenaar18144c82006-04-12 21:52:12 +000011458 /*
11459 * Restore the view of the window (options, file, cursor, etc.).
11460 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011461 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011462 {
11463 if (!ses_do_win(wp))
11464 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011465 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11466 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011467 return FAIL;
11468 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11469 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011470 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011471 }
11472
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011473 /* The argument index in the first tab page is zero, need to set it in
11474 * each window. For further tab pages it's the window where we do
11475 * "tabedit". */
11476 cur_arg_idx = next_arg_idx;
11477
Bram Moolenaar18144c82006-04-12 21:52:12 +000011478 /*
11479 * Restore cursor to the current window if it's not the first one.
11480 */
11481 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11482 || put_eol(fd) == FAIL))
11483 return FAIL;
11484
11485 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011486 * Restore window sizes again after jumping around in windows, because
11487 * the current window has a minimum size while others may not.
11488 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011489 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011490 return FAIL;
11491
Bram Moolenaar18144c82006-04-12 21:52:12 +000011492 /* Don't continue in another tab page when doing only the current one
11493 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011494 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011495 break;
11496 }
11497
11498 if (ssop_flags & SSOP_TABPAGES)
11499 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011500 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11501 || put_eol(fd) == FAIL)
11502 return FAIL;
11503 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011504 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11505 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011506
Bram Moolenaard39e2752019-01-26 20:07:38 +010011507 // Now put the remaining buffers into the buffer list.
11508 // This is near the end, so that when 'hidden' is set we don't create extra
11509 // buffers. If the buffer was already created with another command the
11510 // ":badd" will have no effect.
11511 FOR_ALL_BUFFERS(buf)
11512 {
11513 if (!(only_save_windows && buf->b_nwindows == 0)
11514 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11515#ifdef FEAT_TERMINAL
11516 // Skip terminal buffers: finished ones are not useful, others
11517 // will be resurrected and result in a new buffer.
11518 && !bt_terminal(buf)
11519#endif
11520 && buf->b_fname != NULL
11521 && buf->b_p_bl)
11522 {
11523 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11524 : buf->b_wininfo->wi_fpos.lnum) < 0
11525 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
11526 return FAIL;
11527 }
11528 }
11529
Bram Moolenaar9c102382006-05-03 21:26:49 +000011530 /*
11531 * Wipe out an empty unnamed buffer we started in.
11532 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011533 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011534 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011535 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011536 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011537 return FAIL;
11538 if (put_line(fd, "endif") == FAIL)
11539 return FAIL;
11540 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11541 return FAIL;
11542
11543 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11544 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11545 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11546 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011547 /* Re-apply 'winminheight' and 'winminwidth'. */
11548 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11549 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551
11552 /*
11553 * Lastly, execute the x.vim file if it exists.
11554 */
11555 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11556 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011557 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 || put_line(fd, "endif") == FAIL)
11559 return FAIL;
11560
11561 return OK;
11562}
11563
11564 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011565ses_winsizes(
11566 FILE *fd,
11567 int restore_size,
11568 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
11570 int n = 0;
11571 win_T *wp;
11572
11573 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11574 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011575 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 {
11577 if (!ses_do_win(wp))
11578 continue;
11579 ++n;
11580
11581 /* restore height when not full height */
11582 if (wp->w_height + wp->w_status_height < topframe->fr_height
11583 && (fprintf(fd,
11584 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11585 n, (long)wp->w_height, Rows / 2, Rows) < 0
11586 || put_eol(fd) == FAIL))
11587 return FAIL;
11588
11589 /* restore width when not full width */
11590 if (wp->w_width < Columns && (fprintf(fd,
11591 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11592 n, (long)wp->w_width, Columns / 2, Columns) < 0
11593 || put_eol(fd) == FAIL))
11594 return FAIL;
11595 }
11596 }
11597 else
11598 {
11599 /* Just equalise window sizes */
11600 if (put_line(fd, "wincmd =") == FAIL)
11601 return FAIL;
11602 }
11603 return OK;
11604}
11605
11606/*
11607 * Write commands to "fd" to recursively create windows for frame "fr",
11608 * horizontally and vertically split.
11609 * After the commands the last window in the frame is the current window.
11610 * Returns FAIL when writing the commands to "fd" fails.
11611 */
11612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011613ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614{
11615 frame_T *frc;
11616 int count = 0;
11617
11618 if (fr->fr_layout != FR_LEAF)
11619 {
11620 /* Find first frame that's not skipped and then create a window for
11621 * each following one (first frame is already there). */
11622 frc = ses_skipframe(fr->fr_child);
11623 if (frc != NULL)
11624 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11625 {
11626 /* Make window as big as possible so that we have lots of room
11627 * to split. */
11628 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11629 || put_line(fd, fr->fr_layout == FR_COL
11630 ? "split" : "vsplit") == FAIL)
11631 return FAIL;
11632 ++count;
11633 }
11634
11635 /* Go back to the first window. */
11636 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11637 ? "%dwincmd k" : "%dwincmd h", count) < 0
11638 || put_eol(fd) == FAIL))
11639 return FAIL;
11640
11641 /* Recursively create frames/windows in each window of this column or
11642 * row. */
11643 frc = ses_skipframe(fr->fr_child);
11644 while (frc != NULL)
11645 {
11646 ses_win_rec(fd, frc);
11647 frc = ses_skipframe(frc->fr_next);
11648 /* Go to next window. */
11649 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11650 return FAIL;
11651 }
11652 }
11653 return OK;
11654}
11655
11656/*
11657 * Skip frames that don't contain windows we want to save in the Session.
11658 * Returns NULL when there none.
11659 */
11660 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011661ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
11663 frame_T *frc;
11664
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011665 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 if (ses_do_frame(frc))
11667 break;
11668 return frc;
11669}
11670
11671/*
11672 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11673 * the Session.
11674 */
11675 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011676ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677{
11678 frame_T *frc;
11679
11680 if (fr->fr_layout == FR_LEAF)
11681 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011682 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 if (ses_do_frame(frc))
11684 return TRUE;
11685 return FALSE;
11686}
11687
11688/*
11689 * Return non-zero if window "wp" is to be stored in the Session.
11690 */
11691 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011692ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011694#ifdef FEAT_TERMINAL
11695 if (bt_terminal(wp->w_buffer))
11696 return !term_is_finished(wp->w_buffer)
11697 && (ssop_flags & SSOP_TERMINAL)
11698 && term_should_restore(wp->w_buffer);
11699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 if (wp->w_buffer->b_fname == NULL
11701#ifdef FEAT_QUICKFIX
11702 /* When 'buftype' is "nofile" can't restore the window contents. */
11703 || bt_nofile(wp->w_buffer)
11704#endif
11705 )
11706 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011707 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 return (ssop_flags & SSOP_HELP);
11709 return TRUE;
11710}
11711
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011712 static int
11713put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11714{
11715 int r;
11716
11717 if (wp->w_curswant == MAXCOL)
11718 r = fprintf(fd, "%snormal! $", spaces);
11719 else
11720 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11721 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11722}
11723
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724/*
11725 * Write commands to "fd" to restore the view of a window.
11726 * Caller must make sure 'scrolloff' is zero.
11727 */
11728 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011729put_view(
11730 FILE *fd,
11731 win_T *wp,
11732 int add_edit, /* add ":edit" command to view */
11733 unsigned *flagp, /* vop_flags or ssop_flags */
11734 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011735 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736{
11737 win_T *save_curwin;
11738 int f;
11739 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011740 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741
11742 /* Always restore cursor position for ":mksession". For ":mkview" only
11743 * when 'viewoptions' contains "cursor". */
11744 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11745
11746 /*
11747 * Local argument list.
11748 */
11749 if (wp->w_alist == &global_alist)
11750 {
11751 if (put_line(fd, "argglobal") == FAIL)
11752 return FAIL;
11753 }
11754 else
11755 {
11756 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11757 flagp == &vop_flags
11758 || !(*flagp & SSOP_CURDIR)
11759 || wp->w_localdir != NULL, flagp) == FAIL)
11760 return FAIL;
11761 }
11762
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011763 /* Only when part of a session: restore the argument index. Some
11764 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011765 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011766 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011768 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 || put_eol(fd) == FAIL)
11770 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011771 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 }
11773
11774 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011775 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011777# ifdef FEAT_TERMINAL
11778 if (bt_terminal(wp->w_buffer))
11779 {
11780 if (term_write_session(fd, wp) == FAIL)
11781 return FAIL;
11782 }
11783 else
11784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 /*
11786 * Load the file.
11787 */
11788 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011789# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 )
11793 {
11794 /*
11795 * Editing a file in this buffer: use ":edit file".
11796 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011797 *
11798 * Note, if a buffer for that file already exists, use :badd to
11799 * edit that buffer, to not lose folding information (:edit resets
11800 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010011802 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011803 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010011804 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011805 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11806 || fputs(" | else | edit ", fd) < 0
11807 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11808 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011809 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 return FAIL;
11811 }
11812 else
11813 {
11814 /* No file in this buffer, just make it empty. */
11815 if (put_line(fd, "enew") == FAIL)
11816 return FAIL;
11817#ifdef FEAT_QUICKFIX
11818 if (wp->w_buffer->b_ffname != NULL)
11819 {
11820 /* The buffer does have a name, but it's not a file name. */
11821 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011822 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 return FAIL;
11824 }
11825#endif
11826 do_cursor = FALSE;
11827 }
11828 }
11829
11830 /*
11831 * Local mappings and abbreviations.
11832 */
11833 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11834 && makemap(fd, wp->w_buffer) == FAIL)
11835 return FAIL;
11836
11837 /*
11838 * Local options. Need to go to the window temporarily.
11839 * Store only local values when using ":mkview" and when ":mksession" is
11840 * used and 'sessionoptions' doesn't include "options".
11841 * Some folding options are always stored when "folds" is included,
11842 * otherwise the folds would not be restored correctly.
11843 */
11844 save_curwin = curwin;
11845 curwin = wp;
11846 curbuf = curwin->w_buffer;
11847 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11848 f = makeset(fd, OPT_LOCAL,
11849 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11850#ifdef FEAT_FOLDING
11851 else if (*flagp & SSOP_FOLDS)
11852 f = makefoldset(fd);
11853#endif
11854 else
11855 f = OK;
11856 curwin = save_curwin;
11857 curbuf = curwin->w_buffer;
11858 if (f == FAIL)
11859 return FAIL;
11860
11861#ifdef FEAT_FOLDING
11862 /*
11863 * Save Folds when 'buftype' is empty and for help files.
11864 */
11865 if ((*flagp & SSOP_FOLDS)
11866 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011867 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 {
11869 if (put_folds(fd, wp) == FAIL)
11870 return FAIL;
11871 }
11872#endif
11873
11874 /*
11875 * Set the cursor after creating folds, since that moves the cursor.
11876 */
11877 if (do_cursor)
11878 {
11879
11880 /* Restore the cursor line in the file and relatively in the
11881 * window. Don't use "G", it changes the jumplist. */
11882 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11883 (long)wp->w_cursor.lnum,
11884 (long)(wp->w_cursor.lnum - wp->w_topline),
11885 (long)wp->w_height / 2, (long)wp->w_height) < 0
11886 || put_eol(fd) == FAIL
11887 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11888 || put_line(fd, "exe s:l") == FAIL
11889 || put_line(fd, "normal! zt") == FAIL
11890 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11891 || put_eol(fd) == FAIL)
11892 return FAIL;
11893 /* Restore the cursor column and left offset when not wrapping. */
11894 if (wp->w_cursor.col == 0)
11895 {
11896 if (put_line(fd, "normal! 0") == FAIL)
11897 return FAIL;
11898 }
11899 else
11900 {
11901 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11902 {
11903 if (fprintf(fd,
11904 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011905 (long)wp->w_virtcol + 1,
11906 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 (long)wp->w_width / 2, (long)wp->w_width) < 0
11908 || put_eol(fd) == FAIL
11909 || put_line(fd, "if s:c > 0") == FAIL
11910 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011911 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11912 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 || put_eol(fd) == FAIL
11914 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011915 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 || put_line(fd, "endif") == FAIL)
11917 return FAIL;
11918 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011919 else if (put_view_curpos(fd, wp, "") == FAIL)
11920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 }
11922 }
11923
11924 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011925 * Local directory, if the current flag is not view options or the "curdir"
11926 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011928 if (wp->w_localdir != NULL
11929 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 {
11931 if (fputs("lcd ", fd) < 0
11932 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11933 || put_eol(fd) == FAIL)
11934 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011935 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 }
11937
11938 return OK;
11939}
11940
11941/*
11942 * Write an argument list to the session file.
11943 * Returns FAIL if writing fails.
11944 */
11945 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011946ses_arglist(
11947 FILE *fd,
11948 char *cmd,
11949 garray_T *gap,
11950 int fullname, /* TRUE: use full path name */
11951 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952{
11953 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011954 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 char_u *s;
11956
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011957 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11958 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011959 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 return FAIL;
11961 for (i = 0; i < gap->ga_len; ++i)
11962 {
11963 /* NULL file names are skipped (only happens when out of memory). */
11964 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11965 if (s != NULL)
11966 {
11967 if (fullname)
11968 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011969 buf = alloc(MAXPATHL);
11970 if (buf != NULL)
11971 {
11972 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11973 s = buf;
11974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011976 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011977 || ses_put_fname(fd, s, flagp) == FAIL
11978 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011979 {
11980 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011982 }
11983 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 }
11985 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011986 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987}
11988
11989/*
11990 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011991 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 * Returns FAIL if writing fails.
11993 */
11994 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011995ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996{
11997 char_u *name;
11998
11999 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012000 * the session file will be sourced.
12001 * Don't do this for ":mkview", we don't know the current directory.
12002 * Don't do this after ":lcd", we don't keep track of what the current
12003 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 if (buf->b_sfname != NULL
12005 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012006 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012007#ifdef FEAT_AUTOCHDIR
12008 && !p_acd
12009#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012010 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 name = buf->b_sfname;
12012 else
12013 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012014 if (ses_put_fname(fd, name, flagp) == FAIL
12015 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 return FAIL;
12017 return OK;
12018}
12019
12020/*
12021 * Write a file name to the session file.
12022 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12023 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012024 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 */
12026 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012027ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028{
12029 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012030 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
12033 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012034 if (sname == NULL)
12035 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012037 if (*flagp & SSOP_SLASH)
12038 {
12039 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012040 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012041 if (*p == '\\')
12042 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012044
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012045 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012046 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012048 if (p == NULL)
12049 return FAIL;
12050
12051 /* write the result */
12052 if (fputs((char *)p, fd) < 0)
12053 retval = FAIL;
12054
12055 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 return retval;
12057}
12058
12059/*
12060 * ":loadview [nr]"
12061 */
12062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012063ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064{
12065 char_u *fname;
12066
12067 fname = get_view_file(*eap->arg);
12068 if (fname != NULL)
12069 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012070 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 vim_free(fname);
12072 }
12073}
12074
12075/*
12076 * Get the name of the view file for the current buffer.
12077 */
12078 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012079get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080{
12081 int len = 0;
12082 char_u *p, *s;
12083 char_u *retval;
12084 char_u *sname;
12085
12086 if (curbuf->b_ffname == NULL)
12087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012088 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 return NULL;
12090 }
12091 sname = home_replace_save(NULL, curbuf->b_ffname);
12092 if (sname == NULL)
12093 return NULL;
12094
12095 /*
12096 * We want a file name without separators, because we're not going to make
12097 * a directory.
12098 * "normal" path separator -> "=+"
12099 * "=" -> "=="
12100 * ":" path separator -> "=-"
12101 */
12102 for (p = sname; *p; ++p)
12103 if (*p == '=' || vim_ispathsep(*p))
12104 ++len;
12105 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12106 if (retval != NULL)
12107 {
12108 STRCPY(retval, p_vdir);
12109 add_pathsep(retval);
12110 s = retval + STRLEN(retval);
12111 for (p = sname; *p; ++p)
12112 {
12113 if (*p == '=')
12114 {
12115 *s++ = '=';
12116 *s++ = '=';
12117 }
12118 else if (vim_ispathsep(*p))
12119 {
12120 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012121#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 if (*p == ':')
12123 *s++ = '-';
12124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012126 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 }
12128 else
12129 *s++ = *p;
12130 }
12131 *s++ = '=';
12132 *s++ = c;
12133 STRCPY(s, ".vim");
12134 }
12135
12136 vim_free(sname);
12137 return retval;
12138}
12139
12140#endif /* FEAT_SESSION */
12141
12142/*
12143 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12144 * Return FAIL for a write error.
12145 */
12146 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012147put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148{
12149 if (
12150#ifdef USE_CRNL
12151 (
12152# ifdef MKSESSION_NL
12153 !mksession_nl &&
12154# endif
12155 (putc('\r', fd) < 0)) ||
12156#endif
12157 (putc('\n', fd) < 0))
12158 return FAIL;
12159 return OK;
12160}
12161
12162/*
12163 * Write a line to "fd".
12164 * Return FAIL for a write error.
12165 */
12166 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012167put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168{
12169 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12170 return FAIL;
12171 return OK;
12172}
12173
12174#ifdef FEAT_VIMINFO
12175/*
12176 * ":rviminfo" and ":wviminfo".
12177 */
12178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012179ex_viminfo(
12180 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182 char_u *save_viminfo;
12183
12184 save_viminfo = p_viminfo;
12185 if (*p_viminfo == NUL)
12186 p_viminfo = (char_u *)"'100";
12187 if (eap->cmdidx == CMD_rviminfo)
12188 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012189 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12190 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012191 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 }
12193 else
12194 write_viminfo(eap->arg, eap->forceit);
12195 p_viminfo = save_viminfo;
12196}
12197#endif
12198
12199#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012200/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012201 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012202 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012205dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 if (fname == NULL)
12208 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012209 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210}
12211#endif
12212
12213/*
12214 * ":behave {mswin,xterm}"
12215 */
12216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012217ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218{
12219 if (STRCMP(eap->arg, "mswin") == 0)
12220 {
12221 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12222 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12223 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12224 set_option_value((char_u *)"keymodel", 0L,
12225 (char_u *)"startsel,stopsel", 0);
12226 }
12227 else if (STRCMP(eap->arg, "xterm") == 0)
12228 {
12229 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12230 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12231 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12232 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12233 }
12234 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012235 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236}
12237
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012238#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12239/*
12240 * Function given to ExpandGeneric() to obtain the possible arguments of the
12241 * ":behave {mswin,xterm}" command.
12242 */
12243 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012244get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012245{
12246 if (idx == 0)
12247 return (char_u *)"mswin";
12248 if (idx == 1)
12249 return (char_u *)"xterm";
12250 return NULL;
12251}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012252
12253/*
12254 * Function given to ExpandGeneric() to obtain the possible arguments of the
12255 * ":messages {clear}" command.
12256 */
12257 char_u *
12258get_messages_arg(expand_T *xp UNUSED, int idx)
12259{
12260 if (idx == 0)
12261 return (char_u *)"clear";
12262 return NULL;
12263}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012264#endif
12265
Bram Moolenaar113e1072019-01-20 15:30:40 +010012266#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012267 char_u *
12268get_mapclear_arg(expand_T *xp UNUSED, int idx)
12269{
12270 if (idx == 0)
12271 return (char_u *)"<buffer>";
12272 return NULL;
12273}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012274#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012275
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276static int filetype_detect = FALSE;
12277static int filetype_plugin = FALSE;
12278static int filetype_indent = FALSE;
12279
12280/*
12281 * ":filetype [plugin] [indent] {on,off,detect}"
12282 * on: Load the filetype.vim file to install autocommands for file types.
12283 * off: Load the ftoff.vim file to remove all autocommands for file types.
12284 * plugin on: load filetype.vim and ftplugin.vim
12285 * plugin off: load ftplugof.vim
12286 * indent on: load filetype.vim and indent.vim
12287 * indent off: load indoff.vim
12288 */
12289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012290ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291{
12292 char_u *arg = eap->arg;
12293 int plugin = FALSE;
12294 int indent = FALSE;
12295
12296 if (*eap->arg == NUL)
12297 {
12298 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012299 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 filetype_detect ? "ON" : "OFF",
12301 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12302 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12303 return;
12304 }
12305
12306 /* Accept "plugin" and "indent" in any order. */
12307 for (;;)
12308 {
12309 if (STRNCMP(arg, "plugin", 6) == 0)
12310 {
12311 plugin = TRUE;
12312 arg = skipwhite(arg + 6);
12313 continue;
12314 }
12315 if (STRNCMP(arg, "indent", 6) == 0)
12316 {
12317 indent = TRUE;
12318 arg = skipwhite(arg + 6);
12319 continue;
12320 }
12321 break;
12322 }
12323 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12324 {
12325 if (*arg == 'o' || !filetype_detect)
12326 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012327 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 filetype_detect = TRUE;
12329 if (plugin)
12330 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012331 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 filetype_plugin = TRUE;
12333 }
12334 if (indent)
12335 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012336 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 filetype_indent = TRUE;
12338 }
12339 }
12340 if (*arg == 'd')
12341 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012342 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012343 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 }
12345 }
12346 else if (STRCMP(arg, "off") == 0)
12347 {
12348 if (plugin || indent)
12349 {
12350 if (plugin)
12351 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012352 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 filetype_plugin = FALSE;
12354 }
12355 if (indent)
12356 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012357 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 filetype_indent = FALSE;
12359 }
12360 }
12361 else
12362 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012363 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 filetype_detect = FALSE;
12365 }
12366 }
12367 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012368 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369}
12370
12371/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012372 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 */
12374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012375ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376{
12377 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012378 {
12379 char_u *arg = eap->arg;
12380
12381 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12382 arg += 9;
12383
12384 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12385 if (arg != eap->arg)
12386 did_filetype = FALSE;
12387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012391ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392{
12393#ifdef FEAT_DIGRAPHS
12394 if (*eap->arg != NUL)
12395 putdigraph(eap->arg);
12396 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012397 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012399 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400#endif
12401}
12402
12403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012404ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405{
12406 int flags = 0;
12407
12408 if (eap->cmdidx == CMD_setlocal)
12409 flags = OPT_LOCAL;
12410 else if (eap->cmdidx == CMD_setglobal)
12411 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012412#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 if (cmdmod.browse && flags == 0)
12414 ex_options(eap);
12415 else
12416#endif
12417 (void)do_set(eap->arg, flags);
12418}
12419
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012420#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12421 void
12422set_no_hlsearch(int flag)
12423{
12424 no_hlsearch = flag;
12425# ifdef FEAT_EVAL
12426 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12427# endif
12428}
12429
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430/*
12431 * ":nohlsearch"
12432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012434ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012436 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012437 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438}
12439
12440/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012441 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 * Sets nextcmd to the start of the next command, if any. Also called when
12443 * skipping commands to find the next command.
12444 */
12445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012446ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447{
12448 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012449 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 char_u *end;
12451 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012452 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012453
12454 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012455 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012456 else
12457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012458 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012459 return;
12460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461
12462 /* First clear any old pattern. */
12463 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012464 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465
12466 if (ends_excmd(*eap->arg))
12467 end = eap->arg;
12468 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012469 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 end = eap->arg + 4;
12471 else
12472 {
12473 p = skiptowhite(eap->arg);
12474 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012475 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 p = skipwhite(p);
12477 if (*p == NUL)
12478 {
12479 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012480 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012481 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 return;
12483 }
12484 end = skip_regexp(p + 1, *p, TRUE, NULL);
12485 if (!eap->skip)
12486 {
12487 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12488 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012489 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 eap->errmsg = e_trailing;
12491 return;
12492 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012493 if (*end != *p)
12494 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012495 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012496 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012497 return;
12498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499
12500 c = *end;
12501 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012502 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012503 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012504 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 }
12506 }
12507 eap->nextcmd = find_nextcmd(end);
12508}
12509#endif
12510
12511#ifdef FEAT_CRYPT
12512/*
12513 * ":X": Get crypt key
12514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012516ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012518 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012519 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520}
12521#endif
12522
12523#ifdef FEAT_FOLDING
12524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012525ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526{
12527 if (foldManualAllowed(TRUE))
12528 foldCreate(eap->line1, eap->line2);
12529}
12530
12531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012532ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533{
12534 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12535 eap->forceit, FALSE);
12536}
12537
12538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012539ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540{
12541 linenr_T lnum;
12542
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012543#ifdef FEAT_CLIPBOARD
12544 start_global_changes();
12545#endif
12546
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547 /* First set the marks for all lines closed/open. */
12548 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12549 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12550 ml_setmarked(lnum);
12551
12552 /* Execute the command on the marked lines. */
12553 global_exe(eap->arg);
12554 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012555#ifdef FEAT_CLIPBOARD
12556 end_global_changes();
12557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558}
12559#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012560
Bram Moolenaar39665952018-08-15 20:59:48 +020012561#ifdef FEAT_QUICKFIX
12562/*
12563 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12564 * instead of a quickfix command.
12565 */
12566 int
12567is_loclist_cmd(int cmdidx)
12568{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012569 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012570 return FALSE;
12571 return cmdnames[cmdidx].cmd_name[0] == 'l';
12572}
12573#endif
12574
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012575# if defined(FEAT_TIMERS) || defined(PROTO)
12576 int
12577get_pressedreturn(void)
12578{
12579 return ex_pressedreturn;
12580}
12581
12582 void
12583set_pressedreturn(int val)
12584{
12585 ex_pressedreturn = val;
12586}
12587#endif