blob: 62f507c2b4aab3434cf73b8244e18ddf3033784e [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
407#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
408 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
409# define ex_language ex_ni
410#endif
411#ifndef FEAT_SIGNS
412# define ex_sign ex_ni
413#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000414#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200415# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000416# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200417# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419
420#ifndef FEAT_EVAL
421# define ex_debug ex_ni
422# define ex_breakadd ex_ni
423# define ex_debuggreedy ex_ni
424# define ex_breakdel ex_ni
425# define ex_breaklist ex_ni
426#endif
427
428#ifndef FEAT_CMDHIST
429# define ex_history ex_ni
430#endif
431#ifndef FEAT_JUMPLIST
432# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200433# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434# define ex_changes ex_ni
435#endif
436
Bram Moolenaar05159a02005-02-26 23:04:13 +0000437#ifndef FEAT_PROFILE
438# define ex_profile ex_ni
439#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200440#ifndef FEAT_TERMINAL
441# define ex_terminal ex_ni
442#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000443
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444/*
445 * Declare cmdnames[].
446 */
447#define DO_DECLARE_EXCMD
448#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200449#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100450
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451static char_u dollar_command[2] = {'$', 0};
452
453
454#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000455/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456typedef struct
457{
458 char_u *line; /* command line */
459 linenr_T lnum; /* sourcing_lnum of the line */
460} wcmd_T;
461
462/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000463 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000465 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000467struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 garray_T *lines_gap; /* growarray with line info */
470 int current_line; /* last read line from growarray */
471 int repeating; /* TRUE when looping a second time */
472 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100473 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 void *cookie;
475};
476
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100477static char_u *get_loop_line(int c, void *cookie, int indent);
478static int store_loop_line(garray_T *gap, char_u *line);
479static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000480
481/* Struct to save a few things while debugging. Used in do_cmdline() only. */
482struct dbg_stuff
483{
484 int trylevel;
485 int force_abort;
486 except_T *caught_stack;
487 char_u *vv_exception;
488 char_u *vv_throwpoint;
489 int did_emsg;
490 int got_int;
491 int did_throw;
492 int need_rethrow;
493 int check_cstack;
494 except_T *current_exception;
495};
496
Bram Moolenaared203462004-06-16 11:19:22 +0000497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100498save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000499{
500 dsp->trylevel = trylevel; trylevel = 0;
501 dsp->force_abort = force_abort; force_abort = FALSE;
502 dsp->caught_stack = caught_stack; caught_stack = NULL;
503 dsp->vv_exception = v_exception(NULL);
504 dsp->vv_throwpoint = v_throwpoint(NULL);
505
506 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
507 dsp->did_emsg = did_emsg; did_emsg = FALSE;
508 dsp->got_int = got_int; got_int = FALSE;
509 dsp->did_throw = did_throw; did_throw = FALSE;
510 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
511 dsp->check_cstack = check_cstack; check_cstack = FALSE;
512 dsp->current_exception = current_exception; current_exception = NULL;
513}
514
515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100516restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000517{
518 suppress_errthrow = FALSE;
519 trylevel = dsp->trylevel;
520 force_abort = dsp->force_abort;
521 caught_stack = dsp->caught_stack;
522 (void)v_exception(dsp->vv_exception);
523 (void)v_throwpoint(dsp->vv_throwpoint);
524 did_emsg = dsp->did_emsg;
525 got_int = dsp->got_int;
526 did_throw = dsp->did_throw;
527 need_rethrow = dsp->need_rethrow;
528 check_cstack = dsp->check_cstack;
529 current_exception = dsp->current_exception;
530}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531#endif
532
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533/*
534 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
535 * command is given.
536 */
537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100538do_exmode(
539 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 int save_msg_scroll;
542 int prev_msg_row;
543 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100544 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545
546 if (improved)
547 exmode_active = EXMODE_VIM;
548 else
549 exmode_active = EXMODE_NORMAL;
550 State = NORMAL;
551
552 /* When using ":global /pat/ visual" and then "Q" we return to continue
553 * the :global command. */
554 if (global_busy)
555 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 save_msg_scroll = msg_scroll;
558 ++RedrawingDisabled; /* don't redisplay the window */
559 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#ifdef FEAT_GUI
561 /* Ignore scrollbar and mouse events in Ex mode */
562 ++hold_gui_events;
563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar32526b32019-01-19 17:43:09 +0100565 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 while (exmode_active)
567 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000568 /* Check for a ":normal" command and no more characters left. */
569 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
570 {
571 exmode_active = FALSE;
572 break;
573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 msg_scroll = TRUE;
575 need_wait_return = FALSE;
576 ex_pressedreturn = FALSE;
577 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100578 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 prev_msg_row = msg_row;
580 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (improved)
582 {
583 cmdline_row = msg_row;
584 do_cmdline(NULL, getexline, NULL, 0);
585 }
586 else
587 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
588 lines_left = Rows - 1;
589
Bram Moolenaardf177f62005-02-22 08:39:57 +0000590 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100591 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000593 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100594 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000597 if (ex_pressedreturn)
598 {
599 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000600 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000601 msg_row = prev_msg_row;
602 if (prev_msg_row == Rows - 1)
603 msg_row--;
604 }
605 msg_col = 0;
606 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
607 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000610 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
611 {
612 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100613 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000614 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100615 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
618
619#ifdef FEAT_GUI
620 --hold_gui_events;
621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 --RedrawingDisabled;
623 --no_wait_return;
624 update_screen(CLEAR);
625 need_wait_return = FALSE;
626 msg_scroll = save_msg_scroll;
627}
628
629/*
630 * Execute a simple command line. Used for translated commands like "*".
631 */
632 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100633do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 return do_cmdline(cmd, NULL, NULL,
636 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
637}
638
639/*
640 * do_cmdline(): execute one Ex command line
641 *
642 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100643 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 * 2. Split up in parts separated with '|'.
645 *
646 * This function can be called recursively!
647 *
648 * flags:
649 * DOCMD_VERBOSE - The command will be included in the error message.
650 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100651 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 * DOCMD_KEYTYPED - Don't reset KeyTyped.
653 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
654 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
655 *
656 * return FAIL if cmdline could not be executed, OK otherwise
657 */
658 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100659do_cmdline(
660 char_u *cmdline,
661 char_u *(*fgetline)(int, void *, int),
662 void *cookie, /* argument for fgetline() */
663 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
665 char_u *next_cmdline; /* next cmd to execute */
666 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100667 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 static int recursive = 0; /* recursive depth */
669 int msg_didout_before_start = 0;
670 int count = 0; /* line number count */
671 int did_inc = FALSE; /* incremented RedrawingDisabled */
672 int retval = OK;
673#ifdef FEAT_EVAL
674 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000675 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int current_line = 0; /* active line in lines_ga */
677 char_u *fname = NULL; /* function or script name */
678 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
679 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000680 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 int initial_trylevel;
682 struct msglist **saved_msg_list = NULL;
683 struct msglist *private_msg_list;
684
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100685 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100686 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000688 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000690 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693# define cmd_cookie cookie
694#endif
695 static int call_depth = 0; /* recursiveness */
696
697#ifdef FEAT_EVAL
698 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
699 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000700 * This ensures that the do_errthrow() call in do_one_cmd() does not
701 * combine the messages stored by an earlier invocation of do_one_cmd()
702 * with the command name of the later one. This would happen when
703 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 saved_msg_list = msg_list;
705 msg_list = &private_msg_list;
706 private_msg_list = NULL;
707#endif
708
709 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100710 * here. The value of 200 allows nested function calls, ":source", etc.
711 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100712 if (call_depth >= 200
713#ifdef FEAT_EVAL
714 && call_depth >= p_mfd
715#endif
716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100718 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#ifdef FEAT_EVAL
720 /* When converting to an exception, we do not include the command name
721 * since this is not an error of the specific command. */
722 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
723 msg_list = saved_msg_list;
724#endif
725 return FAIL;
726 }
727 ++call_depth;
728
729#ifdef FEAT_EVAL
730 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000731 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 cstack.cs_trylevel = 0;
733 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000734 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
736
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100737 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
739 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100740 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000741 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 ++ex_nesting_level;
743
744 /* Get the function or script name and the address where the next breakpoint
745 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000746 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
748 fname = func_name(real_cookie);
749 breakpoint = func_breakpoint(real_cookie);
750 dbg_tick = func_dbg_tick(real_cookie);
751 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 fname = sourcing_name;
755 breakpoint = source_breakpoint(real_cookie);
756 dbg_tick = source_dbg_tick(real_cookie);
757 }
758
759 /*
760 * Initialize "force_abort" and "suppress_errthrow" at the top level.
761 */
762 if (!recursive)
763 {
764 force_abort = FALSE;
765 suppress_errthrow = FALSE;
766 }
767
768 /*
769 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000770 * exception handling (used when debugging). Otherwise clear it to avoid
771 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000773 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000774 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000775 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200776 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
778 initial_trylevel = trylevel;
779
780 /*
781 * "did_throw" will be set to TRUE when an exception is being thrown.
782 */
783 did_throw = FALSE;
784#endif
785 /*
786 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 * If force_abort is set, we cancel everything.
789 */
790 did_emsg = FALSE;
791
792 /*
793 * KeyTyped is only set when calling vgetc(). Reset it here when not
794 * calling vgetc() (sourced command lines).
795 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100796 if (!(flags & DOCMD_KEYTYPED)
797 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 KeyTyped = FALSE;
799
800 /*
801 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000802 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * - for multiple commands on one line, separated with '|'
804 * - when repeating until there are no more lines (for ":source")
805 */
806 next_cmdline = cmdline;
807 do
808 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#endif
812
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000813 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (next_cmdline == NULL
815#ifdef FEAT_EVAL
816 && !force_abort
817 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#endif
820 )
821 did_emsg = FALSE;
822
823 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000824 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 * 3. If a line is given: Make a copy, so we can mess with it.
827 */
828
829#ifdef FEAT_EVAL
830 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000831 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
833 /* Each '|' separated command is stored separately in lines_ga, to
834 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100835 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 /* Check if a function has returned or, unless it has an unclosed
838 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000839 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000842 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 func_line_end(real_cookie);
844# endif
845 if (func_has_ended(real_cookie))
846 {
847 retval = FAIL;
848 break;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000852 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100853 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 script_line_end();
855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 {
860 retval = FAIL;
861 break;
862 }
863
864 /* If breakpoints have been added/deleted need to check for it. */
865 if (breakpoint != NULL && dbg_tick != NULL
866 && *dbg_tick != debug_tick)
867 {
868 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 fname, sourcing_lnum);
871 *dbg_tick = debug_tick;
872 }
873
874 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
875 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
876
877 /* Did we encounter a breakpoint? */
878 if (breakpoint != NULL && *breakpoint != 0
879 && *breakpoint <= sourcing_lnum)
880 {
881 dbg_breakpoint(fname, sourcing_lnum);
882 /* Find next breakpoint. */
883 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 fname, sourcing_lnum);
886 *dbg_tick = debug_tick;
887 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000889 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890 {
891 if (getline_is_func)
892 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100893 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894 script_line_start();
895 }
896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000899 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100902 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 * below, so that it stores lines in or reads them from
904 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 * while/for loop. */
906 cmd_getline = get_loop_line;
907 cmd_cookie = (void *)&cmd_loop_cookie;
908 cmd_loop_cookie.lines_gap = &lines_ga;
909 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100910 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 cmd_loop_cookie.cookie = cookie;
912 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914 else
915 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100916 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 cmd_cookie = cookie;
918 }
919#endif
920
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100921 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (next_cmdline == NULL)
923 {
924 /*
925 * Need to set msg_didout for the first line after an ":if",
926 * otherwise the ":if" will be overwritten.
927 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100928 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100930 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#ifdef FEAT_EVAL
932 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
933#else
934 0
935#endif
936 )) == NULL)
937 {
938 /* Don't call wait_return for aborted command line. The NULL
939 * returned for the end of a sourced file or executed function
940 * doesn't do this. */
941 if (KeyTyped && !(flags & DOCMD_REPEAT))
942 need_wait_return = FALSE;
943 retval = FAIL;
944 break;
945 }
946 used_getline = TRUE;
947
948 /*
949 * Keep the first typed line. Clear it when more lines are typed.
950 */
951 if (flags & DOCMD_KEEPLINE)
952 {
953 vim_free(repeat_cmdline);
954 if (count == 0)
955 repeat_cmdline = vim_strsave(next_cmdline);
956 else
957 repeat_cmdline = NULL;
958 }
959 }
960
961 /* 3. Make a copy of the command so we can mess with it. */
962 else if (cmdline_copy == NULL)
963 {
964 next_cmdline = vim_strsave(next_cmdline);
965 if (next_cmdline == NULL)
966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100967 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 retval = FAIL;
969 break;
970 }
971 }
972 cmdline_copy = next_cmdline;
973
974#ifdef FEAT_EVAL
975 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000976 * Save the current line when inside a ":while" or ":for", and when
977 * the command looks like a ":while" or ":for", because we may need it
978 * later. When there is a '|' and another command, it is stored
979 * separately, because we need to be able to jump back to it from an
980 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000982 if (current_line == lines_ga.ga_len
983 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
987 retval = FAIL;
988 break;
989 }
990 }
991 did_endif = FALSE;
992#endif
993
994 if (count++ == 0)
995 {
996 /*
997 * All output from the commands is put below each other, without
998 * waiting for a return. Don't do this when executing commands
999 * from a script or when being called recursive (e.g. for ":e
1000 * +command file").
1001 */
1002 if (!(flags & DOCMD_NOWAIT) && !recursive)
1003 {
1004 msg_didout_before_start = msg_didout;
1005 msg_didany = FALSE; /* no output yet */
1006 msg_start();
1007 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001008 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 ++RedrawingDisabled;
1010 did_inc = TRUE;
1011 }
1012 }
1013
1014 if (p_verbose >= 15 && sourcing_name != NULL)
1015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001017 verbose_enter_scroll();
1018
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001021 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001022 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001023
1024 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 --no_wait_return;
1026 }
1027
1028 /*
1029 * 2. Execute one '|' separated command.
1030 * do_one_cmd() will return NULL if there is no trailing '|'.
1031 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1032 */
1033 ++recursive;
1034 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1035#ifdef FEAT_EVAL
1036 &cstack,
1037#endif
1038 cmd_getline, cmd_cookie);
1039 --recursive;
1040
1041#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cmd_cookie == (void *)&cmd_loop_cookie)
1043 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046#endif
1047
1048 if (next_cmdline == NULL)
1049 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001050 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051#ifdef FEAT_CMDHIST
1052 /*
1053 * If the command was typed, remember it for the ':' register.
1054 * Do this AFTER executing the command to make :@: work.
1055 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001056 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 && new_last_cmdline != NULL)
1058 {
1059 vim_free(last_cmdline);
1060 last_cmdline = new_last_cmdline;
1061 new_last_cmdline = NULL;
1062 }
1063#endif
1064 }
1065 else
1066 {
1067 /* need to copy the command after the '|' to cmdline_copy, for the
1068 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001069 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 next_cmdline = cmdline_copy;
1071 }
1072
1073
1074#ifdef FEAT_EVAL
1075 /* reset did_emsg for a function that is not aborted by an error */
1076 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001077 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 && !func_has_abort(real_cookie))
1079 did_emsg = FALSE;
1080
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
1083 ++current_line;
1084
1085 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001086 * An ":endwhile", ":endfor" and ":continue" is handled here.
1087 * If we were executing commands, jump back to the ":while" or
1088 * ":for".
1089 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 /* Jump back to the matching ":while" or ":for". Be careful
1096 * not to use a cs_line[] from an entry that isn't a ":while"
1097 * or ":for": It would make "current_line" invalid and can
1098 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (!did_emsg && !got_int && !did_throw
1100 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 && (cstack.cs_flags[cstack.cs_idx]
1102 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 && cstack.cs_line[cstack.cs_idx] >= 0
1104 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1105 {
1106 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001107 /* remember we jumped there */
1108 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 line_breakcheck(); /* check if CTRL-C typed */
1110
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 /* Check for the next breakpoint at or after the ":while"
1112 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 if (breakpoint != NULL)
1114 {
1115 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001116 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 fname,
1118 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1119 *dbg_tick = debug_tick;
1120 }
1121 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001126 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1127 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 }
1130
1131 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1138 }
1139 }
1140
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001141 /* Check for the next breakpoint after a watchexpression */
1142 if (breakpoint != NULL && has_watchexpr())
1143 {
1144 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1145 *dbg_tick = debug_tick;
1146 }
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /*
1149 * When not inside any ":while" loop, clear remembered lines.
1150 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 if (lines_ga.ga_len > 0)
1154 {
1155 sourcing_lnum =
1156 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1157 free_cmdlines(&lines_ga);
1158 }
1159 current_line = 0;
1160 }
1161
1162 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1164 * being restored at the ":endtry". Reset them here and set the
1165 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1166 * This includes the case where a missing ":endif", ":endwhile" or
1167 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 cstack.cs_lflags &= ~CSL_HAD_FINA;
1172 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1173 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 did_throw ? (void *)current_exception : NULL);
1175 did_emsg = got_int = did_throw = FALSE;
1176 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1177 }
1178
1179 /* Update global "trylevel" for recursive calls to do_cmdline() from
1180 * within this loop. */
1181 trylevel = initial_trylevel + cstack.cs_trylevel;
1182
1183 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001184 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 * files) is aborted because of an error, an interrupt, or an uncaught
1186 * exception, cancel everything. If it is left normally, reset
1187 * force_abort to get the non-EH compatible abortion behavior for
1188 * the rest of the script.
1189 */
1190 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1191 force_abort = FALSE;
1192
1193 /* Convert an interrupt to an exception if appropriate. */
1194 (void)do_intthrow(&cstack);
1195#endif /* FEAT_EVAL */
1196
1197 }
1198 /*
1199 * Continue executing command lines when:
1200 * - no CTRL-C typed, no aborting error, no exception thrown or try
1201 * conditionals need to be checked for executing finally clauses or
1202 * catching an interrupt exception
1203 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 * looping for ":source" command or function call.
1206 */
1207 while (!((got_int
1208#ifdef FEAT_EVAL
1209 || (did_emsg && force_abort) || did_throw
1210#endif
1211 )
1212#ifdef FEAT_EVAL
1213 && cstack.cs_trylevel == 0
1214#endif
1215 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001216 && !(did_emsg
1217#ifdef FEAT_EVAL
1218 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001219 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001220 * the :endtry to be missed. */
1221 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1222#endif
1223 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001224 && (getline_equal(fgetline, cookie, getexmodeline)
1225 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 && (next_cmdline != NULL
1227#ifdef FEAT_EVAL
1228 || cstack.cs_idx >= 0
1229#endif
1230 || (flags & DOCMD_REPEAT)));
1231
1232 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001233 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#ifdef FEAT_EVAL
1235 free_cmdlines(&lines_ga);
1236 ga_clear(&lines_ga);
1237
1238 if (cstack.cs_idx >= 0)
1239 {
1240 /*
1241 * If a sourced file or executed function ran to its end, report the
1242 * unclosed conditional.
1243 */
1244 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001245 && ((getline_equal(fgetline, cookie, getsourceline)
1246 && !source_finished(fgetline, cookie))
1247 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && !func_has_ended(real_cookie))))
1249 {
1250 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001251 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001253 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001255 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001257 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259
1260 /*
1261 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1262 * ":endtry" in a sourced file or executed function. If the try
1263 * conditional is in its finally clause, ignore anything pending.
1264 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001265 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
1267 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001268 {
1269 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1270
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001271 if (idx >= 0)
1272 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001273 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1274 &cstack.cs_looplevel);
1275 }
1276 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 trylevel = initial_trylevel;
1278 }
1279
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001280 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1281 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001283 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 ? (char_u *)"endfunction" : (char_u *)NULL);
1285
1286 if (trylevel == 0)
1287 {
1288 /*
1289 * When an exception is being thrown out of the outermost try
1290 * conditional, discard the uncaught exception, disable the conversion
1291 * of interrupts or errors to exceptions, and ensure that no more
1292 * commands are executed.
1293 */
1294 if (did_throw)
1295 {
1296 void *p = NULL;
1297 char_u *saved_sourcing_name;
1298 int saved_sourcing_lnum;
1299 struct msglist *messages = NULL, *next;
1300
1301 /*
1302 * If the uncaught exception is a user exception, report it as an
1303 * error. If it is an error exception, display the saved error
1304 * message now. For an interrupt exception, do nothing; the
1305 * interrupt message is given elsewhere.
1306 */
1307 switch (current_exception->type)
1308 {
1309 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001310 vim_snprintf((char *)IObuff, IOSIZE,
1311 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 current_exception->value);
1313 p = vim_strsave(IObuff);
1314 break;
1315 case ET_ERROR:
1316 messages = current_exception->messages;
1317 current_exception->messages = NULL;
1318 break;
1319 case ET_INTERRUPT:
1320 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322
1323 saved_sourcing_name = sourcing_name;
1324 saved_sourcing_lnum = sourcing_lnum;
1325 sourcing_name = current_exception->throw_name;
1326 sourcing_lnum = current_exception->throw_lnum;
1327 current_exception->throw_name = NULL;
1328
1329 discard_current_exception(); /* uses IObuff if 'verbose' */
1330 suppress_errthrow = TRUE;
1331 force_abort = TRUE;
1332
1333 if (messages != NULL)
1334 {
1335 do
1336 {
1337 next = messages->next;
1338 emsg(messages->msg);
1339 vim_free(messages->msg);
1340 vim_free(messages);
1341 messages = next;
1342 }
1343 while (messages != NULL);
1344 }
1345 else if (p != NULL)
1346 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001347 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 vim_free(p);
1349 }
1350 vim_free(sourcing_name);
1351 sourcing_name = saved_sourcing_name;
1352 sourcing_lnum = saved_sourcing_lnum;
1353 }
1354
1355 /*
1356 * On an interrupt or an aborting error not converted to an exception,
1357 * disable the conversion of errors to exceptions. (Interrupts are not
1358 * converted any more, here.) This enables also the interrupt message
1359 * when force_abort is set and did_emsg unset in case of an interrupt
1360 * from a finally clause after an error.
1361 */
1362 else if (got_int || (did_emsg && force_abort))
1363 suppress_errthrow = TRUE;
1364 }
1365
1366 /*
1367 * The current cstack will be freed when do_cmdline() returns. An uncaught
1368 * exception will have to be rethrown in the previous cstack. If a function
1369 * has just returned or a script file was just finished and the previous
1370 * cstack belongs to the same function or, respectively, script file, it
1371 * will have to be checked for finally clauses to be executed due to the
1372 * ":return" or ":finish". This is done in do_one_cmd().
1373 */
1374 if (did_throw)
1375 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001376 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001378 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 && ex_nesting_level > func_level(real_cookie) + 1))
1380 {
1381 if (!did_throw)
1382 check_cstack = TRUE;
1383 }
1384 else
1385 {
1386 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001387 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 --ex_nesting_level;
1389 /*
1390 * Go to debug mode when returning from a function in which we are
1391 * single-stepping.
1392 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001393 if ((getline_equal(fgetline, cookie, getsourceline)
1394 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001396 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ? (char_u *)_("End of sourced file")
1398 : (char_u *)_("End of function"));
1399 }
1400
1401 /*
1402 * Restore the exception environment (done after returning from the
1403 * debugger).
1404 */
1405 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001406 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 msg_list = saved_msg_list;
1409#endif /* FEAT_EVAL */
1410
1411 /*
1412 * If there was too much output to fit on the command line, ask the user to
1413 * hit return before redrawing the screen. With the ":global" command we do
1414 * this only once after the command is finished.
1415 */
1416 if (did_inc)
1417 {
1418 --RedrawingDisabled;
1419 --no_wait_return;
1420 msg_scroll = FALSE;
1421
1422 /*
1423 * When just finished an ":if"-":else" which was typed, no need to
1424 * wait for hit-return. Also for an error situation.
1425 */
1426 if (retval == FAIL
1427#ifdef FEAT_EVAL
1428 || (did_endif && KeyTyped && !did_emsg)
1429#endif
1430 )
1431 {
1432 need_wait_return = FALSE;
1433 msg_didany = FALSE; /* don't wait when restarting edit */
1434 }
1435 else if (need_wait_return)
1436 {
1437 /*
1438 * The msg_start() above clears msg_didout. The wait_return we do
1439 * here should not overwrite the command that may be shown before
1440 * doing that.
1441 */
1442 msg_didout |= msg_didout_before_start;
1443 wait_return(FALSE);
1444 }
1445 }
1446
Bram Moolenaar2a942252012-11-28 23:03:07 +01001447#ifdef FEAT_EVAL
1448 did_endif = FALSE; /* in case do_cmdline used recursively */
1449#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 /*
1451 * Reset if_level, in case a sourced script file contains more ":if" than
1452 * ":endif" (could be ":if x | foo | endif").
1453 */
1454 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001455#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 --call_depth;
1458 return retval;
1459}
1460
1461#ifdef FEAT_EVAL
1462/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001463 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 */
1465 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001466get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001468 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 wcmd_T *wp;
1470 char_u *line;
1471
1472 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1473 {
1474 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001475 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001477 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (cp->getline == NULL)
1479 line = getcmdline(c, 0L, indent);
1480 else
1481 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001482 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 ++cp->current_line;
1484
1485 return line;
1486 }
1487
1488 KeyTyped = FALSE;
1489 ++cp->current_line;
1490 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1491 sourcing_lnum = wp->lnum;
1492 return vim_strsave(wp->line);
1493}
1494
1495/*
1496 * Store a line in "gap" so that a ":while" loop can execute it again.
1497 */
1498 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001499store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 if (ga_grow(gap, 1) == FAIL)
1502 return FAIL;
1503 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1504 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1505 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 return OK;
1507}
1508
1509/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 */
1512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001513free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 while (gap->ga_len > 0)
1516 {
1517 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1518 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520}
1521#endif
1522
1523/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001524 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1525 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001528getline_equal(
1529 char_u *(*fgetline)(int, void *, int),
1530 void *cookie UNUSED, /* argument for fgetline() */
1531 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001534 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001535 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
Bram Moolenaar89d40322006-08-29 15:30:07 +00001537 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001538 * function that's originally used to obtain the lines. This may be
1539 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001540 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 cp = (struct loop_cookie *)cookie;
1542 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 gp = cp->getline;
1545 cp = cp->cookie;
1546 }
1547 return gp == func;
1548#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001549 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551}
1552
1553#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1554/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001555 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 * getline function. Otherwise return "cookie".
1557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001559getline_cookie(
1560 char_u *(*fgetline)(int, void *, int) UNUSED,
1561 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001564 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001565 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaar89d40322006-08-29 15:30:07 +00001567 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001568 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001570 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001571 cp = (struct loop_cookie *)cookie;
1572 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 gp = cp->getline;
1575 cp = cp->cookie;
1576 }
1577 return cp;
1578# else
1579 return cookie;
1580# endif
1581}
1582#endif
1583
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584
1585/*
1586 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1587 * ":bwipeout", etc.
1588 * Returns the buffer number.
1589 */
1590 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001591compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001592{
1593 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001594 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001595 int count = offset;
1596
1597 buf = firstbuf;
1598 while (buf->b_next != NULL && buf->b_fnum < lnum)
1599 buf = buf->b_next;
1600 while (count != 0)
1601 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001602 count += (offset < 0) ? 1 : -1;
1603 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1604 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001606 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607 if (addr_type == ADDR_LOADED_BUFFERS)
1608 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 while (buf->b_ml.ml_mfp == NULL)
1610 {
1611 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1612 if (nextbuf == NULL)
1613 break;
1614 buf = nextbuf;
1615 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 /* we might have gone too far, last buffer is not loadedd */
1618 if (addr_type == ADDR_LOADED_BUFFERS)
1619 while (buf->b_ml.ml_mfp == NULL)
1620 {
1621 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1622 if (nextbuf == NULL)
1623 break;
1624 buf = nextbuf;
1625 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 return buf->b_fnum;
1627}
1628
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631{
1632 win_T *wp;
1633 int nr = 0;
1634
Bram Moolenaar29323592016-07-24 22:04:11 +02001635 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001636 {
1637 ++nr;
1638 if (wp == win)
1639 break;
1640 }
1641 return nr;
1642}
1643
1644 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001645current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001646{
1647 tabpage_T *tp;
1648 int nr = 0;
1649
Bram Moolenaar29323592016-07-24 22:04:11 +02001650 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001651 {
1652 ++nr;
1653 if (tp == tab)
1654 break;
1655 }
1656 return nr;
1657}
1658
1659# define CURRENT_WIN_NR current_win_nr(curwin)
1660# define LAST_WIN_NR current_win_nr(NULL)
1661# define CURRENT_TAB_NR current_tab_nr(curtab)
1662# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664/*
1665 * Execute one Ex command.
1666 *
1667 * If 'sourcing' is TRUE, the command will be included in the error message.
1668 *
1669 * 1. skip comment lines and leading space
1670 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001671 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001673 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674 * 6. parse arguments
1675 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 *
1679 * This function may be called recursively!
1680 */
1681#if (_MSC_VER == 1200)
1682/*
Bram Moolenaared203462004-06-16 11:19:22 +00001683 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001685 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
1687 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001688do_one_cmd(
1689 char_u **cmdlinep,
1690 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001692 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001694 char_u *(*fgetline)(int, void *, int),
1695 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 char_u *p;
1698 linenr_T lnum;
1699 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001700 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001701 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001703 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 cmdmod_T save_cmdmod;
1705 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 vim_memset(&ea, 0, sizeof(ea));
1709 ea.line1 = 1;
1710 ea.line2 = 1;
1711#ifdef FEAT_EVAL
1712 ++ex_nesting_level;
1713#endif
1714
Bram Moolenaar21691f82012-12-05 19:13:18 +01001715 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (quitmore
1717#ifdef FEAT_EVAL
1718 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001719 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001720#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001721 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001722 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 --quitmore;
1724
1725 /*
1726 * Reset browse, confirm, etc.. They are restored when returning, for
1727 * recursive calls.
1728 */
1729 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001731 /* "#!anything" is handled like a comment. */
1732 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1733 goto doend;
1734
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001735/*
1736 * 1. Skip comment lines and leading white space and colons.
1737 * 2. Handle command modifiers.
1738 */
1739 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001741 ea.cmdlinep = cmdlinep;
1742 ea.getline = fgetline;
1743 ea.cookie = cookie;
1744#ifdef FEAT_EVAL
1745 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001747 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001748 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001750 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752#ifdef FEAT_EVAL
1753 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1754 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1755#else
1756 ea.skip = (if_level > 0);
1757#endif
1758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001760 * 3. Skip over the range to find the command. Let "p" point to after it.
1761 *
1762 * We need the command to know what kind of range it uses.
1763 */
1764 cmd = ea.cmd;
1765 ea.cmd = skip_range(ea.cmd, NULL);
1766 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1767 ea.cmd = skipwhite(ea.cmd + 1);
1768 p = find_command(&ea, NULL);
1769
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001770#ifdef FEAT_EVAL
1771# ifdef FEAT_PROFILE
1772 // Count this line for profiling if skip is TRUE.
1773 if (do_profiling == PROF_YES
1774 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1775 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1776 {
1777 int skip = did_emsg || got_int || did_throw;
1778
1779 if (ea.cmdidx == CMD_catch)
1780 skip = !skip && !(cstack->cs_idx >= 0
1781 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1782 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1783 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1784 skip = skip || !(cstack->cs_idx >= 0
1785 && !(cstack->cs_flags[cstack->cs_idx]
1786 & (CSF_ACTIVE | CSF_TRUE)));
1787 else if (ea.cmdidx == CMD_finally)
1788 skip = FALSE;
1789 else if (ea.cmdidx != CMD_endif
1790 && ea.cmdidx != CMD_endfor
1791 && ea.cmdidx != CMD_endtry
1792 && ea.cmdidx != CMD_endwhile)
1793 skip = ea.skip;
1794
1795 if (!skip)
1796 {
1797 if (getline_equal(fgetline, cookie, get_func_line))
1798 func_line_exec(getline_cookie(fgetline, cookie));
1799 else if (getline_equal(fgetline, cookie, getsourceline))
1800 script_line_exec();
1801 }
1802 }
1803# endif
1804
1805 /* May go to debug mode. If this happens and the ">quit" debug command is
1806 * used, throw an interrupt exception and skip the next command. */
1807 dbg_check_breakpoint(&ea);
1808 if (!ea.skip && got_int)
1809 {
1810 ea.skip = TRUE;
1811 (void)do_intthrow(cstack);
1812 }
1813#endif
1814
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001815/*
1816 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 *
1818 * where 'addr' is:
1819 *
1820 * % (entire file)
1821 * $ [+-NUM]
1822 * 'x [+-NUM] (where x denotes a currently defined mark)
1823 * . [+-NUM]
1824 * [+-NUM]..
1825 * NUM
1826 *
1827 * The ea.cmd pointer is updated to point to the first character following the
1828 * range spec. If an initial address is found, but no second, the upper bound
1829 * is equal to the lower.
1830 */
1831
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001832 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001833 if (!IS_USER_CMDIDX(ea.cmdidx))
1834 {
1835 if (ea.cmdidx != CMD_SIZE)
1836 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1837 else
1838 ea.addr_type = ADDR_LINES;
1839
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001840 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001841 if (ea.cmdidx == CMD_wincmd && p != NULL)
1842 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001843 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001844
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001845 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001846 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001847 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001850 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852
1853 /*
1854 * Skip ':' and any white space
1855 */
1856 ea.cmd = skipwhite(ea.cmd);
1857 while (*ea.cmd == ':')
1858 ea.cmd = skipwhite(ea.cmd + 1);
1859
1860 /*
1861 * If we got a line, but no command, then go to the line.
1862 * If we find a '|' or '\n' we set ea.nextcmd.
1863 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001864 if (*ea.cmd == NUL || *ea.cmd == '"'
1865 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
1867 /*
1868 * strange vi behaviour:
1869 * ":3" jumps to line 3
1870 * ":3|..." prints line 3
1871 * ":|" prints current line
1872 */
1873 if (ea.skip) /* skip this if inside :if */
1874 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001875 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 ea.cmdidx = CMD_print;
1878 ea.argt = RANGE+COUNT+TRLBAR;
1879 if ((errormsg = invalid_range(&ea)) == NULL)
1880 {
1881 correct_range(&ea);
1882 ex_print(&ea);
1883 }
1884 }
1885 else if (ea.addr_count != 0)
1886 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001887 if (ea.line2 > curbuf->b_ml.ml_line_count)
1888 {
1889 /* With '-' in 'cpoptions' a line number past the file is an
1890 * error, otherwise put it at the end of the file. */
1891 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1892 ea.line2 = -1;
1893 else
1894 ea.line2 = curbuf->b_ml.ml_line_count;
1895 }
1896
1897 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001898 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 else
1900 {
1901 if (ea.line2 == 0)
1902 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 else
1904 curwin->w_cursor.lnum = ea.line2;
1905 beginline(BL_SOL | BL_FIX);
1906 }
1907 }
1908 goto doend;
1909 }
1910
Bram Moolenaard5005162014-08-22 23:05:54 +02001911 /* If this looks like an undefined user command and there are CmdUndefined
1912 * autocommands defined, trigger the matching autocommands. */
1913 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1914 && ASCII_ISUPPER(*ea.cmd)
1915 && has_cmdundefined())
1916 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001917 int ret;
1918
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001919 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001920 while (ASCII_ISALNUM(*p))
1921 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001922 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001923 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1924 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001925 /* If the autocommands did something and didn't cause an error, try
1926 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001927 p = (ret
1928#ifdef FEAT_EVAL
1929 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001930#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001931 ) ? find_command(&ea, NULL) : ea.cmd;
1932 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934#ifdef FEAT_USR_CMDS
1935 if (p == NULL)
1936 {
1937 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001938 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 goto doend;
1940 }
1941 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001942 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1943 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 errormsg = uc_fun_cmd();
1946 goto doend;
1947 }
1948#endif
1949 if (ea.cmdidx == CMD_SIZE)
1950 {
1951 if (!ea.skip)
1952 {
1953 STRCPY(IObuff, _("E492: Not an editor command"));
1954 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001955 {
1956 /* If the modifier was parsed OK the error must be in the
1957 * following command */
1958 if (after_modifier != NULL)
1959 append_command(after_modifier);
1960 else
1961 append_command(*cmdlinep);
1962 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001963 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001964 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966 goto doend;
1967 }
1968
Bram Moolenaar958636c2014-10-21 20:01:58 +02001969 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1970 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001971#ifdef HAVE_EX_SCRIPT_NI
1972 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1973#endif
1974 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976#ifndef FEAT_EVAL
1977 /*
1978 * When the expression evaluation is disabled, recognize the ":if" and
1979 * ":endif" commands and ignore everything in between it.
1980 */
1981 if (ea.cmdidx == CMD_if)
1982 ++if_level;
1983 if (if_level)
1984 {
1985 if (ea.cmdidx == CMD_endif)
1986 --if_level;
1987 goto doend;
1988 }
1989
1990#endif
1991
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001992 /* forced commands */
1993 if (*p == '!' && ea.cmdidx != CMD_substitute
1994 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996 ++p;
1997 ea.forceit = TRUE;
1998 }
1999 else
2000 ea.forceit = FALSE;
2001
2002/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002003 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002005 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002006 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
2008 if (!ea.skip)
2009 {
2010#ifdef HAVE_SANDBOX
2011 if (sandbox != 0 && !(ea.argt & SBOXOK))
2012 {
2013 /* Command not allowed in sandbox. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002014 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 goto doend;
2016 }
2017#endif
2018 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2019 {
2020 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 goto doend;
2023 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002024
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002025 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002026 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002028 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 goto doend;
2031 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002032
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002033 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002034 * Do allow ":checktime" (it is postponed).
2035 * Do allow ":edit" (check for an argument later).
2036 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002037 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002038 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002039 && ea.cmdidx != CMD_edit
2040 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002041 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002042 && curbuf_locked())
2043 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2046 {
2047 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002048 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 goto doend;
2050 }
2051 }
2052
2053 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002055 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 goto doend;
2057 }
2058
2059 /*
2060 * Don't complain about the range if it is not used
2061 * (could happen if line_count is accidentally set to 0).
2062 */
2063 if (!ea.skip && !ni)
2064 {
2065 /*
2066 * If the range is backwards, ask for confirmation and, if given, swap
2067 * ea.line1 & ea.line2 so it's forwards again.
2068 * When global command is busy, don't ask, will fail below.
2069 */
2070 if (!global_busy && ea.line1 > ea.line2)
2071 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002072 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002074 if (sourcing || exmode_active)
2075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002076 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002077 goto doend;
2078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (ask_yesno((char_u *)
2080 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002081 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 lnum = ea.line1;
2084 ea.line1 = ea.line2;
2085 ea.line2 = lnum;
2086 }
2087 if ((errormsg = invalid_range(&ea)) != NULL)
2088 goto doend;
2089 }
2090
2091 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2092 ea.line2 = 1;
2093
2094 correct_range(&ea);
2095
2096#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002097 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2098 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100 /* Put the first line at the start of a closed fold, put the last line
2101 * at the end of a closed fold. */
2102 (void)hasFolding(ea.line1, &ea.line1, NULL);
2103 (void)hasFolding(ea.line2, NULL, &ea.line2);
2104 }
2105#endif
2106
2107#ifdef FEAT_QUICKFIX
2108 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002109 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 * option here, so things like % get expanded.
2111 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002112 p = replace_makeprg(&ea, p, cmdlinep);
2113 if (p == NULL)
2114 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115#endif
2116
2117 /*
2118 * Skip to start of argument.
2119 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2120 */
2121 if (ea.cmdidx == CMD_bang)
2122 ea.arg = p;
2123 else
2124 ea.arg = skipwhite(p);
2125
Bram Moolenaar379fb762018-08-30 15:58:28 +02002126 // ":file" cannot be run with an argument when "curbuf_lock" is set
2127 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2128 goto doend;
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 /*
2131 * Check for "++opt=val" argument.
2132 * Must be first, allow ":w ++enc=utf8 !cmd"
2133 */
2134 if (ea.argt & ARGOPT)
2135 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2136 if (getargopt(&ea) == FAIL && !ni)
2137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002138 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 goto doend;
2140 }
2141
2142 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2143 {
2144 if (*ea.arg == '>') /* append */
2145 {
2146 if (*++ea.arg != '>') /* typed wrong */
2147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002148 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 goto doend;
2150 }
2151 ea.arg = skipwhite(ea.arg + 1);
2152 ea.append = TRUE;
2153 }
2154 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2155 {
2156 ++ea.arg;
2157 ea.usefilter = TRUE;
2158 }
2159 }
2160
2161 if (ea.cmdidx == CMD_read)
2162 {
2163 if (ea.forceit)
2164 {
2165 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2166 ea.forceit = FALSE;
2167 }
2168 else if (*ea.arg == '!') /* :r !filter */
2169 {
2170 ++ea.arg;
2171 ea.usefilter = TRUE;
2172 }
2173 }
2174
2175 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2176 {
2177 ea.amount = 1;
2178 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2179 {
2180 ++ea.arg;
2181 ++ea.amount;
2182 }
2183 ea.arg = skipwhite(ea.arg);
2184 }
2185
2186 /*
2187 * Check for "+command" argument, before checking for next command.
2188 * Don't do this for ":read !cmd" and ":write !cmd".
2189 */
2190 if ((ea.argt & EDITCMD) && !ea.usefilter)
2191 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2192
2193 /*
2194 * Check for '|' to separate commands and '"' to start comments.
2195 * Don't do this for ":read !cmd" and ":write !cmd".
2196 */
2197 if ((ea.argt & TRLBAR) && !ea.usefilter)
2198 separate_nextcmd(&ea);
2199
2200 /*
2201 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002202 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2203 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002205 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002206 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002207 || ea.cmdidx == CMD_global
2208 || ea.cmdidx == CMD_vglobal
2209 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
2211 for (p = ea.arg; *p; ++p)
2212 {
2213 /* Remove one backslash before a newline, so that it's possible to
2214 * pass a newline to the shell and also a newline that is preceded
2215 * with a backslash. This makes it impossible to end a shell
2216 * command in a backslash, but that doesn't appear useful.
2217 * Halving the number of backslashes is incompatible with previous
2218 * versions. */
2219 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002220 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 else if (*p == '\n')
2222 {
2223 ea.nextcmd = p + 1;
2224 *p = NUL;
2225 break;
2226 }
2227 }
2228 }
2229
2230 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2231 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002232 buf_T *buf;
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002235 switch (ea.addr_type)
2236 {
2237 case ADDR_LINES:
2238 ea.line2 = curbuf->b_ml.ml_line_count;
2239 break;
2240 case ADDR_LOADED_BUFFERS:
2241 buf = firstbuf;
2242 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2243 buf = buf->b_next;
2244 ea.line1 = buf->b_fnum;
2245 buf = lastbuf;
2246 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2247 buf = buf->b_prev;
2248 ea.line2 = buf->b_fnum;
2249 break;
2250 case ADDR_BUFFERS:
2251 ea.line1 = firstbuf->b_fnum;
2252 ea.line2 = lastbuf->b_fnum;
2253 break;
2254 case ADDR_WINDOWS:
2255 ea.line2 = LAST_WIN_NR;
2256 break;
2257 case ADDR_TABS:
2258 ea.line2 = LAST_TAB_NR;
2259 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002260 case ADDR_TABS_RELATIVE:
2261 ea.line2 = 1;
2262 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002263 case ADDR_ARGUMENTS:
2264 if (ARGCOUNT == 0)
2265 ea.line1 = ea.line2 = 0;
2266 else
2267 ea.line2 = ARGCOUNT;
2268 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002269#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002270 case ADDR_QUICKFIX:
2271 ea.line2 = qf_get_size(&ea);
2272 if (ea.line2 == 0)
2273 ea.line2 = 1;
2274 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002275#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
2278
2279 /* accept numbered register only when no count allowed (:put) */
2280 if ( (ea.argt & REGSTR)
2281 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002282 /* Do not allow register = for user commands */
2283 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2285 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002286#ifndef FEAT_CLIPBOARD
2287 /* check these explicitly for a more specific error message */
2288 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002290 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002291 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002294 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2295 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002296 {
2297 ea.regname = *ea.arg++;
2298#ifdef FEAT_EVAL
2299 /* for '=' register: accept the rest of the line as an expression */
2300 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2301 {
2302 set_expr_line(vim_strsave(ea.arg));
2303 ea.arg += STRLEN(ea.arg);
2304 }
2305#endif
2306 ea.arg = skipwhite(ea.arg);
2307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309
2310 /*
2311 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2312 * count, it's a buffer name.
2313 */
2314 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2315 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002316 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318 n = getdigits(&ea.arg);
2319 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002320 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002322 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 goto doend;
2324 }
2325 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2326 {
2327 ea.line2 = n;
2328 if (ea.addr_count == 0)
2329 ea.addr_count = 1;
2330 }
2331 else
2332 {
2333 ea.line1 = ea.line2;
2334 ea.line2 += n - 1;
2335 ++ea.addr_count;
2336 /*
2337 * Be vi compatible: no error message for out of range.
2338 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002339 if (ea.addr_type == ADDR_LINES
2340 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 ea.line2 = curbuf->b_ml.ml_line_count;
2342 }
2343 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002344
2345 /*
2346 * Check for flags: 'l', 'p' and '#'.
2347 */
2348 if (ea.argt & EXFLAGS)
2349 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002351 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002352 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002354 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 goto doend;
2356 }
2357
2358 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002360 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 goto doend;
2362 }
2363
2364#ifdef FEAT_EVAL
2365 /*
2366 * Skip the command when it's not going to be executed.
2367 * The commands like :if, :endif, etc. always need to be executed.
2368 * Also make an exception for commands that handle a trailing command
2369 * themselves.
2370 */
2371 if (ea.skip)
2372 {
2373 switch (ea.cmdidx)
2374 {
2375 /* commands that need evaluation */
2376 case CMD_while:
2377 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002378 case CMD_for:
2379 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 case CMD_if:
2381 case CMD_elseif:
2382 case CMD_else:
2383 case CMD_endif:
2384 case CMD_try:
2385 case CMD_catch:
2386 case CMD_finally:
2387 case CMD_endtry:
2388 case CMD_function:
2389 break;
2390
2391 /* Commands that handle '|' themselves. Check: A command should
2392 * either have the TRLBAR flag, appear in this list or appear in
2393 * the list at ":help :bar". */
2394 case CMD_aboveleft:
2395 case CMD_and:
2396 case CMD_belowright:
2397 case CMD_botright:
2398 case CMD_browse:
2399 case CMD_call:
2400 case CMD_confirm:
2401 case CMD_delfunction:
2402 case CMD_djump:
2403 case CMD_dlist:
2404 case CMD_dsearch:
2405 case CMD_dsplit:
2406 case CMD_echo:
2407 case CMD_echoerr:
2408 case CMD_echomsg:
2409 case CMD_echon:
2410 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002411 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 case CMD_help:
2413 case CMD_hide:
2414 case CMD_ijump:
2415 case CMD_ilist:
2416 case CMD_isearch:
2417 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002418 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_keepjumps:
2420 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002421 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_leftabove:
2423 case CMD_let:
2424 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002425 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002427 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002428 case CMD_noautocmd:
2429 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 case CMD_perl:
2431 case CMD_psearch:
2432 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002433 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002434 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_return:
2436 case CMD_rightbelow:
2437 case CMD_ruby:
2438 case CMD_silent:
2439 case CMD_smagic:
2440 case CMD_snomagic:
2441 case CMD_substitute:
2442 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002443 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 case CMD_tcl:
2445 case CMD_throw:
2446 case CMD_tilde:
2447 case CMD_topleft:
2448 case CMD_unlet:
2449 case CMD_verbose:
2450 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002451 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 break;
2453
2454 default: goto doend;
2455 }
2456 }
2457#endif
2458
2459 if (ea.argt & XFILE)
2460 {
2461 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2462 goto doend;
2463 }
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 /*
2466 * Accept buffer name. Cannot be used at the same time with a buffer
2467 * number. Don't do this for a user command.
2468 */
2469 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002470 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 /*
2473 * :bdelete, :bwipeout and :bunload take several arguments, separated
2474 * by spaces: find next space (skipping over escaped characters).
2475 * The others take one argument: ignore trailing spaces.
2476 */
2477 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2478 || ea.cmdidx == CMD_bunload)
2479 p = skiptowhite_esc(ea.arg);
2480 else
2481 {
2482 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002483 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 --p;
2485 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002486 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2487 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (ea.line2 < 0) /* failed */
2489 goto doend;
2490 ea.addr_count = 1;
2491 ea.arg = skipwhite(p);
2492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
Bram Moolenaar2be57332018-02-13 18:05:18 +01002494 /* The :try command saves the emsg_silent flag, reset it here when
2495 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002496 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002497 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002498 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002499 if (emsg_silent < 0)
2500 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002501 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002502 }
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002505 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002509 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 /*
2512 * Execute a user-defined command.
2513 */
2514 do_ucmd(&ea);
2515 }
2516 else
2517#endif
2518 {
2519 /*
2520 * Call the function to execute the command.
2521 */
2522 ea.errmsg = NULL;
2523 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2524 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002525 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527
2528#ifdef FEAT_EVAL
2529 /*
2530 * If the command just executed called do_cmdline(), any throw or ":return"
2531 * or ":finish" encountered there must also check the cstack of the still
2532 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2533 * exception, or reanimate a returned function or finished script file and
2534 * return or finish it again.
2535 */
2536 if (need_rethrow)
2537 do_throw(cstack);
2538 else if (check_cstack)
2539 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002540 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002542 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 && current_func_returned())
2544 do_return(&ea, TRUE, FALSE, NULL);
2545 }
2546 need_rethrow = check_cstack = FALSE;
2547#endif
2548
2549doend:
2550 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002553 curwin->w_cursor.col = 0;
2554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2557 {
2558 if (sourcing)
2559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002560 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
2562 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002563 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002565 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 emsg(errormsg);
2568 }
2569#ifdef FEAT_EVAL
2570 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002571 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2572 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#endif
2574
Bram Moolenaareffed932018-08-14 13:38:17 +02002575 if (ea.verbose_save >= 0)
2576 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002577
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002578 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 cmdmod = save_cmdmod;
2580
Bram Moolenaareffed932018-08-14 13:38:17 +02002581 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 /* messages could be enabled for a serious error, need to check if the
2584 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002585 if (!did_emsg || msg_silent > ea.save_msg_silent)
2586 msg_silent = ea.save_msg_silent;
2587 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (emsg_silent < 0)
2589 emsg_silent = 0;
2590 /* Restore msg_scroll, it's set by file I/O commands, even when no
2591 * message is actually displayed. */
2592 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002593
2594 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2595 * somewhere in the line. Put it back in the first column. */
2596 if (redirecting())
2597 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002600#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002601 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002602 --sandbox;
2603#endif
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2606 ea.nextcmd = NULL;
2607
2608#ifdef FEAT_EVAL
2609 --ex_nesting_level;
2610#endif
2611
2612 return ea.nextcmd;
2613}
2614#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002615 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#endif
2617
2618/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 * Parse and skip over command modifiers:
2620 * - update eap->cmd
2621 * - store flags in "cmdmod".
2622 * - Set ex_pressedreturn for an empty command line.
2623 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002624 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002625 * - set p_verbose for ":verbose"
2626 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002627 * When "skip_only" is TRUE the global variables are not changed, except for
2628 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002629 * Return FAIL when the command is not to be executed.
2630 * May set "errormsg" to an error message.
2631 */
2632 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002633parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002634{
2635 char_u *p;
2636
2637 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2638 eap->verbose_save = -1;
2639 eap->save_msg_silent = -1;
2640
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002641 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 for (;;)
2643 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2645 ++eap->cmd;
2646
2647 /* in ex mode, an empty line works like :+ */
2648 if (*eap->cmd == NUL && exmode_active
2649 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2650 || getline_equal(eap->getline, eap->cookie, getexline))
2651 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2652 {
2653 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002654 if (!skip_only)
2655 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 }
2657
2658 /* ignore comment and empty lines */
2659 if (*eap->cmd == '"')
2660 return FAIL;
2661 if (*eap->cmd == NUL)
2662 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002663 if (!skip_only)
2664 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002665 return FAIL;
2666 }
2667
Bram Moolenaareffed932018-08-14 13:38:17 +02002668 p = skip_range(eap->cmd, NULL);
2669 switch (*p)
2670 {
2671 /* When adding an entry, also modify cmd_exists(). */
2672 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2673 break;
2674 cmdmod.split |= WSP_ABOVE;
2675 continue;
2676
2677 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2678 {
2679 cmdmod.split |= WSP_BELOW;
2680 continue;
2681 }
2682 if (checkforcmd(&eap->cmd, "browse", 3))
2683 {
2684#ifdef FEAT_BROWSE_CMD
2685 cmdmod.browse = TRUE;
2686#endif
2687 continue;
2688 }
2689 if (!checkforcmd(&eap->cmd, "botright", 2))
2690 break;
2691 cmdmod.split |= WSP_BOT;
2692 continue;
2693
2694 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2695 break;
2696#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2697 cmdmod.confirm = TRUE;
2698#endif
2699 continue;
2700
2701 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2702 {
2703 cmdmod.keepmarks = TRUE;
2704 continue;
2705 }
2706 if (checkforcmd(&eap->cmd, "keepalt", 5))
2707 {
2708 cmdmod.keepalt = TRUE;
2709 continue;
2710 }
2711 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2712 {
2713 cmdmod.keeppatterns = TRUE;
2714 continue;
2715 }
2716 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2717 break;
2718 cmdmod.keepjumps = TRUE;
2719 continue;
2720
2721 case 'f': /* only accept ":filter {pat} cmd" */
2722 {
2723 char_u *reg_pat;
2724
2725 if (!checkforcmd(&p, "filter", 4)
2726 || *p == NUL || ends_excmd(*p))
2727 break;
2728 if (*p == '!')
2729 {
2730 cmdmod.filter_force = TRUE;
2731 p = skipwhite(p + 1);
2732 if (*p == NUL || ends_excmd(*p))
2733 break;
2734 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002735 if (skip_only)
2736 p = skip_vimgrep_pat(p, NULL, NULL);
2737 else
2738 // NOTE: This puts a NUL after the pattern.
2739 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 if (p == NULL || *p == NUL)
2741 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002742 if (!skip_only)
2743 {
2744 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002745 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002746 if (cmdmod.filter_regmatch.regprog == NULL)
2747 break;
2748 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002749 eap->cmd = p;
2750 continue;
2751 }
2752
2753 /* ":hide" and ":hide | cmd" are not modifiers */
2754 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2755 || *p == NUL || ends_excmd(*p))
2756 break;
2757 eap->cmd = p;
2758 cmdmod.hide = TRUE;
2759 continue;
2760
2761 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2762 {
2763 cmdmod.lockmarks = TRUE;
2764 continue;
2765 }
2766
2767 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2768 break;
2769 cmdmod.split |= WSP_ABOVE;
2770 continue;
2771
2772 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2773 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002774 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 {
2776 /* Set 'eventignore' to "all". Restore the
2777 * existing option value later. */
2778 cmdmod.save_ei = vim_strsave(p_ei);
2779 set_string_option_direct((char_u *)"ei", -1,
2780 (char_u *)"all", OPT_FREE, SID_NONE);
2781 }
2782 continue;
2783 }
2784 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2785 break;
2786 cmdmod.noswapfile = TRUE;
2787 continue;
2788
2789 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2790 break;
2791 cmdmod.split |= WSP_BELOW;
2792 continue;
2793
2794 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2795 {
2796#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002797 if (!skip_only)
2798 {
2799 if (!eap->did_sandbox)
2800 ++sandbox;
2801 eap->did_sandbox = TRUE;
2802 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002803#endif
2804 continue;
2805 }
2806 if (!checkforcmd(&eap->cmd, "silent", 3))
2807 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002808 if (!skip_only)
2809 {
2810 if (eap->save_msg_silent == -1)
2811 eap->save_msg_silent = msg_silent;
2812 ++msg_silent;
2813 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2815 {
2816 /* ":silent!", but not "silent !cmd" */
2817 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002818 if (!skip_only)
2819 {
2820 ++emsg_silent;
2821 ++eap->did_esilent;
2822 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 }
2824 continue;
2825
2826 case 't': if (checkforcmd(&p, "tab", 3))
2827 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002828 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002830 long tabnr = get_address(eap, &eap->cmd,
2831 ADDR_TABS, eap->skip,
2832 skip_only, FALSE, 1);
2833 if (tabnr == MAXLNUM)
2834 cmdmod.tab = tabpage_index(curtab) + 1;
2835 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002837 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002839 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002840 return FAIL;
2841 }
2842 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 }
2845 eap->cmd = p;
2846 continue;
2847 }
2848 if (!checkforcmd(&eap->cmd, "topleft", 2))
2849 break;
2850 cmdmod.split |= WSP_TOP;
2851 continue;
2852
2853 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2854 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002855 if (!skip_only)
2856 {
2857 if (eap->save_msg_silent == -1)
2858 eap->save_msg_silent = msg_silent;
2859 msg_silent = 0;
2860 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 continue;
2862
2863 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2864 {
2865 cmdmod.split |= WSP_VERT;
2866 continue;
2867 }
2868 if (!checkforcmd(&p, "verbose", 4))
2869 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002870 if (!skip_only)
2871 {
2872 if (eap->verbose_save < 0)
2873 eap->verbose_save = p_verbose;
2874 if (vim_isdigit(*eap->cmd))
2875 p_verbose = atoi((char *)eap->cmd);
2876 else
2877 p_verbose = 1;
2878 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 eap->cmd = p;
2880 continue;
2881 }
2882 break;
2883 }
2884
2885 return OK;
2886}
2887
2888/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002889 * Free contents of "cmdmod".
2890 */
2891 static void
2892free_cmdmod(void)
2893{
2894 if (cmdmod.save_ei != NULL)
2895 {
2896 /* Restore 'eventignore' to the value before ":noautocmd". */
2897 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2898 OPT_FREE, SID_NONE);
2899 free_string_option(cmdmod.save_ei);
2900 }
2901
2902 if (cmdmod.filter_regmatch.regprog != NULL)
2903 vim_regfree(cmdmod.filter_regmatch.regprog);
2904}
2905
2906/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002907 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002908 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909 * Return FAIL and set "errormsg" or return OK.
2910 */
2911 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002912parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002913{
2914 int address_count = 1;
2915 linenr_T lnum;
2916
2917 // Repeat for all ',' or ';' separated addresses.
2918 for (;;)
2919 {
2920 eap->line1 = eap->line2;
2921 switch (eap->addr_type)
2922 {
2923 case ADDR_LINES:
2924 // default is current line number
2925 eap->line2 = curwin->w_cursor.lnum;
2926 break;
2927 case ADDR_WINDOWS:
2928 eap->line2 = CURRENT_WIN_NR;
2929 break;
2930 case ADDR_ARGUMENTS:
2931 eap->line2 = curwin->w_arg_idx + 1;
2932 if (eap->line2 > ARGCOUNT)
2933 eap->line2 = ARGCOUNT;
2934 break;
2935 case ADDR_LOADED_BUFFERS:
2936 case ADDR_BUFFERS:
2937 eap->line2 = curbuf->b_fnum;
2938 break;
2939 case ADDR_TABS:
2940 eap->line2 = CURRENT_TAB_NR;
2941 break;
2942 case ADDR_TABS_RELATIVE:
2943 eap->line2 = 1;
2944 break;
2945#ifdef FEAT_QUICKFIX
2946 case ADDR_QUICKFIX:
2947 eap->line2 = qf_get_cur_valid_idx(eap);
2948 break;
2949#endif
2950 }
2951 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002952 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953 eap->addr_count == 0, address_count++);
2954 if (eap->cmd == NULL) // error detected
2955 return FAIL;
2956 if (lnum == MAXLNUM)
2957 {
2958 if (*eap->cmd == '%') // '%' - all lines
2959 {
2960 ++eap->cmd;
2961 switch (eap->addr_type)
2962 {
2963 case ADDR_LINES:
2964 eap->line1 = 1;
2965 eap->line2 = curbuf->b_ml.ml_line_count;
2966 break;
2967 case ADDR_LOADED_BUFFERS:
2968 {
2969 buf_T *buf = firstbuf;
2970
2971 while (buf->b_next != NULL
2972 && buf->b_ml.ml_mfp == NULL)
2973 buf = buf->b_next;
2974 eap->line1 = buf->b_fnum;
2975 buf = lastbuf;
2976 while (buf->b_prev != NULL
2977 && buf->b_ml.ml_mfp == NULL)
2978 buf = buf->b_prev;
2979 eap->line2 = buf->b_fnum;
2980 break;
2981 }
2982 case ADDR_BUFFERS:
2983 eap->line1 = firstbuf->b_fnum;
2984 eap->line2 = lastbuf->b_fnum;
2985 break;
2986 case ADDR_WINDOWS:
2987 case ADDR_TABS:
2988 if (IS_USER_CMDIDX(eap->cmdidx))
2989 {
2990 eap->line1 = 1;
2991 eap->line2 = eap->addr_type == ADDR_WINDOWS
2992 ? LAST_WIN_NR : LAST_TAB_NR;
2993 }
2994 else
2995 {
2996 // there is no Vim command which uses '%' and
2997 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002998 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999 return FAIL;
3000 }
3001 break;
3002 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003003 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003004 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 return FAIL;
3006 case ADDR_ARGUMENTS:
3007 if (ARGCOUNT == 0)
3008 eap->line1 = eap->line2 = 0;
3009 else
3010 {
3011 eap->line1 = 1;
3012 eap->line2 = ARGCOUNT;
3013 }
3014 break;
3015#ifdef FEAT_QUICKFIX
3016 case ADDR_QUICKFIX:
3017 eap->line1 = 1;
3018 eap->line2 = qf_get_size(eap);
3019 if (eap->line2 == 0)
3020 eap->line2 = 1;
3021 break;
3022#endif
3023 }
3024 ++eap->addr_count;
3025 }
3026 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3027 {
3028 pos_T *fp;
3029
3030 // '*' - visual area
3031 if (eap->addr_type != ADDR_LINES)
3032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003033 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003034 return FAIL;
3035 }
3036
3037 ++eap->cmd;
3038 if (!eap->skip)
3039 {
3040 fp = getmark('<', FALSE);
3041 if (check_mark(fp) == FAIL)
3042 return FAIL;
3043 eap->line1 = fp->lnum;
3044 fp = getmark('>', FALSE);
3045 if (check_mark(fp) == FAIL)
3046 return FAIL;
3047 eap->line2 = fp->lnum;
3048 ++eap->addr_count;
3049 }
3050 }
3051 }
3052 else
3053 eap->line2 = lnum;
3054 eap->addr_count++;
3055
3056 if (*eap->cmd == ';')
3057 {
3058 if (!eap->skip)
3059 {
3060 curwin->w_cursor.lnum = eap->line2;
3061 // don't leave the cursor on an illegal line or column
3062 check_cursor();
3063 }
3064 }
3065 else if (*eap->cmd != ',')
3066 break;
3067 ++eap->cmd;
3068 }
3069
3070 // One address given: set start and end lines.
3071 if (eap->addr_count == 1)
3072 {
3073 eap->line1 = eap->line2;
3074 // ... but only implicit: really no address given
3075 if (lnum == MAXLNUM)
3076 eap->addr_count = 0;
3077 }
3078 return OK;
3079}
3080
3081/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003082 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * If there is a match advance "pp" to the argument and return TRUE.
3084 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003085 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003086checkforcmd(
3087 char_u **pp, /* start of command */
3088 char *cmd, /* name of command */
3089 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 int i;
3092
3093 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003094 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 break;
3096 if (i >= len && !isalpha((*pp)[i]))
3097 {
3098 *pp = skipwhite(*pp + i);
3099 return TRUE;
3100 }
3101 return FALSE;
3102}
3103
3104/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003105 * Append "cmd" to the error message in IObuff.
3106 * Takes care of limiting the length and handling 0xa0, which would be
3107 * invisible otherwise.
3108 */
3109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003111{
3112 char_u *s = cmd;
3113 char_u *d;
3114
3115 STRCAT(IObuff, ": ");
3116 d = IObuff + STRLEN(IObuff);
3117 while (*s != NUL && d - IObuff < IOSIZE - 7)
3118 {
3119 if (
3120#ifdef FEAT_MBYTE
3121 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3122#endif
3123 *s == 0xa0)
3124 {
3125 s +=
3126#ifdef FEAT_MBYTE
3127 enc_utf8 ? 2 :
3128#endif
3129 1;
3130 STRCPY(d, "<a0>");
3131 d += 4;
3132 }
3133 else
3134 MB_COPY_CHAR(s, d);
3135 }
3136 *d = NUL;
3137}
3138
3139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003141 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003143 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Returns NULL for an ambiguous user command.
3145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 int len;
3150 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003151 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 /*
3154 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003155 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 * - the 'k' command can directly be followed by any character.
3157 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003158 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003160 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 */
3162 p = eap->cmd;
3163 if (*p == 'k')
3164 {
3165 eap->cmdidx = CMD_k;
3166 ++p;
3167 }
3168 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003169 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3170 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 || p[1] == 'g'
3172 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3173 || p[1] == 'I'
3174 || (p[1] == 'r' && p[2] != 'e')))
3175 {
3176 eap->cmdidx = CMD_substitute;
3177 ++p;
3178 }
3179 else
3180 {
3181 while (ASCII_ISALPHA(*p))
3182 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003183 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003184 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003185 while (ASCII_ISALNUM(*p))
3186 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 /* check for non-alpha command */
3189 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3190 ++p;
3191 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003192 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3193 {
3194 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3195 * :delete with the 'l' flag. Same for 'p'. */
3196 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003197 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003198 break;
3199 if (i == len - 1)
3200 {
3201 --len;
3202 if (p[-1] == 'l')
3203 eap->flags |= EXFLAG_LIST;
3204 else
3205 eap->flags |= EXFLAG_PRINT;
3206 }
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003209 if (ASCII_ISLOWER(eap->cmd[0]))
3210 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003211 int c1 = eap->cmd[0];
3212 int c2 = eap->cmd[1];
3213
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003214 if (command_count != (int)CMD_SIZE)
3215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003216 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003217 getout(1);
3218 }
3219
3220 /* Use a precomputed index for fast look-up in cmdnames[]
3221 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003222 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3223 if (ASCII_ISLOWER(c2))
3224 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003227 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3230 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3231 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3232 (size_t)len) == 0)
3233 {
3234#ifdef FEAT_EVAL
3235 if (full != NULL
3236 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3237 *full = TRUE;
3238#endif
3239 break;
3240 }
3241
3242#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003243 /* Look for a user defined command as a last resort. Let ":Print" be
3244 * overruled by a user defined command. */
3245 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3246 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 while (ASCII_ISALNUM(*p))
3250 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 eap->cmdidx = CMD_SIZE;
3256 }
3257
3258 return p;
3259}
3260
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261#ifdef FEAT_USR_CMDS
3262/*
3263 * Search for a user command that matches "eap->cmd".
3264 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3265 * Return a pointer to just after the command.
3266 * Return NULL if there is no matching command.
3267 */
3268 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003269find_ucmd(
3270 exarg_T *eap,
3271 char_u *p, /* end of the command (possibly including count) */
3272 int *full, /* set to TRUE for a full match */
3273 expand_T *xp, /* used for completion, NULL otherwise */
3274 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003275{
3276 int len = (int)(p - eap->cmd);
3277 int j, k, matchlen = 0;
3278 ucmd_T *uc;
3279 int found = FALSE;
3280 int possible = FALSE;
3281 char_u *cp, *np; /* Point into typed cmd and test name */
3282 garray_T *gap;
3283 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3284 only full match global is accepted. */
3285
3286 /*
3287 * Look for buffer-local user commands first, then global ones.
3288 */
3289 gap = &curbuf->b_ucmds;
3290 for (;;)
3291 {
3292 for (j = 0; j < gap->ga_len; ++j)
3293 {
3294 uc = USER_CMD_GA(gap, j);
3295 cp = eap->cmd;
3296 np = uc->uc_name;
3297 k = 0;
3298 while (k < len && *np != NUL && *cp++ == *np++)
3299 k++;
3300 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3301 {
3302 /* If finding a second match, the command is ambiguous. But
3303 * not if a buffer-local command wasn't a full match and a
3304 * global command is a full match. */
3305 if (k == len && found && *np != NUL)
3306 {
3307 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003308 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 amb_local = TRUE;
3310 }
3311
3312 if (!found || (k == len && *np == NUL))
3313 {
3314 /* If we matched up to a digit, then there could
3315 * be another command including the digit that we
3316 * should use instead.
3317 */
3318 if (k == len)
3319 found = TRUE;
3320 else
3321 possible = TRUE;
3322
3323 if (gap == &ucmds)
3324 eap->cmdidx = CMD_USER;
3325 else
3326 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003327 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003328 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003329 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330
3331# ifdef FEAT_CMDL_COMPL
3332 if (compl != NULL)
3333 *compl = uc->uc_compl;
3334# ifdef FEAT_EVAL
3335 if (xp != NULL)
3336 {
3337 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003338 xp->xp_script_ctx = uc->uc_script_ctx;
3339 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003340 }
3341# endif
3342# endif
3343 /* Do not search for further abbreviations
3344 * if this is an exact match. */
3345 matchlen = k;
3346 if (k == len && *np == NUL)
3347 {
3348 if (full != NULL)
3349 *full = TRUE;
3350 amb_local = FALSE;
3351 break;
3352 }
3353 }
3354 }
3355 }
3356
3357 /* Stop if we found a full match or searched all. */
3358 if (j < gap->ga_len || gap == &ucmds)
3359 break;
3360 gap = &ucmds;
3361 }
3362
3363 /* Only found ambiguous matches. */
3364 if (amb_local)
3365 {
3366 if (xp != NULL)
3367 xp->xp_context = EXPAND_UNSUCCESSFUL;
3368 return NULL;
3369 }
3370
3371 /* The match we found may be followed immediately by a number. Move "p"
3372 * back to point to it. */
3373 if (found || possible)
3374 return p + (matchlen - len);
3375 return p;
3376}
3377#endif
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003380static struct cmdmod
3381{
3382 char *name;
3383 int minlen;
3384 int has_count; /* :123verbose :3tab */
3385} cmdmods[] = {
3386 {"aboveleft", 3, FALSE},
3387 {"belowright", 3, FALSE},
3388 {"botright", 2, FALSE},
3389 {"browse", 3, FALSE},
3390 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003391 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003392 {"hide", 3, FALSE},
3393 {"keepalt", 5, FALSE},
3394 {"keepjumps", 5, FALSE},
3395 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003396 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 {"leftabove", 5, FALSE},
3398 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003399 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003400 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003401 {"rightbelow", 6, FALSE},
3402 {"sandbox", 3, FALSE},
3403 {"silent", 3, FALSE},
3404 {"tab", 3, TRUE},
3405 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003406 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003407 {"verbose", 4, TRUE},
3408 {"vertical", 4, FALSE},
3409};
3410
3411/*
3412 * Return length of a command modifier (including optional count).
3413 * Return zero when it's not a modifier.
3414 */
3415 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003416modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003417{
3418 int i, j;
3419 char_u *p = cmd;
3420
3421 if (VIM_ISDIGIT(*cmd))
3422 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003423 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003424 {
3425 for (j = 0; p[j] != NUL; ++j)
3426 if (p[j] != cmdmods[i].name[j])
3427 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003428 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003429 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003430 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003431 }
3432 return 0;
3433}
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435/*
3436 * Return > 0 if an Ex command "name" exists.
3437 * Return 2 if there is an exact match.
3438 * Return 3 if there is an ambiguous match.
3439 */
3440 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003441cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 exarg_T ea;
3444 int full = FALSE;
3445 int i;
3446 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003447 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003450 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
3452 for (j = 0; name[j] != NUL; ++j)
3453 if (name[j] != cmdmods[i].name[j])
3454 break;
3455 if (name[j] == NUL && j >= cmdmods[i].minlen)
3456 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3457 }
3458
Bram Moolenaara9587612006-05-04 21:47:50 +00003459 /* Check built-in commands and user defined commands.
3460 * For ":2match" and ":3match" we need to skip the number. */
3461 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003463 p = find_command(&ea, &full);
3464 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003466 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3467 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003468 if (*skipwhite(p) != NUL)
3469 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3471}
3472#endif
3473
3474/*
3475 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3476 * we don't need/want deleted. Maybe this could be done better if we didn't
3477 * repeat all this stuff. The only problem is that they may not stay
3478 * perfectly compatible with each other, but then the command line syntax
3479 * probably won't change that much -- webb.
3480 */
3481 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003482set_one_cmd_context(
3483 expand_T *xp,
3484 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
3486 char_u *p;
3487 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003488 int len = 0;
3489 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3491 int compl = EXPAND_NOTHING;
3492#endif
3493#ifdef FEAT_CMDL_COMPL
3494 int delim;
3495#endif
3496 int forceit = FALSE;
3497 int usefilter = FALSE; /* filter instead of file name */
3498
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003499 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 xp->xp_pattern = buff;
3501 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003502 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
3504/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003505 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 */
3507 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3508 ;
3509 xp->xp_pattern = cmd;
3510
3511 if (*cmd == NUL)
3512 return NULL;
3513 if (*cmd == '"') /* ignore comment lines */
3514 {
3515 xp->xp_context = EXPAND_NOTHING;
3516 return NULL;
3517 }
3518
3519/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003520 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 */
3522 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 xp->xp_pattern = cmd;
3524 if (*cmd == NUL)
3525 return NULL;
3526 if (*cmd == '"')
3527 {
3528 xp->xp_context = EXPAND_NOTHING;
3529 return NULL;
3530 }
3531
3532 if (*cmd == '|' || *cmd == '\n')
3533 return cmd + 1; /* There's another command */
3534
3535 /*
3536 * Isolate the command and search for it in the command table.
3537 * Exceptions:
3538 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003539 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3541 */
3542 if (*cmd == 'k' && cmd[1] != 'e')
3543 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003544 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 p = cmd + 1;
3546 }
3547 else
3548 {
3549 p = cmd;
3550 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3551 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003552 /* a user command may contain digits */
3553 if (ASCII_ISUPPER(cmd[0]))
3554 while (ASCII_ISALNUM(*p) || *p == '*')
3555 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003556 /* for python 3.x: ":py3*" commands completion */
3557 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003558 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003559 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003560 while (ASCII_ISALPHA(*p) || *p == '*')
3561 ++p;
3562 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003563 /* check for non-alpha command */
3564 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3565 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003566 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 xp->xp_context = EXPAND_UNSUCCESSFUL;
3571 return NULL;
3572 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003574 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3575 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3576 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 break;
3578
3579#ifdef FEAT_USR_CMDS
3580 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3582 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#endif
3584 }
3585
3586 /*
3587 * If the cursor is touching the command, and it ends in an alpha-numeric
3588 * character, complete the command name.
3589 */
3590 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3591 return NULL;
3592
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3596 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003597 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 p = cmd + 1;
3599 }
3600#ifdef FEAT_USR_CMDS
3601 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3602 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003603 ea.cmd = cmd;
3604 p = find_ucmd(&ea, p, NULL, xp,
3605# if defined(FEAT_CMDL_COMPL)
3606 &compl
3607# else
3608 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003610 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003611 if (p == NULL)
3612 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614#endif
3615 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003616 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 /* Not still touching the command and it was an illegal one */
3619 xp->xp_context = EXPAND_UNSUCCESSFUL;
3620 return NULL;
3621 }
3622
3623 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3624
3625 if (*p == '!') /* forced commands */
3626 {
3627 forceit = TRUE;
3628 ++p;
3629 }
3630
3631/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003632 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003634 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003635 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 arg = skipwhite(p);
3638
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641 if (*arg == '>') /* append */
3642 {
3643 if (*++arg == '>')
3644 ++arg;
3645 arg = skipwhite(arg);
3646 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003647 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
3649 ++arg;
3650 usefilter = TRUE;
3651 }
3652 }
3653
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003654 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
3656 usefilter = forceit; /* :r! filter if forced */
3657 if (*arg == '!') /* :r !filter */
3658 {
3659 ++arg;
3660 usefilter = TRUE;
3661 }
3662 }
3663
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003664 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
3666 while (*arg == *cmd) /* allow any number of '>' or '<' */
3667 ++arg;
3668 arg = skipwhite(arg);
3669 }
3670
3671 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003672 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 /* Check if we're in the +command */
3675 p = arg + 1;
3676 arg = skip_cmd_arg(arg, FALSE);
3677
3678 /* Still touching the command after '+'? */
3679 if (*arg == NUL)
3680 return p;
3681
3682 /* Skip space(s) after +command to get to the real argument */
3683 arg = skipwhite(arg);
3684 }
3685
3686 /*
3687 * Check for '|' to separate commands and '"' to start comments.
3688 * Don't do this for ":read !cmd" and ":write !cmd".
3689 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003690 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
3692 p = arg;
3693 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003694 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 p += 2;
3696 while (*p)
3697 {
3698 if (*p == Ctrl_V)
3699 {
3700 if (p[1] != NUL)
3701 ++p;
3702 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003703 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 || *p == '|' || *p == '\n')
3705 {
3706 if (*(p - 1) != '\\')
3707 {
3708 if (*p == '|' || *p == '\n')
3709 return p + 1;
3710 return NULL; /* It's a comment */
3711 }
3712 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003713 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715 }
3716
3717 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003718 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 vim_strchr((char_u *)"|\"", *arg) == NULL)
3720 return NULL;
3721
3722 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003723 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003725 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003726 while (*p && p < buff + len)
3727 {
3728 if (*p == ' ' || *p == TAB)
3729 {
3730 /* argument starts after a space */
3731 xp->xp_pattern = ++p;
3732 }
3733 else
3734 {
3735 if (*p == '\\' && *(p + 1) != NUL)
3736 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003737 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003738 }
3739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003741 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003743 int c;
3744 int in_quote = FALSE;
3745 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
3747 /*
3748 * Allow spaces within back-quotes to count as part of the argument
3749 * being expanded.
3750 */
3751 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003752 p = xp->xp_pattern;
3753 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003755#ifdef FEAT_MBYTE
3756 if (has_mbyte)
3757 c = mb_ptr2char(p);
3758 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003760 c = *p;
3761 if (c == '\\' && p[1] != NUL)
3762 ++p;
3763 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
3765 if (!in_quote)
3766 {
3767 xp->xp_pattern = p;
3768 bow = p + 1;
3769 }
3770 in_quote = !in_quote;
3771 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003772 /* An argument can contain just about everything, except
3773 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003774 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003775#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003776 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003777#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003778 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003779 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003780 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003781 while (*p != NUL)
3782 {
3783#ifdef FEAT_MBYTE
3784 if (has_mbyte)
3785 c = mb_ptr2char(p);
3786 else
3787#endif
3788 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003789 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003790 break;
3791#ifdef FEAT_MBYTE
3792 if (has_mbyte)
3793 len = (*mb_ptr2len)(p);
3794 else
3795#endif
3796 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003797 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003798 }
3799 if (in_quote)
3800 bow = p;
3801 else
3802 xp->xp_pattern = p;
3803 p -= len;
3804 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003805 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807
3808 /*
3809 * If we are still inside the quotes, and we passed a space, just
3810 * expand from there.
3811 */
3812 if (bow != NULL && in_quote)
3813 xp->xp_pattern = bow;
3814 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003815
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003816 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003817 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003818 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003819#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003820 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003821#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003822 /* When still after the command name expand executables. */
3823 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003824 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
3827 /* Check for environment variable */
3828 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003829#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 || *xp->xp_pattern == '%'
3831#endif
3832 )
3833 {
3834 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3835 if (!vim_isIDc(*p))
3836 break;
3837 if (*p == NUL)
3838 {
3839 xp->xp_context = EXPAND_ENV_VARS;
3840 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003841#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3842 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003843 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003844 compl = EXPAND_ENV_VARS;
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003848#if defined(FEAT_CMDL_COMPL)
3849 /* Check for user names */
3850 if (*xp->xp_pattern == '~')
3851 {
3852 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3853 ;
3854 /* Complete ~user only if it partially matches a user name.
3855 * A full match ~user<Tab> will be replaced by user's home
3856 * directory i.e. something like ~user<Tab> -> /home/user/ */
3857 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003858 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003859 {
3860 xp->xp_context = EXPAND_USER;
3861 ++xp->xp_pattern;
3862 }
3863 }
3864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866
3867/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003868 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003870 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003872 case CMD_find:
3873 case CMD_sfind:
3874 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003875 if (xp->xp_context == EXPAND_FILES)
3876 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003877 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_cd:
3879 case CMD_chdir:
3880 case CMD_lcd:
3881 case CMD_lchdir:
3882 if (xp->xp_context == EXPAND_FILES)
3883 xp->xp_context = EXPAND_DIRECTORIES;
3884 break;
3885 case CMD_help:
3886 xp->xp_context = EXPAND_HELP;
3887 xp->xp_pattern = arg;
3888 break;
3889
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003890 /* Command modifiers: return the argument.
3891 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003893 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_belowright:
3895 case CMD_botright:
3896 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003897 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003898 case CMD_cdo:
3899 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003901 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 case CMD_folddoclosed:
3903 case CMD_folddoopen:
3904 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003905 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 case CMD_keepjumps:
3907 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003908 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003909 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003911 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003913 case CMD_noautocmd:
3914 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003916 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003918 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003919 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 case CMD_topleft:
3921 case CMD_verbose:
3922 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003923 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 return arg;
3925
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003926 case CMD_filter:
3927 if (*arg != NUL)
3928 arg = skip_vimgrep_pat(arg, NULL, NULL);
3929 if (arg == NULL || *arg == NUL)
3930 {
3931 xp->xp_context = EXPAND_NOTHING;
3932 return NULL;
3933 }
3934 return skipwhite(arg);
3935
Bram Moolenaar4f688582007-07-24 12:34:30 +00003936#ifdef FEAT_CMDL_COMPL
3937# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 case CMD_match:
3939 if (*arg == NUL || !ends_excmd(*arg))
3940 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003941 /* also complete "None" */
3942 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 arg = skipwhite(skiptowhite(arg));
3944 if (*arg != NUL)
3945 {
3946 xp->xp_context = EXPAND_NOTHING;
3947 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3948 }
3949 }
3950 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953/*
3954 * All completion for the +cmdline_compl feature goes here.
3955 */
3956
3957# ifdef FEAT_USR_CMDS
3958 case CMD_command:
3959 /* Check for attributes */
3960 while (*arg == '-')
3961 {
3962 arg++; /* Skip "-" */
3963 p = skiptowhite(arg);
3964 if (*p == NUL)
3965 {
3966 /* Cursor is still in the attribute */
3967 p = vim_strchr(arg, '=');
3968 if (p == NULL)
3969 {
3970 /* No "=", so complete attribute names */
3971 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3972 xp->xp_pattern = arg;
3973 return NULL;
3974 }
3975
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003976 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 * their arguments as well.
3978 */
3979 if (STRNICMP(arg, "complete", p - arg) == 0)
3980 {
3981 xp->xp_context = EXPAND_USER_COMPLETE;
3982 xp->xp_pattern = p + 1;
3983 return NULL;
3984 }
3985 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3986 {
3987 xp->xp_context = EXPAND_USER_NARGS;
3988 xp->xp_pattern = p + 1;
3989 return NULL;
3990 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003991 else if (STRNICMP(arg, "addr", p - arg) == 0)
3992 {
3993 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3994 xp->xp_pattern = p + 1;
3995 return NULL;
3996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 return NULL;
3998 }
3999 arg = skipwhite(p);
4000 }
4001
4002 /* After the attributes comes the new command name */
4003 p = skiptowhite(arg);
4004 if (*p == NUL)
4005 {
4006 xp->xp_context = EXPAND_USER_COMMANDS;
4007 xp->xp_pattern = arg;
4008 break;
4009 }
4010
4011 /* And finally comes a normal command */
4012 return skipwhite(p);
4013
4014 case CMD_delcommand:
4015 xp->xp_context = EXPAND_USER_COMMANDS;
4016 xp->xp_pattern = arg;
4017 break;
4018# endif
4019
4020 case CMD_global:
4021 case CMD_vglobal:
4022 delim = *arg; /* get the delimiter */
4023 if (delim)
4024 ++arg; /* skip delimiter if there is one */
4025
4026 while (arg[0] != NUL && arg[0] != delim)
4027 {
4028 if (arg[0] == '\\' && arg[1] != NUL)
4029 ++arg;
4030 ++arg;
4031 }
4032 if (arg[0] != NUL)
4033 return arg + 1;
4034 break;
4035 case CMD_and:
4036 case CMD_substitute:
4037 delim = *arg;
4038 if (delim)
4039 {
4040 /* skip "from" part */
4041 ++arg;
4042 arg = skip_regexp(arg, delim, p_magic, NULL);
4043 }
4044 /* skip "to" part */
4045 while (arg[0] != NUL && arg[0] != delim)
4046 {
4047 if (arg[0] == '\\' && arg[1] != NUL)
4048 ++arg;
4049 ++arg;
4050 }
4051 if (arg[0] != NUL) /* skip delimiter */
4052 ++arg;
4053 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4054 ++arg;
4055 if (arg[0] != NUL)
4056 return arg;
4057 break;
4058 case CMD_isearch:
4059 case CMD_dsearch:
4060 case CMD_ilist:
4061 case CMD_dlist:
4062 case CMD_ijump:
4063 case CMD_psearch:
4064 case CMD_djump:
4065 case CMD_isplit:
4066 case CMD_dsplit:
4067 arg = skipwhite(skipdigits(arg)); /* skip count */
4068 if (*arg == '/') /* Match regexp, not just whole words */
4069 {
4070 for (++arg; *arg && *arg != '/'; arg++)
4071 if (*arg == '\\' && arg[1] != NUL)
4072 arg++;
4073 if (*arg)
4074 {
4075 arg = skipwhite(arg + 1);
4076
4077 /* Check for trailing illegal characters */
4078 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4079 xp->xp_context = EXPAND_NOTHING;
4080 else
4081 return arg;
4082 }
4083 }
4084 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 case CMD_autocmd:
4087 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004089 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 case CMD_set:
4092 set_context_in_set_cmd(xp, arg, 0);
4093 break;
4094 case CMD_setglobal:
4095 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4096 break;
4097 case CMD_setlocal:
4098 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4099 break;
4100 case CMD_tag:
4101 case CMD_stag:
4102 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004103 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 case CMD_tselect:
4105 case CMD_stselect:
4106 case CMD_ptselect:
4107 case CMD_tjump:
4108 case CMD_stjump:
4109 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004110 if (*p_wop != NUL)
4111 xp->xp_context = EXPAND_TAGS_LISTFILES;
4112 else
4113 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 xp->xp_pattern = arg;
4115 break;
4116 case CMD_augroup:
4117 xp->xp_context = EXPAND_AUGROUP;
4118 xp->xp_pattern = arg;
4119 break;
4120#ifdef FEAT_SYN_HL
4121 case CMD_syntax:
4122 set_context_in_syntax_cmd(xp, arg);
4123 break;
4124#endif
4125#ifdef FEAT_EVAL
4126 case CMD_let:
4127 case CMD_if:
4128 case CMD_elseif:
4129 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004130 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 case CMD_echo:
4132 case CMD_echon:
4133 case CMD_execute:
4134 case CMD_echomsg:
4135 case CMD_echoerr:
4136 case CMD_call:
4137 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004138 case CMD_cexpr:
4139 case CMD_caddexpr:
4140 case CMD_cgetexpr:
4141 case CMD_lexpr:
4142 case CMD_laddexpr:
4143 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004144 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 break;
4146
4147 case CMD_unlet:
4148 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4149 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 xp->xp_context = EXPAND_USER_VARS;
4152 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004153
4154 if (*xp->xp_pattern == '$')
4155 {
4156 xp->xp_context = EXPAND_ENV_VARS;
4157 ++xp->xp_pattern;
4158 }
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 break;
4161
4162 case CMD_function:
4163 case CMD_delfunction:
4164 xp->xp_context = EXPAND_USER_FUNC;
4165 xp->xp_pattern = arg;
4166 break;
4167
4168 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004169 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 break;
4171#endif
4172 case CMD_highlight:
4173 set_context_in_highlight_cmd(xp, arg);
4174 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004175#ifdef FEAT_CSCOPE
4176 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004177 case CMD_lcscope:
4178 case CMD_scscope:
4179 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004180 break;
4181#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004182#ifdef FEAT_SIGNS
4183 case CMD_sign:
4184 set_context_in_sign_cmd(xp, arg);
4185 break;
4186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 case CMD_bdelete:
4188 case CMD_bwipeout:
4189 case CMD_bunload:
4190 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4191 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004192 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 case CMD_buffer:
4194 case CMD_sbuffer:
4195 case CMD_checktime:
4196 xp->xp_context = EXPAND_BUFFERS;
4197 xp->xp_pattern = arg;
4198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#ifdef FEAT_USR_CMDS
4200 case CMD_USER:
4201 case CMD_USER_BUF:
4202 if (compl != EXPAND_NOTHING)
4203 {
4204 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004205 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
4207# ifdef FEAT_MENU
4208 if (compl == EXPAND_MENUS)
4209 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4210# endif
4211 if (compl == EXPAND_COMMANDS)
4212 return arg;
4213 if (compl == EXPAND_MAPPINGS)
4214 return set_context_in_map_cmd(xp, (char_u *)"map",
4215 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004216 /* Find start of last argument. */
4217 p = arg;
4218 while (*p)
4219 {
4220 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004221 /* argument starts after a space */
4222 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004223 else if (*p == '\\' && *(p + 1) != NUL)
4224 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004225 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 xp->xp_pattern = arg;
4228 }
4229 xp->xp_context = compl;
4230 }
4231 break;
4232#endif
4233 case CMD_map: case CMD_noremap:
4234 case CMD_nmap: case CMD_nnoremap:
4235 case CMD_vmap: case CMD_vnoremap:
4236 case CMD_omap: case CMD_onoremap:
4237 case CMD_imap: case CMD_inoremap:
4238 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004239 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004240 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004241 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004242 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004244 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 case CMD_unmap:
4246 case CMD_nunmap:
4247 case CMD_vunmap:
4248 case CMD_ounmap:
4249 case CMD_iunmap:
4250 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004251 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004252 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004253 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004254 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004256 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004257 case CMD_mapclear:
4258 case CMD_nmapclear:
4259 case CMD_vmapclear:
4260 case CMD_omapclear:
4261 case CMD_imapclear:
4262 case CMD_cmapclear:
4263 case CMD_lmapclear:
4264 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004265 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004266 case CMD_xmapclear:
4267 xp->xp_context = EXPAND_MAPCLEAR;
4268 xp->xp_pattern = arg;
4269 break;
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 case CMD_abbreviate: case CMD_noreabbrev:
4272 case CMD_cabbrev: case CMD_cnoreabbrev:
4273 case CMD_iabbrev: case CMD_inoreabbrev:
4274 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004275 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 case CMD_unabbreviate:
4277 case CMD_cunabbrev:
4278 case CMD_iunabbrev:
4279 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004280 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#ifdef FEAT_MENU
4282 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4283 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4284 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4285 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4286 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4287 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4288 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004289 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 case CMD_tmenu: case CMD_tunmenu:
4291 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4292 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4293#endif
4294
4295 case CMD_colorscheme:
4296 xp->xp_context = EXPAND_COLORS;
4297 xp->xp_pattern = arg;
4298 break;
4299
4300 case CMD_compiler:
4301 xp->xp_context = EXPAND_COMPILER;
4302 xp->xp_pattern = arg;
4303 break;
4304
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004305 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004306 xp->xp_context = EXPAND_OWNSYNTAX;
4307 xp->xp_pattern = arg;
4308 break;
4309
4310 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004311 xp->xp_context = EXPAND_FILETYPE;
4312 xp->xp_pattern = arg;
4313 break;
4314
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004315 case CMD_packadd:
4316 xp->xp_context = EXPAND_PACKADD;
4317 xp->xp_pattern = arg;
4318 break;
4319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4321 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4322 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004323 p = skiptowhite(arg);
4324 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
4326 xp->xp_context = EXPAND_LANGUAGE;
4327 xp->xp_pattern = arg;
4328 }
4329 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004330 {
4331 if ( STRNCMP(arg, "messages", p - arg) == 0
4332 || STRNCMP(arg, "ctype", p - arg) == 0
4333 || STRNCMP(arg, "time", p - arg) == 0)
4334 {
4335 xp->xp_context = EXPAND_LOCALES;
4336 xp->xp_pattern = skipwhite(p);
4337 }
4338 else
4339 xp->xp_context = EXPAND_NOTHING;
4340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 break;
4342#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004343#if defined(FEAT_PROFILE)
4344 case CMD_profile:
4345 set_context_in_profile_cmd(xp, arg);
4346 break;
4347#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004348 case CMD_behave:
4349 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004350 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004353 case CMD_messages:
4354 xp->xp_context = EXPAND_MESSAGES;
4355 xp->xp_pattern = arg;
4356 break;
4357
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004358#if defined(FEAT_CMDHIST)
4359 case CMD_history:
4360 xp->xp_context = EXPAND_HISTORY;
4361 xp->xp_pattern = arg;
4362 break;
4363#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004364#if defined(FEAT_PROFILE)
4365 case CMD_syntime:
4366 xp->xp_context = EXPAND_SYNTIME;
4367 xp->xp_pattern = arg;
4368 break;
4369#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004370
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004371 case CMD_argdelete:
4372 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4373 arg = xp->xp_pattern + 1;
4374 xp->xp_context = EXPAND_ARGLIST;
4375 xp->xp_pattern = arg;
4376 break;
4377
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378#endif /* FEAT_CMDL_COMPL */
4379
4380 default:
4381 break;
4382 }
4383 return NULL;
4384}
4385
4386/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004387 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 *
4389 * Backslashed delimiters after / or ? will be skipped, and commands will
4390 * not be expanded between /'s and ?'s or after "'".
4391 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004392 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 * Returns the "cmd" pointer advanced to beyond the range.
4394 */
4395 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004396skip_range(
4397 char_u *cmd,
4398 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004400 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004402 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004404 if (*cmd == '\\')
4405 {
4406 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4407 ++cmd;
4408 else
4409 break;
4410 }
4411 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
4413 if (*++cmd == NUL && ctx != NULL)
4414 *ctx = EXPAND_NOTHING;
4415 }
4416 else if (*cmd == '/' || *cmd == '?')
4417 {
4418 delim = *cmd++;
4419 while (*cmd != NUL && *cmd != delim)
4420 if (*cmd++ == '\\' && *cmd != NUL)
4421 ++cmd;
4422 if (*cmd == NUL && ctx != NULL)
4423 *ctx = EXPAND_NOTHING;
4424 }
4425 if (*cmd != NUL)
4426 ++cmd;
4427 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004428
4429 /* Skip ":" and white space. */
4430 while (*cmd == ':')
4431 cmd = skipwhite(cmd + 1);
4432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 return cmd;
4434}
4435
4436/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004437 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 *
4439 * Set ptr to the next character after the part that was interpreted.
4440 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004441 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 *
4443 * Return MAXLNUM when no Ex address was found.
4444 */
4445 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004446get_address(
4447 exarg_T *eap UNUSED,
4448 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004449 int addr_type, // flag: one of ADDR_LINES, ...
4450 int skip, // only skip the address, don't use it
4451 int silent, // no errors or side effects
4452 int to_other_file, // flag: may jump to other file
4453 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
4455 int c;
4456 int i;
4457 long n;
4458 char_u *cmd;
4459 pos_T pos;
4460 pos_T *fp;
4461 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004462 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 cmd = skipwhite(*ptr);
4465 lnum = MAXLNUM;
4466 do
4467 {
4468 switch (*cmd)
4469 {
4470 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 ++cmd;
4472 switch (addr_type)
4473 {
4474 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 lnum = curwin->w_cursor.lnum;
4476 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004477 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004478 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 break;
4480 case ADDR_ARGUMENTS:
4481 lnum = curwin->w_arg_idx + 1;
4482 break;
4483 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004484 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 lnum = curbuf->b_fnum;
4486 break;
4487 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004488 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004490 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004491 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004492 cmd = NULL;
4493 goto error;
4494 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004495#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004496 case ADDR_QUICKFIX:
4497 lnum = qf_get_cur_valid_idx(eap);
4498 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004499#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 }
4501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 ++cmd;
4505 switch (addr_type)
4506 {
4507 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 lnum = curbuf->b_ml.ml_line_count;
4509 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004511 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 break;
4513 case ADDR_ARGUMENTS:
4514 lnum = ARGCOUNT;
4515 break;
4516 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004517 buf = lastbuf;
4518 while (buf->b_ml.ml_mfp == NULL)
4519 {
4520 if (buf->b_prev == NULL)
4521 break;
4522 buf = buf->b_prev;
4523 }
4524 lnum = buf->b_fnum;
4525 break;
4526 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004527 lnum = lastbuf->b_fnum;
4528 break;
4529 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004530 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004532 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004533 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004534 cmd = NULL;
4535 goto error;
4536 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004537#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004538 case ADDR_QUICKFIX:
4539 lnum = qf_get_size(eap);
4540 if (lnum == 0)
4541 lnum = 1;
4542 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004543#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 }
4545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
4547 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004548 if (*++cmd == NUL)
4549 {
4550 cmd = NULL;
4551 goto error;
4552 }
4553 if (addr_type != ADDR_LINES)
4554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004555 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004556 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 goto error;
4558 }
4559 if (skip)
4560 ++cmd;
4561 else
4562 {
4563 /* Only accept a mark in another file when it is
4564 * used by itself: ":'M". */
4565 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4566 ++cmd;
4567 if (fp == (pos_T *)-1)
4568 /* Jumped to another file. */
4569 lnum = curwin->w_cursor.lnum;
4570 else
4571 {
4572 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
4574 cmd = NULL;
4575 goto error;
4576 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 lnum = fp->lnum;
4578 }
4579 }
4580 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582 case '/':
4583 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004584 c = *cmd++;
4585 if (addr_type != ADDR_LINES)
4586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004587 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004588 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004589 goto error;
4590 }
4591 if (skip) /* skip "/pat/" */
4592 {
4593 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4594 if (*cmd == c)
4595 ++cmd;
4596 }
4597 else
4598 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004599 int flags;
4600
4601 pos = curwin->w_cursor; // save curwin->w_cursor
4602
4603 // When '/' or '?' follows another address, start from
4604 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 if (lnum != MAXLNUM)
4606 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004607
4608 // Start a forward search at the end of the line (unless
4609 // before the first line).
4610 // Start a backward search at the start of the line.
4611 // This makes sure we never match in the current
4612 // line, and can match anywhere in the
4613 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004614 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 curwin->w_cursor.col = MAXCOL;
4616 else
4617 curwin->w_cursor.col = 0;
4618 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004619 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4620 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 {
4622 curwin->w_cursor = pos;
4623 cmd = NULL;
4624 goto error;
4625 }
4626 lnum = curwin->w_cursor.lnum;
4627 curwin->w_cursor = pos;
4628 /* adjust command string pointer */
4629 cmd += searchcmdlen;
4630 }
4631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
4633 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 ++cmd;
4635 if (addr_type != ADDR_LINES)
4636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004637 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004638 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004639 goto error;
4640 }
4641 if (*cmd == '&')
4642 i = RE_SUBST;
4643 else if (*cmd == '?' || *cmd == '/')
4644 i = RE_SEARCH;
4645 else
4646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004647 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 cmd = NULL;
4649 goto error;
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004652 if (!skip)
4653 {
4654 /*
4655 * When search follows another address, start from
4656 * there.
4657 */
4658 if (lnum != MAXLNUM)
4659 pos.lnum = lnum;
4660 else
4661 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004663 /*
4664 * Start the search just like for the above
4665 * do_search().
4666 */
4667 if (*cmd != '?')
4668 pos.col = MAXCOL;
4669 else
4670 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004671#ifdef FEAT_VIRTUALEDIT
4672 pos.coladd = 0;
4673#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004674 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004675 *cmd == '?' ? BACKWARD : FORWARD,
4676 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004677 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004678 lnum = pos.lnum;
4679 else
4680 {
4681 cmd = NULL;
4682 goto error;
4683 }
4684 }
4685 ++cmd;
4686 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004689 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4690 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692
4693 for (;;)
4694 {
4695 cmd = skipwhite(cmd);
4696 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4697 break;
4698
4699 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004700 {
4701 switch (addr_type)
4702 {
4703 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004704 /* "+1" is same as ".+1" */
4705 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004706 break;
4707 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004708 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004709 break;
4710 case ADDR_ARGUMENTS:
4711 lnum = curwin->w_arg_idx + 1;
4712 break;
4713 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004714 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004715 lnum = curbuf->b_fnum;
4716 break;
4717 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004718 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004719 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004720 case ADDR_TABS_RELATIVE:
4721 lnum = 1;
4722 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004723#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004724 case ADDR_QUICKFIX:
4725 lnum = qf_get_cur_valid_idx(eap);
4726 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004727#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004728 }
4729 }
4730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (VIM_ISDIGIT(*cmd))
4732 i = '+'; /* "number" is same as "+number" */
4733 else
4734 i = *cmd++;
4735 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4736 n = 1;
4737 else
4738 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004739
4740 if (addr_type == ADDR_TABS_RELATIVE)
4741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004742 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004743 cmd = NULL;
4744 goto error;
4745 }
4746 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004747 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004748 lnum = compute_buffer_local_count(
4749 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004751 {
4752#ifdef FEAT_FOLDING
4753 /* Relative line addressing, need to adjust for folded lines
4754 * now, but only do it after the first address. */
4755 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4756 && address_count >= 2)
4757 (void)hasFolding(lnum, NULL, &lnum);
4758#endif
4759 if (i == '-')
4760 lnum -= n;
4761 else
4762 lnum += n;
4763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765 } while (*cmd == '/' || *cmd == '?');
4766
4767error:
4768 *ptr = cmd;
4769 return lnum;
4770}
4771
4772/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004773 * Get flags from an Ex command argument.
4774 */
4775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004777{
4778 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4779 {
4780 if (*eap->arg == 'l')
4781 eap->flags |= EXFLAG_LIST;
4782 else if (*eap->arg == 'p')
4783 eap->flags |= EXFLAG_PRINT;
4784 else
4785 eap->flags |= EXFLAG_NR;
4786 eap->arg = skipwhite(eap->arg + 1);
4787 }
4788}
4789
4790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 * Function called for command which is Not Implemented. NI!
4792 */
4793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004794ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004797 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798}
4799
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004800#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801/*
4802 * Function called for script command which is Not Implemented. NI!
4803 * Skips over ":perl <<EOF" constructs.
4804 */
4805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004806ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808 if (!eap->skip)
4809 ex_ni(eap);
4810 else
4811 vim_free(script_get(eap, eap->arg));
4812}
4813#endif
4814
4815/*
4816 * Check range in Ex command for validity.
4817 * Return NULL when valid, error message when invalid.
4818 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004820invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004822 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if ( eap->line1 < 0
4824 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004826 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004827
4828 if (eap->argt & RANGE)
4829 {
4830 switch(eap->addr_type)
4831 {
4832 case ADDR_LINES:
4833 if (!(eap->argt & NOTADR)
4834 && eap->line2 > curbuf->b_ml.ml_line_count
4835#ifdef FEAT_DIFF
4836 + (eap->cmdidx == CMD_diffget)
4837#endif
4838 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004840 break;
4841 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004842 /* add 1 if ARGCOUNT is 0 */
4843 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004844 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845 break;
4846 case ADDR_BUFFERS:
4847 if (eap->line1 < firstbuf->b_fnum
4848 || eap->line2 > lastbuf->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_LOADED_BUFFERS:
4852 buf = firstbuf;
4853 while (buf->b_ml.ml_mfp == NULL)
4854 {
4855 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004857 buf = buf->b_next;
4858 }
4859 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004860 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861 buf = lastbuf;
4862 while (buf->b_ml.ml_mfp == NULL)
4863 {
4864 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004865 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004866 buf = buf->b_prev;
4867 }
4868 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004869 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004870 break;
4871 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004872 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004873 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004874 break;
4875 case ADDR_TABS:
4876 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004877 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004878 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004879 case ADDR_TABS_RELATIVE:
4880 /* Do nothing */
4881 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004882#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004883 case ADDR_QUICKFIX:
4884 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004885 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004886 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004887#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004888 }
4889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return NULL;
4891}
4892
4893/*
4894 * Correct the range for zero line number, if required.
4895 */
4896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4900 {
4901 if (eap->line1 == 0)
4902 eap->line1 = 1;
4903 if (eap->line2 == 0)
4904 eap->line2 = 1;
4905 }
4906}
4907
Bram Moolenaar748bf032005-02-02 23:04:36 +00004908#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004909/*
4910 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4911 * pattern. Otherwise return eap->arg.
4912 */
4913 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004914skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004915{
4916 char_u *p = eap->arg;
4917
Bram Moolenaara37420f2006-02-04 22:37:47 +00004918 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4919 || eap->cmdidx == CMD_vimgrepadd
4920 || eap->cmdidx == CMD_lvimgrepadd
4921 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004922 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004923 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004924 if (p == NULL)
4925 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004926 }
4927 return p;
4928}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004929
4930/*
4931 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4932 * in the command line, so that things like % get expanded.
4933 */
4934 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004936{
4937 char_u *new_cmdline;
4938 char_u *program;
4939 char_u *pos;
4940 char_u *ptr;
4941 int len;
4942 int i;
4943
4944 /*
4945 * Don't do it when ":vimgrep" is used for ":grep".
4946 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004947 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4948 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4949 || eap->cmdidx == CMD_grepadd
4950 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004951 && !grep_internal(eap->cmdidx))
4952 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004953 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4954 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004955 {
4956 if (*curbuf->b_p_gp == NUL)
4957 program = p_gp;
4958 else
4959 program = curbuf->b_p_gp;
4960 }
4961 else
4962 {
4963 if (*curbuf->b_p_mp == NUL)
4964 program = p_mp;
4965 else
4966 program = curbuf->b_p_mp;
4967 }
4968
4969 p = skipwhite(p);
4970
4971 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4972 {
4973 /* replace $* by given arguments */
4974 i = 1;
4975 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4976 ++i;
4977 len = (int)STRLEN(p);
4978 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4979 if (new_cmdline == NULL)
4980 return NULL; /* out of memory */
4981 ptr = new_cmdline;
4982 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4983 {
4984 i = (int)(pos - program);
4985 STRNCPY(ptr, program, i);
4986 STRCPY(ptr += i, p);
4987 ptr += len;
4988 program = pos + 2;
4989 }
4990 STRCPY(ptr, program);
4991 }
4992 else
4993 {
4994 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4995 if (new_cmdline == NULL)
4996 return NULL; /* out of memory */
4997 STRCPY(new_cmdline, program);
4998 STRCAT(new_cmdline, " ");
4999 STRCAT(new_cmdline, p);
5000 }
5001 msg_make(p);
5002
5003 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
5004 vim_free(*cmdlinep);
5005 *cmdlinep = new_cmdline;
5006 p = new_cmdline;
5007 }
5008 return p;
5009}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005010#endif
5011
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012/*
5013 * Expand file name in Ex command argument.
5014 * Return FAIL for failure, OK otherwise.
5015 */
5016 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005017expand_filename(
5018 exarg_T *eap,
5019 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005020 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
5022 int has_wildcards; /* need to expand wildcards */
5023 char_u *repl;
5024 int srclen;
5025 char_u *p;
5026 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005027 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
Bram Moolenaar748bf032005-02-02 23:04:36 +00005029#ifdef FEAT_QUICKFIX
5030 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5031 p = skip_grep_pat(eap);
5032#else
5033 p = eap->arg;
5034#endif
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 /*
5037 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5038 * the file name contains a wildcard it should not cause expanding.
5039 * (it will be expanded anyway if there is a wildcard before replacing).
5040 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005041 has_wildcards = mch_has_wildcard(p);
5042 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005044#ifdef FEAT_EVAL
5045 /* Skip over `=expr`, wildcards in it are not expanded. */
5046 if (p[0] == '`' && p[1] == '=')
5047 {
5048 p += 2;
5049 (void)skip_expr(&p);
5050 if (*p == '`')
5051 ++p;
5052 continue;
5053 }
5054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 /*
5056 * Quick check if this cannot be the start of a special string.
5057 * Also removes backslash before '%', '#' and '<'.
5058 */
5059 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5060 {
5061 ++p;
5062 continue;
5063 }
5064
5065 /*
5066 * Try to find a match at this position.
5067 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005068 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5069 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (*errormsgp != NULL) /* error detected */
5071 return FAIL;
5072 if (repl == NULL) /* no match found */
5073 {
5074 p += srclen;
5075 continue;
5076 }
5077
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005078 /* Wildcards won't be expanded below, the replacement is taken
5079 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5080 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5081 {
5082 char_u *l = repl;
5083
5084 repl = expand_env_save(repl);
5085 vim_free(l);
5086 }
5087
Bram Moolenaar63b92542007-03-27 14:57:09 +00005088 /* Need to escape white space et al. with a backslash.
5089 * Don't do this for:
5090 * - replacement that already has been escaped: "##"
5091 * - shell commands (may have to use quotes instead).
5092 * - non-unix systems when there is a single argument (spaces don't
5093 * separate arguments then).
5094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005096 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 && eap->cmdidx != CMD_grep
5099 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005100 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005101 && eap->cmdidx != CMD_lgrep
5102 && eap->cmdidx != CMD_lgrepadd
5103 && eap->cmdidx != CMD_lmake
5104 && eap->cmdidx != CMD_make
5105 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106#ifndef UNIX
5107 && !(eap->argt & NOSPC)
5108#endif
5109 )
5110 {
5111 char_u *l;
5112#ifdef BACKSLASH_IN_FILENAME
5113 /* Don't escape a backslash here, because rem_backslash() doesn't
5114 * remove it later. */
5115 static char_u *nobslash = (char_u *)" \t\"|";
5116# define ESCAPE_CHARS nobslash
5117#else
5118# define ESCAPE_CHARS escape_chars
5119#endif
5120
5121 for (l = repl; *l; ++l)
5122 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5123 {
5124 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5125 if (l != NULL)
5126 {
5127 vim_free(repl);
5128 repl = l;
5129 }
5130 break;
5131 }
5132 }
5133
5134 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005135 if ((eap->usefilter || eap->cmdidx == CMD_bang
5136 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005137 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
5139 char_u *l;
5140
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005141 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (l != NULL)
5143 {
5144 vim_free(repl);
5145 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 }
5148
5149 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5150 vim_free(repl);
5151 if (p == NULL)
5152 return FAIL;
5153 }
5154
5155 /*
5156 * One file argument: Expand wildcards.
5157 * Don't do this with ":r !command" or ":w !command".
5158 */
5159 if ((eap->argt & NOSPC) && !eap->usefilter)
5160 {
5161 /*
5162 * May do this twice:
5163 * 1. Replace environment variables.
5164 * 2. Replace any other wildcards, remove backslashes.
5165 */
5166 for (n = 1; n <= 2; ++n)
5167 {
5168 if (n == 2)
5169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 /*
5171 * Halve the number of backslashes (this is Vi compatible).
5172 * For Unix and OS/2, when wildcards are expanded, this is
5173 * done by ExpandOne() below.
5174 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005175#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if (!has_wildcards)
5177#endif
5178 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180
5181 if (has_wildcards)
5182 {
5183 if (n == 1)
5184 {
5185 /*
5186 * First loop: May expand environment variables. This
5187 * can be done much faster with expand_env() than with
5188 * something else (e.g., calling a shell).
5189 * After expanding environment variables, check again
5190 * if there are still wildcards present.
5191 */
5192 if (vim_strchr(eap->arg, '$') != NULL
5193 || vim_strchr(eap->arg, '~') != NULL)
5194 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005195 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005196 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 has_wildcards = mch_has_wildcard(NameBuff);
5198 p = NameBuff;
5199 }
5200 else
5201 p = NULL;
5202 }
5203 else /* n == 2 */
5204 {
5205 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005206 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
5208 ExpandInit(&xpc);
5209 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005210 if (p_wic)
5211 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005213 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (p == NULL)
5215 return FAIL;
5216 }
5217 if (p != NULL)
5218 {
5219 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5220 p, cmdlinep);
5221 if (n == 2) /* p came from ExpandOne() */
5222 vim_free(p);
5223 }
5224 }
5225 }
5226 }
5227 return OK;
5228}
5229
5230/*
5231 * Replace part of the command line, keeping eap->cmd, eap->arg and
5232 * eap->nextcmd correct.
5233 * "src" points to the part that is to be replaced, of length "srclen".
5234 * "repl" is the replacement string.
5235 * Returns a pointer to the character after the replaced string.
5236 * Returns NULL for failure.
5237 */
5238 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005239repl_cmdline(
5240 exarg_T *eap,
5241 char_u *src,
5242 int srclen,
5243 char_u *repl,
5244 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 int len;
5247 int i;
5248 char_u *new_cmdline;
5249
5250 /*
5251 * The new command line is build in new_cmdline[].
5252 * First allocate it.
5253 * Careful: a "+cmd" argument may have been NUL terminated.
5254 */
5255 len = (int)STRLEN(repl);
5256 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005257 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5259 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5260 return NULL; /* out of memory! */
5261
5262 /*
5263 * Copy the stuff before the expanded part.
5264 * Copy the expanded stuff.
5265 * Copy what came after the expanded part.
5266 * Copy the next commands, if there are any.
5267 */
5268 i = (int)(src - *cmdlinep); /* length of part before match */
5269 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 mch_memmove(new_cmdline + i, repl, (size_t)len);
5272 i += len; /* remember the end of the string */
5273 STRCPY(new_cmdline + i, src + srclen);
5274 src = new_cmdline + i; /* remember where to continue */
5275
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005276 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 i = (int)STRLEN(new_cmdline) + 1;
5279 STRCPY(new_cmdline + i, eap->nextcmd);
5280 eap->nextcmd = new_cmdline + i;
5281 }
5282 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5283 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5284 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5285 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5286 vim_free(*cmdlinep);
5287 *cmdlinep = new_cmdline;
5288
5289 return src;
5290}
5291
5292/*
5293 * Check for '|' to separate commands and '"' to start comments.
5294 */
5295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005296separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
5298 char_u *p;
5299
Bram Moolenaar86b68352004-12-27 21:59:20 +00005300#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005301 p = skip_grep_pat(eap);
5302#else
5303 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005304#endif
5305
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005306 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
5308 if (*p == Ctrl_V)
5309 {
5310 if (eap->argt & (USECTRLV | XFILE))
5311 ++p; /* skip CTRL-V and next char */
5312 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005313 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005314 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (*p == NUL) /* stop at NUL after CTRL-V */
5316 break;
5317 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005318
5319#ifdef FEAT_EVAL
5320 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005321 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005322 {
5323 p += 2;
5324 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005325 }
5326#endif
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 /* Check for '"': start of comment or '|': next command */
5329 /* :@" and :*" do not start a comment!
5330 * :redir @" doesn't either. */
5331 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5332 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5333 || p != eap->arg)
5334 && (eap->cmdidx != CMD_redir
5335 || p != eap->arg + 1 || p[-1] != '@'))
5336 || *p == '|' || *p == '\n')
5337 {
5338 /*
5339 * We remove the '\' before the '|', unless USECTRLV is used
5340 * AND 'b' is present in 'cpoptions'.
5341 */
5342 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5343 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5344 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005345 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 --p;
5347 }
5348 else
5349 {
5350 eap->nextcmd = check_nextcmd(p);
5351 *p = NUL;
5352 break;
5353 }
5354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5358 del_trailing_spaces(eap->arg);
5359}
5360
5361/*
5362 * get + command from ex argument
5363 */
5364 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005365getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 char_u *arg = *argp;
5368 char_u *command = NULL;
5369
5370 if (*arg == '+') /* +[command] */
5371 {
5372 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005373 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 command = dollar_command;
5375 else
5376 {
5377 command = arg;
5378 arg = skip_cmd_arg(command, TRUE);
5379 if (*arg != NUL)
5380 *arg++ = NUL; /* terminate command with NUL */
5381 }
5382
5383 arg = skipwhite(arg); /* skip over spaces */
5384 *argp = arg;
5385 }
5386 return command;
5387}
5388
5389/*
5390 * Find end of "+command" argument. Skip over "\ " and "\\".
5391 */
5392 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005393skip_cmd_arg(
5394 char_u *p,
5395 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
5397 while (*p && !vim_isspace(*p))
5398 {
5399 if (*p == '\\' && p[1] != NUL)
5400 {
5401 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005402 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 else
5404 ++p;
5405 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005406 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408 return p;
5409}
5410
Bram Moolenaar86676c92018-04-05 18:56:48 +02005411#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005412 int
5413get_bad_opt(char_u *p, exarg_T *eap)
5414{
5415 if (STRICMP(p, "keep") == 0)
5416 eap->bad_char = BAD_KEEP;
5417 else if (STRICMP(p, "drop") == 0)
5418 eap->bad_char = BAD_DROP;
5419 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5420 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005421 else
5422 return FAIL;
5423 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005424}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005425#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427/*
5428 * Get "++opt=arg" argument.
5429 * Return FAIL or OK.
5430 */
5431 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005432getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
5434 char_u *arg = eap->arg + 2;
5435 int *pp = NULL;
5436#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005437 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 char_u *p;
5439#endif
5440
5441 /* ":edit ++[no]bin[ary] file" */
5442 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5443 {
5444 if (*arg == 'n')
5445 {
5446 arg += 2;
5447 eap->force_bin = FORCE_NOBIN;
5448 }
5449 else
5450 eap->force_bin = FORCE_BIN;
5451 if (!checkforcmd(&arg, "binary", 3))
5452 return FAIL;
5453 eap->arg = skipwhite(arg);
5454 return OK;
5455 }
5456
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005457 /* ":read ++edit file" */
5458 if (STRNCMP(arg, "edit", 4) == 0)
5459 {
5460 eap->read_edit = TRUE;
5461 eap->arg = skipwhite(arg + 4);
5462 return OK;
5463 }
5464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 if (STRNCMP(arg, "ff", 2) == 0)
5466 {
5467 arg += 2;
5468 pp = &eap->force_ff;
5469 }
5470 else if (STRNCMP(arg, "fileformat", 10) == 0)
5471 {
5472 arg += 10;
5473 pp = &eap->force_ff;
5474 }
5475#ifdef FEAT_MBYTE
5476 else if (STRNCMP(arg, "enc", 3) == 0)
5477 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005478 if (STRNCMP(arg, "encoding", 8) == 0)
5479 arg += 8;
5480 else
5481 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 pp = &eap->force_enc;
5483 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005484 else if (STRNCMP(arg, "bad", 3) == 0)
5485 {
5486 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005487 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489#endif
5490
5491 if (pp == NULL || *arg != '=')
5492 return FAIL;
5493
5494 ++arg;
5495 *pp = (int)(arg - eap->cmd);
5496 arg = skip_cmd_arg(arg, FALSE);
5497 eap->arg = skipwhite(arg);
5498 *arg = NUL;
5499
5500#ifdef FEAT_MBYTE
5501 if (pp == &eap->force_ff)
5502 {
5503#endif
5504 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5505 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005506 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#ifdef FEAT_MBYTE
5508 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005509 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 /* Make 'fileencoding' lower case. */
5512 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5513 *p = TOLOWER_ASC(*p);
5514 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005515 else
5516 {
5517 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5518 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005519 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005520 return FAIL;
5521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522#endif
5523
5524 return OK;
5525}
5526
5527/*
5528 * ":abbreviate" and friends.
5529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 do_exmap(eap, TRUE); /* almost the same as mapping */
5534}
5535
5536/*
5537 * ":map" and friends.
5538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 /*
5543 * If we are sourcing .exrc or .vimrc in current directory we
5544 * print the mappings for security reasons.
5545 */
5546 if (secure)
5547 {
5548 secure = 2;
5549 msg_outtrans(eap->cmd);
5550 msg_putchar('\n');
5551 }
5552 do_exmap(eap, FALSE);
5553}
5554
5555/*
5556 * ":unmap" and friends.
5557 */
5558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 do_exmap(eap, FALSE);
5562}
5563
5564/*
5565 * ":mapclear" and friends.
5566 */
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5571}
5572
5573/*
5574 * ":abclear" and friends.
5575 */
5576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5580}
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005583ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
5585 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005586 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 * directory for security reasons.
5588 */
5589 if (secure)
5590 {
5591 secure = 2;
5592 eap->errmsg = e_curdir;
5593 }
5594 else if (eap->cmdidx == CMD_autocmd)
5595 do_autocmd(eap->arg, eap->forceit);
5596 else
5597 do_augroup(eap->arg, eap->forceit);
5598}
5599
5600/*
5601 * ":doautocmd": Apply the automatic commands to the current buffer.
5602 */
5603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005604ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005606 char_u *arg = eap->arg;
5607 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005608 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005609
Bram Moolenaar1610d052016-06-09 22:53:01 +02005610 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5611 /* Only when there is no <nomodeline>. */
5612 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005613 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616/*
5617 * :[N]bunload[!] [N] [bufname] unload buffer
5618 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5619 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5620 */
5621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 eap->errmsg = do_bufdel(
5625 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5626 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5627 : DOBUF_UNLOAD, eap->arg,
5628 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5629}
5630
5631/*
5632 * :[N]buffer [N] to buffer N
5633 * :[N]sbuffer [N] to buffer N
5634 */
5635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 if (*eap->arg)
5639 eap->errmsg = e_trailing;
5640 else
5641 {
5642 if (eap->addr_count == 0) /* default is current buffer */
5643 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5644 else
5645 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005646 if (eap->do_ecmd_cmd != NULL)
5647 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649}
5650
5651/*
5652 * :[N]bmodified [N] to next mod. buffer
5653 * :[N]sbmodified [N] to next mod. buffer
5654 */
5655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005659 if (eap->do_ecmd_cmd != NULL)
5660 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661}
5662
5663/*
5664 * :[N]bnext [N] to next buffer
5665 * :[N]sbnext [N] split and to next buffer
5666 */
5667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005671 if (eap->do_ecmd_cmd != NULL)
5672 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673}
5674
5675/*
5676 * :[N]bNext [N] to previous buffer
5677 * :[N]bprevious [N] to previous buffer
5678 * :[N]sbNext [N] split and to previous buffer
5679 * :[N]sbprevious [N] split and to previous buffer
5680 */
5681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005682ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005685 if (eap->do_ecmd_cmd != NULL)
5686 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687}
5688
5689/*
5690 * :brewind to first buffer
5691 * :bfirst to first buffer
5692 * :sbrewind split and to first buffer
5693 * :sbfirst split and to first buffer
5694 */
5695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005699 if (eap->do_ecmd_cmd != NULL)
5700 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701}
5702
5703/*
5704 * :blast to last buffer
5705 * :sblast split and to last buffer
5706 */
5707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005708ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005711 if (eap->do_ecmd_cmd != NULL)
5712 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005716ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 return (c == NUL || c == '|' || c == '"' || c == '\n');
5719}
5720
5721#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5722 || defined(PROTO)
5723/*
5724 * Return the next command, after the first '|' or '\n'.
5725 * Return NULL if not found.
5726 */
5727 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 while (*p != '|' && *p != '\n')
5731 {
5732 if (*p == NUL)
5733 return NULL;
5734 ++p;
5735 }
5736 return (p + 1);
5737}
5738#endif
5739
5740/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005741 * Check if *p is a separator between Ex commands, skipping over white space.
5742 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 */
5744 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005745check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005747 char_u *s = skipwhite(p);
5748
5749 if (*s == '|' || *s == '\n')
5750 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 else
5752 return NULL;
5753}
5754
5755/*
5756 * - if there are more files to edit
5757 * - and this is the last window
5758 * - and forceit not used
5759 * - and not repeated twice on a row
5760 * return FAIL and give error message if 'message' TRUE
5761 * return OK otherwise
5762 */
5763 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764check_more(
5765 int message, /* when FALSE check only, no messages */
5766 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5769
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005770 if (!forceit && only_one_window()
5771 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
5773 if (message)
5774 {
5775#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5776 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5777 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005778 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005780 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5781 NGETTEXT("%d more file to edit. Quit anyway?",
5782 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5784 return OK;
5785 return FAIL;
5786 }
5787#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005788 semsg(NGETTEXT("E173: %d more file to edit",
5789 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 quitmore = 2; /* next try to quit is allowed */
5791 }
5792 return FAIL;
5793 }
5794 return OK;
5795}
5796
5797#ifdef FEAT_CMDL_COMPL
5798/*
5799 * Function given to ExpandGeneric() to obtain the list of command names.
5800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005802get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 if (idx >= (int)CMD_SIZE)
5805# ifdef FEAT_USR_CMDS
5806 return get_user_command_name(idx);
5807# else
5808 return NULL;
5809# endif
5810 return cmdnames[idx].cmd_name;
5811}
5812#endif
5813
5814#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005816uc_add_command(
5817 char_u *name,
5818 size_t name_len,
5819 char_u *rep,
5820 long argt,
5821 long def,
5822 int flags,
5823 int compl,
5824 char_u *compl_arg,
5825 int addr_type,
5826 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 ucmd_T *cmd = NULL;
5829 char_u *p;
5830 int i;
5831 int cmp = 1;
5832 char_u *rep_buf = NULL;
5833 garray_T *gap;
5834
Bram Moolenaar9c102382006-05-03 21:26:49 +00005835 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 if (rep_buf == NULL)
5837 {
5838 /* Can't replace termcodes - try using the string as is */
5839 rep_buf = vim_strsave(rep);
5840
5841 /* Give up if out of memory */
5842 if (rep_buf == NULL)
5843 return FAIL;
5844 }
5845
5846 /* get address of growarray: global or in curbuf */
5847 if (flags & UC_BUFFER)
5848 {
5849 gap = &curbuf->b_ucmds;
5850 if (gap->ga_itemsize == 0)
5851 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5852 }
5853 else
5854 gap = &ucmds;
5855
5856 /* Search for the command in the already defined commands. */
5857 for (i = 0; i < gap->ga_len; ++i)
5858 {
5859 size_t len;
5860
5861 cmd = USER_CMD_GA(gap, i);
5862 len = STRLEN(cmd->uc_name);
5863 cmp = STRNCMP(name, cmd->uc_name, name_len);
5864 if (cmp == 0)
5865 {
5866 if (name_len < len)
5867 cmp = -1;
5868 else if (name_len > len)
5869 cmp = 1;
5870 }
5871
5872 if (cmp == 0)
5873 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005874 // Command can be replaced with "command!" and when sourcing the
5875 // same script again, but only once.
5876 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5877 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005879 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005880 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 goto fail;
5882 }
5883
Bram Moolenaard23a8232018-02-10 18:45:26 +01005884 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005885#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005886 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 break;
5889 }
5890
5891 /* Stop as soon as we pass the name to add */
5892 if (cmp < 0)
5893 break;
5894 }
5895
5896 /* Extend the array unless we're replacing an existing command */
5897 if (cmp != 0)
5898 {
5899 if (ga_grow(gap, 1) != OK)
5900 goto fail;
5901 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5902 goto fail;
5903
5904 cmd = USER_CMD_GA(gap, i);
5905 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5906
5907 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
5909 cmd->uc_name = p;
5910 }
5911
5912 cmd->uc_rep = rep_buf;
5913 cmd->uc_argt = argt;
5914 cmd->uc_def = def;
5915 cmd->uc_compl = compl;
5916#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005917 cmd->uc_script_ctx = current_sctx;
5918 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919# ifdef FEAT_CMDL_COMPL
5920 cmd->uc_compl_arg = compl_arg;
5921# endif
5922#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005923 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
5925 return OK;
5926
5927fail:
5928 vim_free(rep_buf);
5929#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5930 vim_free(compl_arg);
5931#endif
5932 return FAIL;
5933}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005936#if defined(FEAT_USR_CMDS)
5937static struct
5938{
5939 int expand;
5940 char *name;
5941} addr_type_complete[] =
5942{
5943 {ADDR_ARGUMENTS, "arguments"},
5944 {ADDR_LINES, "lines"},
5945 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5946 {ADDR_TABS, "tabs"},
5947 {ADDR_BUFFERS, "buffers"},
5948 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005949 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005950 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005951 {-1, NULL}
5952};
5953#endif
5954
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005955#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956/*
5957 * List of names for completion for ":command" with the EXPAND_ flag.
5958 * Must be alphabetical for completion.
5959 */
5960static struct
5961{
5962 int expand;
5963 char *name;
5964} command_complete[] =
5965{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005966 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005968 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005970 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005972 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005973#if defined(FEAT_CSCOPE)
5974 {EXPAND_CSCOPE, "cscope"},
5975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5977 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005978 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#endif
5980 {EXPAND_DIRECTORIES, "dir"},
5981 {EXPAND_ENV_VARS, "environment"},
5982 {EXPAND_EVENTS, "event"},
5983 {EXPAND_EXPRESSION, "expression"},
5984 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005985 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005986 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {EXPAND_FUNCTIONS, "function"},
5988 {EXPAND_HELP, "help"},
5989 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005990#if defined(FEAT_CMDHIST)
5991 {EXPAND_HISTORY, "history"},
5992#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005993#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005994 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005995 {EXPAND_LOCALES, "locale"},
5996#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005997 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 {EXPAND_MAPPINGS, "mapping"},
5999 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02006000 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006001 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006002#if defined(FEAT_PROFILE)
6003 {EXPAND_SYNTIME, "syntime"},
6004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006006 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006007 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006008#if defined(FEAT_SIGNS)
6009 {EXPAND_SIGN, "sign"},
6010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 {EXPAND_TAGS, "tag"},
6012 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006013 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {EXPAND_USER_VARS, "var"},
6015 {0, NULL}
6016};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006019#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006021uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 int i, j;
6024 int found = FALSE;
6025 ucmd_T *cmd;
6026 int len;
6027 long a;
6028 garray_T *gap;
6029
6030 gap = &curbuf->b_ucmds;
6031 for (;;)
6032 {
6033 for (i = 0; i < gap->ga_len; ++i)
6034 {
6035 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006036 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
Bram Moolenaard29459b2016-08-26 22:29:11 +02006038 /* Skip commands which don't match the requested prefix and
6039 * commands filtered out. */
6040 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6041 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 continue;
6043
6044 /* Put out the title first time */
6045 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006046 msg_puts_title(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 found = TRUE;
6048 msg_putchar('\n');
6049 if (got_int)
6050 break;
6051
6052 /* Special cases */
6053 msg_putchar(a & BANG ? '!' : ' ');
6054 msg_putchar(a & REGSTR ? '"' : ' ');
6055 msg_putchar(gap != &ucmds ? 'b' : ' ');
6056 msg_putchar(' ');
6057
Bram Moolenaar8820b482017-03-16 17:23:31 +01006058 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 len = (int)STRLEN(cmd->uc_name) + 4;
6060
6061 do {
6062 msg_putchar(' ');
6063 ++len;
6064 } while (len < 16);
6065
6066 len = 0;
6067
6068 /* Arguments */
6069 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6070 {
6071 case 0: IObuff[len++] = '0'; break;
6072 case (EXTRA): IObuff[len++] = '*'; break;
6073 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6074 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6075 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6076 }
6077
6078 do {
6079 IObuff[len++] = ' ';
6080 } while (len < 5);
6081
6082 /* Range */
6083 if (a & (RANGE|COUNT))
6084 {
6085 if (a & COUNT)
6086 {
6087 /* -count=N */
6088 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6089 len += (int)STRLEN(IObuff + len);
6090 }
6091 else if (a & DFLALL)
6092 IObuff[len++] = '%';
6093 else if (cmd->uc_def >= 0)
6094 {
6095 /* -range=N */
6096 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6097 len += (int)STRLEN(IObuff + len);
6098 }
6099 else
6100 IObuff[len++] = '.';
6101 }
6102
6103 do {
6104 IObuff[len++] = ' ';
6105 } while (len < 11);
6106
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006107 /* Address Type */
6108 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6109 if (addr_type_complete[j].expand != ADDR_LINES
6110 && addr_type_complete[j].expand == cmd->uc_addr_type)
6111 {
6112 STRCPY(IObuff + len, addr_type_complete[j].name);
6113 len += (int)STRLEN(IObuff + len);
6114 break;
6115 }
6116
6117 do {
6118 IObuff[len++] = ' ';
6119 } while (len < 21);
6120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 /* Completion */
6122 for (j = 0; command_complete[j].expand != 0; ++j)
6123 if (command_complete[j].expand == cmd->uc_compl)
6124 {
6125 STRCPY(IObuff + len, command_complete[j].name);
6126 len += (int)STRLEN(IObuff + len);
6127 break;
6128 }
6129
6130 do {
6131 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006132 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 IObuff[len] = '\0';
6135 msg_outtrans(IObuff);
6136
6137 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006138#ifdef FEAT_EVAL
6139 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006140 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 out_flush();
6143 ui_breakcheck();
6144 if (got_int)
6145 break;
6146 }
6147 if (gap == &ucmds || i < gap->ga_len)
6148 break;
6149 gap = &ucmds;
6150 }
6151
6152 if (!found)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006153 msg(_("No user-defined commands found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154}
6155
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006156 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006157uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6160 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6161 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6162 0xb9, 0x7f, 0};
6163 int i;
6164
6165 for (i = 0; fcmd[i]; ++i)
6166 IObuff[i] = fcmd[i] - 0x40;
6167 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006168 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169}
6170
6171 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006172uc_scan_attr(
6173 char_u *attr,
6174 size_t len,
6175 long *argt,
6176 long *def,
6177 int *flags,
6178 int *compl,
6179 char_u **compl_arg,
6180 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 char_u *p;
6183
6184 if (len == 0)
6185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006186 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 return FAIL;
6188 }
6189
6190 /* First, try the simple attributes (no arguments) */
6191 if (STRNICMP(attr, "bang", len) == 0)
6192 *argt |= BANG;
6193 else if (STRNICMP(attr, "buffer", len) == 0)
6194 *flags |= UC_BUFFER;
6195 else if (STRNICMP(attr, "register", len) == 0)
6196 *argt |= REGSTR;
6197 else if (STRNICMP(attr, "bar", len) == 0)
6198 *argt |= TRLBAR;
6199 else
6200 {
6201 int i;
6202 char_u *val = NULL;
6203 size_t vallen = 0;
6204 size_t attrlen = len;
6205
6206 /* Look for the attribute name - which is the part before any '=' */
6207 for (i = 0; i < (int)len; ++i)
6208 {
6209 if (attr[i] == '=')
6210 {
6211 val = &attr[i + 1];
6212 vallen = len - i - 1;
6213 attrlen = i;
6214 break;
6215 }
6216 }
6217
6218 if (STRNICMP(attr, "nargs", attrlen) == 0)
6219 {
6220 if (vallen == 1)
6221 {
6222 if (*val == '0')
6223 /* Do nothing - this is the default */;
6224 else if (*val == '1')
6225 *argt |= (EXTRA | NOSPC | NEEDARG);
6226 else if (*val == '*')
6227 *argt |= EXTRA;
6228 else if (*val == '?')
6229 *argt |= (EXTRA | NOSPC);
6230 else if (*val == '+')
6231 *argt |= (EXTRA | NEEDARG);
6232 else
6233 goto wrong_nargs;
6234 }
6235 else
6236 {
6237wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006238 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 return FAIL;
6240 }
6241 }
6242 else if (STRNICMP(attr, "range", attrlen) == 0)
6243 {
6244 *argt |= RANGE;
6245 if (vallen == 1 && *val == '%')
6246 *argt |= DFLALL;
6247 else if (val != NULL)
6248 {
6249 p = val;
6250 if (*def >= 0)
6251 {
6252two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006253 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 return FAIL;
6255 }
6256
6257 *def = getdigits(&p);
6258 *argt |= (ZEROR | NOTADR);
6259
6260 if (p != val + vallen || vallen == 0)
6261 {
6262invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006263 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 return FAIL;
6265 }
6266 }
6267 }
6268 else if (STRNICMP(attr, "count", attrlen) == 0)
6269 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006270 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
6272 if (val != NULL)
6273 {
6274 p = val;
6275 if (*def >= 0)
6276 goto two_count;
6277
6278 *def = getdigits(&p);
6279
6280 if (p != val + vallen)
6281 goto invalid_count;
6282 }
6283
6284 if (*def < 0)
6285 *def = 0;
6286 }
6287 else if (STRNICMP(attr, "complete", attrlen) == 0)
6288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (val == NULL)
6290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006291 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 return FAIL;
6293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006295 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6296 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006299 else if (STRNICMP(attr, "addr", attrlen) == 0)
6300 {
6301 *argt |= RANGE;
6302 if (val == NULL)
6303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006304 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006305 return FAIL;
6306 }
6307 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6308 == FAIL)
6309 return FAIL;
6310 if (addr_type_arg != ADDR_LINES)
6311 *argt |= (ZEROR | NOTADR) ;
6312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 else
6314 {
6315 char_u ch = attr[len];
6316 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006317 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 attr[len] = ch;
6319 return FAIL;
6320 }
6321 }
6322
6323 return OK;
6324}
6325
Bram Moolenaara850a712009-01-28 14:42:59 +00006326/*
6327 * ":command ..."
6328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006330ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 char_u *name;
6333 char_u *end;
6334 char_u *p;
6335 long argt = 0;
6336 long def = -1;
6337 int flags = 0;
6338 int compl = EXPAND_NOTHING;
6339 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006340 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006342 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344 p = eap->arg;
6345
6346 /* Check for attributes */
6347 while (*p == '-')
6348 {
6349 ++p;
6350 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006351 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6352 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 == FAIL)
6354 return;
6355 p = skipwhite(end);
6356 }
6357
6358 /* Get the name (if any) and skip to the following argument */
6359 name = p;
6360 if (ASCII_ISALPHA(*p))
6361 while (ASCII_ISALNUM(*p))
6362 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006363 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006365 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 return;
6367 }
6368 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006369 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370
6371 /* If there is nothing after the name, and no attributes were specified,
6372 * we are listing commands
6373 */
6374 p = skipwhite(end);
6375 if (!has_attr && ends_excmd(*p))
6376 {
6377 uc_list(name, end - name);
6378 }
6379 else if (!ASCII_ISUPPER(*name))
6380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006381 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return;
6383 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006384 else if ((name_len == 1 && *name == 'X')
6385 || (name_len <= 4
6386 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6387 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006388 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006389 return;
6390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 else
6392 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006393 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394}
6395
6396/*
6397 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * Clear all user commands, global and for current buffer.
6399 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006400 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006401ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 uc_clear(&ucmds);
6404 uc_clear(&curbuf->b_ucmds);
6405}
6406
6407/*
6408 * Clear all user commands for "gap".
6409 */
6410 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006411uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 int i;
6414 ucmd_T *cmd;
6415
6416 for (i = 0; i < gap->ga_len; ++i)
6417 {
6418 cmd = USER_CMD_GA(gap, i);
6419 vim_free(cmd->uc_name);
6420 vim_free(cmd->uc_rep);
6421# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6422 vim_free(cmd->uc_compl_arg);
6423# endif
6424 }
6425 ga_clear(gap);
6426}
6427
6428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006429ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 int i = 0;
6432 ucmd_T *cmd = NULL;
6433 int cmp = -1;
6434 garray_T *gap;
6435
6436 gap = &curbuf->b_ucmds;
6437 for (;;)
6438 {
6439 for (i = 0; i < gap->ga_len; ++i)
6440 {
6441 cmd = USER_CMD_GA(gap, i);
6442 cmp = STRCMP(eap->arg, cmd->uc_name);
6443 if (cmp <= 0)
6444 break;
6445 }
6446 if (gap == &ucmds || cmp == 0)
6447 break;
6448 gap = &ucmds;
6449 }
6450
6451 if (cmp != 0)
6452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006453 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 return;
6455 }
6456
6457 vim_free(cmd->uc_name);
6458 vim_free(cmd->uc_rep);
6459# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6460 vim_free(cmd->uc_compl_arg);
6461# endif
6462
6463 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 if (i < gap->ga_len)
6466 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6467}
6468
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006469/*
6470 * split and quote args for <f-args>
6471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006473uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 char_u *buf;
6476 char_u *p;
6477 char_u *q;
6478 int len;
6479
6480 /* Precalculate length */
6481 p = arg;
6482 len = 2; /* Initial and final quotes */
6483
6484 while (*p)
6485 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006486 if (p[0] == '\\' && p[1] == '\\')
6487 {
6488 len += 2;
6489 p += 2;
6490 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006491 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
6493 len += 1;
6494 p += 2;
6495 }
6496 else if (*p == '\\' || *p == '"')
6497 {
6498 len += 2;
6499 p += 1;
6500 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006501 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 p = skipwhite(p);
6504 if (*p == NUL)
6505 break;
6506 len += 3; /* "," */
6507 }
6508 else
6509 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006510#ifdef FEAT_MBYTE
6511 int charlen = (*mb_ptr2len)(p);
6512 len += charlen;
6513 p += charlen;
6514#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 ++len;
6516 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519 }
6520
6521 buf = alloc(len + 1);
6522 if (buf == NULL)
6523 {
6524 *lenp = 0;
6525 return buf;
6526 }
6527
6528 p = arg;
6529 q = buf;
6530 *q++ = '"';
6531 while (*p)
6532 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006533 if (p[0] == '\\' && p[1] == '\\')
6534 {
6535 *q++ = '\\';
6536 *q++ = '\\';
6537 p += 2;
6538 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006539 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
6541 *q++ = p[1];
6542 p += 2;
6543 }
6544 else if (*p == '\\' || *p == '"')
6545 {
6546 *q++ = '\\';
6547 *q++ = *p++;
6548 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006549 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
6551 p = skipwhite(p);
6552 if (*p == NUL)
6553 break;
6554 *q++ = '"';
6555 *q++ = ',';
6556 *q++ = '"';
6557 }
6558 else
6559 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006560 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
6562 }
6563 *q++ = '"';
6564 *q = 0;
6565
6566 *lenp = len;
6567 return buf;
6568}
6569
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006570 static size_t
6571add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6572{
6573 size_t result;
6574
6575 result = STRLEN(mod_str);
6576 if (*multi_mods)
6577 result += 1;
6578 if (buf != NULL)
6579 {
6580 if (*multi_mods)
6581 STRCAT(buf, " ");
6582 STRCAT(buf, mod_str);
6583 }
6584
6585 *multi_mods = 1;
6586
6587 return result;
6588}
6589
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590/*
6591 * Check for a <> code in a user command.
6592 * "code" points to the '<'. "len" the length of the <> (inclusive).
6593 * "buf" is where the result is to be added.
6594 * "split_buf" points to a buffer used for splitting, caller should free it.
6595 * "split_len" is the length of what "split_buf" contains.
6596 * Returns the length of the replacement, which has been added to "buf".
6597 * Returns -1 if there was no match, and only the "<" has been copied.
6598 */
6599 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006600uc_check_code(
6601 char_u *code,
6602 size_t len,
6603 char_u *buf,
6604 ucmd_T *cmd, /* the user command we're expanding */
6605 exarg_T *eap, /* ex arguments */
6606 char_u **split_buf,
6607 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 size_t result = 0;
6610 char_u *p = code + 1;
6611 size_t l = len - 2;
6612 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006613 enum {
6614 ct_ARGS,
6615 ct_BANG,
6616 ct_COUNT,
6617 ct_LINE1,
6618 ct_LINE2,
6619 ct_RANGE,
6620 ct_MODS,
6621 ct_REGISTER,
6622 ct_LT,
6623 ct_NONE
6624 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
6626 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6627 {
6628 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6629 p += 2;
6630 l -= 2;
6631 }
6632
Bram Moolenaar371d5402006-03-20 21:47:49 +00006633 ++l;
6634 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006636 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006638 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006640 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006642 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006644 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006646 else if (STRNICMP(p, "range>", l) == 0)
6647 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006648 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006650 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006652 else if (STRNICMP(p, "mods>", l) == 0)
6653 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
6655 switch (type)
6656 {
6657 case ct_ARGS:
6658 /* Simple case first */
6659 if (*eap->arg == NUL)
6660 {
6661 if (quote == 1)
6662 {
6663 result = 2;
6664 if (buf != NULL)
6665 STRCPY(buf, "''");
6666 }
6667 else
6668 result = 0;
6669 break;
6670 }
6671
6672 /* When specified there is a single argument don't split it.
6673 * Works for ":Cmd %" when % is "a b c". */
6674 if ((eap->argt & NOSPC) && quote == 2)
6675 quote = 1;
6676
6677 switch (quote)
6678 {
6679 case 0: /* No quoting, no splitting */
6680 result = STRLEN(eap->arg);
6681 if (buf != NULL)
6682 STRCPY(buf, eap->arg);
6683 break;
6684 case 1: /* Quote, but don't split */
6685 result = STRLEN(eap->arg) + 2;
6686 for (p = eap->arg; *p; ++p)
6687 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006688#ifdef FEAT_MBYTE
6689 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6690 /* DBCS can contain \ in a trail byte, skip the
6691 * double-byte character. */
6692 ++p;
6693 else
6694#endif
6695 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 ++result;
6697 }
6698
6699 if (buf != NULL)
6700 {
6701 *buf++ = '"';
6702 for (p = eap->arg; *p; ++p)
6703 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006704#ifdef FEAT_MBYTE
6705 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6706 /* DBCS can contain \ in a trail byte, copy the
6707 * double-byte character to avoid escaping. */
6708 *buf++ = *p++;
6709 else
6710#endif
6711 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 *buf++ = '\\';
6713 *buf++ = *p;
6714 }
6715 *buf = '"';
6716 }
6717
6718 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006719 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 /* This is hard, so only do it once, and cache the result */
6721 if (*split_buf == NULL)
6722 *split_buf = uc_split_args(eap->arg, split_len);
6723
6724 result = *split_len;
6725 if (buf != NULL && result != 0)
6726 STRCPY(buf, *split_buf);
6727
6728 break;
6729 }
6730 break;
6731
6732 case ct_BANG:
6733 result = eap->forceit ? 1 : 0;
6734 if (quote)
6735 result += 2;
6736 if (buf != NULL)
6737 {
6738 if (quote)
6739 *buf++ = '"';
6740 if (eap->forceit)
6741 *buf++ = '!';
6742 if (quote)
6743 *buf = '"';
6744 }
6745 break;
6746
6747 case ct_LINE1:
6748 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006749 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 case ct_COUNT:
6751 {
6752 char num_buf[20];
6753 long num = (type == ct_LINE1) ? eap->line1 :
6754 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006755 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6757 size_t num_len;
6758
6759 sprintf(num_buf, "%ld", num);
6760 num_len = STRLEN(num_buf);
6761 result = num_len;
6762
6763 if (quote)
6764 result += 2;
6765
6766 if (buf != NULL)
6767 {
6768 if (quote)
6769 *buf++ = '"';
6770 STRCPY(buf, num_buf);
6771 buf += num_len;
6772 if (quote)
6773 *buf = '"';
6774 }
6775
6776 break;
6777 }
6778
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006779 case ct_MODS:
6780 {
6781 int multi_mods = 0;
6782 typedef struct {
6783 int *varp;
6784 char *name;
6785 } mod_entry_T;
6786 static mod_entry_T mod_entries[] = {
6787#ifdef FEAT_BROWSE_CMD
6788 {&cmdmod.browse, "browse"},
6789#endif
6790#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6791 {&cmdmod.confirm, "confirm"},
6792#endif
6793 {&cmdmod.hide, "hide"},
6794 {&cmdmod.keepalt, "keepalt"},
6795 {&cmdmod.keepjumps, "keepjumps"},
6796 {&cmdmod.keepmarks, "keepmarks"},
6797 {&cmdmod.keeppatterns, "keeppatterns"},
6798 {&cmdmod.lockmarks, "lockmarks"},
6799 {&cmdmod.noswapfile, "noswapfile"},
6800 {NULL, NULL}
6801 };
6802 int i;
6803
6804 result = quote ? 2 : 0;
6805 if (buf != NULL)
6806 {
6807 if (quote)
6808 *buf++ = '"';
6809 *buf = '\0';
6810 }
6811
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006812 /* :aboveleft and :leftabove */
6813 if (cmdmod.split & WSP_ABOVE)
6814 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6815 /* :belowright and :rightbelow */
6816 if (cmdmod.split & WSP_BELOW)
6817 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6818 /* :botright */
6819 if (cmdmod.split & WSP_BOT)
6820 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006821
6822 /* the modifiers that are simple flags */
6823 for (i = 0; mod_entries[i].varp != NULL; ++i)
6824 if (*mod_entries[i].varp)
6825 result += add_cmd_modifier(buf, mod_entries[i].name,
6826 &multi_mods);
6827
6828 /* TODO: How to support :noautocmd? */
6829#ifdef HAVE_SANDBOX
6830 /* TODO: How to support :sandbox?*/
6831#endif
6832 /* :silent */
6833 if (msg_silent > 0)
6834 result += add_cmd_modifier(buf,
6835 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006836 /* :tab */
6837 if (cmdmod.tab > 0)
6838 result += add_cmd_modifier(buf, "tab", &multi_mods);
6839 /* :topleft */
6840 if (cmdmod.split & WSP_TOP)
6841 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006842 /* TODO: How to support :unsilent?*/
6843 /* :verbose */
6844 if (p_verbose > 0)
6845 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006846 /* :vertical */
6847 if (cmdmod.split & WSP_VERT)
6848 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006849 if (quote && buf != NULL)
6850 {
6851 buf += result - 2;
6852 *buf = '"';
6853 }
6854 break;
6855 }
6856
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 case ct_REGISTER:
6858 result = eap->regname ? 1 : 0;
6859 if (quote)
6860 result += 2;
6861 if (buf != NULL)
6862 {
6863 if (quote)
6864 *buf++ = '\'';
6865 if (eap->regname)
6866 *buf++ = eap->regname;
6867 if (quote)
6868 *buf = '\'';
6869 }
6870 break;
6871
6872 case ct_LT:
6873 result = 1;
6874 if (buf != NULL)
6875 *buf = '<';
6876 break;
6877
6878 default:
6879 /* Not recognized: just copy the '<' and return -1. */
6880 result = (size_t)-1;
6881 if (buf != NULL)
6882 *buf = '<';
6883 break;
6884 }
6885
6886 return result;
6887}
6888
6889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006890do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 char_u *buf;
6893 char_u *p;
6894 char_u *q;
6895
6896 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006897 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006898 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 size_t len, totlen;
6900
6901 size_t split_len = 0;
6902 char_u *split_buf = NULL;
6903 ucmd_T *cmd;
6904#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006905 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906#endif
6907
6908 if (eap->cmdidx == CMD_USER)
6909 cmd = USER_CMD(eap->useridx);
6910 else
6911 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6912
6913 /*
6914 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006915 * First round: "buf" is NULL, compute length, allocate "buf".
6916 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 */
6918 buf = NULL;
6919 for (;;)
6920 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006921 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006922 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006924
6925 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006927 start = vim_strchr(p, '<');
6928 if (start != NULL)
6929 end = vim_strchr(start + 1, '>');
6930 if (buf != NULL)
6931 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006932 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6933 ;
6934 if (*ksp == K_SPECIAL
6935 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006936 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6937# ifdef FEAT_GUI
6938 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6939# endif
6940 ))
6941 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006942 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006943 * KS_SPECIAL KE_FILLER, like for mappings, but
6944 * do_cmdline() doesn't handle that, so convert it back.
6945 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6946 len = ksp - p;
6947 if (len > 0)
6948 {
6949 mch_memmove(q, p, len);
6950 q += len;
6951 }
6952 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6953 p = ksp + 3;
6954 continue;
6955 }
6956 }
6957
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006958 /* break if no <item> is found */
Bram Moolenaara850a712009-01-28 14:42:59 +00006959 if (start == NULL || end == NULL)
6960 break;
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 /* Include the '>' */
6963 ++end;
6964
6965 /* Take everything up to the '<' */
6966 len = start - p;
6967 if (buf == NULL)
6968 totlen += len;
6969 else
6970 {
6971 mch_memmove(q, p, len);
6972 q += len;
6973 }
6974
6975 len = uc_check_code(start, end - start, q, cmd, eap,
6976 &split_buf, &split_len);
6977 if (len == (size_t)-1)
6978 {
6979 /* no match, continue after '<' */
6980 p = start + 1;
6981 len = 1;
6982 }
6983 else
6984 p = end;
6985 if (buf == NULL)
6986 totlen += len;
6987 else
6988 q += len;
6989 }
6990 if (buf != NULL) /* second time here, finished */
6991 {
6992 STRCPY(q, p);
6993 break;
6994 }
6995
6996 totlen += STRLEN(p); /* Add on the trailing characters */
6997 buf = alloc((unsigned)(totlen + 1));
6998 if (buf == NULL)
6999 {
7000 vim_free(split_buf);
7001 return;
7002 }
7003 }
7004
7005#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007006 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007#endif
7008 (void)do_cmdline(buf, eap->getline, eap->cookie,
7009 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7010#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007011 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012#endif
7013 vim_free(buf);
7014 vim_free(split_buf);
7015}
7016
7017# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7018 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7022}
7023
7024/*
7025 * Function given to ExpandGeneric() to obtain the list of user command names.
7026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007028get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 if (idx < curbuf->b_ucmds.ga_len)
7031 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7032 idx -= curbuf->b_ucmds.ga_len;
7033 if (idx < ucmds.ga_len)
7034 return USER_CMD(idx)->uc_name;
7035 return NULL;
7036}
7037
7038/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007039 * Function given to ExpandGeneric() to obtain the list of user address type names.
7040 */
7041 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007042get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007043{
7044 return (char_u *)addr_type_complete[idx].name;
7045}
7046
7047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 * Function given to ExpandGeneric() to obtain the list of user command
7049 * attributes.
7050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007052get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007055 {"addr", "bang", "bar", "buffer", "complete",
7056 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007058 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 return NULL;
7060 return (char_u *)user_cmd_flags[idx];
7061}
7062
7063/*
7064 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007067get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7070
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007071 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 return NULL;
7073 return (char_u *)user_cmd_nargs[idx];
7074}
7075
7076/*
7077 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007080get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 return (char_u *)command_complete[idx].name;
7083}
7084# endif /* FEAT_CMDL_COMPL */
7085
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007086/*
7087 * Parse address type argument
7088 */
7089 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007090parse_addr_type_arg(
7091 char_u *value,
7092 int vallen,
7093 long *argt,
7094 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007095{
7096 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007097
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007098 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7099 {
7100 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7101 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7102 if (a && b)
7103 {
7104 *addr_type_arg = addr_type_complete[i].expand;
7105 break;
7106 }
7107 }
7108
7109 if (addr_type_complete[i].expand == -1)
7110 {
7111 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007112
Bram Moolenaar1c465442017-03-12 20:10:05 +01007113 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007114 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007115 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007116 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007117 return FAIL;
7118 }
7119
7120 if (*addr_type_arg != ADDR_LINES)
7121 *argt |= NOTADR;
7122
7123 return OK;
7124}
7125
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126#endif /* FEAT_USR_CMDS */
7127
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007128#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7129/*
7130 * Parse a completion argument "value[vallen]".
7131 * The detected completion goes in "*complp", argument type in "*argt".
7132 * When there is an argument, for function and user defined completion, it's
7133 * copied to allocated memory and stored in "*compl_arg".
7134 * Returns FAIL if something is wrong.
7135 */
7136 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007137parse_compl_arg(
7138 char_u *value,
7139 int vallen,
7140 int *complp,
7141 long *argt,
7142 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007143{
7144 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007145# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007146 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007147# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007148 int i;
7149 int valend = vallen;
7150
7151 /* Look for any argument part - which is the part after any ',' */
7152 for (i = 0; i < vallen; ++i)
7153 {
7154 if (value[i] == ',')
7155 {
7156 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007157# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007158 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007159# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007160 valend = i;
7161 break;
7162 }
7163 }
7164
7165 for (i = 0; command_complete[i].expand != 0; ++i)
7166 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007167 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007168 && STRNCMP(value, command_complete[i].name, valend) == 0)
7169 {
7170 *complp = command_complete[i].expand;
7171 if (command_complete[i].expand == EXPAND_BUFFERS)
7172 *argt |= BUFNAME;
7173 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7174 || command_complete[i].expand == EXPAND_FILES)
7175 *argt |= XFILE;
7176 break;
7177 }
7178 }
7179
7180 if (command_complete[i].expand == 0)
7181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007182 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007183 return FAIL;
7184 }
7185
7186# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7187 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7188 && arg != NULL)
7189# else
7190 if (arg != NULL)
7191# endif
7192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007193 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007194 return FAIL;
7195 }
7196
7197# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7198 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7199 && arg == NULL)
7200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007201 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007202 return FAIL;
7203 }
7204
7205 if (arg != NULL)
7206 *compl_arg = vim_strnsave(arg, (int)arglen);
7207# endif
7208 return OK;
7209}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007210
7211 int
7212cmdcomplete_str_to_type(char_u *complete_str)
7213{
7214 int i;
7215
7216 for (i = 0; command_complete[i].expand != 0; ++i)
7217 if (STRCMP(complete_str, command_complete[i].name) == 0)
7218 return command_complete[i].expand;
7219
7220 return EXPAND_NOTHING;
7221}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007222#endif
7223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007225ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
Bram Moolenaare6850792010-05-14 15:28:44 +02007227 if (*eap->arg == NUL)
7228 {
7229#ifdef FEAT_EVAL
7230 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7231 char_u *p = NULL;
7232
7233 if (expr != NULL)
7234 {
7235 ++emsg_off;
7236 p = eval_to_string(expr, NULL, FALSE);
7237 --emsg_off;
7238 vim_free(expr);
7239 }
7240 if (p != NULL)
7241 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007242 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02007243 vim_free(p);
7244 }
7245 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007246 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02007247#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007248 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02007249#endif
7250 }
7251 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007252 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253}
7254
7255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007256ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01007259 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 do_highlight(eap->arg, eap->forceit, FALSE);
7261}
7262
7263
7264/*
7265 * Call this function if we thought we were going to exit, but we won't
7266 * (because of an error). May need to restore the terminal mode.
7267 */
7268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007269not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271 exiting = FALSE;
7272 settmode(TMODE_RAW);
7273}
7274
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007275 static int
7276before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7277{
7278 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7279
7280 /* Bail out when autocommands closed the window.
7281 * Refuse to quit when the buffer in the last window is being closed (can
7282 * only happen in autocommands). */
7283 if (!win_valid(wp)
7284 || curbuf_locked()
7285 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7286 return TRUE;
7287
7288 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7289 {
7290 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7291 /* Refuse to quit when locked or when the buffer in the last window is
7292 * being closed (can only happen in autocommands). */
7293 if (curbuf_locked()
7294 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7295 return TRUE;
7296 }
7297
7298 return FALSE;
7299}
7300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007302 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007303 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 */
7305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007306ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007308 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007309
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310#ifdef FEAT_CMDWIN
7311 if (cmdwin_type != 0)
7312 {
7313 cmdwin_result = Ctrl_C;
7314 return;
7315 }
7316#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007317 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007318 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007319 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007320 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007321 return;
7322 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007323 if (eap->addr_count > 0)
7324 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007325 int wnr = eap->line2;
7326
7327 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7328 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007329 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007330 }
7331 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007332 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007333
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007334 /* Refuse to quit when locked. */
7335 if (curbuf_locked())
7336 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007337
7338 /* Trigger QuitPre and maybe ExitPre */
7339 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007340 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
7342#ifdef FEAT_NETBEANS_INTG
7343 netbeansForcedQuit = eap->forceit;
7344#endif
7345
7346 /*
7347 * If there are more files or windows we won't exit.
7348 */
7349 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7350 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007351 if ((!buf_hide(wp->w_buffer)
7352 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007353 | (eap->forceit ? CCGD_FORCEIT : 0)
7354 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007356 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
7358 not_exiting();
7359 }
7360 else
7361 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007362 /* quit last window
7363 * Note: only_one_window() returns true, even so a help window is
7364 * still open. In that case only quit, if no address has been
7365 * specified. Example:
7366 * :h|wincmd w|1q - don't quit
7367 * :h|wincmd w|q - quit
7368 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007369 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007371 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007372#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007376 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 }
7378}
7379
7380/*
7381 * ":cquit".
7382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007384ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386 getout(1); /* this does not always pass on the exit code to the Manx
7387 compiler. why? */
7388}
7389
7390/*
7391 * ":qall": try to quit all windows
7392 */
7393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007394ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396# ifdef FEAT_CMDWIN
7397 if (cmdwin_type != 0)
7398 {
7399 if (eap->forceit)
7400 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7401 else
7402 cmdwin_result = K_XF2;
7403 return;
7404 }
7405# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007406
7407 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007408 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007409 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007410 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007411 return;
7412 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007413
7414 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007415 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007418 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 getout(0);
7420 not_exiting();
7421}
7422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423/*
7424 * ":close": close current window, unless it is the last one
7425 */
7426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007429 win_T *win;
7430 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007431#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007433 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007435#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007436 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007437 {
7438 if (eap->addr_count == 0)
7439 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007440 else
7441 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007442 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007443 {
7444 winnr++;
7445 if (winnr == eap->line2)
7446 break;
7447 }
7448 if (win == NULL)
7449 win = lastwin;
7450 ex_win_close(eap->forceit, win, NULL);
7451 }
7452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453}
7454
Bram Moolenaar4033c552017-09-16 20:54:51 +02007455#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007456/*
7457 * ":pclose": Close any preview window.
7458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007460ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007461{
7462 win_T *win;
7463
Bram Moolenaar29323592016-07-24 22:04:11 +02007464 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007465 if (win->w_p_pvw)
7466 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007467 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007468 break;
7469 }
7470}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007471#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007472
Bram Moolenaarf740b292006-02-16 22:11:02 +00007473/*
7474 * Close window "win" and take care of handling closing the last window for a
7475 * modified buffer.
7476 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007478ex_win_close(
7479 int forceit,
7480 win_T *win,
7481 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 int need_hide;
7484 buf_T *buf = win->w_buffer;
7485
7486 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007487 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007489#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 if ((p_confirm || cmdmod.confirm) && p_write)
7491 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007492 bufref_T bufref;
7493
7494 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007496 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 return;
7498 need_hide = FALSE;
7499 }
7500 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007503 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 return;
7505 }
7506 }
7507
Bram Moolenaar4033c552017-09-16 20:54:51 +02007508#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007510#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007513 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007514 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007515 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007516 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007520 * Handle the argument for a tabpage related ex command.
7521 * Returns a tabpage number.
7522 * When an error is encountered then eap->errmsg is set.
7523 */
7524 static int
7525get_tabpage_arg(exarg_T *eap)
7526{
7527 int tab_number;
7528 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7529
7530 if (eap->arg && *eap->arg != NUL)
7531 {
7532 char_u *p = eap->arg;
7533 char_u *p_save;
7534 int relative = 0; /* argument +N/-N means: go to N places to the
7535 * right/left relative to the current position. */
7536
7537 if (*p == '-')
7538 {
7539 relative = -1;
7540 p++;
7541 }
7542 else if (*p == '+')
7543 {
7544 relative = 1;
7545 p++;
7546 }
7547
7548 p_save = p;
7549 tab_number = getdigits(&p);
7550
7551 if (relative == 0)
7552 {
7553 if (STRCMP(p, "$") == 0)
7554 tab_number = LAST_TAB_NR;
7555 else if (p == p_save || *p_save == '-' || *p != NUL
7556 || tab_number > LAST_TAB_NR)
7557 {
7558 /* No numbers as argument. */
7559 eap->errmsg = e_invarg;
7560 goto theend;
7561 }
7562 }
7563 else
7564 {
7565 if (*p_save == NUL)
7566 tab_number = 1;
7567 else if (p == p_save || *p_save == '-' || *p != NUL
7568 || tab_number == 0)
7569 {
7570 /* No numbers as argument. */
7571 eap->errmsg = e_invarg;
7572 goto theend;
7573 }
7574 tab_number = tab_number * relative + tabpage_index(curtab);
7575 if (!unaccept_arg0 && relative == -1)
7576 --tab_number;
7577 }
7578 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7579 eap->errmsg = e_invarg;
7580 }
7581 else if (eap->addr_count > 0)
7582 {
7583 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007584 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007585 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007586 tab_number = 0;
7587 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007588 else
7589 {
7590 tab_number = eap->line2;
7591 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7592 {
7593 --tab_number;
7594 if (tab_number < unaccept_arg0)
7595 eap->errmsg = e_invarg;
7596 }
7597 }
7598 }
7599 else
7600 {
7601 switch (eap->cmdidx)
7602 {
7603 case CMD_tabnext:
7604 tab_number = tabpage_index(curtab) + 1;
7605 if (tab_number > LAST_TAB_NR)
7606 tab_number = 1;
7607 break;
7608 case CMD_tabmove:
7609 tab_number = LAST_TAB_NR;
7610 break;
7611 default:
7612 tab_number = tabpage_index(curtab);
7613 }
7614 }
7615
7616theend:
7617 return tab_number;
7618}
7619
7620/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007621 * ":tabclose": close current tab page, unless it is the last one.
7622 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 */
7624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007625ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007627 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007628 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007630# ifdef FEAT_CMDWIN
7631 if (cmdwin_type != 0)
7632 cmdwin_result = K_IGNORE;
7633 else
7634# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007635 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007636 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007639 tab_number = get_tabpage_arg(eap);
7640 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007641 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007642 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007643 if (tp == NULL)
7644 {
7645 beep_flush();
7646 return;
7647 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007648 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007649 {
7650 tabpage_close_other(tp, eap->forceit);
7651 return;
7652 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007653 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007654 tabpage_close(eap->forceit);
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007657}
7658
7659/*
7660 * ":tabonly": close all tab pages except the current one
7661 */
7662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007663ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007664{
7665 tabpage_T *tp;
7666 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007667 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007668
7669# ifdef FEAT_CMDWIN
7670 if (cmdwin_type != 0)
7671 cmdwin_result = K_IGNORE;
7672 else
7673# endif
7674 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007675 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007676 else
7677 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007678 tab_number = get_tabpage_arg(eap);
7679 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007680 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007681 goto_tabpage(tab_number);
7682 /* Repeat this up to a 1000 times, because autocommands may
7683 * mess up the lists. */
7684 for (done = 0; done < 1000; ++done)
7685 {
7686 FOR_ALL_TABPAGES(tp)
7687 if (tp->tp_topframe != topframe)
7688 {
7689 tabpage_close_other(tp, eap->forceit);
7690 /* if we failed to close it quit */
7691 if (valid_tabpage(tp))
7692 done = 1000;
7693 /* start over, "tp" is now invalid */
7694 break;
7695 }
7696 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007697 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007698 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007699 }
7700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
7703/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007704 * Close the current tab page.
7705 */
7706 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007707tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007708{
7709 /* First close all the windows but the current one. If that worked then
7710 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007711 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007712 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007713 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007714 ex_win_close(forceit, curwin, NULL);
7715# ifdef FEAT_GUI
7716 need_mouse_correct = TRUE;
7717# endif
7718}
7719
7720/*
7721 * Close tab page "tp", which is not the current tab page.
7722 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007723 * Also takes care of the tab pages line disappearing when closing the
7724 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007725 */
7726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007727tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007728{
7729 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007730 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007731 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007732
7733 /* Limit to 1000 windows, autocommands may add a window while we close
7734 * one. OK, so I'm paranoid... */
7735 while (++done < 1000)
7736 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007737 wp = tp->tp_firstwin;
7738 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007739
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007740 /* Autocommands may delete the tab page under our fingers and we may
7741 * fail to close a window with a modified buffer. */
7742 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007743 break;
7744 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007745
Bram Moolenaar29323592016-07-24 22:04:11 +02007746 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007747
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007748 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007749 if (h != tabline_height())
7750 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007751}
7752
7753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 * ":only".
7755 */
7756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007757ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007759 win_T *wp;
7760 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761# ifdef FEAT_GUI
7762 need_mouse_correct = TRUE;
7763# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007764 if (eap->addr_count > 0)
7765 {
7766 wnr = eap->line2;
7767 for (wp = firstwin; --wnr > 0; )
7768 {
7769 if (wp->w_next == NULL)
7770 break;
7771 else
7772 wp = wp->w_next;
7773 }
7774 win_goto(wp);
7775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 close_others(TRUE, eap->forceit);
7777}
7778
7779/*
7780 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007781 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 if (eap->addr_count == 0)
7787 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007788 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007792ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007794 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007795 if (!eap->skip)
7796 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007797#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007798 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007799#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007800 if (eap->addr_count == 0)
7801 win_close(curwin, FALSE); /* don't free buffer */
7802 else
7803 {
7804 int winnr = 0;
7805 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007806
Bram Moolenaar2256c992016-11-15 21:17:07 +01007807 FOR_ALL_WINDOWS(win)
7808 {
7809 winnr++;
7810 if (winnr == eap->line2)
7811 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007812 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007813 if (win == NULL)
7814 win = lastwin;
7815 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818}
7819
7820/*
7821 * ":stop" and ":suspend": Suspend Vim.
7822 */
7823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007824ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 /*
7827 * Disallow suspending for "rvim".
7828 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007829 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831 if (!eap->forceit)
7832 autowrite_all();
7833 windgoto((int)Rows - 1, 0);
7834 out_char('\n');
7835 out_flush();
7836 stoptermcap();
7837 out_flush(); /* needed for SUN to restore xterm buffer */
7838#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007839 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840#endif
7841 ui_suspend(); /* call machine specific function */
7842#ifdef FEAT_TITLE
7843 maketitle();
7844 resettitle(); /* force updating the title */
7845#endif
7846 starttermcap();
7847 scroll_start(); /* scroll screen before redrawing */
7848 redraw_later_clear();
7849 shell_resized(); /* may have resized window */
7850 }
7851}
7852
7853/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007854 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 */
7856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007857ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859#ifdef FEAT_CMDWIN
7860 if (cmdwin_type != 0)
7861 {
7862 cmdwin_result = Ctrl_C;
7863 return;
7864 }
7865#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007866 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007867 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007868 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007869 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007870 return;
7871 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007872
7873 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
7876 /*
7877 * if more files or windows we won't exit
7878 */
7879 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7880 exiting = TRUE;
7881 if ( ((eap->cmdidx == CMD_wq
7882 || curbufIsChanged())
7883 && do_write(eap) == FAIL)
7884 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007885 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 not_exiting();
7888 }
7889 else
7890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007893 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894# ifdef FEAT_GUI
7895 need_mouse_correct = TRUE;
7896# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007897 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007898 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900}
7901
7902/*
7903 * ":print", ":list", ":number".
7904 */
7905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007908 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007909 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007912 for ( ;!got_int; ui_breakcheck())
7913 {
7914 print_line(eap->line1,
7915 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7916 || (eap->flags & EXFLAG_NR)),
7917 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7918 if (++eap->line1 > eap->line2)
7919 break;
7920 out_flush(); /* show one line at a time */
7921 }
7922 setpcmark();
7923 /* put cursor at last line */
7924 curwin->w_cursor.lnum = eap->line2;
7925 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929}
7930
7931#ifdef FEAT_BYTEOFF
7932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 goto_byte(eap->line2);
7936}
7937#endif
7938
7939/*
7940 * ":shell".
7941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007943ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 do_shell(NULL, 0);
7946}
7947
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007948#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007950static int drop_busy = FALSE;
7951static int drop_filec;
7952static char_u **drop_filev = NULL;
7953static int drop_split;
7954static void (*drop_callback)(void *);
7955static void *drop_cookie;
7956
7957 static void
7958handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959{
7960 exarg_T ea;
7961 int save_msg_scroll = msg_scroll;
7962
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007963 // Setting the argument list may cause screen updates and being called
7964 // recursively. Avoid that by setting drop_busy.
7965 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
7967 /* Check whether the current buffer is changed. If so, we will need
7968 * to split the current window or data could be lost.
7969 * We don't need to check if the 'hidden' option is set, as in this
7970 * case the buffer won't be lost.
7971 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007972 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
7974 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007975 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 --emsg_off;
7977 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007978 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (win_split(0, 0) == FAIL)
7981 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007982 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983
7984 /* When splitting the window, create a new alist. Otherwise the
7985 * existing one is overwritten. */
7986 alist_unlink(curwin->w_alist);
7987 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
7989
7990 /*
7991 * Set up the new argument list.
7992 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007993 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
7995 /*
7996 * Move to the first file.
7997 */
7998 /* Fake up a minimal "next" command for do_argfile() */
7999 vim_memset(&ea, 0, sizeof(ea));
8000 ea.cmd = (char_u *)"next";
8001 do_argfile(&ea, 0);
8002
8003 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8004 * mode that is not needed here. */
8005 need_start_insertmode = FALSE;
8006
8007 /* Restore msg_scroll, otherwise a following command may cause scrolling
8008 * unexpectedly. The screen will be redrawn by the caller, thus
8009 * msg_scroll being set by displaying a message is irrelevant. */
8010 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008011
8012 if (drop_callback != NULL)
8013 drop_callback(drop_cookie);
8014
8015 drop_filev = NULL;
8016 drop_busy = FALSE;
8017}
8018
8019/*
8020 * Handle a file drop. The code is here because a drop is *nearly* like an
8021 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008022 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008023 * code is very similar to :args and hence needs access to a lot of the static
8024 * functions in this file.
8025 *
8026 * The "filev" list must have been allocated using alloc(), as should each item
8027 * in the list. This function takes over responsibility for freeing the "filev"
8028 * list.
8029 */
8030 void
8031handle_drop(
8032 int filec, // the number of files dropped
8033 char_u **filev, // the list of files dropped
8034 int split, // force splitting the window
8035 void (*callback)(void *), // to be called after setting the argument
8036 // list
8037 void *cookie) // argument for "callback" (allocated)
8038{
8039 // Cannot handle recursive drops, finish the pending one.
8040 if (drop_busy)
8041 {
8042 FreeWild(filec, filev);
8043 vim_free(cookie);
8044 return;
8045 }
8046
8047 // When calling handle_drop() more than once in a row we only use the last
8048 // one.
8049 if (drop_filev != NULL)
8050 {
8051 FreeWild(drop_filec, drop_filev);
8052 vim_free(drop_cookie);
8053 }
8054
8055 drop_filec = filec;
8056 drop_filev = filev;
8057 drop_split = split;
8058 drop_callback = callback;
8059 drop_cookie = cookie;
8060
8061 // Postpone this when:
8062 // - editing the command line
8063 // - not possible to change the current buffer
8064 // - updating the screen
8065 // As it may change buffers and window structures that are in use and cause
8066 // freed memory to be used.
8067 if (text_locked() || curbuf_locked() || updating_screen)
8068 return;
8069
8070 handle_drop_internal();
8071}
8072
8073/*
8074 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8075 * reset: Handle a postponed drop.
8076 */
8077 void
8078handle_any_postponed_drop(void)
8079{
8080 if (!drop_busy && drop_filev != NULL
8081 && !text_locked() && !curbuf_locked() && !updating_screen)
8082 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083}
8084#endif
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086/*
8087 * Clear an argument list: free all file names and reset it to zero entries.
8088 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008089 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 while (--al->al_ga.ga_len >= 0)
8093 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8094 ga_clear(&al->al_ga);
8095}
8096
8097/*
8098 * Init an argument list.
8099 */
8100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008101alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8104}
8105
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106/*
8107 * Remove a reference from an argument list.
8108 * Ignored when the argument list is the global one.
8109 * If the argument list is no longer used by any window, free it.
8110 */
8111 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008112alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 if (al != &global_alist && --al->al_refcount <= 0)
8115 {
8116 alist_clear(al);
8117 vim_free(al);
8118 }
8119}
8120
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121/*
8122 * Create a new argument list and use it for the current window.
8123 */
8124 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008125alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8128 if (curwin->w_alist == NULL)
8129 {
8130 curwin->w_alist = &global_alist;
8131 ++global_alist.al_refcount;
8132 }
8133 else
8134 {
8135 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008136 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 alist_init(curwin->w_alist);
8138 }
8139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008141#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142/*
8143 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008144 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8145 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 */
8147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008148alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
8150 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008151 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 char_u **new_arg_files;
8153 int new_arg_file_count;
8154 char_u *save_p_su = p_su;
8155 int i;
8156
8157 /* Don't use 'suffixes' here. This should work like the shell did the
8158 * expansion. Also, the vimrc file isn't read yet, thus the user
8159 * can't set the options. */
8160 p_su = empty_option;
8161 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8162 if (old_arg_files != NULL)
8163 {
8164 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008165 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8166 old_arg_count = GARGCOUNT;
8167 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008169 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 && new_arg_file_count > 0)
8171 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008172 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8173 TRUE, fnum_list, fnum_len);
8174 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
8177 p_su = save_p_su;
8178}
8179#endif
8180
8181/*
8182 * Set the argument list for the current window.
8183 * Takes over the allocated files[] and the allocated fnames in it.
8184 */
8185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008186alist_set(
8187 alist_T *al,
8188 int count,
8189 char_u **files,
8190 int use_curbuf,
8191 int *fnum_list,
8192 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
8194 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008195 static int recursive = 0;
8196
8197 if (recursive)
8198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008199 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008200 return;
8201 }
8202 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
8204 alist_clear(al);
8205 if (ga_grow(&al->al_ga, count) == OK)
8206 {
8207 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008208 {
8209 if (got_int)
8210 {
8211 /* When adding many buffers this can take a long time. Allow
8212 * interrupting here. */
8213 while (i < count)
8214 vim_free(files[i++]);
8215 break;
8216 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008217
8218 /* May set buffer name of a buffer previously used for the
8219 * argument list, so that it's re-used by alist_add. */
8220 if (fnum_list != NULL && i < fnum_len)
8221 buf_set_name(fnum_list[i], files[i]);
8222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008224 ui_breakcheck();
8225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 vim_free(files);
8227 }
8228 else
8229 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008232
8233 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234}
8235
8236/*
8237 * Add file "fname" to argument list "al".
8238 * "fname" must have been allocated and "al" must have been checked for room.
8239 */
8240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008241alist_add(
8242 alist_T *al,
8243 char_u *fname,
8244 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246 if (fname == NULL) /* don't add NULL file names */
8247 return;
8248#ifdef BACKSLASH_IN_FILENAME
8249 slash_adjust(fname);
8250#endif
8251 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8252 if (set_fnum > 0)
8253 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8254 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8255 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256}
8257
8258#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8259/*
8260 * Adjust slashes in file names. Called after 'shellslash' was set.
8261 */
8262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008263alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264{
8265 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008267 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
8269 for (i = 0; i < GARGCOUNT; ++i)
8270 if (GARGLIST[i].ae_fname != NULL)
8271 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008272 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 if (wp->w_alist != &global_alist)
8274 for (i = 0; i < WARGCOUNT(wp); ++i)
8275 if (WARGLIST(wp)[i].ae_fname != NULL)
8276 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278#endif
8279
8280/*
8281 * ":preserve".
8282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008284ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008286 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 ml_preserve(curbuf, TRUE);
8288}
8289
8290/*
8291 * ":recover".
8292 */
8293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008294ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8297 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008298 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8299 | CCGD_MULTWIN
8300 | (eap->forceit ? CCGD_FORCEIT : 0)
8301 | CCGD_EXCMD)
8302
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 && (*eap->arg == NUL
8304 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8305 ml_recover();
8306 recoverymode = FALSE;
8307}
8308
8309/*
8310 * Command modifier used in a wrong way.
8311 */
8312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008313ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 eap->errmsg = e_invcmd;
8316}
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318/*
8319 * :sview [+command] file split window with new file, read-only
8320 * :split [[+command] file] split window with current or new file
8321 * :vsplit [[+command] file] split window vertically with current or new file
8322 * :new [[+command] file] split window with no or new file
8323 * :vnew [[+command] file] split vertically window with no or new file
8324 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008325 *
8326 * :tabedit open new Tab page with empty window
8327 * :tabedit [+command] file open new Tab page and edit "file"
8328 * :tabnew [[+command] file] just like :tabedit
8329 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 */
8331 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008332ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008334 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008335#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008337#endif
8338#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008340#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008341 int use_tab = eap->cmdidx == CMD_tabedit
8342 || eap->cmdidx == CMD_tabfind
8343 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar4033c552017-09-16 20:54:51 +02008345#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
Bram Moolenaar4033c552017-09-16 20:54:51 +02008349#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008351 * quickfix windows. But it's OK when doing ":tab split". */
8352 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
8354 if (eap->cmdidx == CMD_split)
8355 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 if (eap->cmdidx == CMD_vsplit)
8357 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar4033c552017-09-16 20:54:51 +02008361#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008362 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {
8364 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8365 FNAME_MESS, TRUE, curbuf->b_ffname);
8366 if (fname == NULL)
8367 goto theend;
8368 eap->arg = fname;
8369 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008370# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008371 else
8372# endif
8373#endif
8374#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 && eap->cmdidx != CMD_new)
8378 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008379 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008380# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008381 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008382# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008383 au_has_group((char_u *)"FileExplorer"))
8384 {
8385 /* No browsing supported but we do have the file explorer:
8386 * Edit the directory. */
8387 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8388 eap->arg = (char_u *)".";
8389 }
8390 else
8391 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008392 fname = do_browse(0, (char_u *)(use_tab
8393 ? _("Edit File in new tab page")
8394 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008396 if (fname == NULL)
8397 goto theend;
8398 eap->arg = fname;
8399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 }
8401 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008404 /*
8405 * Either open new tab page or split the window.
8406 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008407 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008408 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008409 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8410 : eap->addr_count == 0 ? 0
8411 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008412 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008413 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008414
8415 /* set the alternate buffer for the window we came from */
8416 if (curwin != old_curwin
8417 && win_valid(old_curwin)
8418 && old_curwin->w_buffer != curbuf
8419 && !cmdmod.keepalt)
8420 old_curwin->w_alt_fnum = curbuf->b_fnum;
8421 }
8422 }
8423 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 /* Reset 'scrollbind' when editing another file, but keep it when
8427 * doing ":split" without arguments. */
8428 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008429# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008433 {
8434 RESET_BINDING(curwin);
8435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 else
8437 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 do_exedit(eap, old_curwin);
8439 }
8440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008441# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008445# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446theend:
8447 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008450
8451/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008452 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008453 */
8454 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008455tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008456{
8457 exarg_T ea;
8458
8459 vim_memset(&ea, 0, sizeof(ea));
8460 ea.cmdidx = CMD_tabnew;
8461 ea.cmd = (char_u *)"tabn";
8462 ea.arg = (char_u *)"";
8463 ex_splitview(&ea);
8464}
8465
8466/*
8467 * :tabnext command
8468 */
8469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008470ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008471{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008472 int tab_number;
8473
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008474 switch (eap->cmdidx)
8475 {
8476 case CMD_tabfirst:
8477 case CMD_tabrewind:
8478 goto_tabpage(1);
8479 break;
8480 case CMD_tablast:
8481 goto_tabpage(9999);
8482 break;
8483 case CMD_tabprevious:
8484 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008485 if (eap->arg && *eap->arg != NUL)
8486 {
8487 char_u *p = eap->arg;
8488 char_u *p_save = p;
8489
8490 tab_number = getdigits(&p);
8491 if (p == p_save || *p_save == '-' || *p != NUL
8492 || tab_number == 0)
8493 {
8494 /* No numbers as argument. */
8495 eap->errmsg = e_invarg;
8496 return;
8497 }
8498 }
8499 else
8500 {
8501 if (eap->addr_count == 0)
8502 tab_number = 1;
8503 else
8504 {
8505 tab_number = eap->line2;
8506 if (tab_number < 1)
8507 {
8508 eap->errmsg = e_invrange;
8509 return;
8510 }
8511 }
8512 }
8513 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008514 break;
8515 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008516 tab_number = get_tabpage_arg(eap);
8517 if (eap->errmsg == NULL)
8518 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008519 break;
8520 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008521}
8522
8523/*
8524 * :tabmove command
8525 */
8526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008527ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008528{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008529 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008530
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008531 tab_number = get_tabpage_arg(eap);
8532 if (eap->errmsg == NULL)
8533 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008534}
8535
8536/*
8537 * :tabs command: List tabs and their contents.
8538 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008540ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008541{
8542 tabpage_T *tp;
8543 win_T *wp;
8544 int tabcount = 1;
8545
8546 msg_start();
8547 msg_scroll = TRUE;
8548 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8549 {
8550 msg_putchar('\n');
8551 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008552 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008553 out_flush(); /* output one line at a time */
8554 ui_breakcheck();
8555
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008556 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008557 wp = firstwin;
8558 else
8559 wp = tp->tp_firstwin;
8560 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8561 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008562 msg_putchar('\n');
8563 msg_putchar(wp == curwin ? '>' : ' ');
8564 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008565 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8566 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008567 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008568 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008569 else
8570 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8571 IObuff, IOSIZE, TRUE);
8572 msg_outtrans(IObuff);
8573 out_flush(); /* output one line at a time */
8574 ui_breakcheck();
8575 }
8576 }
8577}
8578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579/*
8580 * ":mode": Set screen mode.
8581 * If no argument given, just get the screen size and redraw.
8582 */
8583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008584ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
8586 if (*eap->arg == NUL)
8587 shell_resized();
8588 else
8589 mch_screenmode(eap->arg);
8590}
8591
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592/*
8593 * ":resize".
8594 * set, increment or decrement current window height
8595 */
8596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008597ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 int n;
8600 win_T *wp = curwin;
8601
8602 if (eap->addr_count > 0)
8603 {
8604 n = eap->line2;
8605 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8606 ;
8607 }
8608
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008609# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 if (cmdmod.split & WSP_VERT)
8614 {
8615 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008616 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8618 n = 9999;
8619 win_setwidth_win((int)n, wp);
8620 }
8621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {
8623 if (*eap->arg == '-' || *eap->arg == '+')
8624 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008625 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 n = 9999;
8627 win_setheight_win((int)n, wp);
8628 }
8629}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630
8631/*
8632 * ":find [+command] <file>" command.
8633 */
8634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008635ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636{
8637#ifdef FEAT_SEARCHPATH
8638 char_u *fname;
8639 int count;
8640
8641 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8642 TRUE, curbuf->b_ffname);
8643 if (eap->addr_count > 0)
8644 {
8645 /* Repeat finding the file "count" times. This matters when it
8646 * appears several times in the path. */
8647 count = eap->line2;
8648 while (fname != NULL && --count > 0)
8649 {
8650 vim_free(fname);
8651 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8652 FALSE, curbuf->b_ffname);
8653 }
8654 }
8655
8656 if (fname != NULL)
8657 {
8658 eap->arg = fname;
8659#endif
8660 do_exedit(eap, NULL);
8661#ifdef FEAT_SEARCHPATH
8662 vim_free(fname);
8663 }
8664#endif
8665}
8666
8667/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008668 * ":open" simulation: for now just work like ":visual".
8669 */
8670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008671ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008672{
8673 regmatch_T regmatch;
8674 char_u *p;
8675
8676 curwin->w_cursor.lnum = eap->line2;
8677 beginline(BL_SOL | BL_FIX);
8678 if (*eap->arg == '/')
8679 {
8680 /* ":open /pattern/": put cursor in column found with pattern */
8681 ++eap->arg;
8682 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8683 *p = NUL;
8684 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8685 if (regmatch.regprog != NULL)
8686 {
8687 regmatch.rm_ic = p_ic;
8688 p = ml_get_curline();
8689 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008690 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008691 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008692 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008693 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008694 }
8695 /* Move to the NUL, ignore any other arguments. */
8696 eap->arg += STRLEN(eap->arg);
8697 }
8698 check_cursor();
8699
8700 eap->cmdidx = CMD_visual;
8701 do_exedit(eap, NULL);
8702}
8703
8704/*
8705 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 */
8707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008708ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
8710 do_exedit(eap, NULL);
8711}
8712
8713/*
8714 * ":edit <file>" command and alikes.
8715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008717do_exedit(
8718 exarg_T *eap,
8719 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008723 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 /*
8726 * ":vi" command ends Ex mode.
8727 */
8728 if (exmode_active && (eap->cmdidx == CMD_visual
8729 || eap->cmdidx == CMD_view))
8730 {
8731 exmode_active = FALSE;
8732 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008733 {
8734 /* Special case: ":global/pat/visual\NLvi-commands" */
8735 if (global_busy)
8736 {
8737 int rd = RedrawingDisabled;
8738 int nwr = no_wait_return;
8739 int ms = msg_scroll;
8740#ifdef FEAT_GUI
8741 int he = hold_gui_events;
8742#endif
8743
8744 if (eap->nextcmd != NULL)
8745 {
8746 stuffReadbuff(eap->nextcmd);
8747 eap->nextcmd = NULL;
8748 }
8749
8750 if (exmode_was != EXMODE_VIM)
8751 settmode(TMODE_RAW);
8752 RedrawingDisabled = 0;
8753 no_wait_return = 0;
8754 need_wait_return = FALSE;
8755 msg_scroll = 0;
8756#ifdef FEAT_GUI
8757 hold_gui_events = 0;
8758#endif
8759 must_redraw = CLEAR;
8760
8761 main_loop(FALSE, TRUE);
8762
8763 RedrawingDisabled = rd;
8764 no_wait_return = nwr;
8765 msg_scroll = ms;
8766#ifdef FEAT_GUI
8767 hold_gui_events = he;
8768#endif
8769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 }
8773
8774 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008775 || eap->cmdidx == CMD_tabnew
8776 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008777 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008779 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 setpcmark();
8781 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008782 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8783 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008785 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 || *eap->arg != NUL
8787#ifdef FEAT_BROWSE
8788 || cmdmod.browse
8789#endif
8790 )
8791 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008792 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8793 * can bring us here, others are stopped earlier. */
8794 if (*eap->arg != NUL && curbuf_locked())
8795 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008796
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 n = readonlymode;
8798 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8799 readonlymode = TRUE;
8800 else if (eap->cmdidx == CMD_enew)
8801 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8802 empty buffer */
8803 setpcmark();
8804 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8805 NULL, eap,
8806 /* ":edit" goes to first line if Vi compatible */
8807 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8808 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8809 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008810 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008812 /* after a split we can use an existing buffer */
8813 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008815 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
8817 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (old_curwin != NULL)
8819 {
8820 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008821 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008823#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008824 cleanup_T cs;
8825
8826 /* Reset the error/interrupt/exception state here so that
8827 * aborting() returns FALSE when closing a window. */
8828 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008829#endif
8830#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008832#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008833 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008834
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008835#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008836 /* Restore the error/interrupt/exception state if not
8837 * discarded by a new aborting error, interrupt, or
8838 * uncaught exception. */
8839 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 }
8842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 }
8844 else if (readonlymode && curbuf->b_nwindows == 1)
8845 {
8846 /* When editing an already visited buffer, 'readonly' won't be set
8847 * but the previous value is kept. With ":view" and ":sview" we
8848 * want the file to be readonly, except when another window is
8849 * editing the same buffer. */
8850 curbuf->b_p_ro = TRUE;
8851 }
8852 readonlymode = n;
8853 }
8854 else
8855 {
8856 if (eap->do_ecmd_cmd != NULL)
8857 do_cmdline_cmd(eap->do_ecmd_cmd);
8858#ifdef FEAT_TITLE
8859 n = curwin->w_arg_idx_invalid;
8860#endif
8861 check_arg_idx(curwin);
8862#ifdef FEAT_TITLE
8863 if (n != curwin->w_arg_idx_invalid)
8864 maketitle();
8865#endif
8866 }
8867
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 /*
8869 * if ":split file" worked, set alternate file name in old window to new
8870 * file
8871 */
8872 if (old_curwin != NULL
8873 && *eap->arg != NUL
8874 && curwin != old_curwin
8875 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008876 && old_curwin->w_buffer != curbuf
8877 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879
8880 ex_no_reprint = TRUE;
8881}
8882
8883#ifndef FEAT_GUI
8884/*
8885 * ":gui" and ":gvim" when there is no GUI.
8886 */
8887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008888ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 eap->errmsg = e_nogvim;
8891}
8892#endif
8893
8894#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008896ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 gui_make_tearoff(eap->arg);
8899}
8900#endif
8901
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008902#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8903 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008905ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008907# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8908 if (gui.in_use)
8909 gui_make_popup(eap->arg, eap->forceit);
8910# ifdef FEAT_TERM_POPUP_MENU
8911 else
8912# endif
8913# endif
8914# ifdef FEAT_TERM_POPUP_MENU
8915 pum_make_popup(eap->arg, eap->forceit);
8916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917}
8918#endif
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008921ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008924 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01008926 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927}
8928
8929/*
8930 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8931 * offset.
8932 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008935ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008938 win_T *save_curwin = curwin;
8939 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 long topline;
8941 long y;
8942 linenr_T old_linenr = curwin->w_cursor.lnum;
8943
8944 setpcmark();
8945
8946 /*
8947 * determine max topline
8948 */
8949 if (curwin->w_p_scb)
8950 {
8951 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008952 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 {
8954 if (wp->w_p_scb && wp->w_buffer)
8955 {
8956 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8957 if (topline > y)
8958 topline = y;
8959 }
8960 }
8961 if (topline < 1)
8962 topline = 1;
8963 }
8964 else
8965 {
8966 topline = 1;
8967 }
8968
8969
8970 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008971 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008973 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 {
8975 if (curwin->w_p_scb)
8976 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008977 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 y = topline - curwin->w_topline;
8979 if (y > 0)
8980 scrollup(y, TRUE);
8981 else
8982 scrolldown(-y, TRUE);
8983 curwin->w_scbind_pos = topline;
8984 redraw_later(VALID);
8985 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 }
8988 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008989 curwin = save_curwin;
8990 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 if (curwin->w_p_scb)
8992 {
8993 did_syncbind = TRUE;
8994 checkpcmark();
8995 if (old_linenr != curwin->w_cursor.lnum)
8996 {
8997 char_u ctrl_o[2];
8998
8999 ctrl_o[0] = Ctrl_O;
9000 ctrl_o[1] = 0;
9001 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9002 }
9003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004}
9005
9006
9007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009008ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009010 int i;
9011 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9012 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
9014 if (eap->usefilter) /* :r!cmd */
9015 do_bang(1, eap, FALSE, FALSE, TRUE);
9016 else
9017 {
9018 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9019 return;
9020
9021#ifdef FEAT_BROWSE
9022 if (cmdmod.browse)
9023 {
9024 char_u *browseFile;
9025
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009026 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 NULL, NULL, NULL, curbuf);
9028 if (browseFile != NULL)
9029 {
9030 i = readfile(browseFile, NULL,
9031 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9032 vim_free(browseFile);
9033 }
9034 else
9035 i = OK;
9036 }
9037 else
9038#endif
9039 if (*eap->arg == NUL)
9040 {
9041 if (check_fname() == FAIL) /* check for no file name */
9042 return;
9043 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9044 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9045 }
9046 else
9047 {
9048 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9049 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9050 i = readfile(eap->arg, NULL,
9051 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9052
9053 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009054 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009056#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (!aborting())
9058#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009059 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009062 {
9063 if (empty && exmode_active)
9064 {
9065 /* Delete the empty line that remains. Historically ex does
9066 * this but vi doesn't. */
9067 if (eap->line2 == 0)
9068 lnum = curbuf->b_ml.ml_line_count;
9069 else
9070 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009071 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009072 {
9073 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009074 if (curwin->w_cursor.lnum > 1
9075 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009076 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009077 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009078 }
9079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 }
9083}
9084
Bram Moolenaarea408852005-06-25 22:49:46 +00009085static char_u *prev_dir = NULL;
9086
9087#if defined(EXITFREE) || defined(PROTO)
9088 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009089free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009090{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009091 VIM_CLEAR(prev_dir);
9092 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009093}
9094#endif
9095
Bram Moolenaarf4258302013-06-02 18:20:17 +02009096/*
9097 * Deal with the side effects of changing the current directory.
9098 * When "local" is TRUE then this was after an ":lcd" command.
9099 */
9100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009101post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009102{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009103 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009104 if (local)
9105 {
9106 /* If still in global directory, need to remember current
9107 * directory as global directory. */
9108 if (globaldir == NULL && prev_dir != NULL)
9109 globaldir = vim_strsave(prev_dir);
9110 /* Remember this local directory for the window. */
9111 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9112 curwin->w_localdir = vim_strsave(NameBuff);
9113 }
9114 else
9115 {
9116 /* We are now in the global directory, no need to remember its
9117 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009118 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009119 }
9120
9121 shorten_fnames(TRUE);
9122}
9123
Bram Moolenaarea408852005-06-25 22:49:46 +00009124
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125/*
9126 * ":cd", ":lcd", ":chdir" and ":lchdir".
9127 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009128 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009129ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009131 char_u *new_dir;
9132 char_u *tofree;
9133 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134
9135 new_dir = eap->arg;
9136#if !defined(UNIX) && !defined(VMS)
9137 /* for non-UNIX ":cd" means: print current directory */
9138 if (*new_dir == NUL)
9139 ex_pwd(NULL);
9140 else
9141#endif
9142 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009143 if (allbuf_locked())
9144 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009145 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9146 && !eap->forceit)
9147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009148 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009149 return;
9150 }
9151
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 /* ":cd -": Change to previous directory */
9153 if (STRCMP(new_dir, "-") == 0)
9154 {
9155 if (prev_dir == NULL)
9156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009157 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 return;
9159 }
9160 new_dir = prev_dir;
9161 }
9162
9163 /* Save current directory for next ":cd -" */
9164 tofree = prev_dir;
9165 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9166 prev_dir = vim_strsave(NameBuff);
9167 else
9168 prev_dir = NULL;
9169
9170#if defined(UNIX) || defined(VMS)
9171 /* for UNIX ":cd" means: go to home directory */
9172 if (*new_dir == NUL)
9173 {
9174 /* use NameBuff for home directory name */
9175# ifdef VMS
9176 char_u *p;
9177
9178 p = mch_getenv((char_u *)"SYS$LOGIN");
9179 if (p == NULL || *p == NUL) /* empty is the same as not set */
9180 NameBuff[0] = NUL;
9181 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009182 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183# else
9184 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9185# endif
9186 new_dir = NameBuff;
9187 }
9188#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009189 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009190 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009191 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009192 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 else
9194 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009195 int is_local_chdir = eap->cmdidx == CMD_lcd
9196 || eap->cmdidx == CMD_lchdir;
9197
9198 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
9200 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009201 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009203
9204 if (dir_differs)
9205 apply_autocmds(EVENT_DIRCHANGED,
9206 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9207 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 }
9209 vim_free(tofree);
9210 }
9211}
9212
9213/*
9214 * ":pwd".
9215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009217ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218{
9219 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9220 {
9221#ifdef BACKSLASH_IN_FILENAME
9222 slash_adjust(NameBuff);
9223#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01009224 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 }
9226 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009227 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228}
9229
9230/*
9231 * ":=".
9232 */
9233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009234ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009236 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009237 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238}
9239
9240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009241ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009243 int n;
9244 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
9246 if (cursor_valid())
9247 {
9248 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9249 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009250 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009252
9253 len = eap->line2;
9254 switch (*eap->arg)
9255 {
9256 case 'm': break;
9257 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009258 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009259 }
9260 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261}
9262
9263/*
9264 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9265 */
9266 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009267do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
9269 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009270 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271
9272 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009273 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009274 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009276 wait_now = msec - done > 1000L ? 1000L : msec - done;
9277#ifdef FEAT_TIMERS
9278 {
9279 long due_time = check_due_timer();
9280
9281 if (due_time > 0 && due_time < wait_now)
9282 wait_now = due_time;
9283 }
9284#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009285#ifdef FEAT_JOB_CHANNEL
9286 if (has_any_channel() && wait_now > 100L)
9287 wait_now = 100L;
9288#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009289 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009290#ifdef FEAT_JOB_CHANNEL
9291 if (has_any_channel())
9292 ui_breakcheck_force(TRUE);
9293 else
9294#endif
9295 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009296#ifdef MESSAGE_QUEUE
9297 /* Process the netbeans and clientserver messages that may have been
9298 * received in the call to ui_breakcheck() when the GUI is in use. This
9299 * may occur when running a test case. */
9300 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009303
9304 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9305 // input buffer, otherwise a following call to input() fails.
9306 if (got_int)
9307 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308}
9309
9310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009311do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 int mode;
9314 char_u *cmdp;
9315
9316 cmdp = eap->cmd;
9317 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9318
9319 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9320 eap->arg, mode, isabbrev))
9321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009322 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009324 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 break;
9326 }
9327}
9328
9329/*
9330 * ":winsize" command (obsolete).
9331 */
9332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009333ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
9335 int w, h;
9336 char_u *arg = eap->arg;
9337 char_u *p;
9338
9339 w = getdigits(&arg);
9340 arg = skipwhite(arg);
9341 p = arg;
9342 h = getdigits(&arg);
9343 if (*p != NUL && *arg == NUL)
9344 set_shellsize(w, h, TRUE);
9345 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009346 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347}
9348
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009350ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351{
9352 int xchar = NUL;
9353 char_u *p;
9354
9355 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9356 {
9357 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9358 if (eap->arg[1] == NUL)
9359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009360 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 return;
9362 }
9363 xchar = eap->arg[1];
9364 p = eap->arg + 2;
9365 }
9366 else
9367 p = eap->arg + 1;
9368
9369 eap->nextcmd = check_nextcmd(p);
9370 p = skipwhite(p);
9371 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009372 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009373 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
9375 /* Pass flags on for ":vertical wincmd ]". */
9376 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009377 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9379 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009380 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 }
9382}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383
Bram Moolenaar843ee412004-06-30 16:16:41 +00009384#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385/*
9386 * ":winpos".
9387 */
9388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009389ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 int x, y;
9392 char_u *arg = eap->arg;
9393 char_u *p;
9394
9395 if (*arg == NUL)
9396 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009397# if defined(FEAT_GUI) || defined(MSWIN)
9398# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009400# else
9401 if (mch_get_winpos(&x, &y) != FAIL)
9402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 {
9404 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01009405 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 }
9407 else
9408# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009409 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411 else
9412 {
9413 x = getdigits(&arg);
9414 arg = skipwhite(arg);
9415 p = arg;
9416 y = getdigits(&arg);
9417 if (*p == NUL || *arg != NUL)
9418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009419 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 return;
9421 }
9422# ifdef FEAT_GUI
9423 if (gui.in_use)
9424 gui_mch_set_winpos(x, y);
9425 else if (gui.starting)
9426 {
9427 /* Remember the coordinates for when the window is opened. */
9428 gui_win_x = x;
9429 gui_win_y = y;
9430 }
9431# ifdef HAVE_TGETENT
9432 else
9433# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009434# else
9435# ifdef MSWIN
9436 mch_set_winpos(x, y);
9437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438# endif
9439# ifdef HAVE_TGETENT
9440 if (*T_CWP)
9441 term_set_winpos(x, y);
9442# endif
9443 }
9444}
9445#endif
9446
9447/*
9448 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9449 */
9450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009451ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 oparg_T oa;
9454
9455 clear_oparg(&oa);
9456 oa.regname = eap->regname;
9457 oa.start.lnum = eap->line1;
9458 oa.end.lnum = eap->line2;
9459 oa.line_count = eap->line2 - eap->line1 + 1;
9460 oa.motion_type = MLINE;
9461#ifdef FEAT_VIRTUALEDIT
9462 virtual_op = FALSE;
9463#endif
9464 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9465 {
9466 setpcmark();
9467 curwin->w_cursor.lnum = eap->line1;
9468 beginline(BL_SOL | BL_FIX);
9469 }
9470
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009471 if (VIsual_active)
9472 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009473
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 switch (eap->cmdidx)
9475 {
9476 case CMD_delete:
9477 oa.op_type = OP_DELETE;
9478 op_delete(&oa);
9479 break;
9480
9481 case CMD_yank:
9482 oa.op_type = OP_YANK;
9483 (void)op_yank(&oa, FALSE, TRUE);
9484 break;
9485
9486 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009487 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009489 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9490#else
9491 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009493 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 oa.op_type = OP_RSHIFT;
9495 else
9496 oa.op_type = OP_LSHIFT;
9497 op_shift(&oa, FALSE, eap->amount);
9498 break;
9499 }
9500#ifdef FEAT_VIRTUALEDIT
9501 virtual_op = MAYBE;
9502#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009503 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504}
9505
9506/*
9507 * ":put".
9508 */
9509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009510ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
9512 /* ":0put" works like ":1put!". */
9513 if (eap->line2 == 0)
9514 {
9515 eap->line2 = 1;
9516 eap->forceit = TRUE;
9517 }
9518 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009519 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9520 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
9523/*
9524 * Handle ":copy" and ":move".
9525 */
9526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009527ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529 long n;
9530
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009531 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 if (eap->arg == NULL) /* error detected */
9533 {
9534 eap->nextcmd = NULL;
9535 return;
9536 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009537 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538
9539 /*
9540 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9541 */
9542 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009544 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 return;
9546 }
9547
9548 if (eap->cmdidx == CMD_move)
9549 {
9550 if (do_move(eap->line1, eap->line2, n) == FAIL)
9551 return;
9552 }
9553 else
9554 ex_copy(eap->line1, eap->line2, n);
9555 u_clearline();
9556 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009557 ex_may_print(eap);
9558}
9559
9560/*
9561 * Print the current line if flags were given to the Ex command.
9562 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009563 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009564ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009565{
9566 if (eap->flags != 0)
9567 {
9568 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9569 (eap->flags & EXFLAG_LIST));
9570 ex_no_reprint = TRUE;
9571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572}
9573
9574/*
9575 * ":smagic" and ":snomagic".
9576 */
9577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009578ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 int magic_save = p_magic;
9581
9582 p_magic = (eap->cmdidx == CMD_smagic);
9583 do_sub(eap);
9584 p_magic = magic_save;
9585}
9586
9587/*
9588 * ":join".
9589 */
9590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009591ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
9593 curwin->w_cursor.lnum = eap->line1;
9594 if (eap->line1 == eap->line2)
9595 {
9596 if (eap->addr_count >= 2) /* :2,2join does nothing */
9597 return;
9598 if (eap->line2 == curbuf->b_ml.ml_line_count)
9599 {
9600 beep_flush();
9601 return;
9602 }
9603 ++eap->line2;
9604 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009605 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009607 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608}
9609
9610/*
9611 * ":[addr]@r" or ":[addr]*r": execute register
9612 */
9613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009614ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009617 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
9619 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009620 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621
9622#ifdef USE_ON_FLY_SCROLL
9623 dont_scroll = TRUE; /* disallow scrolling here */
9624#endif
9625
9626 /* get the register name. No name means to use the previous one */
9627 c = *eap->arg;
9628 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9629 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009630 /* Put the register in the typeahead buffer with the "silent" flag. */
9631 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9632 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 else
9637 {
9638 int save_efr = exec_from_reg;
9639
9640 exec_from_reg = TRUE;
9641
9642 /*
9643 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009644 * Continue until the stuff buffer is empty and all added characters
9645 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009647 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9649
9650 exec_from_reg = save_efr;
9651 }
9652}
9653
9654/*
9655 * ":!".
9656 */
9657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009658ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659{
9660 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9661}
9662
9663/*
9664 * ":undo".
9665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009667ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009669 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009670 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009671 else
9672 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673}
9674
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009675#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009677ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009678{
9679 char_u hash[UNDO_HASH_SIZE];
9680
9681 u_compute_hash(hash);
9682 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9683}
9684
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009686ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009687{
9688 char_u hash[UNDO_HASH_SIZE];
9689
9690 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009691 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009692}
9693#endif
9694
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695/*
9696 * ":redo".
9697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009699ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
9701 u_redo(1);
9702}
9703
9704/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009705 * ":earlier" and ":later".
9706 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009708ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009709{
9710 long count = 0;
9711 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009712 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009713 char_u *p = eap->arg;
9714
9715 if (*p == NUL)
9716 count = 1;
9717 else if (isdigit(*p))
9718 {
9719 count = getdigits(&p);
9720 switch (*p)
9721 {
9722 case 's': ++p; sec = TRUE; break;
9723 case 'm': ++p; sec = TRUE; count *= 60; break;
9724 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009725 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9726 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009727 }
9728 }
9729
9730 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009731 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009732 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009733 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9734 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009735}
9736
9737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 * ":redir": start/stop redirection.
9739 */
9740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009741ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742{
9743 char *mode;
9744 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009745 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746
Bram Moolenaarba768492016-07-08 15:32:54 +02009747#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009748 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009750 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009751 return;
9752 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009753#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009754
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (STRICMP(eap->arg, "END") == 0)
9756 close_redir();
9757 else
9758 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009759 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009761 ++arg;
9762 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009764 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 mode = "a";
9766 }
9767 else
9768 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009769 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770
9771 close_redir();
9772
9773 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009774 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (fname == NULL)
9776 return;
9777#ifdef FEAT_BROWSE
9778 if (cmdmod.browse)
9779 {
9780 char_u *browseFile;
9781
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009782 browseFile = do_browse(BROWSE_SAVE,
9783 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009784 fname, NULL, NULL,
9785 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 if (browseFile == NULL)
9787 return; /* operation cancelled */
9788 vim_free(fname);
9789 fname = browseFile;
9790 eap->forceit = TRUE; /* since dialog already asked */
9791 }
9792#endif
9793
9794 redir_fd = open_exfile(fname, eap->forceit, mode);
9795 vim_free(fname);
9796 }
9797#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009798 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 {
9800 /* redirect to a register a-z (resp. A-Z for appending) */
9801 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009802 ++arg;
9803 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009805 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009806 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009808 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009810 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009811 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009812 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009815 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009816 if (*arg == '>')
9817 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009818 /* Make register empty when not using @A-@Z and the
9819 * command is valid. */
9820 if (*arg == NUL && !isupper(redir_reg))
9821 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009823 }
9824 if (*arg != NUL)
9825 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009826 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009827 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009828 }
9829 }
9830 else if (*arg == '=' && arg[1] == '>')
9831 {
9832 int append;
9833
9834 /* redirect to a variable */
9835 close_redir();
9836 arg += 2;
9837
9838 if (*arg == '>')
9839 {
9840 ++arg;
9841 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009844 append = FALSE;
9845
9846 if (var_redir_start(skipwhite(arg), append) == OK)
9847 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 }
9849#endif
9850
9851 /* TODO: redirect to a buffer */
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009854 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009856
9857 /* Make sure redirection is not off. Can happen for cmdline completion
9858 * that indirectly invokes a command to catch its output. */
9859 if (redir_fd != NULL
9860#ifdef FEAT_EVAL
9861 || redir_reg || redir_vname
9862#endif
9863 )
9864 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
9867/*
9868 * ":redraw": force redraw
9869 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009871ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873 int r = RedrawingDisabled;
9874 int p = p_lz;
9875
9876 RedrawingDisabled = 0;
9877 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009878 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009880 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881#ifdef FEAT_TITLE
9882 if (need_maketitle)
9883 maketitle();
9884#endif
9885 RedrawingDisabled = r;
9886 p_lz = p;
9887
9888 /* Reset msg_didout, so that a message that's there is overwritten. */
9889 msg_didout = FALSE;
9890 msg_col = 0;
9891
Bram Moolenaar56667a52013-07-17 11:54:28 +02009892 /* No need to wait after an intentional redraw. */
9893 need_wait_return = FALSE;
9894
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 out_flush();
9896}
9897
9898/*
9899 * ":redrawstatus": force redraw of status line(s)
9900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009902ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 int r = RedrawingDisabled;
9905 int p = p_lz;
9906
9907 RedrawingDisabled = 0;
9908 p_lz = FALSE;
9909 if (eap->forceit)
9910 status_redraw_all();
9911 else
9912 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009913 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 RedrawingDisabled = r;
9915 p_lz = p;
9916 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917}
9918
Bram Moolenaare12bab32019-01-08 22:02:56 +01009919/*
9920 * ":redrawtabline": force redraw of the tabline
9921 */
9922 static void
9923ex_redrawtabline(exarg_T *eap UNUSED)
9924{
9925 int r = RedrawingDisabled;
9926 int p = p_lz;
9927
9928 RedrawingDisabled = 0;
9929 p_lz = FALSE;
9930
9931 draw_tabline();
9932
9933 RedrawingDisabled = r;
9934 p_lz = p;
9935 out_flush();
9936}
9937
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009939close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 if (redir_fd != NULL)
9942 {
9943 fclose(redir_fd);
9944 redir_fd = NULL;
9945 }
9946#ifdef FEAT_EVAL
9947 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009948 if (redir_vname)
9949 {
9950 var_redir_stop();
9951 redir_vname = 0;
9952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953#endif
9954}
9955
9956#if defined(FEAT_SESSION) && defined(USE_CRNL)
9957# define MKSESSION_NL
9958static int mksession_nl = FALSE; /* use NL only in put_eol() */
9959#endif
9960
9961/*
9962 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9963 */
9964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009965ex_mkrc(
9966 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
9968 FILE *fd;
9969 int failed = FALSE;
9970 char_u *fname;
9971#ifdef FEAT_BROWSE
9972 char_u *browseFile = NULL;
9973#endif
9974#ifdef FEAT_SESSION
9975 int view_session = FALSE;
9976 int using_vdir = FALSE; /* using 'viewdir'? */
9977 char_u *viewFile = NULL;
9978 unsigned *flagp;
9979#endif
9980
9981 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9982 {
9983#ifdef FEAT_SESSION
9984 view_session = TRUE;
9985#else
9986 ex_ni(eap);
9987 return;
9988#endif
9989 }
9990
9991#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009992 /* Use the short file name until ":lcd" is used. We also don't use the
9993 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009994 did_lcd = FALSE;
9995
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9997 if (eap->cmdidx == CMD_mkview
9998 && (*eap->arg == NUL
9999 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
10000 {
10001 eap->forceit = TRUE;
10002 fname = get_view_file(*eap->arg);
10003 if (fname == NULL)
10004 return;
10005 viewFile = fname;
10006 using_vdir = TRUE;
10007 }
10008 else
10009#endif
10010 if (*eap->arg != NUL)
10011 fname = eap->arg;
10012 else if (eap->cmdidx == CMD_mkvimrc)
10013 fname = (char_u *)VIMRC_FILE;
10014#ifdef FEAT_SESSION
10015 else if (eap->cmdidx == CMD_mksession)
10016 fname = (char_u *)SESSION_FILE;
10017#endif
10018 else
10019 fname = (char_u *)EXRC_FILE;
10020
10021#ifdef FEAT_BROWSE
10022 if (cmdmod.browse)
10023 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +000010024 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025# ifdef FEAT_SESSION
10026 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10027 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10028# endif
10029 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010030 fname, (char_u *)"vim", NULL,
10031 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 if (browseFile == NULL)
10033 goto theend;
10034 fname = browseFile;
10035 eap->forceit = TRUE; /* since dialog already asked */
10036 }
10037#endif
10038
10039#if defined(FEAT_SESSION) && defined(vim_mkdir)
10040 /* When using 'viewdir' may have to create the directory. */
10041 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010042 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043#endif
10044
10045 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10046 if (fd != NULL)
10047 {
10048#ifdef FEAT_SESSION
10049 if (eap->cmdidx == CMD_mkview)
10050 flagp = &vop_flags;
10051 else
10052 flagp = &ssop_flags;
10053#endif
10054
10055#ifdef MKSESSION_NL
10056 /* "unix" in 'sessionoptions': use NL line separator */
10057 if (view_session && (*flagp & SSOP_UNIX))
10058 mksession_nl = TRUE;
10059#endif
10060
10061 /* Write the version command for :mkvimrc */
10062 if (eap->cmdidx == CMD_mkvimrc)
10063 (void)put_line(fd, "version 6.0");
10064
10065#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010066 if (eap->cmdidx == CMD_mksession)
10067 {
10068 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10069 failed = TRUE;
10070 }
10071
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (eap->cmdidx != CMD_mkview)
10073#endif
10074 {
10075 /* Write setting 'compatible' first, because it has side effects.
10076 * For that same reason only do it when needed. */
10077 if (p_cp)
10078 (void)put_line(fd, "if !&cp | set cp | endif");
10079 else
10080 (void)put_line(fd, "if &cp | set nocp | endif");
10081 }
10082
10083#ifdef FEAT_SESSION
10084 if (!view_session
10085 || (eap->cmdidx == CMD_mksession
10086 && (*flagp & SSOP_OPTIONS)))
10087#endif
10088 failed |= (makemap(fd, NULL) == FAIL
10089 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10090
10091#ifdef FEAT_SESSION
10092 if (!failed && view_session)
10093 {
10094 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10095 failed = TRUE;
10096 if (eap->cmdidx == CMD_mksession)
10097 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010098 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099
Bram Moolenaard9462e32011-04-11 21:35:11 +020010100 dirnow = alloc(MAXPATHL);
10101 if (dirnow == NULL)
10102 failed = TRUE;
10103 else
10104 {
10105 /*
10106 * Change to session file's dir.
10107 */
10108 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010110 *dirnow = NUL;
10111 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10112 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010113 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010114 shorten_fnames(TRUE);
10115 }
10116 else if (*dirnow != NUL
10117 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10118 {
10119 if (mch_chdir((char *)globaldir) == 0)
10120 shorten_fnames(TRUE);
10121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122
Bram Moolenaard9462e32011-04-11 21:35:11 +020010123 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
Bram Moolenaard9462e32011-04-11 21:35:11 +020010125 /* restore original dir */
10126 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010128 {
10129 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010130 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010131 shorten_fnames(TRUE);
10132 }
10133 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135 }
10136 else
10137 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010138 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10139 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 }
10141 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10142 == FAIL)
10143 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010144# ifdef FEAT_SEARCH_EXTRA
10145 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10146 failed = TRUE;
10147# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010148 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10149 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010150 if (eap->cmdidx == CMD_mksession)
10151 {
10152 if (put_line(fd, "unlet SessionLoad") == FAIL)
10153 failed = TRUE;
10154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 }
10156#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010157 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10158 failed = TRUE;
10159
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 failed |= fclose(fd);
10161
10162 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010163 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10165 else if (eap->cmdidx == CMD_mksession)
10166 {
10167 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010168 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169
Bram Moolenaard9462e32011-04-11 21:35:11 +020010170 tbuf = alloc(MAXPATHL);
10171 if (tbuf != NULL)
10172 {
10173 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10174 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10175 vim_free(tbuf);
10176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 }
10178#endif
10179#ifdef MKSESSION_NL
10180 mksession_nl = FALSE;
10181#endif
10182 }
10183
10184#ifdef FEAT_BROWSE
10185theend:
10186 vim_free(browseFile);
10187#endif
10188#ifdef FEAT_SESSION
10189 vim_free(viewFile);
10190#endif
10191}
10192
Bram Moolenaardf177f62005-02-22 08:39:57 +000010193#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10194 || defined(PROTO)
10195 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010196vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010197{
10198 if (vim_mkdir(name, prot) != 0)
10199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010200 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010201 return FAIL;
10202 }
10203 return OK;
10204}
10205#endif
10206
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207/*
10208 * Open a file for writing for an Ex command, with some checks.
10209 * Return file descriptor, or NULL on failure.
10210 */
10211 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010212open_exfile(
10213 char_u *fname,
10214 int forceit,
10215 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216{
10217 FILE *fd;
10218
10219#ifdef UNIX
10220 /* with Unix it is possible to open a directory */
10221 if (mch_isdir(fname))
10222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010223 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 return NULL;
10225 }
10226#endif
10227 if (!forceit && *mode != 'a' && vim_fexists(fname))
10228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010229 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 return NULL;
10231 }
10232
10233 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010234 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235
10236 return fd;
10237}
10238
10239/*
10240 * ":mark" and ":k".
10241 */
10242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010243ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245 pos_T pos;
10246
10247 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010248 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010250 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 else
10252 {
10253 pos = curwin->w_cursor; /* save curwin->w_cursor */
10254 curwin->w_cursor.lnum = eap->line2;
10255 beginline(BL_WHITE | BL_FIX);
10256 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010257 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10259 }
10260}
10261
10262/*
10263 * Update w_topline, w_leftcol and the cursor position.
10264 */
10265 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010266update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267{
10268 check_cursor(); /* put cursor on valid line */
10269 update_topline();
10270 if (!curwin->w_p_wrap)
10271 validate_cursor();
10272 update_curswant();
10273}
10274
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010276 * Save the current State and go to Normal mode.
10277 * Return TRUE if the typeahead could be saved.
10278 */
10279 int
10280save_current_state(save_state_T *sst)
10281{
10282 sst->save_msg_scroll = msg_scroll;
10283 sst->save_restart_edit = restart_edit;
10284 sst->save_msg_didout = msg_didout;
10285 sst->save_State = State;
10286 sst->save_insertmode = p_im;
10287 sst->save_finish_op = finish_op;
10288 sst->save_opcount = opcount;
10289
10290 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10291 restart_edit = 0; /* don't go to Insert mode */
10292 p_im = FALSE; /* don't use 'insertmode' */
10293
10294 /*
10295 * Save the current typeahead. This is required to allow using ":normal"
10296 * from an event handler and makes sure we don't hang when the argument
10297 * ends with half a command.
10298 */
10299 save_typeahead(&sst->tabuf);
10300 return sst->tabuf.typebuf_valid;
10301}
10302
10303 void
10304restore_current_state(save_state_T *sst)
10305{
10306 /* Restore the previous typeahead. */
10307 restore_typeahead(&sst->tabuf);
10308
10309 msg_scroll = sst->save_msg_scroll;
10310 restart_edit = sst->save_restart_edit;
10311 p_im = sst->save_insertmode;
10312 finish_op = sst->save_finish_op;
10313 opcount = sst->save_opcount;
10314 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10315
10316 /* Restore the state (needed when called from a function executed for
10317 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10318 State = sst->save_State;
10319#ifdef CURSOR_SHAPE
10320 ui_cursor_shape(); /* may show different cursor shape */
10321#endif
10322}
10323
10324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 * ":normal[!] {commands}": Execute normal mode commands.
10326 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010328ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010330 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331#ifdef FEAT_MBYTE
10332 char_u *arg = NULL;
10333 int l;
10334 char_u *p;
10335#endif
10336
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010337 if (ex_normal_lock > 0)
10338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010339 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010340 return;
10341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (ex_normal_busy >= p_mmd)
10343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010344 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 return;
10346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347
10348#ifdef FEAT_MBYTE
10349 /*
10350 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10351 * this for the K_SPECIAL leading byte, otherwise special keys will not
10352 * work.
10353 */
10354 if (has_mbyte)
10355 {
10356 int len = 0;
10357
10358 /* Count the number of characters to be escaped. */
10359 for (p = eap->arg; *p != NUL; ++p)
10360 {
10361# ifdef FEAT_GUI
10362 if (*p == CSI) /* leadbyte CSI */
10363 len += 2;
10364# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010365 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10367# ifdef FEAT_GUI
10368 || *p == CSI
10369# endif
10370 )
10371 len += 2;
10372 }
10373 if (len > 0)
10374 {
10375 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10376 if (arg != NULL)
10377 {
10378 len = 0;
10379 for (p = eap->arg; *p != NUL; ++p)
10380 {
10381 arg[len++] = *p;
10382# ifdef FEAT_GUI
10383 if (*p == CSI)
10384 {
10385 arg[len++] = KS_EXTRA;
10386 arg[len++] = (int)KE_CSI;
10387 }
10388# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010389 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 {
10391 arg[len++] = *++p;
10392 if (*p == K_SPECIAL)
10393 {
10394 arg[len++] = KS_SPECIAL;
10395 arg[len++] = KE_FILLER;
10396 }
10397# ifdef FEAT_GUI
10398 else if (*p == CSI)
10399 {
10400 arg[len++] = KS_EXTRA;
10401 arg[len++] = (int)KE_CSI;
10402 }
10403# endif
10404 }
10405 arg[len] = NUL;
10406 }
10407 }
10408 }
10409 }
10410#endif
10411
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010412 ++ex_normal_busy;
10413 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 {
10415 /*
10416 * Repeat the :normal command for each line in the range. When no
10417 * range given, execute it just once, without positioning the cursor
10418 * first.
10419 */
10420 do
10421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 if (eap->addr_count != 0)
10423 {
10424 curwin->w_cursor.lnum = eap->line1++;
10425 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010426 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 }
10428
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010429 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#ifdef FEAT_MBYTE
10431 arg != NULL ? arg :
10432#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010433 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 }
10435 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10436 }
10437
10438 /* Might not return to the main loop when in an event handler. */
10439 update_topline_cursor();
10440
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010441 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010443#ifdef FEAT_MOUSE
10444 setmouse();
10445#endif
10446#ifdef CURSOR_SHAPE
10447 ui_cursor_shape(); /* may show different cursor shape */
10448#endif
10449
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450#ifdef FEAT_MBYTE
10451 vim_free(arg);
10452#endif
10453}
10454
10455/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010456 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 */
10458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010459ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010461 if (eap->forceit)
10462 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010463 /* cursor line can be zero on startup */
10464 if (!curwin->w_cursor.lnum)
10465 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010466 coladvance((colnr_T)MAXCOL);
10467 curwin->w_curswant = MAXCOL;
10468 curwin->w_set_curswant = FALSE;
10469 }
10470
Bram Moolenaara40c5002005-01-09 21:16:21 +000010471 /* Ignore the command when already in Insert mode. Inserting an
10472 * expression register that invokes a function can do this. */
10473 if (State & INSERT)
10474 return;
10475
Bram Moolenaar64969662005-12-14 21:59:55 +000010476 if (eap->cmdidx == CMD_startinsert)
10477 restart_edit = 'a';
10478 else if (eap->cmdidx == CMD_startreplace)
10479 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010481 restart_edit = 'V';
10482
10483 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010485 if (eap->cmdidx == CMD_startinsert)
10486 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 curwin->w_curswant = 0; /* avoid MAXCOL */
10488 }
10489}
10490
10491/*
10492 * ":stopinsert"
10493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010495ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497 restart_edit = 0;
10498 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010499 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010502/*
10503 * Execute normal mode command "cmd".
10504 * "remap" can be REMAP_NONE or REMAP_YES.
10505 */
10506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010507exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010508{
Bram Moolenaar25281632016-01-21 23:32:32 +010010509 /* Stuff the argument into the typeahead buffer. */
10510 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010511 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010512}
Bram Moolenaar25281632016-01-21 23:32:32 +010010513
Bram Moolenaar25281632016-01-21 23:32:32 +010010514/*
10515 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010516 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010517 */
10518 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010519exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010520{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010521 oparg_T oa;
10522
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010523 clear_oparg(&oa);
10524 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010525 while ((!stuff_empty()
10526 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10527 || (use_vpeekc && vpeekc() != NUL))
10528 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010529 {
10530 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010531#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010532 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010533 && oa.op_type == OP_NOP && oa.regname == NUL
10534 && !VIsual_active)
10535 {
10536 /* If terminal_loop() returns OK we got a key that is handled
10537 * in Normal model. With FAIL we first need to position the
10538 * cursor and the screen needs to be redrawn. */
10539 if (terminal_loop(TRUE) == OK)
10540 normal_cmd(&oa, TRUE);
10541 }
10542 else
10543#endif
10544 /* execute a Normal mode cmd */
10545 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010546 }
10547}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010548
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549#ifdef FEAT_FIND_ID
10550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010551ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10554 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10555 (linenr_T)1, (linenr_T)MAXLNUM);
10556}
10557
Bram Moolenaar4033c552017-09-16 20:54:51 +020010558#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559/*
10560 * ":psearch"
10561 */
10562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010563ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565 g_do_tagpreview = p_pvh;
10566 ex_findpat(eap);
10567 g_do_tagpreview = 0;
10568}
10569#endif
10570
10571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010572ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
10574 int whole = TRUE;
10575 long n;
10576 char_u *p;
10577 int action;
10578
10579 switch (cmdnames[eap->cmdidx].cmd_name[2])
10580 {
10581 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10582 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10583 action = ACTION_GOTO;
10584 else
10585 action = ACTION_SHOW;
10586 break;
10587 case 'i': /* ":ilist" and ":dlist" */
10588 action = ACTION_SHOW_ALL;
10589 break;
10590 case 'u': /* ":ijump" and ":djump" */
10591 action = ACTION_GOTO;
10592 break;
10593 default: /* ":isplit" and ":dsplit" */
10594 action = ACTION_SPLIT;
10595 break;
10596 }
10597
10598 n = 1;
10599 if (vim_isdigit(*eap->arg)) /* get count */
10600 {
10601 n = getdigits(&eap->arg);
10602 eap->arg = skipwhite(eap->arg);
10603 }
10604 if (*eap->arg == '/') /* Match regexp, not just whole words */
10605 {
10606 whole = FALSE;
10607 ++eap->arg;
10608 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10609 if (*p)
10610 {
10611 *p++ = NUL;
10612 p = skipwhite(p);
10613
10614 /* Check for trailing illegal characters */
10615 if (!ends_excmd(*p))
10616 eap->errmsg = e_trailing;
10617 else
10618 eap->nextcmd = check_nextcmd(p);
10619 }
10620 }
10621 if (!eap->skip)
10622 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10623 whole, !eap->forceit,
10624 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10625 n, action, eap->line1, eap->line2);
10626}
10627#endif
10628
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629
Bram Moolenaar4033c552017-09-16 20:54:51 +020010630#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631/*
10632 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10633 */
10634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010635ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010637 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10639}
10640
10641/*
10642 * ":pedit"
10643 */
10644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010645ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647 win_T *curwin_save = curwin;
10648
10649 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010650 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010651 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 do_exedit(eap, NULL);
10653 keep_help_flag = FALSE;
10654 if (curwin != curwin_save && win_valid(curwin_save))
10655 {
10656 /* Return cursor to where we were */
10657 validate_cursor();
10658 redraw_later(VALID);
10659 win_enter(curwin_save, TRUE);
10660 }
10661 g_do_tagpreview = 0;
10662}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664
10665/*
10666 * ":stag", ":stselect" and ":stjump".
10667 */
10668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010669ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
10671 postponed_split = -1;
10672 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010673 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10675 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010676 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678
10679/*
10680 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10681 */
10682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010683ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
10685 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10686}
10687
10688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010689ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
10691 int cmd;
10692
10693 switch (name[1])
10694 {
10695 case 'j': cmd = DT_JUMP; /* ":tjump" */
10696 break;
10697 case 's': cmd = DT_SELECT; /* ":tselect" */
10698 break;
10699 case 'p': cmd = DT_PREV; /* ":tprevious" */
10700 break;
10701 case 'N': cmd = DT_PREV; /* ":tNext" */
10702 break;
10703 case 'n': cmd = DT_NEXT; /* ":tnext" */
10704 break;
10705 case 'o': cmd = DT_POP; /* ":pop" */
10706 break;
10707 case 'f': /* ":tfirst" */
10708 case 'r': cmd = DT_FIRST; /* ":trewind" */
10709 break;
10710 case 'l': cmd = DT_LAST; /* ":tlast" */
10711 break;
10712 default: /* ":tag" */
10713#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010714 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010716 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 return;
10718 }
10719#endif
10720 cmd = DT_TAG;
10721 break;
10722 }
10723
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010724 if (name[0] == 'l')
10725 {
10726#ifndef FEAT_QUICKFIX
10727 ex_ni(eap);
10728 return;
10729#else
10730 cmd = DT_LTAG;
10731#endif
10732 }
10733
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10735 eap->forceit, TRUE);
10736}
10737
10738/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010739 * Check "str" for starting with a special cmdline variable.
10740 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10741 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10742 */
10743 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010744find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010745{
10746 int len;
10747 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010748 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010749 "%",
10750#define SPEC_PERC 0
10751 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010752#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010753 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010754#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010755 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010756#define SPEC_CCWORD (SPEC_CWORD + 1)
10757 "<cexpr>", /* expr under cursor */
10758#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010759 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010760#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010761 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010762#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010763 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010764#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010765 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010766#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010767 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010768#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010769 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010770#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010771 "<sflnum>", /* script file line number */
10772#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010773#ifdef FEAT_CLIENTSERVER
10774 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010775# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010776#endif
10777 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010778
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010779 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010780 {
10781 len = (int)STRLEN(spec_str[i]);
10782 if (STRNCMP(src, spec_str[i], len) == 0)
10783 {
10784 *usedlen = len;
10785 return i;
10786 }
10787 }
10788 return -1;
10789}
10790
10791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 * Evaluate cmdline variables.
10793 *
10794 * change '%' to curbuf->b_ffname
10795 * '#' to curwin->w_altfile
10796 * '<cword>' to word under the cursor
10797 * '<cWORD>' to WORD under the cursor
10798 * '<cfile>' to path name under the cursor
10799 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010800 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 * '<afile>' to file name for autocommand
10802 * '<abuf>' to buffer number for autocommand
10803 * '<amatch>' to matching name for autocommand
10804 *
10805 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10806 * "" for error without a message) and NULL is returned.
10807 * Returns an allocated string if a valid match was found.
10808 * Returns NULL if no match was found. "usedlen" then still contains the
10809 * number of characters to skip.
10810 */
10811 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010812eval_vars(
10813 char_u *src, /* pointer into commandline */
10814 char_u *srcstart, /* beginning of valid memory for src */
10815 int *usedlen, /* characters after src that are used */
10816 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010817 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010818 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010819 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 int i;
10822 char_u *s;
10823 char_u *result;
10824 char_u *resultbuf = NULL;
10825 int resultlen;
10826 buf_T *buf;
10827 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10828 int spec_idx;
10829#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010830 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 int skip_mod = FALSE;
10832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834
10835 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010836 if (escaped != NULL)
10837 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838
10839 /*
10840 * Check if there is something to do.
10841 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010842 spec_idx = find_cmdline_var(src, usedlen);
10843 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 {
10845 *usedlen = 1;
10846 return NULL;
10847 }
10848
10849 /*
10850 * Skip when preceded with a backslash "\%" and "\#".
10851 * Note: In "\\%" the % is also not recognized!
10852 */
10853 if (src > srcstart && src[-1] == '\\')
10854 {
10855 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010856 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 return NULL;
10858 }
10859
10860 /*
10861 * word or WORD under cursor
10862 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010863 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10864 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010866 resultlen = find_ident_under_cursor(&result,
10867 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10868 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10869 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 if (resultlen == 0)
10871 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010872 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 return NULL;
10874 }
10875 }
10876
10877 /*
10878 * '#': Alternate file name
10879 * '%': Current file name
10880 * File name under the cursor
10881 * File name for autocommand
10882 * and following modifiers
10883 */
10884 else
10885 {
10886 switch (spec_idx)
10887 {
10888 case SPEC_PERC: /* '%': current file */
10889 if (curbuf->b_fname == NULL)
10890 {
10891 result = (char_u *)"";
10892 valid = 0; /* Must have ":p:h" to be valid */
10893 }
10894 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010897#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010898 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010899#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 break;
10902
10903 case SPEC_HASH: /* '#' or "#99": alternate file */
10904 if (src[1] == '#') /* "##": the argument list */
10905 {
10906 result = arg_all();
10907 resultbuf = result;
10908 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010909 if (escaped != NULL)
10910 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef FEAT_MODIFY_FNAME
10912 skip_mod = TRUE;
10913#endif
10914 break;
10915 }
10916 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010917 if (*s == '<') /* "#<99" uses v:oldfiles */
10918 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010920 if (s == src + 2 && src[1] == '-')
10921 /* just a minus sign, don't skip over it */
10922 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 *usedlen = (int)(s - src); /* length of what we expand */
10924
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010925 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010927 if (*usedlen < 2)
10928 {
10929 /* Should we give an error message for #<text? */
10930 *usedlen = 1;
10931 return NULL;
10932 }
10933#ifdef FEAT_EVAL
10934 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10935 (long)i);
10936 if (result == NULL)
10937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010938 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010939 return NULL;
10940 }
10941#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +010010942 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 }
10946 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010947 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010948 if (i == 0 && src[1] == '<' && *usedlen > 1)
10949 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010950 buf = buflist_findnr(i);
10951 if (buf == NULL)
10952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010953 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010954 return NULL;
10955 }
10956 if (lnump != NULL)
10957 *lnump = ECMD_LAST;
10958 if (buf->b_fname == NULL)
10959 {
10960 result = (char_u *)"";
10961 valid = 0; /* Must have ":p:h" to be valid */
10962 }
10963 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010964 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010965 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010966#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010967 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010968#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010969 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 break;
10972
10973#ifdef FEAT_SEARCHPATH
10974 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010975 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 if (result == NULL)
10977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010978 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 return NULL;
10980 }
10981 resultbuf = result; /* remember allocated string */
10982 break;
10983#endif
10984
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 case SPEC_AFILE: /* file name for autocommand */
10986 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010987 if (result != NULL && !autocmd_fname_full)
10988 {
10989 /* Still need to turn the fname into a full path. It is
10990 * postponed to avoid a delay when <afile> is not used. */
10991 autocmd_fname_full = TRUE;
10992 result = FullName_save(autocmd_fname, FALSE);
10993 vim_free(autocmd_fname);
10994 autocmd_fname = result;
10995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 if (result == NULL)
10997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010998 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 return NULL;
11000 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000011001 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 break;
11003
11004 case SPEC_ABUF: /* buffer number for autocommand */
11005 if (autocmd_bufnr <= 0)
11006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011007 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 return NULL;
11009 }
11010 sprintf((char *)strbuf, "%d", autocmd_bufnr);
11011 result = strbuf;
11012 break;
11013
11014 case SPEC_AMATCH: /* match name for autocommand */
11015 result = autocmd_match;
11016 if (result == NULL)
11017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011018 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 return NULL;
11020 }
11021 break;
11022
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 case SPEC_SFILE: /* file name for ":so" command */
11024 result = sourcing_name;
11025 if (result == NULL)
11026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011027 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 return NULL;
11029 }
11030 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011031
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011032 case SPEC_SLNUM: /* line in file for ":so" command */
11033 if (sourcing_name == NULL || sourcing_lnum == 0)
11034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011035 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011036 return NULL;
11037 }
11038 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11039 result = strbuf;
11040 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011041
11042#ifdef FEAT_EVAL
11043 case SPEC_SFLNUM: /* line in script file */
11044 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011046 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011047 return NULL;
11048 }
11049 sprintf((char *)strbuf, "%ld",
11050 (long)(current_sctx.sc_lnum + sourcing_lnum));
11051 result = strbuf;
11052 break;
11053#endif
11054
11055#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011057 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11058 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 result = strbuf;
11060 break;
11061#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011062
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011063 default:
11064 result = (char_u *)""; /* avoid gcc warning */
11065 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 }
11067
11068 resultlen = (int)STRLEN(result); /* length of new string */
11069 if (src[*usedlen] == '<') /* remove the file name extension */
11070 {
11071 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 resultlen = (int)(s - result);
11074 }
11075#ifdef FEAT_MODIFY_FNAME
11076 else if (!skip_mod)
11077 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011078 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 &resultlen);
11080 if (result == NULL)
11081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011082 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 return NULL;
11084 }
11085 }
11086#endif
11087 }
11088
11089 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11090 {
11091 if (valid != VALID_HEAD + VALID_PATH)
11092 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011093 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011095 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 result = NULL;
11097 }
11098 else
11099 result = vim_strnsave(result, resultlen);
11100 vim_free(resultbuf);
11101 return result;
11102}
11103
11104/*
11105 * Concatenate all files in the argument list, separated by spaces, and return
11106 * it in one allocated string.
11107 * Spaces and backslashes in the file names are escaped with a backslash.
11108 * Returns NULL when out of memory.
11109 */
11110 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011111arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
11113 int len;
11114 int idx;
11115 char_u *retval = NULL;
11116 char_u *p;
11117
11118 /*
11119 * Do this loop two times:
11120 * first time: compute the total length
11121 * second time: concatenate the names
11122 */
11123 for (;;)
11124 {
11125 len = 0;
11126 for (idx = 0; idx < ARGCOUNT; ++idx)
11127 {
11128 p = alist_name(&ARGLIST[idx]);
11129 if (p != NULL)
11130 {
11131 if (len > 0)
11132 {
11133 /* insert a space in between names */
11134 if (retval != NULL)
11135 retval[len] = ' ';
11136 ++len;
11137 }
11138 for ( ; *p != NUL; ++p)
11139 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011140 if (*p == ' '
11141#ifndef BACKSLASH_IN_FILENAME
11142 || *p == '\\'
11143#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011144 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 {
11146 /* insert a backslash */
11147 if (retval != NULL)
11148 retval[len] = '\\';
11149 ++len;
11150 }
11151 if (retval != NULL)
11152 retval[len] = *p;
11153 ++len;
11154 }
11155 }
11156 }
11157
11158 /* second time: break here */
11159 if (retval != NULL)
11160 {
11161 retval[len] = NUL;
11162 break;
11163 }
11164
11165 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011166 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 if (retval == NULL)
11168 break;
11169 }
11170
11171 return retval;
11172}
11173
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174/*
11175 * Expand the <sfile> string in "arg".
11176 *
11177 * Returns an allocated string, or NULL for any error.
11178 */
11179 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011180expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011182 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 int len;
11184 char_u *result;
11185 char_u *newres;
11186 char_u *repl;
11187 int srclen;
11188 char_u *p;
11189
11190 result = vim_strsave(arg);
11191 if (result == NULL)
11192 return NULL;
11193
11194 for (p = result; *p; )
11195 {
11196 if (STRNCMP(p, "<sfile>", 7) != 0)
11197 ++p;
11198 else
11199 {
11200 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011201 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 if (errormsg != NULL)
11203 {
11204 if (*errormsg)
11205 emsg(errormsg);
11206 vim_free(result);
11207 return NULL;
11208 }
11209 if (repl == NULL) /* no match (cannot happen) */
11210 {
11211 p += srclen;
11212 continue;
11213 }
11214 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11215 newres = alloc(len);
11216 if (newres == NULL)
11217 {
11218 vim_free(repl);
11219 vim_free(result);
11220 return NULL;
11221 }
11222 mch_memmove(newres, result, (size_t)(p - result));
11223 STRCPY(newres + (p - result), repl);
11224 len = (int)STRLEN(newres);
11225 STRCAT(newres, p + srclen);
11226 vim_free(repl);
11227 vim_free(result);
11228 result = newres;
11229 p = newres + len; /* continue after the match */
11230 }
11231 }
11232
11233 return result;
11234}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235
11236#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011237static int ses_winsizes(FILE *fd, int restore_size,
11238 win_T *tab_firstwin);
11239static int ses_win_rec(FILE *fd, frame_T *fr);
11240static frame_T *ses_skipframe(frame_T *fr);
11241static int ses_do_frame(frame_T *fr);
11242static int ses_do_win(win_T *wp);
11243static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11244static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011245static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246
11247/*
11248 * Write openfile commands for the current buffers to an .exrc file.
11249 * Return FAIL on error, OK otherwise.
11250 */
11251 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011252makeopens(
11253 FILE *fd,
11254 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255{
11256 buf_T *buf;
11257 int only_save_windows = TRUE;
11258 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 int restore_size = TRUE;
11260 win_T *wp;
11261 char_u *sname;
11262 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011263 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011264 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011265 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011266 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011267 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011268 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269
11270 if (ssop_flags & SSOP_BUFFERS)
11271 only_save_windows = FALSE; /* Save ALL buffers */
11272
11273 /*
11274 * Begin by setting the this_session variable, and then other
11275 * sessionable variables.
11276 */
11277#ifdef FEAT_EVAL
11278 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11279 return FAIL;
11280 if (ssop_flags & SSOP_GLOBALS)
11281 if (store_session_globals(fd) == FAIL)
11282 return FAIL;
11283#endif
11284
11285 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011286 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 */
11288 if (put_line(fd, "silent only") == FAIL)
11289 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011290 if ((ssop_flags & SSOP_TABPAGES)
11291 && put_line(fd, "silent tabonly") == FAIL)
11292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
11294 /*
11295 * Now a :cd command to the session directory or the current directory
11296 */
11297 if (ssop_flags & SSOP_SESDIR)
11298 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011299 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11300 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 return FAIL;
11302 }
11303 else if (ssop_flags & SSOP_CURDIR)
11304 {
11305 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11306 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011307 || fputs("cd ", fd) < 0
11308 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11309 || put_eol(fd) == FAIL)
11310 {
11311 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 vim_free(sname);
11315 }
11316
11317 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011318 * If there is an empty, unnamed buffer we will wipe it out later.
11319 * Remember the buffer number.
11320 */
11321 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11322 return FAIL;
11323 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11324 return FAIL;
11325 if (put_line(fd, "endif") == FAIL)
11326 return FAIL;
11327
11328 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 * Now save the current files, current buffer first.
11330 */
11331 if (put_line(fd, "set shortmess=aoO") == FAIL)
11332 return FAIL;
11333
11334 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011335 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 {
11337 if (!(only_save_windows && buf->b_nwindows == 0)
11338 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011339#ifdef FEAT_TERMINAL
11340 /* skip terminal buffers: finished ones are not useful, others
11341 * will be resurrected and result in a new buffer */
11342 && !bt_terminal(buf)
11343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 && buf->b_fname != NULL
11345 && buf->b_p_bl)
11346 {
11347 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11348 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011349 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 return FAIL;
11351 }
11352 }
11353
11354 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011355 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11357 return FAIL;
11358
11359 if (ssop_flags & SSOP_RESIZE)
11360 {
11361 /* Note: after the restore we still check it worked!*/
11362 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11363 || put_eol(fd) == FAIL)
11364 return FAIL;
11365 }
11366
11367#ifdef FEAT_GUI
11368 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11369 {
11370 int x, y;
11371
11372 if (gui_mch_get_winpos(&x, &y) == OK)
11373 {
11374 /* Note: after the restore we still check it worked!*/
11375 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11376 return FAIL;
11377 }
11378 }
11379#endif
11380
11381 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011382 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11383 * will be displayed when creating the next tab. That resizes the windows
11384 * in the first tab, which may cause problems. Set 'showtabline' to 2
11385 * temporarily to avoid that.
11386 */
11387 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11388 {
11389 if (put_line(fd, "set stal=2") == FAIL)
11390 return FAIL;
11391 restore_stal = TRUE;
11392 }
11393
11394 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011395 * May repeat putting Windows for each tab, when "tabpages" is in
11396 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011397 * Don't use goto_tabpage(), it may change directory and trigger
11398 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011400 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011401 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011402 if ((ssop_flags & SSOP_TABPAGES))
11403 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011404 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011405
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011406 // Similar to ses_win_rec() below, populate the tab pages first so
11407 // later local options won't be copied to the new tabs.
11408 FOR_ALL_TABPAGES(tp)
11409 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011410 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +010011411 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011412 return FAIL;
11413 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011414 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011416 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011417 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011418
Bram Moolenaar18144c82006-04-12 21:52:12 +000011419 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011421 tabpage_T *tp = find_tabpage(tabnr);
11422
11423 if (tp == NULL)
11424 break; /* done all tab pages */
11425 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011426 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011427 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011428 tab_topframe = topframe;
11429 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011430 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011431 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011432 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011433 tab_topframe = tp->tp_topframe;
11434 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011435 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011436 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438
Bram Moolenaar18144c82006-04-12 21:52:12 +000011439 /*
11440 * Before creating the window layout, try loading one file. If this
11441 * is aborted we don't end up with a number of useless windows.
11442 * This may have side effects! (e.g., compressed or network file).
11443 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011444 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011445 {
11446 if (ses_do_win(wp)
11447 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011448 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011449#ifdef FEAT_QUICKFIX
11450 && !bt_nofile(wp->w_buffer)
11451#endif
11452 )
11453 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011454 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11455 return FAIL;
11456 need_tabnext = FALSE;
11457
11458 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011459 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11460 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011461 return FAIL;
11462 if (!wp->w_arg_idx_invalid)
11463 edited_win = wp;
11464 break;
11465 }
11466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011468 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011469 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011470 return FAIL;
11471
Bram Moolenaar18144c82006-04-12 21:52:12 +000011472 /*
11473 * Save current window layout.
11474 */
11475 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011477 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011479 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11480 return FAIL;
11481 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11482 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483
Bram Moolenaar18144c82006-04-12 21:52:12 +000011484 /*
11485 * Check if window sizes can be restored (no windows omitted).
11486 * Remember the window number of the current window after restoring.
11487 */
11488 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011489 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011490 {
11491 if (ses_do_win(wp))
11492 ++nr;
11493 else
11494 restore_size = FALSE;
11495 if (curwin == wp)
11496 cnr = nr;
11497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498
Bram Moolenaar18144c82006-04-12 21:52:12 +000011499 /* Go to the first window. */
11500 if (put_line(fd, "wincmd t") == FAIL)
11501 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011502
Bram Moolenaar18144c82006-04-12 21:52:12 +000011503 /*
11504 * If more than one window, see if sizes can be restored.
11505 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11506 * resized when moving between windows.
11507 * Do this before restoring the view, so that the topline and the
11508 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011509 * winminheight and winminwidth need to be set to avoid an error if the
11510 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011511 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011512 if (put_line(fd, "set winminheight=0") == FAIL
11513 || put_line(fd, "set winheight=1") == FAIL
11514 || put_line(fd, "set winminwidth=0") == FAIL
11515 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011516 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011517 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011518 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519
Bram Moolenaar18144c82006-04-12 21:52:12 +000011520 /*
11521 * Restore the view of the window (options, file, cursor, etc.).
11522 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011523 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011524 {
11525 if (!ses_do_win(wp))
11526 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011527 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11528 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011529 return FAIL;
11530 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11531 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011532 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011533 }
11534
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011535 /* The argument index in the first tab page is zero, need to set it in
11536 * each window. For further tab pages it's the window where we do
11537 * "tabedit". */
11538 cur_arg_idx = next_arg_idx;
11539
Bram Moolenaar18144c82006-04-12 21:52:12 +000011540 /*
11541 * Restore cursor to the current window if it's not the first one.
11542 */
11543 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11544 || put_eol(fd) == FAIL))
11545 return FAIL;
11546
11547 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011548 * Restore window sizes again after jumping around in windows, because
11549 * the current window has a minimum size while others may not.
11550 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011551 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011552 return FAIL;
11553
Bram Moolenaar18144c82006-04-12 21:52:12 +000011554 /* Don't continue in another tab page when doing only the current one
11555 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011556 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011557 break;
11558 }
11559
11560 if (ssop_flags & SSOP_TABPAGES)
11561 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011562 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11563 || put_eol(fd) == FAIL)
11564 return FAIL;
11565 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011566 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11567 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011568
Bram Moolenaar9c102382006-05-03 21:26:49 +000011569 /*
11570 * Wipe out an empty unnamed buffer we started in.
11571 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011572 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011573 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011574 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011575 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011576 return FAIL;
11577 if (put_line(fd, "endif") == FAIL)
11578 return FAIL;
11579 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11580 return FAIL;
11581
11582 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11583 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11584 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11585 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011586 /* Re-apply 'winminheight' and 'winminwidth'. */
11587 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11588 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590
11591 /*
11592 * Lastly, execute the x.vim file if it exists.
11593 */
11594 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11595 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011596 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 || put_line(fd, "endif") == FAIL)
11598 return FAIL;
11599
11600 return OK;
11601}
11602
11603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011604ses_winsizes(
11605 FILE *fd,
11606 int restore_size,
11607 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
11609 int n = 0;
11610 win_T *wp;
11611
11612 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11613 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011614 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 {
11616 if (!ses_do_win(wp))
11617 continue;
11618 ++n;
11619
11620 /* restore height when not full height */
11621 if (wp->w_height + wp->w_status_height < topframe->fr_height
11622 && (fprintf(fd,
11623 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11624 n, (long)wp->w_height, Rows / 2, Rows) < 0
11625 || put_eol(fd) == FAIL))
11626 return FAIL;
11627
11628 /* restore width when not full width */
11629 if (wp->w_width < Columns && (fprintf(fd,
11630 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11631 n, (long)wp->w_width, Columns / 2, Columns) < 0
11632 || put_eol(fd) == FAIL))
11633 return FAIL;
11634 }
11635 }
11636 else
11637 {
11638 /* Just equalise window sizes */
11639 if (put_line(fd, "wincmd =") == FAIL)
11640 return FAIL;
11641 }
11642 return OK;
11643}
11644
11645/*
11646 * Write commands to "fd" to recursively create windows for frame "fr",
11647 * horizontally and vertically split.
11648 * After the commands the last window in the frame is the current window.
11649 * Returns FAIL when writing the commands to "fd" fails.
11650 */
11651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011652ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653{
11654 frame_T *frc;
11655 int count = 0;
11656
11657 if (fr->fr_layout != FR_LEAF)
11658 {
11659 /* Find first frame that's not skipped and then create a window for
11660 * each following one (first frame is already there). */
11661 frc = ses_skipframe(fr->fr_child);
11662 if (frc != NULL)
11663 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11664 {
11665 /* Make window as big as possible so that we have lots of room
11666 * to split. */
11667 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11668 || put_line(fd, fr->fr_layout == FR_COL
11669 ? "split" : "vsplit") == FAIL)
11670 return FAIL;
11671 ++count;
11672 }
11673
11674 /* Go back to the first window. */
11675 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11676 ? "%dwincmd k" : "%dwincmd h", count) < 0
11677 || put_eol(fd) == FAIL))
11678 return FAIL;
11679
11680 /* Recursively create frames/windows in each window of this column or
11681 * row. */
11682 frc = ses_skipframe(fr->fr_child);
11683 while (frc != NULL)
11684 {
11685 ses_win_rec(fd, frc);
11686 frc = ses_skipframe(frc->fr_next);
11687 /* Go to next window. */
11688 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11689 return FAIL;
11690 }
11691 }
11692 return OK;
11693}
11694
11695/*
11696 * Skip frames that don't contain windows we want to save in the Session.
11697 * Returns NULL when there none.
11698 */
11699 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011700ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701{
11702 frame_T *frc;
11703
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011704 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 if (ses_do_frame(frc))
11706 break;
11707 return frc;
11708}
11709
11710/*
11711 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11712 * the Session.
11713 */
11714 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011715ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 frame_T *frc;
11718
11719 if (fr->fr_layout == FR_LEAF)
11720 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011721 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 if (ses_do_frame(frc))
11723 return TRUE;
11724 return FALSE;
11725}
11726
11727/*
11728 * Return non-zero if window "wp" is to be stored in the Session.
11729 */
11730 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011731ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011733#ifdef FEAT_TERMINAL
11734 if (bt_terminal(wp->w_buffer))
11735 return !term_is_finished(wp->w_buffer)
11736 && (ssop_flags & SSOP_TERMINAL)
11737 && term_should_restore(wp->w_buffer);
11738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 if (wp->w_buffer->b_fname == NULL
11740#ifdef FEAT_QUICKFIX
11741 /* When 'buftype' is "nofile" can't restore the window contents. */
11742 || bt_nofile(wp->w_buffer)
11743#endif
11744 )
11745 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011746 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 return (ssop_flags & SSOP_HELP);
11748 return TRUE;
11749}
11750
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011751 static int
11752put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11753{
11754 int r;
11755
11756 if (wp->w_curswant == MAXCOL)
11757 r = fprintf(fd, "%snormal! $", spaces);
11758 else
11759 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11760 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11761}
11762
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763/*
11764 * Write commands to "fd" to restore the view of a window.
11765 * Caller must make sure 'scrolloff' is zero.
11766 */
11767 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011768put_view(
11769 FILE *fd,
11770 win_T *wp,
11771 int add_edit, /* add ":edit" command to view */
11772 unsigned *flagp, /* vop_flags or ssop_flags */
11773 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011774 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775{
11776 win_T *save_curwin;
11777 int f;
11778 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011779 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780
11781 /* Always restore cursor position for ":mksession". For ":mkview" only
11782 * when 'viewoptions' contains "cursor". */
11783 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11784
11785 /*
11786 * Local argument list.
11787 */
11788 if (wp->w_alist == &global_alist)
11789 {
11790 if (put_line(fd, "argglobal") == FAIL)
11791 return FAIL;
11792 }
11793 else
11794 {
11795 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11796 flagp == &vop_flags
11797 || !(*flagp & SSOP_CURDIR)
11798 || wp->w_localdir != NULL, flagp) == FAIL)
11799 return FAIL;
11800 }
11801
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011802 /* Only when part of a session: restore the argument index. Some
11803 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011804 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011805 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011807 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 || put_eol(fd) == FAIL)
11809 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011810 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 }
11812
11813 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011814 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011816# ifdef FEAT_TERMINAL
11817 if (bt_terminal(wp->w_buffer))
11818 {
11819 if (term_write_session(fd, wp) == FAIL)
11820 return FAIL;
11821 }
11822 else
11823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 /*
11825 * Load the file.
11826 */
11827 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011828# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 )
11832 {
11833 /*
11834 * Editing a file in this buffer: use ":edit file".
11835 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011836 *
11837 * Note, if a buffer for that file already exists, use :badd to
11838 * edit that buffer, to not lose folding information (:edit resets
11839 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011841 if (fputs("if bufexists('", fd) < 0
11842 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11843 || fputs("') | buffer ", fd) < 0
11844 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11845 || fputs(" | else | edit ", fd) < 0
11846 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11847 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011848 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 return FAIL;
11850 }
11851 else
11852 {
11853 /* No file in this buffer, just make it empty. */
11854 if (put_line(fd, "enew") == FAIL)
11855 return FAIL;
11856#ifdef FEAT_QUICKFIX
11857 if (wp->w_buffer->b_ffname != NULL)
11858 {
11859 /* The buffer does have a name, but it's not a file name. */
11860 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011861 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 return FAIL;
11863 }
11864#endif
11865 do_cursor = FALSE;
11866 }
11867 }
11868
11869 /*
11870 * Local mappings and abbreviations.
11871 */
11872 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11873 && makemap(fd, wp->w_buffer) == FAIL)
11874 return FAIL;
11875
11876 /*
11877 * Local options. Need to go to the window temporarily.
11878 * Store only local values when using ":mkview" and when ":mksession" is
11879 * used and 'sessionoptions' doesn't include "options".
11880 * Some folding options are always stored when "folds" is included,
11881 * otherwise the folds would not be restored correctly.
11882 */
11883 save_curwin = curwin;
11884 curwin = wp;
11885 curbuf = curwin->w_buffer;
11886 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11887 f = makeset(fd, OPT_LOCAL,
11888 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11889#ifdef FEAT_FOLDING
11890 else if (*flagp & SSOP_FOLDS)
11891 f = makefoldset(fd);
11892#endif
11893 else
11894 f = OK;
11895 curwin = save_curwin;
11896 curbuf = curwin->w_buffer;
11897 if (f == FAIL)
11898 return FAIL;
11899
11900#ifdef FEAT_FOLDING
11901 /*
11902 * Save Folds when 'buftype' is empty and for help files.
11903 */
11904 if ((*flagp & SSOP_FOLDS)
11905 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011906 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 {
11908 if (put_folds(fd, wp) == FAIL)
11909 return FAIL;
11910 }
11911#endif
11912
11913 /*
11914 * Set the cursor after creating folds, since that moves the cursor.
11915 */
11916 if (do_cursor)
11917 {
11918
11919 /* Restore the cursor line in the file and relatively in the
11920 * window. Don't use "G", it changes the jumplist. */
11921 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11922 (long)wp->w_cursor.lnum,
11923 (long)(wp->w_cursor.lnum - wp->w_topline),
11924 (long)wp->w_height / 2, (long)wp->w_height) < 0
11925 || put_eol(fd) == FAIL
11926 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11927 || put_line(fd, "exe s:l") == FAIL
11928 || put_line(fd, "normal! zt") == FAIL
11929 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11930 || put_eol(fd) == FAIL)
11931 return FAIL;
11932 /* Restore the cursor column and left offset when not wrapping. */
11933 if (wp->w_cursor.col == 0)
11934 {
11935 if (put_line(fd, "normal! 0") == FAIL)
11936 return FAIL;
11937 }
11938 else
11939 {
11940 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11941 {
11942 if (fprintf(fd,
11943 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011944 (long)wp->w_virtcol + 1,
11945 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 (long)wp->w_width / 2, (long)wp->w_width) < 0
11947 || put_eol(fd) == FAIL
11948 || put_line(fd, "if s:c > 0") == FAIL
11949 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011950 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11951 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 || put_eol(fd) == FAIL
11953 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011954 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 || put_line(fd, "endif") == FAIL)
11956 return FAIL;
11957 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011958 else if (put_view_curpos(fd, wp, "") == FAIL)
11959 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 }
11961 }
11962
11963 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011964 * Local directory, if the current flag is not view options or the "curdir"
11965 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011967 if (wp->w_localdir != NULL
11968 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 {
11970 if (fputs("lcd ", fd) < 0
11971 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11972 || put_eol(fd) == FAIL)
11973 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011974 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 }
11976
11977 return OK;
11978}
11979
11980/*
11981 * Write an argument list to the session file.
11982 * Returns FAIL if writing fails.
11983 */
11984 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011985ses_arglist(
11986 FILE *fd,
11987 char *cmd,
11988 garray_T *gap,
11989 int fullname, /* TRUE: use full path name */
11990 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991{
11992 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011993 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 char_u *s;
11995
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011996 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11997 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010011998 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 return FAIL;
12000 for (i = 0; i < gap->ga_len; ++i)
12001 {
12002 /* NULL file names are skipped (only happens when out of memory). */
12003 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
12004 if (s != NULL)
12005 {
12006 if (fullname)
12007 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012008 buf = alloc(MAXPATHL);
12009 if (buf != NULL)
12010 {
12011 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
12012 s = buf;
12013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010012015 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012016 || ses_put_fname(fd, s, flagp) == FAIL
12017 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020012018 {
12019 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012021 }
12022 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 }
12024 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012025 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026}
12027
12028/*
12029 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012030 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 * Returns FAIL if writing fails.
12032 */
12033 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012034ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035{
12036 char_u *name;
12037
12038 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012039 * the session file will be sourced.
12040 * Don't do this for ":mkview", we don't know the current directory.
12041 * Don't do this after ":lcd", we don't keep track of what the current
12042 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 if (buf->b_sfname != NULL
12044 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012045 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012046#ifdef FEAT_AUTOCHDIR
12047 && !p_acd
12048#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012049 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 name = buf->b_sfname;
12051 else
12052 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012053 if (ses_put_fname(fd, name, flagp) == FAIL
12054 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 return FAIL;
12056 return OK;
12057}
12058
12059/*
12060 * Write a file name to the session file.
12061 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12062 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012063 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 */
12065 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012066ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
12068 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012069 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071
12072 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012073 if (sname == NULL)
12074 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012076 if (*flagp & SSOP_SLASH)
12077 {
12078 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012079 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012080 if (*p == '\\')
12081 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012083
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012084 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012085 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012087 if (p == NULL)
12088 return FAIL;
12089
12090 /* write the result */
12091 if (fputs((char *)p, fd) < 0)
12092 retval = FAIL;
12093
12094 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 return retval;
12096}
12097
12098/*
12099 * ":loadview [nr]"
12100 */
12101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012102ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103{
12104 char_u *fname;
12105
12106 fname = get_view_file(*eap->arg);
12107 if (fname != NULL)
12108 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012109 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 vim_free(fname);
12111 }
12112}
12113
12114/*
12115 * Get the name of the view file for the current buffer.
12116 */
12117 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012118get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119{
12120 int len = 0;
12121 char_u *p, *s;
12122 char_u *retval;
12123 char_u *sname;
12124
12125 if (curbuf->b_ffname == NULL)
12126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012127 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 return NULL;
12129 }
12130 sname = home_replace_save(NULL, curbuf->b_ffname);
12131 if (sname == NULL)
12132 return NULL;
12133
12134 /*
12135 * We want a file name without separators, because we're not going to make
12136 * a directory.
12137 * "normal" path separator -> "=+"
12138 * "=" -> "=="
12139 * ":" path separator -> "=-"
12140 */
12141 for (p = sname; *p; ++p)
12142 if (*p == '=' || vim_ispathsep(*p))
12143 ++len;
12144 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12145 if (retval != NULL)
12146 {
12147 STRCPY(retval, p_vdir);
12148 add_pathsep(retval);
12149 s = retval + STRLEN(retval);
12150 for (p = sname; *p; ++p)
12151 {
12152 if (*p == '=')
12153 {
12154 *s++ = '=';
12155 *s++ = '=';
12156 }
12157 else if (vim_ispathsep(*p))
12158 {
12159 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012160#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 if (*p == ':')
12162 *s++ = '-';
12163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012165 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 }
12167 else
12168 *s++ = *p;
12169 }
12170 *s++ = '=';
12171 *s++ = c;
12172 STRCPY(s, ".vim");
12173 }
12174
12175 vim_free(sname);
12176 return retval;
12177}
12178
12179#endif /* FEAT_SESSION */
12180
12181/*
12182 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12183 * Return FAIL for a write error.
12184 */
12185 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012186put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187{
12188 if (
12189#ifdef USE_CRNL
12190 (
12191# ifdef MKSESSION_NL
12192 !mksession_nl &&
12193# endif
12194 (putc('\r', fd) < 0)) ||
12195#endif
12196 (putc('\n', fd) < 0))
12197 return FAIL;
12198 return OK;
12199}
12200
12201/*
12202 * Write a line to "fd".
12203 * Return FAIL for a write error.
12204 */
12205 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012206put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207{
12208 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12209 return FAIL;
12210 return OK;
12211}
12212
12213#ifdef FEAT_VIMINFO
12214/*
12215 * ":rviminfo" and ":wviminfo".
12216 */
12217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012218ex_viminfo(
12219 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220{
12221 char_u *save_viminfo;
12222
12223 save_viminfo = p_viminfo;
12224 if (*p_viminfo == NUL)
12225 p_viminfo = (char_u *)"'100";
12226 if (eap->cmdidx == CMD_rviminfo)
12227 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012228 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12229 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012230 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 }
12232 else
12233 write_viminfo(eap->arg, eap->forceit);
12234 p_viminfo = save_viminfo;
12235}
12236#endif
12237
12238#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012239/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012240 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012241 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012244dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 if (fname == NULL)
12247 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012248 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249}
12250#endif
12251
12252/*
12253 * ":behave {mswin,xterm}"
12254 */
12255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012256ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257{
12258 if (STRCMP(eap->arg, "mswin") == 0)
12259 {
12260 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12261 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12262 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12263 set_option_value((char_u *)"keymodel", 0L,
12264 (char_u *)"startsel,stopsel", 0);
12265 }
12266 else if (STRCMP(eap->arg, "xterm") == 0)
12267 {
12268 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12269 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12270 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12271 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12272 }
12273 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012274 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275}
12276
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012277#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12278/*
12279 * Function given to ExpandGeneric() to obtain the possible arguments of the
12280 * ":behave {mswin,xterm}" command.
12281 */
12282 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012283get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012284{
12285 if (idx == 0)
12286 return (char_u *)"mswin";
12287 if (idx == 1)
12288 return (char_u *)"xterm";
12289 return NULL;
12290}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012291
12292/*
12293 * Function given to ExpandGeneric() to obtain the possible arguments of the
12294 * ":messages {clear}" command.
12295 */
12296 char_u *
12297get_messages_arg(expand_T *xp UNUSED, int idx)
12298{
12299 if (idx == 0)
12300 return (char_u *)"clear";
12301 return NULL;
12302}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012303#endif
12304
Bram Moolenaar113e1072019-01-20 15:30:40 +010012305#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012306 char_u *
12307get_mapclear_arg(expand_T *xp UNUSED, int idx)
12308{
12309 if (idx == 0)
12310 return (char_u *)"<buffer>";
12311 return NULL;
12312}
Bram Moolenaar113e1072019-01-20 15:30:40 +010012313#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012314
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315static int filetype_detect = FALSE;
12316static int filetype_plugin = FALSE;
12317static int filetype_indent = FALSE;
12318
12319/*
12320 * ":filetype [plugin] [indent] {on,off,detect}"
12321 * on: Load the filetype.vim file to install autocommands for file types.
12322 * off: Load the ftoff.vim file to remove all autocommands for file types.
12323 * plugin on: load filetype.vim and ftplugin.vim
12324 * plugin off: load ftplugof.vim
12325 * indent on: load filetype.vim and indent.vim
12326 * indent off: load indoff.vim
12327 */
12328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012329ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331 char_u *arg = eap->arg;
12332 int plugin = FALSE;
12333 int indent = FALSE;
12334
12335 if (*eap->arg == NUL)
12336 {
12337 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012338 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339 filetype_detect ? "ON" : "OFF",
12340 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12341 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12342 return;
12343 }
12344
12345 /* Accept "plugin" and "indent" in any order. */
12346 for (;;)
12347 {
12348 if (STRNCMP(arg, "plugin", 6) == 0)
12349 {
12350 plugin = TRUE;
12351 arg = skipwhite(arg + 6);
12352 continue;
12353 }
12354 if (STRNCMP(arg, "indent", 6) == 0)
12355 {
12356 indent = TRUE;
12357 arg = skipwhite(arg + 6);
12358 continue;
12359 }
12360 break;
12361 }
12362 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12363 {
12364 if (*arg == 'o' || !filetype_detect)
12365 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012366 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 filetype_detect = TRUE;
12368 if (plugin)
12369 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012370 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 filetype_plugin = TRUE;
12372 }
12373 if (indent)
12374 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012375 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376 filetype_indent = TRUE;
12377 }
12378 }
12379 if (*arg == 'd')
12380 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012381 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012382 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 }
12384 }
12385 else if (STRCMP(arg, "off") == 0)
12386 {
12387 if (plugin || indent)
12388 {
12389 if (plugin)
12390 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012391 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 filetype_plugin = FALSE;
12393 }
12394 if (indent)
12395 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012396 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 filetype_indent = FALSE;
12398 }
12399 }
12400 else
12401 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012402 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 filetype_detect = FALSE;
12404 }
12405 }
12406 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012407 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408}
12409
12410/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012411 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 */
12413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012414ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415{
12416 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012417 {
12418 char_u *arg = eap->arg;
12419
12420 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12421 arg += 9;
12422
12423 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12424 if (arg != eap->arg)
12425 did_filetype = FALSE;
12426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012430ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
12432#ifdef FEAT_DIGRAPHS
12433 if (*eap->arg != NUL)
12434 putdigraph(eap->arg);
12435 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012436 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012438 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#endif
12440}
12441
12442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012443ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444{
12445 int flags = 0;
12446
12447 if (eap->cmdidx == CMD_setlocal)
12448 flags = OPT_LOCAL;
12449 else if (eap->cmdidx == CMD_setglobal)
12450 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012451#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452 if (cmdmod.browse && flags == 0)
12453 ex_options(eap);
12454 else
12455#endif
12456 (void)do_set(eap->arg, flags);
12457}
12458
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012459#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12460 void
12461set_no_hlsearch(int flag)
12462{
12463 no_hlsearch = flag;
12464# ifdef FEAT_EVAL
12465 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12466# endif
12467}
12468
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469/*
12470 * ":nohlsearch"
12471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012473ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012475 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012476 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477}
12478
12479/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012480 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 * Sets nextcmd to the start of the next command, if any. Also called when
12482 * skipping commands to find the next command.
12483 */
12484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012485ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486{
12487 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012488 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 char_u *end;
12490 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012491 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012492
12493 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012494 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012495 else
12496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012497 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012498 return;
12499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500
12501 /* First clear any old pattern. */
12502 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012503 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504
12505 if (ends_excmd(*eap->arg))
12506 end = eap->arg;
12507 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012508 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 end = eap->arg + 4;
12510 else
12511 {
12512 p = skiptowhite(eap->arg);
12513 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012514 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 p = skipwhite(p);
12516 if (*p == NUL)
12517 {
12518 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012519 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012520 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 return;
12522 }
12523 end = skip_regexp(p + 1, *p, TRUE, NULL);
12524 if (!eap->skip)
12525 {
12526 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12527 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012528 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 eap->errmsg = e_trailing;
12530 return;
12531 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012532 if (*end != *p)
12533 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012534 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012535 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012536 return;
12537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538
12539 c = *end;
12540 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012541 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012542 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012543 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544 }
12545 }
12546 eap->nextcmd = find_nextcmd(end);
12547}
12548#endif
12549
12550#ifdef FEAT_CRYPT
12551/*
12552 * ":X": Get crypt key
12553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012555ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012557 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012558 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559}
12560#endif
12561
12562#ifdef FEAT_FOLDING
12563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012564ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565{
12566 if (foldManualAllowed(TRUE))
12567 foldCreate(eap->line1, eap->line2);
12568}
12569
12570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012571ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572{
12573 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12574 eap->forceit, FALSE);
12575}
12576
12577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012578ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579{
12580 linenr_T lnum;
12581
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012582#ifdef FEAT_CLIPBOARD
12583 start_global_changes();
12584#endif
12585
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586 /* First set the marks for all lines closed/open. */
12587 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12588 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12589 ml_setmarked(lnum);
12590
12591 /* Execute the command on the marked lines. */
12592 global_exe(eap->arg);
12593 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012594#ifdef FEAT_CLIPBOARD
12595 end_global_changes();
12596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597}
12598#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012599
Bram Moolenaar39665952018-08-15 20:59:48 +020012600#ifdef FEAT_QUICKFIX
12601/*
12602 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12603 * instead of a quickfix command.
12604 */
12605 int
12606is_loclist_cmd(int cmdidx)
12607{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012608 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012609 return FALSE;
12610 return cmdnames[cmdidx].cmd_name[0] == 'l';
12611}
12612#endif
12613
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012614# if defined(FEAT_TIMERS) || defined(PROTO)
12615 int
12616get_pressedreturn(void)
12617{
12618 return ex_pressedreturn;
12619}
12620
12621 void
12622set_pressedreturn(int val)
12623{
12624 ex_pressedreturn = val;
12625}
12626#endif