blob: 6f9f1b98a03335912685bb98c0ae79f90ad7d570 [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
414#ifndef FEAT_SUN_WORKSHOP
415# define ex_wsverb ex_ni
416#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000417#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200418# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000419# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200420# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
423#ifndef FEAT_EVAL
424# define ex_debug ex_ni
425# define ex_breakadd ex_ni
426# define ex_debuggreedy ex_ni
427# define ex_breakdel ex_ni
428# define ex_breaklist ex_ni
429#endif
430
431#ifndef FEAT_CMDHIST
432# define ex_history ex_ni
433#endif
434#ifndef FEAT_JUMPLIST
435# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200436# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437# define ex_changes ex_ni
438#endif
439
Bram Moolenaar05159a02005-02-26 23:04:13 +0000440#ifndef FEAT_PROFILE
441# define ex_profile ex_ni
442#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200443#ifndef FEAT_TERMINAL
444# define ex_terminal ex_ni
445#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000446
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447/*
448 * Declare cmdnames[].
449 */
450#define DO_DECLARE_EXCMD
451#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200452#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454static char_u dollar_command[2] = {'$', 0};
455
456
457#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000458/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459typedef struct
460{
461 char_u *line; /* command line */
462 linenr_T lnum; /* sourcing_lnum of the line */
463} wcmd_T;
464
465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000466 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000468 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000470struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 garray_T *lines_gap; /* growarray with line info */
473 int current_line; /* last read line from growarray */
474 int repeating; /* TRUE when looping a second time */
475 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100476 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 void *cookie;
478};
479
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100480static char_u *get_loop_line(int c, void *cookie, int indent);
481static int store_loop_line(garray_T *gap, char_u *line);
482static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000483
484/* Struct to save a few things while debugging. Used in do_cmdline() only. */
485struct dbg_stuff
486{
487 int trylevel;
488 int force_abort;
489 except_T *caught_stack;
490 char_u *vv_exception;
491 char_u *vv_throwpoint;
492 int did_emsg;
493 int got_int;
494 int did_throw;
495 int need_rethrow;
496 int check_cstack;
497 except_T *current_exception;
498};
499
Bram Moolenaared203462004-06-16 11:19:22 +0000500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100501save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000502{
503 dsp->trylevel = trylevel; trylevel = 0;
504 dsp->force_abort = force_abort; force_abort = FALSE;
505 dsp->caught_stack = caught_stack; caught_stack = NULL;
506 dsp->vv_exception = v_exception(NULL);
507 dsp->vv_throwpoint = v_throwpoint(NULL);
508
509 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
510 dsp->did_emsg = did_emsg; did_emsg = FALSE;
511 dsp->got_int = got_int; got_int = FALSE;
512 dsp->did_throw = did_throw; did_throw = FALSE;
513 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
514 dsp->check_cstack = check_cstack; check_cstack = FALSE;
515 dsp->current_exception = current_exception; current_exception = NULL;
516}
517
518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100519restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000520{
521 suppress_errthrow = FALSE;
522 trylevel = dsp->trylevel;
523 force_abort = dsp->force_abort;
524 caught_stack = dsp->caught_stack;
525 (void)v_exception(dsp->vv_exception);
526 (void)v_throwpoint(dsp->vv_throwpoint);
527 did_emsg = dsp->did_emsg;
528 got_int = dsp->got_int;
529 did_throw = dsp->did_throw;
530 need_rethrow = dsp->need_rethrow;
531 check_cstack = dsp->check_cstack;
532 current_exception = dsp->current_exception;
533}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534#endif
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536/*
537 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
538 * command is given.
539 */
540 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100541do_exmode(
542 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544 int save_msg_scroll;
545 int prev_msg_row;
546 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100547 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548
549 if (improved)
550 exmode_active = EXMODE_VIM;
551 else
552 exmode_active = EXMODE_NORMAL;
553 State = NORMAL;
554
555 /* When using ":global /pat/ visual" and then "Q" we return to continue
556 * the :global command. */
557 if (global_busy)
558 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 save_msg_scroll = msg_scroll;
561 ++RedrawingDisabled; /* don't redisplay the window */
562 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#ifdef FEAT_GUI
564 /* Ignore scrollbar and mouse events in Ex mode */
565 ++hold_gui_events;
566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
568 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
569 while (exmode_active)
570 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000571 /* Check for a ":normal" command and no more characters left. */
572 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
573 {
574 exmode_active = FALSE;
575 break;
576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 msg_scroll = TRUE;
578 need_wait_return = FALSE;
579 ex_pressedreturn = FALSE;
580 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100581 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 prev_msg_row = msg_row;
583 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (improved)
585 {
586 cmdline_row = msg_row;
587 do_cmdline(NULL, getexline, NULL, 0);
588 }
589 else
590 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
591 lines_left = Rows - 1;
592
Bram Moolenaardf177f62005-02-22 08:39:57 +0000593 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100594 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000596 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100597 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000598 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000600 if (ex_pressedreturn)
601 {
602 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000603 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000604 msg_row = prev_msg_row;
605 if (prev_msg_row == Rows - 1)
606 msg_row--;
607 }
608 msg_col = 0;
609 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
610 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000613 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
614 {
615 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100616 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000617 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100618 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
621
622#ifdef FEAT_GUI
623 --hold_gui_events;
624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 --RedrawingDisabled;
626 --no_wait_return;
627 update_screen(CLEAR);
628 need_wait_return = FALSE;
629 msg_scroll = save_msg_scroll;
630}
631
632/*
633 * Execute a simple command line. Used for translated commands like "*".
634 */
635 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100636do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 return do_cmdline(cmd, NULL, NULL,
639 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
640}
641
642/*
643 * do_cmdline(): execute one Ex command line
644 *
645 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * 2. Split up in parts separated with '|'.
648 *
649 * This function can be called recursively!
650 *
651 * flags:
652 * DOCMD_VERBOSE - The command will be included in the error message.
653 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100654 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 * DOCMD_KEYTYPED - Don't reset KeyTyped.
656 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
657 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
658 *
659 * return FAIL if cmdline could not be executed, OK otherwise
660 */
661 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100662do_cmdline(
663 char_u *cmdline,
664 char_u *(*fgetline)(int, void *, int),
665 void *cookie, /* argument for fgetline() */
666 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 char_u *next_cmdline; /* next cmd to execute */
669 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100670 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 static int recursive = 0; /* recursive depth */
672 int msg_didout_before_start = 0;
673 int count = 0; /* line number count */
674 int did_inc = FALSE; /* incremented RedrawingDisabled */
675 int retval = OK;
676#ifdef FEAT_EVAL
677 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000678 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 int current_line = 0; /* active line in lines_ga */
680 char_u *fname = NULL; /* function or script name */
681 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
682 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000683 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 int initial_trylevel;
685 struct msglist **saved_msg_list = NULL;
686 struct msglist *private_msg_list;
687
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100688 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100689 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000693 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# define cmd_cookie cookie
697#endif
698 static int call_depth = 0; /* recursiveness */
699
700#ifdef FEAT_EVAL
701 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
702 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000703 * This ensures that the do_errthrow() call in do_one_cmd() does not
704 * combine the messages stored by an earlier invocation of do_one_cmd()
705 * with the command name of the later one. This would happen when
706 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 saved_msg_list = msg_list;
708 msg_list = &private_msg_list;
709 private_msg_list = NULL;
710#endif
711
712 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100713 * here. The value of 200 allows nested function calls, ":source", etc.
714 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100715 if (call_depth >= 200
716#ifdef FEAT_EVAL
717 && call_depth >= p_mfd
718#endif
719 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_EVAL
723 /* When converting to an exception, we do not include the command name
724 * since this is not an error of the specific command. */
725 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
726 msg_list = saved_msg_list;
727#endif
728 return FAIL;
729 }
730 ++call_depth;
731
732#ifdef FEAT_EVAL
733 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000734 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 cstack.cs_trylevel = 0;
736 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000737 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
739
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100740 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
742 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000744 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 ++ex_nesting_level;
746
747 /* Get the function or script name and the address where the next breakpoint
748 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000749 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
751 fname = func_name(real_cookie);
752 breakpoint = func_breakpoint(real_cookie);
753 dbg_tick = func_dbg_tick(real_cookie);
754 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
757 fname = sourcing_name;
758 breakpoint = source_breakpoint(real_cookie);
759 dbg_tick = source_dbg_tick(real_cookie);
760 }
761
762 /*
763 * Initialize "force_abort" and "suppress_errthrow" at the top level.
764 */
765 if (!recursive)
766 {
767 force_abort = FALSE;
768 suppress_errthrow = FALSE;
769 }
770
771 /*
772 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000773 * exception handling (used when debugging). Otherwise clear it to avoid
774 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000776 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000777 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000778 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200779 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 initial_trylevel = trylevel;
782
783 /*
784 * "did_throw" will be set to TRUE when an exception is being thrown.
785 */
786 did_throw = FALSE;
787#endif
788 /*
789 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 * If force_abort is set, we cancel everything.
792 */
793 did_emsg = FALSE;
794
795 /*
796 * KeyTyped is only set when calling vgetc(). Reset it here when not
797 * calling vgetc() (sourced command lines).
798 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100799 if (!(flags & DOCMD_KEYTYPED)
800 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 KeyTyped = FALSE;
802
803 /*
804 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * - for multiple commands on one line, separated with '|'
807 * - when repeating until there are no more lines (for ":source")
808 */
809 next_cmdline = cmdline;
810 do
811 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
815
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000816 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (next_cmdline == NULL
818#ifdef FEAT_EVAL
819 && !force_abort
820 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822#endif
823 )
824 did_emsg = FALSE;
825
826 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000827 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 * 3. If a line is given: Make a copy, so we can mess with it.
830 */
831
832#ifdef FEAT_EVAL
833 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000834 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 /* Each '|' separated command is stored separately in lines_ga, to
837 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100838 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
840 /* Check if a function has returned or, unless it has an unclosed
841 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 func_line_end(real_cookie);
847# endif
848 if (func_has_ended(real_cookie))
849 {
850 retval = FAIL;
851 break;
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000855 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100856 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857 script_line_end();
858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
860 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
863 retval = FAIL;
864 break;
865 }
866
867 /* If breakpoints have been added/deleted need to check for it. */
868 if (breakpoint != NULL && dbg_tick != NULL
869 && *dbg_tick != debug_tick)
870 {
871 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100872 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 fname, sourcing_lnum);
874 *dbg_tick = debug_tick;
875 }
876
877 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
878 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
879
880 /* Did we encounter a breakpoint? */
881 if (breakpoint != NULL && *breakpoint != 0
882 && *breakpoint <= sourcing_lnum)
883 {
884 dbg_breakpoint(fname, sourcing_lnum);
885 /* Find next breakpoint. */
886 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100887 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 fname, sourcing_lnum);
889 *dbg_tick = debug_tick;
890 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000892 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 {
894 if (getline_is_func)
895 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100896 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897 script_line_start();
898 }
899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000902 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100905 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 * below, so that it stores lines in or reads them from
907 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000908 * while/for loop. */
909 cmd_getline = get_loop_line;
910 cmd_cookie = (void *)&cmd_loop_cookie;
911 cmd_loop_cookie.lines_gap = &lines_ga;
912 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000914 cmd_loop_cookie.cookie = cookie;
915 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917 else
918 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100919 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 cmd_cookie = cookie;
921 }
922#endif
923
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100924 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (next_cmdline == NULL)
926 {
927 /*
928 * Need to set msg_didout for the first line after an ":if",
929 * otherwise the ":if" will be overwritten.
930 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100931 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100933 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#ifdef FEAT_EVAL
935 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
936#else
937 0
938#endif
939 )) == NULL)
940 {
941 /* Don't call wait_return for aborted command line. The NULL
942 * returned for the end of a sourced file or executed function
943 * doesn't do this. */
944 if (KeyTyped && !(flags & DOCMD_REPEAT))
945 need_wait_return = FALSE;
946 retval = FAIL;
947 break;
948 }
949 used_getline = TRUE;
950
951 /*
952 * Keep the first typed line. Clear it when more lines are typed.
953 */
954 if (flags & DOCMD_KEEPLINE)
955 {
956 vim_free(repeat_cmdline);
957 if (count == 0)
958 repeat_cmdline = vim_strsave(next_cmdline);
959 else
960 repeat_cmdline = NULL;
961 }
962 }
963
964 /* 3. Make a copy of the command so we can mess with it. */
965 else if (cmdline_copy == NULL)
966 {
967 next_cmdline = vim_strsave(next_cmdline);
968 if (next_cmdline == NULL)
969 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100970 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 retval = FAIL;
972 break;
973 }
974 }
975 cmdline_copy = next_cmdline;
976
977#ifdef FEAT_EVAL
978 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000979 * Save the current line when inside a ":while" or ":for", and when
980 * the command looks like a ":while" or ":for", because we may need it
981 * later. When there is a '|' and another command, it is stored
982 * separately, because we need to be able to jump back to it from an
983 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 if (current_line == lines_ga.ga_len
986 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
990 retval = FAIL;
991 break;
992 }
993 }
994 did_endif = FALSE;
995#endif
996
997 if (count++ == 0)
998 {
999 /*
1000 * All output from the commands is put below each other, without
1001 * waiting for a return. Don't do this when executing commands
1002 * from a script or when being called recursive (e.g. for ":e
1003 * +command file").
1004 */
1005 if (!(flags & DOCMD_NOWAIT) && !recursive)
1006 {
1007 msg_didout_before_start = msg_didout;
1008 msg_didany = FALSE; /* no output yet */
1009 msg_start();
1010 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001011 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 ++RedrawingDisabled;
1013 did_inc = TRUE;
1014 }
1015 }
1016
1017 if (p_verbose >= 15 && sourcing_name != NULL)
1018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001020 verbose_enter_scroll();
1021
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001022 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001024 if (msg_silent == 0)
1025 msg_puts((char_u *)"\n"); /* don't overwrite this */
1026
1027 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 --no_wait_return;
1029 }
1030
1031 /*
1032 * 2. Execute one '|' separated command.
1033 * do_one_cmd() will return NULL if there is no trailing '|'.
1034 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1035 */
1036 ++recursive;
1037 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1038#ifdef FEAT_EVAL
1039 &cstack,
1040#endif
1041 cmd_getline, cmd_cookie);
1042 --recursive;
1043
1044#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 if (cmd_cookie == (void *)&cmd_loop_cookie)
1046 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#endif
1050
1051 if (next_cmdline == NULL)
1052 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001053 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054#ifdef FEAT_CMDHIST
1055 /*
1056 * If the command was typed, remember it for the ':' register.
1057 * Do this AFTER executing the command to make :@: work.
1058 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001059 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 && new_last_cmdline != NULL)
1061 {
1062 vim_free(last_cmdline);
1063 last_cmdline = new_last_cmdline;
1064 new_last_cmdline = NULL;
1065 }
1066#endif
1067 }
1068 else
1069 {
1070 /* need to copy the command after the '|' to cmdline_copy, for the
1071 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001072 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 next_cmdline = cmdline_copy;
1074 }
1075
1076
1077#ifdef FEAT_EVAL
1078 /* reset did_emsg for a function that is not aborted by an error */
1079 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001080 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 && !func_has_abort(real_cookie))
1082 did_emsg = FALSE;
1083
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 ++current_line;
1087
1088 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 * An ":endwhile", ":endfor" and ":continue" is handled here.
1090 * If we were executing commands, jump back to the ":while" or
1091 * ":for".
1092 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 /* Jump back to the matching ":while" or ":for". Be careful
1099 * not to use a cs_line[] from an entry that isn't a ":while"
1100 * or ":for": It would make "current_line" invalid and can
1101 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (!did_emsg && !got_int && !did_throw
1103 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 && (cstack.cs_flags[cstack.cs_idx]
1105 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 && cstack.cs_line[cstack.cs_idx] >= 0
1107 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1108 {
1109 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 /* remember we jumped there */
1111 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 line_breakcheck(); /* check if CTRL-C typed */
1113
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 /* Check for the next breakpoint at or after the ":while"
1115 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (breakpoint != NULL)
1117 {
1118 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001119 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 fname,
1121 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1122 *dbg_tick = debug_tick;
1123 }
1124 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001129 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1130 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 }
1133
1134 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1141 }
1142 }
1143
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001144 /* Check for the next breakpoint after a watchexpression */
1145 if (breakpoint != NULL && has_watchexpr())
1146 {
1147 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1148 *dbg_tick = debug_tick;
1149 }
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 /*
1152 * When not inside any ":while" loop, clear remembered lines.
1153 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
1156 if (lines_ga.ga_len > 0)
1157 {
1158 sourcing_lnum =
1159 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1160 free_cmdlines(&lines_ga);
1161 }
1162 current_line = 0;
1163 }
1164
1165 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1167 * being restored at the ":endtry". Reset them here and set the
1168 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1169 * This includes the case where a missing ":endif", ":endwhile" or
1170 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 cstack.cs_lflags &= ~CSL_HAD_FINA;
1175 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1176 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 did_throw ? (void *)current_exception : NULL);
1178 did_emsg = got_int = did_throw = FALSE;
1179 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1180 }
1181
1182 /* Update global "trylevel" for recursive calls to do_cmdline() from
1183 * within this loop. */
1184 trylevel = initial_trylevel + cstack.cs_trylevel;
1185
1186 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001187 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 * files) is aborted because of an error, an interrupt, or an uncaught
1189 * exception, cancel everything. If it is left normally, reset
1190 * force_abort to get the non-EH compatible abortion behavior for
1191 * the rest of the script.
1192 */
1193 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1194 force_abort = FALSE;
1195
1196 /* Convert an interrupt to an exception if appropriate. */
1197 (void)do_intthrow(&cstack);
1198#endif /* FEAT_EVAL */
1199
1200 }
1201 /*
1202 * Continue executing command lines when:
1203 * - no CTRL-C typed, no aborting error, no exception thrown or try
1204 * conditionals need to be checked for executing finally clauses or
1205 * catching an interrupt exception
1206 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 * looping for ":source" command or function call.
1209 */
1210 while (!((got_int
1211#ifdef FEAT_EVAL
1212 || (did_emsg && force_abort) || did_throw
1213#endif
1214 )
1215#ifdef FEAT_EVAL
1216 && cstack.cs_trylevel == 0
1217#endif
1218 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001219 && !(did_emsg
1220#ifdef FEAT_EVAL
1221 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001222 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001223 * the :endtry to be missed. */
1224 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1225#endif
1226 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001227 && (getline_equal(fgetline, cookie, getexmodeline)
1228 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 && (next_cmdline != NULL
1230#ifdef FEAT_EVAL
1231 || cstack.cs_idx >= 0
1232#endif
1233 || (flags & DOCMD_REPEAT)));
1234
1235 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001236 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237#ifdef FEAT_EVAL
1238 free_cmdlines(&lines_ga);
1239 ga_clear(&lines_ga);
1240
1241 if (cstack.cs_idx >= 0)
1242 {
1243 /*
1244 * If a sourced file or executed function ran to its end, report the
1245 * unclosed conditional.
1246 */
1247 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001248 && ((getline_equal(fgetline, cookie, getsourceline)
1249 && !source_finished(fgetline, cookie))
1250 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 && !func_has_ended(real_cookie))))
1252 {
1253 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001256 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001257 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262
1263 /*
1264 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1265 * ":endtry" in a sourced file or executed function. If the try
1266 * conditional is in its finally clause, ignore anything pending.
1267 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001268 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 */
1270 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001271 {
1272 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1273
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001274 if (idx >= 0)
1275 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001276 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1277 &cstack.cs_looplevel);
1278 }
1279 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 trylevel = initial_trylevel;
1281 }
1282
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001283 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1284 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001286 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 ? (char_u *)"endfunction" : (char_u *)NULL);
1288
1289 if (trylevel == 0)
1290 {
1291 /*
1292 * When an exception is being thrown out of the outermost try
1293 * conditional, discard the uncaught exception, disable the conversion
1294 * of interrupts or errors to exceptions, and ensure that no more
1295 * commands are executed.
1296 */
1297 if (did_throw)
1298 {
1299 void *p = NULL;
1300 char_u *saved_sourcing_name;
1301 int saved_sourcing_lnum;
1302 struct msglist *messages = NULL, *next;
1303
1304 /*
1305 * If the uncaught exception is a user exception, report it as an
1306 * error. If it is an error exception, display the saved error
1307 * message now. For an interrupt exception, do nothing; the
1308 * interrupt message is given elsewhere.
1309 */
1310 switch (current_exception->type)
1311 {
1312 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001313 vim_snprintf((char *)IObuff, IOSIZE,
1314 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 current_exception->value);
1316 p = vim_strsave(IObuff);
1317 break;
1318 case ET_ERROR:
1319 messages = current_exception->messages;
1320 current_exception->messages = NULL;
1321 break;
1322 case ET_INTERRUPT:
1323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325
1326 saved_sourcing_name = sourcing_name;
1327 saved_sourcing_lnum = sourcing_lnum;
1328 sourcing_name = current_exception->throw_name;
1329 sourcing_lnum = current_exception->throw_lnum;
1330 current_exception->throw_name = NULL;
1331
1332 discard_current_exception(); /* uses IObuff if 'verbose' */
1333 suppress_errthrow = TRUE;
1334 force_abort = TRUE;
1335
1336 if (messages != NULL)
1337 {
1338 do
1339 {
1340 next = messages->next;
1341 emsg(messages->msg);
1342 vim_free(messages->msg);
1343 vim_free(messages);
1344 messages = next;
1345 }
1346 while (messages != NULL);
1347 }
1348 else if (p != NULL)
1349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001350 semsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 vim_free(p);
1352 }
1353 vim_free(sourcing_name);
1354 sourcing_name = saved_sourcing_name;
1355 sourcing_lnum = saved_sourcing_lnum;
1356 }
1357
1358 /*
1359 * On an interrupt or an aborting error not converted to an exception,
1360 * disable the conversion of errors to exceptions. (Interrupts are not
1361 * converted any more, here.) This enables also the interrupt message
1362 * when force_abort is set and did_emsg unset in case of an interrupt
1363 * from a finally clause after an error.
1364 */
1365 else if (got_int || (did_emsg && force_abort))
1366 suppress_errthrow = TRUE;
1367 }
1368
1369 /*
1370 * The current cstack will be freed when do_cmdline() returns. An uncaught
1371 * exception will have to be rethrown in the previous cstack. If a function
1372 * has just returned or a script file was just finished and the previous
1373 * cstack belongs to the same function or, respectively, script file, it
1374 * will have to be checked for finally clauses to be executed due to the
1375 * ":return" or ":finish". This is done in do_one_cmd().
1376 */
1377 if (did_throw)
1378 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001379 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001381 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 && ex_nesting_level > func_level(real_cookie) + 1))
1383 {
1384 if (!did_throw)
1385 check_cstack = TRUE;
1386 }
1387 else
1388 {
1389 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001390 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 --ex_nesting_level;
1392 /*
1393 * Go to debug mode when returning from a function in which we are
1394 * single-stepping.
1395 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001396 if ((getline_equal(fgetline, cookie, getsourceline)
1397 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001399 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 ? (char_u *)_("End of sourced file")
1401 : (char_u *)_("End of function"));
1402 }
1403
1404 /*
1405 * Restore the exception environment (done after returning from the
1406 * debugger).
1407 */
1408 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001409 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
1411 msg_list = saved_msg_list;
1412#endif /* FEAT_EVAL */
1413
1414 /*
1415 * If there was too much output to fit on the command line, ask the user to
1416 * hit return before redrawing the screen. With the ":global" command we do
1417 * this only once after the command is finished.
1418 */
1419 if (did_inc)
1420 {
1421 --RedrawingDisabled;
1422 --no_wait_return;
1423 msg_scroll = FALSE;
1424
1425 /*
1426 * When just finished an ":if"-":else" which was typed, no need to
1427 * wait for hit-return. Also for an error situation.
1428 */
1429 if (retval == FAIL
1430#ifdef FEAT_EVAL
1431 || (did_endif && KeyTyped && !did_emsg)
1432#endif
1433 )
1434 {
1435 need_wait_return = FALSE;
1436 msg_didany = FALSE; /* don't wait when restarting edit */
1437 }
1438 else if (need_wait_return)
1439 {
1440 /*
1441 * The msg_start() above clears msg_didout. The wait_return we do
1442 * here should not overwrite the command that may be shown before
1443 * doing that.
1444 */
1445 msg_didout |= msg_didout_before_start;
1446 wait_return(FALSE);
1447 }
1448 }
1449
Bram Moolenaar2a942252012-11-28 23:03:07 +01001450#ifdef FEAT_EVAL
1451 did_endif = FALSE; /* in case do_cmdline used recursively */
1452#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 /*
1454 * Reset if_level, in case a sourced script file contains more ":if" than
1455 * ":endif" (could be ":if x | foo | endif").
1456 */
1457 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001458#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 --call_depth;
1461 return retval;
1462}
1463
1464#ifdef FEAT_EVAL
1465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
1468 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001471 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 wcmd_T *wp;
1473 char_u *line;
1474
1475 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1476 {
1477 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001478 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001480 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (cp->getline == NULL)
1482 line = getcmdline(c, 0L, indent);
1483 else
1484 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001485 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 ++cp->current_line;
1487
1488 return line;
1489 }
1490
1491 KeyTyped = FALSE;
1492 ++cp->current_line;
1493 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1494 sourcing_lnum = wp->lnum;
1495 return vim_strsave(wp->line);
1496}
1497
1498/*
1499 * Store a line in "gap" so that a ":while" loop can execute it again.
1500 */
1501 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001502store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 if (ga_grow(gap, 1) == FAIL)
1505 return FAIL;
1506 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1507 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1508 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 return OK;
1510}
1511
1512/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001513 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 */
1515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001516free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 while (gap->ga_len > 0)
1519 {
1520 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1521 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523}
1524#endif
1525
1526/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1528 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001531getline_equal(
1532 char_u *(*fgetline)(int, void *, int),
1533 void *cookie UNUSED, /* argument for fgetline() */
1534 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
1536#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001537 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaar89d40322006-08-29 15:30:07 +00001540 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001541 * function that's originally used to obtain the lines. This may be
1542 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001543 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001544 cp = (struct loop_cookie *)cookie;
1545 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 {
1547 gp = cp->getline;
1548 cp = cp->cookie;
1549 }
1550 return gp == func;
1551#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001552 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#endif
1554}
1555
1556#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1557/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001558 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 * getline function. Otherwise return "cookie".
1560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001562getline_cookie(
1563 char_u *(*fgetline)(int, void *, int) UNUSED,
1564 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001567 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar89d40322006-08-29 15:30:07 +00001570 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001571 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001573 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001574 cp = (struct loop_cookie *)cookie;
1575 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 gp = cp->getline;
1578 cp = cp->cookie;
1579 }
1580 return cp;
1581# else
1582 return cookie;
1583# endif
1584}
1585#endif
1586
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001587
1588/*
1589 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1590 * ":bwipeout", etc.
1591 * Returns the buffer number.
1592 */
1593 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001594compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001595{
1596 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001597 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001598 int count = offset;
1599
1600 buf = firstbuf;
1601 while (buf->b_next != NULL && buf->b_fnum < lnum)
1602 buf = buf->b_next;
1603 while (count != 0)
1604 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001605 count += (offset < 0) ? 1 : -1;
1606 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1607 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001608 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610 if (addr_type == ADDR_LOADED_BUFFERS)
1611 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001612 while (buf->b_ml.ml_mfp == NULL)
1613 {
1614 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1615 if (nextbuf == NULL)
1616 break;
1617 buf = nextbuf;
1618 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001620 /* we might have gone too far, last buffer is not loadedd */
1621 if (addr_type == ADDR_LOADED_BUFFERS)
1622 while (buf->b_ml.ml_mfp == NULL)
1623 {
1624 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1625 if (nextbuf == NULL)
1626 break;
1627 buf = nextbuf;
1628 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 return buf->b_fnum;
1630}
1631
Bram Moolenaarf240e182014-11-27 18:33:02 +01001632 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001633current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001634{
1635 win_T *wp;
1636 int nr = 0;
1637
Bram Moolenaar29323592016-07-24 22:04:11 +02001638 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001639 {
1640 ++nr;
1641 if (wp == win)
1642 break;
1643 }
1644 return nr;
1645}
1646
1647 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001648current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001649{
1650 tabpage_T *tp;
1651 int nr = 0;
1652
Bram Moolenaar29323592016-07-24 22:04:11 +02001653 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001654 {
1655 ++nr;
1656 if (tp == tab)
1657 break;
1658 }
1659 return nr;
1660}
1661
1662# define CURRENT_WIN_NR current_win_nr(curwin)
1663# define LAST_WIN_NR current_win_nr(NULL)
1664# define CURRENT_TAB_NR current_tab_nr(curtab)
1665# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667/*
1668 * Execute one Ex command.
1669 *
1670 * If 'sourcing' is TRUE, the command will be included in the error message.
1671 *
1672 * 1. skip comment lines and leading space
1673 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001674 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001676 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 * 6. parse arguments
1678 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001680 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
1682 * This function may be called recursively!
1683 */
1684#if (_MSC_VER == 1200)
1685/*
Bram Moolenaared203462004-06-16 11:19:22 +00001686 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001688 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
1690 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001691do_one_cmd(
1692 char_u **cmdlinep,
1693 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001695 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001697 char_u *(*fgetline)(int, void *, int),
1698 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 char_u *p;
1701 linenr_T lnum;
1702 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001703 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001704 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001706 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 cmdmod_T save_cmdmod;
1708 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001709 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 vim_memset(&ea, 0, sizeof(ea));
1712 ea.line1 = 1;
1713 ea.line2 = 1;
1714#ifdef FEAT_EVAL
1715 ++ex_nesting_level;
1716#endif
1717
Bram Moolenaar21691f82012-12-05 19:13:18 +01001718 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (quitmore
1720#ifdef FEAT_EVAL
1721 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001722 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001723#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001724 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001725 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 --quitmore;
1727
1728 /*
1729 * Reset browse, confirm, etc.. They are restored when returning, for
1730 * recursive calls.
1731 */
1732 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001734 /* "#!anything" is handled like a comment. */
1735 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1736 goto doend;
1737
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001738/*
1739 * 1. Skip comment lines and leading white space and colons.
1740 * 2. Handle command modifiers.
1741 */
1742 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001744 ea.cmdlinep = cmdlinep;
1745 ea.getline = fgetline;
1746 ea.cookie = cookie;
1747#ifdef FEAT_EVAL
1748 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001750 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001751 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001753 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755#ifdef FEAT_EVAL
1756 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1757 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1758#else
1759 ea.skip = (if_level > 0);
1760#endif
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001763 * 3. Skip over the range to find the command. Let "p" point to after it.
1764 *
1765 * We need the command to know what kind of range it uses.
1766 */
1767 cmd = ea.cmd;
1768 ea.cmd = skip_range(ea.cmd, NULL);
1769 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1770 ea.cmd = skipwhite(ea.cmd + 1);
1771 p = find_command(&ea, NULL);
1772
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001773#ifdef FEAT_EVAL
1774# ifdef FEAT_PROFILE
1775 // Count this line for profiling if skip is TRUE.
1776 if (do_profiling == PROF_YES
1777 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1778 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1779 {
1780 int skip = did_emsg || got_int || did_throw;
1781
1782 if (ea.cmdidx == CMD_catch)
1783 skip = !skip && !(cstack->cs_idx >= 0
1784 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1785 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1786 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1787 skip = skip || !(cstack->cs_idx >= 0
1788 && !(cstack->cs_flags[cstack->cs_idx]
1789 & (CSF_ACTIVE | CSF_TRUE)));
1790 else if (ea.cmdidx == CMD_finally)
1791 skip = FALSE;
1792 else if (ea.cmdidx != CMD_endif
1793 && ea.cmdidx != CMD_endfor
1794 && ea.cmdidx != CMD_endtry
1795 && ea.cmdidx != CMD_endwhile)
1796 skip = ea.skip;
1797
1798 if (!skip)
1799 {
1800 if (getline_equal(fgetline, cookie, get_func_line))
1801 func_line_exec(getline_cookie(fgetline, cookie));
1802 else if (getline_equal(fgetline, cookie, getsourceline))
1803 script_line_exec();
1804 }
1805 }
1806# endif
1807
1808 /* May go to debug mode. If this happens and the ">quit" debug command is
1809 * used, throw an interrupt exception and skip the next command. */
1810 dbg_check_breakpoint(&ea);
1811 if (!ea.skip && got_int)
1812 {
1813 ea.skip = TRUE;
1814 (void)do_intthrow(cstack);
1815 }
1816#endif
1817
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001818/*
1819 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 *
1821 * where 'addr' is:
1822 *
1823 * % (entire file)
1824 * $ [+-NUM]
1825 * 'x [+-NUM] (where x denotes a currently defined mark)
1826 * . [+-NUM]
1827 * [+-NUM]..
1828 * NUM
1829 *
1830 * The ea.cmd pointer is updated to point to the first character following the
1831 * range spec. If an initial address is found, but no second, the upper bound
1832 * is equal to the lower.
1833 */
1834
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001835 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001836 if (!IS_USER_CMDIDX(ea.cmdidx))
1837 {
1838 if (ea.cmdidx != CMD_SIZE)
1839 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1840 else
1841 ea.addr_type = ADDR_LINES;
1842
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001843 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001844 if (ea.cmdidx == CMD_wincmd && p != NULL)
1845 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001846 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001847
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001848 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001849 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001850 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001853 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855
1856 /*
1857 * Skip ':' and any white space
1858 */
1859 ea.cmd = skipwhite(ea.cmd);
1860 while (*ea.cmd == ':')
1861 ea.cmd = skipwhite(ea.cmd + 1);
1862
1863 /*
1864 * If we got a line, but no command, then go to the line.
1865 * If we find a '|' or '\n' we set ea.nextcmd.
1866 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001867 if (*ea.cmd == NUL || *ea.cmd == '"'
1868 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 /*
1871 * strange vi behaviour:
1872 * ":3" jumps to line 3
1873 * ":3|..." prints line 3
1874 * ":|" prints current line
1875 */
1876 if (ea.skip) /* skip this if inside :if */
1877 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001878 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
1880 ea.cmdidx = CMD_print;
1881 ea.argt = RANGE+COUNT+TRLBAR;
1882 if ((errormsg = invalid_range(&ea)) == NULL)
1883 {
1884 correct_range(&ea);
1885 ex_print(&ea);
1886 }
1887 }
1888 else if (ea.addr_count != 0)
1889 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001890 if (ea.line2 > curbuf->b_ml.ml_line_count)
1891 {
1892 /* With '-' in 'cpoptions' a line number past the file is an
1893 * error, otherwise put it at the end of the file. */
1894 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1895 ea.line2 = -1;
1896 else
1897 ea.line2 = curbuf->b_ml.ml_line_count;
1898 }
1899
1900 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001901 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 else
1903 {
1904 if (ea.line2 == 0)
1905 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 else
1907 curwin->w_cursor.lnum = ea.line2;
1908 beginline(BL_SOL | BL_FIX);
1909 }
1910 }
1911 goto doend;
1912 }
1913
Bram Moolenaard5005162014-08-22 23:05:54 +02001914 /* If this looks like an undefined user command and there are CmdUndefined
1915 * autocommands defined, trigger the matching autocommands. */
1916 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1917 && ASCII_ISUPPER(*ea.cmd)
1918 && has_cmdundefined())
1919 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001920 int ret;
1921
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001922 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001923 while (ASCII_ISALNUM(*p))
1924 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001925 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001926 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1927 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001928 /* If the autocommands did something and didn't cause an error, try
1929 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001930 p = (ret
1931#ifdef FEAT_EVAL
1932 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001933#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001934 ) ? find_command(&ea, NULL) : ea.cmd;
1935 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937#ifdef FEAT_USR_CMDS
1938 if (p == NULL)
1939 {
1940 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001941 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 goto doend;
1943 }
1944 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001945 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1946 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
1948 errormsg = uc_fun_cmd();
1949 goto doend;
1950 }
1951#endif
1952 if (ea.cmdidx == CMD_SIZE)
1953 {
1954 if (!ea.skip)
1955 {
1956 STRCPY(IObuff, _("E492: Not an editor command"));
1957 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001958 {
1959 /* If the modifier was parsed OK the error must be in the
1960 * following command */
1961 if (after_modifier != NULL)
1962 append_command(after_modifier);
1963 else
1964 append_command(*cmdlinep);
1965 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001966 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001967 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969 goto doend;
1970 }
1971
Bram Moolenaar958636c2014-10-21 20:01:58 +02001972 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1973 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001974#ifdef HAVE_EX_SCRIPT_NI
1975 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1976#endif
1977 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979#ifndef FEAT_EVAL
1980 /*
1981 * When the expression evaluation is disabled, recognize the ":if" and
1982 * ":endif" commands and ignore everything in between it.
1983 */
1984 if (ea.cmdidx == CMD_if)
1985 ++if_level;
1986 if (if_level)
1987 {
1988 if (ea.cmdidx == CMD_endif)
1989 --if_level;
1990 goto doend;
1991 }
1992
1993#endif
1994
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001995 /* forced commands */
1996 if (*p == '!' && ea.cmdidx != CMD_substitute
1997 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 ++p;
2000 ea.forceit = TRUE;
2001 }
2002 else
2003 ea.forceit = FALSE;
2004
2005/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002006 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002008 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002009 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
2011 if (!ea.skip)
2012 {
2013#ifdef HAVE_SANDBOX
2014 if (sandbox != 0 && !(ea.argt & SBOXOK))
2015 {
2016 /* Command not allowed in sandbox. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002017 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 goto doend;
2019 }
2020#endif
2021 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2022 {
2023 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002024 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 goto doend;
2026 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002027
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002028 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002029 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002031 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 goto doend;
2034 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002035
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002036 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002037 * Do allow ":checktime" (it is postponed).
2038 * Do allow ":edit" (check for an argument later).
2039 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002040 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002041 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002042 && ea.cmdidx != CMD_edit
2043 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002044 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002045 && curbuf_locked())
2046 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2049 {
2050 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002051 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 goto doend;
2053 }
2054 }
2055
2056 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002058 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 goto doend;
2060 }
2061
2062 /*
2063 * Don't complain about the range if it is not used
2064 * (could happen if line_count is accidentally set to 0).
2065 */
2066 if (!ea.skip && !ni)
2067 {
2068 /*
2069 * If the range is backwards, ask for confirmation and, if given, swap
2070 * ea.line1 & ea.line2 so it's forwards again.
2071 * When global command is busy, don't ask, will fail below.
2072 */
2073 if (!global_busy && ea.line1 > ea.line2)
2074 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002075 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002077 if (sourcing || exmode_active)
2078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002080 goto doend;
2081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (ask_yesno((char_u *)
2083 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002084 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086 lnum = ea.line1;
2087 ea.line1 = ea.line2;
2088 ea.line2 = lnum;
2089 }
2090 if ((errormsg = invalid_range(&ea)) != NULL)
2091 goto doend;
2092 }
2093
2094 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2095 ea.line2 = 1;
2096
2097 correct_range(&ea);
2098
2099#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002100 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2101 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 /* Put the first line at the start of a closed fold, put the last line
2104 * at the end of a closed fold. */
2105 (void)hasFolding(ea.line1, &ea.line1, NULL);
2106 (void)hasFolding(ea.line2, NULL, &ea.line2);
2107 }
2108#endif
2109
2110#ifdef FEAT_QUICKFIX
2111 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002112 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 * option here, so things like % get expanded.
2114 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002115 p = replace_makeprg(&ea, p, cmdlinep);
2116 if (p == NULL)
2117 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#endif
2119
2120 /*
2121 * Skip to start of argument.
2122 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2123 */
2124 if (ea.cmdidx == CMD_bang)
2125 ea.arg = p;
2126 else
2127 ea.arg = skipwhite(p);
2128
Bram Moolenaar379fb762018-08-30 15:58:28 +02002129 // ":file" cannot be run with an argument when "curbuf_lock" is set
2130 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2131 goto doend;
2132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 /*
2134 * Check for "++opt=val" argument.
2135 * Must be first, allow ":w ++enc=utf8 !cmd"
2136 */
2137 if (ea.argt & ARGOPT)
2138 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2139 if (getargopt(&ea) == FAIL && !ni)
2140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002141 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 goto doend;
2143 }
2144
2145 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2146 {
2147 if (*ea.arg == '>') /* append */
2148 {
2149 if (*++ea.arg != '>') /* typed wrong */
2150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002151 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 goto doend;
2153 }
2154 ea.arg = skipwhite(ea.arg + 1);
2155 ea.append = TRUE;
2156 }
2157 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2158 {
2159 ++ea.arg;
2160 ea.usefilter = TRUE;
2161 }
2162 }
2163
2164 if (ea.cmdidx == CMD_read)
2165 {
2166 if (ea.forceit)
2167 {
2168 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2169 ea.forceit = FALSE;
2170 }
2171 else if (*ea.arg == '!') /* :r !filter */
2172 {
2173 ++ea.arg;
2174 ea.usefilter = TRUE;
2175 }
2176 }
2177
2178 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2179 {
2180 ea.amount = 1;
2181 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2182 {
2183 ++ea.arg;
2184 ++ea.amount;
2185 }
2186 ea.arg = skipwhite(ea.arg);
2187 }
2188
2189 /*
2190 * Check for "+command" argument, before checking for next command.
2191 * Don't do this for ":read !cmd" and ":write !cmd".
2192 */
2193 if ((ea.argt & EDITCMD) && !ea.usefilter)
2194 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2195
2196 /*
2197 * Check for '|' to separate commands and '"' to start comments.
2198 * Don't do this for ":read !cmd" and ":write !cmd".
2199 */
2200 if ((ea.argt & TRLBAR) && !ea.usefilter)
2201 separate_nextcmd(&ea);
2202
2203 /*
2204 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002205 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2206 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002208 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002209 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002210 || ea.cmdidx == CMD_global
2211 || ea.cmdidx == CMD_vglobal
2212 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 for (p = ea.arg; *p; ++p)
2215 {
2216 /* Remove one backslash before a newline, so that it's possible to
2217 * pass a newline to the shell and also a newline that is preceded
2218 * with a backslash. This makes it impossible to end a shell
2219 * command in a backslash, but that doesn't appear useful.
2220 * Halving the number of backslashes is incompatible with previous
2221 * versions. */
2222 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002223 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 else if (*p == '\n')
2225 {
2226 ea.nextcmd = p + 1;
2227 *p = NUL;
2228 break;
2229 }
2230 }
2231 }
2232
2233 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2234 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002235 buf_T *buf;
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002238 switch (ea.addr_type)
2239 {
2240 case ADDR_LINES:
2241 ea.line2 = curbuf->b_ml.ml_line_count;
2242 break;
2243 case ADDR_LOADED_BUFFERS:
2244 buf = firstbuf;
2245 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2246 buf = buf->b_next;
2247 ea.line1 = buf->b_fnum;
2248 buf = lastbuf;
2249 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2250 buf = buf->b_prev;
2251 ea.line2 = buf->b_fnum;
2252 break;
2253 case ADDR_BUFFERS:
2254 ea.line1 = firstbuf->b_fnum;
2255 ea.line2 = lastbuf->b_fnum;
2256 break;
2257 case ADDR_WINDOWS:
2258 ea.line2 = LAST_WIN_NR;
2259 break;
2260 case ADDR_TABS:
2261 ea.line2 = LAST_TAB_NR;
2262 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002263 case ADDR_TABS_RELATIVE:
2264 ea.line2 = 1;
2265 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002266 case ADDR_ARGUMENTS:
2267 if (ARGCOUNT == 0)
2268 ea.line1 = ea.line2 = 0;
2269 else
2270 ea.line2 = ARGCOUNT;
2271 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002272#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002273 case ADDR_QUICKFIX:
2274 ea.line2 = qf_get_size(&ea);
2275 if (ea.line2 == 0)
2276 ea.line2 = 1;
2277 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002278#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281
2282 /* accept numbered register only when no count allowed (:put) */
2283 if ( (ea.argt & REGSTR)
2284 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002285 /* Do not allow register = for user commands */
2286 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2288 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002289#ifndef FEAT_CLIPBOARD
2290 /* check these explicitly for a more specific error message */
2291 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002293 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002294 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002297 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2298 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002299 {
2300 ea.regname = *ea.arg++;
2301#ifdef FEAT_EVAL
2302 /* for '=' register: accept the rest of the line as an expression */
2303 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2304 {
2305 set_expr_line(vim_strsave(ea.arg));
2306 ea.arg += STRLEN(ea.arg);
2307 }
2308#endif
2309 ea.arg = skipwhite(ea.arg);
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312
2313 /*
2314 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2315 * count, it's a buffer name.
2316 */
2317 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2318 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002319 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 n = getdigits(&ea.arg);
2322 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002323 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002325 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 goto doend;
2327 }
2328 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2329 {
2330 ea.line2 = n;
2331 if (ea.addr_count == 0)
2332 ea.addr_count = 1;
2333 }
2334 else
2335 {
2336 ea.line1 = ea.line2;
2337 ea.line2 += n - 1;
2338 ++ea.addr_count;
2339 /*
2340 * Be vi compatible: no error message for out of range.
2341 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002342 if (ea.addr_type == ADDR_LINES
2343 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 ea.line2 = curbuf->b_ml.ml_line_count;
2345 }
2346 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002347
2348 /*
2349 * Check for flags: 'l', 'p' and '#'.
2350 */
2351 if (ea.argt & EXFLAGS)
2352 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002354 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002355 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002357 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 goto doend;
2359 }
2360
2361 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002363 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 goto doend;
2365 }
2366
2367#ifdef FEAT_EVAL
2368 /*
2369 * Skip the command when it's not going to be executed.
2370 * The commands like :if, :endif, etc. always need to be executed.
2371 * Also make an exception for commands that handle a trailing command
2372 * themselves.
2373 */
2374 if (ea.skip)
2375 {
2376 switch (ea.cmdidx)
2377 {
2378 /* commands that need evaluation */
2379 case CMD_while:
2380 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002381 case CMD_for:
2382 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 case CMD_if:
2384 case CMD_elseif:
2385 case CMD_else:
2386 case CMD_endif:
2387 case CMD_try:
2388 case CMD_catch:
2389 case CMD_finally:
2390 case CMD_endtry:
2391 case CMD_function:
2392 break;
2393
2394 /* Commands that handle '|' themselves. Check: A command should
2395 * either have the TRLBAR flag, appear in this list or appear in
2396 * the list at ":help :bar". */
2397 case CMD_aboveleft:
2398 case CMD_and:
2399 case CMD_belowright:
2400 case CMD_botright:
2401 case CMD_browse:
2402 case CMD_call:
2403 case CMD_confirm:
2404 case CMD_delfunction:
2405 case CMD_djump:
2406 case CMD_dlist:
2407 case CMD_dsearch:
2408 case CMD_dsplit:
2409 case CMD_echo:
2410 case CMD_echoerr:
2411 case CMD_echomsg:
2412 case CMD_echon:
2413 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002414 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_help:
2416 case CMD_hide:
2417 case CMD_ijump:
2418 case CMD_ilist:
2419 case CMD_isearch:
2420 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002421 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_keepjumps:
2423 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002424 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_leftabove:
2426 case CMD_let:
2427 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002428 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002430 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002431 case CMD_noautocmd:
2432 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_perl:
2434 case CMD_psearch:
2435 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002436 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002437 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 case CMD_return:
2439 case CMD_rightbelow:
2440 case CMD_ruby:
2441 case CMD_silent:
2442 case CMD_smagic:
2443 case CMD_snomagic:
2444 case CMD_substitute:
2445 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002446 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_tcl:
2448 case CMD_throw:
2449 case CMD_tilde:
2450 case CMD_topleft:
2451 case CMD_unlet:
2452 case CMD_verbose:
2453 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002454 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 break;
2456
2457 default: goto doend;
2458 }
2459 }
2460#endif
2461
2462 if (ea.argt & XFILE)
2463 {
2464 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2465 goto doend;
2466 }
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 /*
2469 * Accept buffer name. Cannot be used at the same time with a buffer
2470 * number. Don't do this for a user command.
2471 */
2472 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 /*
2476 * :bdelete, :bwipeout and :bunload take several arguments, separated
2477 * by spaces: find next space (skipping over escaped characters).
2478 * The others take one argument: ignore trailing spaces.
2479 */
2480 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2481 || ea.cmdidx == CMD_bunload)
2482 p = skiptowhite_esc(ea.arg);
2483 else
2484 {
2485 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002486 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 --p;
2488 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002489 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2490 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (ea.line2 < 0) /* failed */
2492 goto doend;
2493 ea.addr_count = 1;
2494 ea.arg = skipwhite(p);
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
Bram Moolenaar2be57332018-02-13 18:05:18 +01002497 /* The :try command saves the emsg_silent flag, reset it here when
2498 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002499 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002500 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002501 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002502 if (emsg_silent < 0)
2503 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002504 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002505 }
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002508 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002512 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 /*
2515 * Execute a user-defined command.
2516 */
2517 do_ucmd(&ea);
2518 }
2519 else
2520#endif
2521 {
2522 /*
2523 * Call the function to execute the command.
2524 */
2525 ea.errmsg = NULL;
2526 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2527 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002528 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530
2531#ifdef FEAT_EVAL
2532 /*
2533 * If the command just executed called do_cmdline(), any throw or ":return"
2534 * or ":finish" encountered there must also check the cstack of the still
2535 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2536 * exception, or reanimate a returned function or finished script file and
2537 * return or finish it again.
2538 */
2539 if (need_rethrow)
2540 do_throw(cstack);
2541 else if (check_cstack)
2542 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002543 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 && current_func_returned())
2547 do_return(&ea, TRUE, FALSE, NULL);
2548 }
2549 need_rethrow = check_cstack = FALSE;
2550#endif
2551
2552doend:
2553 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002556 curwin->w_cursor.col = 0;
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2560 {
2561 if (sourcing)
2562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002563 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002568 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 emsg(errormsg);
2571 }
2572#ifdef FEAT_EVAL
2573 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002574 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2575 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
2577
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 if (ea.verbose_save >= 0)
2579 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002580
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002581 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 cmdmod = save_cmdmod;
2583
Bram Moolenaareffed932018-08-14 13:38:17 +02002584 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 /* messages could be enabled for a serious error, need to check if the
2587 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002588 if (!did_emsg || msg_silent > ea.save_msg_silent)
2589 msg_silent = ea.save_msg_silent;
2590 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (emsg_silent < 0)
2592 emsg_silent = 0;
2593 /* Restore msg_scroll, it's set by file I/O commands, even when no
2594 * message is actually displayed. */
2595 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002596
2597 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2598 * somewhere in the line. Put it back in the first column. */
2599 if (redirecting())
2600 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002603#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002605 --sandbox;
2606#endif
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2609 ea.nextcmd = NULL;
2610
2611#ifdef FEAT_EVAL
2612 --ex_nesting_level;
2613#endif
2614
2615 return ea.nextcmd;
2616}
2617#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002618 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619#endif
2620
2621/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 * Parse and skip over command modifiers:
2623 * - update eap->cmd
2624 * - store flags in "cmdmod".
2625 * - Set ex_pressedreturn for an empty command line.
2626 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002627 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002628 * - set p_verbose for ":verbose"
2629 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002630 * When "skip_only" is TRUE the global variables are not changed, except for
2631 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 * Return FAIL when the command is not to be executed.
2633 * May set "errormsg" to an error message.
2634 */
2635 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002637{
2638 char_u *p;
2639
2640 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2641 eap->verbose_save = -1;
2642 eap->save_msg_silent = -1;
2643
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002644 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 for (;;)
2646 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2648 ++eap->cmd;
2649
2650 /* in ex mode, an empty line works like :+ */
2651 if (*eap->cmd == NUL && exmode_active
2652 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2653 || getline_equal(eap->getline, eap->cookie, getexline))
2654 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2655 {
2656 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002657 if (!skip_only)
2658 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002659 }
2660
2661 /* ignore comment and empty lines */
2662 if (*eap->cmd == '"')
2663 return FAIL;
2664 if (*eap->cmd == NUL)
2665 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002666 if (!skip_only)
2667 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002668 return FAIL;
2669 }
2670
Bram Moolenaareffed932018-08-14 13:38:17 +02002671 p = skip_range(eap->cmd, NULL);
2672 switch (*p)
2673 {
2674 /* When adding an entry, also modify cmd_exists(). */
2675 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2676 break;
2677 cmdmod.split |= WSP_ABOVE;
2678 continue;
2679
2680 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2681 {
2682 cmdmod.split |= WSP_BELOW;
2683 continue;
2684 }
2685 if (checkforcmd(&eap->cmd, "browse", 3))
2686 {
2687#ifdef FEAT_BROWSE_CMD
2688 cmdmod.browse = TRUE;
2689#endif
2690 continue;
2691 }
2692 if (!checkforcmd(&eap->cmd, "botright", 2))
2693 break;
2694 cmdmod.split |= WSP_BOT;
2695 continue;
2696
2697 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2698 break;
2699#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2700 cmdmod.confirm = TRUE;
2701#endif
2702 continue;
2703
2704 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2705 {
2706 cmdmod.keepmarks = TRUE;
2707 continue;
2708 }
2709 if (checkforcmd(&eap->cmd, "keepalt", 5))
2710 {
2711 cmdmod.keepalt = TRUE;
2712 continue;
2713 }
2714 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2715 {
2716 cmdmod.keeppatterns = TRUE;
2717 continue;
2718 }
2719 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2720 break;
2721 cmdmod.keepjumps = TRUE;
2722 continue;
2723
2724 case 'f': /* only accept ":filter {pat} cmd" */
2725 {
2726 char_u *reg_pat;
2727
2728 if (!checkforcmd(&p, "filter", 4)
2729 || *p == NUL || ends_excmd(*p))
2730 break;
2731 if (*p == '!')
2732 {
2733 cmdmod.filter_force = TRUE;
2734 p = skipwhite(p + 1);
2735 if (*p == NUL || ends_excmd(*p))
2736 break;
2737 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002738 if (skip_only)
2739 p = skip_vimgrep_pat(p, NULL, NULL);
2740 else
2741 // NOTE: This puts a NUL after the pattern.
2742 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 if (p == NULL || *p == NUL)
2744 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002745 if (!skip_only)
2746 {
2747 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002748 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002749 if (cmdmod.filter_regmatch.regprog == NULL)
2750 break;
2751 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 eap->cmd = p;
2753 continue;
2754 }
2755
2756 /* ":hide" and ":hide | cmd" are not modifiers */
2757 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2758 || *p == NUL || ends_excmd(*p))
2759 break;
2760 eap->cmd = p;
2761 cmdmod.hide = TRUE;
2762 continue;
2763
2764 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2765 {
2766 cmdmod.lockmarks = TRUE;
2767 continue;
2768 }
2769
2770 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2771 break;
2772 cmdmod.split |= WSP_ABOVE;
2773 continue;
2774
2775 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2776 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002777 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 {
2779 /* Set 'eventignore' to "all". Restore the
2780 * existing option value later. */
2781 cmdmod.save_ei = vim_strsave(p_ei);
2782 set_string_option_direct((char_u *)"ei", -1,
2783 (char_u *)"all", OPT_FREE, SID_NONE);
2784 }
2785 continue;
2786 }
2787 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2788 break;
2789 cmdmod.noswapfile = TRUE;
2790 continue;
2791
2792 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2793 break;
2794 cmdmod.split |= WSP_BELOW;
2795 continue;
2796
2797 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2798 {
2799#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002800 if (!skip_only)
2801 {
2802 if (!eap->did_sandbox)
2803 ++sandbox;
2804 eap->did_sandbox = TRUE;
2805 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002806#endif
2807 continue;
2808 }
2809 if (!checkforcmd(&eap->cmd, "silent", 3))
2810 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002811 if (!skip_only)
2812 {
2813 if (eap->save_msg_silent == -1)
2814 eap->save_msg_silent = msg_silent;
2815 ++msg_silent;
2816 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2818 {
2819 /* ":silent!", but not "silent !cmd" */
2820 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002821 if (!skip_only)
2822 {
2823 ++emsg_silent;
2824 ++eap->did_esilent;
2825 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 }
2827 continue;
2828
2829 case 't': if (checkforcmd(&p, "tab", 3))
2830 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002831 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002833 long tabnr = get_address(eap, &eap->cmd,
2834 ADDR_TABS, eap->skip,
2835 skip_only, FALSE, 1);
2836 if (tabnr == MAXLNUM)
2837 cmdmod.tab = tabpage_index(curtab) + 1;
2838 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002840 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002843 return FAIL;
2844 }
2845 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 }
2848 eap->cmd = p;
2849 continue;
2850 }
2851 if (!checkforcmd(&eap->cmd, "topleft", 2))
2852 break;
2853 cmdmod.split |= WSP_TOP;
2854 continue;
2855
2856 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2857 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002858 if (!skip_only)
2859 {
2860 if (eap->save_msg_silent == -1)
2861 eap->save_msg_silent = msg_silent;
2862 msg_silent = 0;
2863 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 continue;
2865
2866 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2867 {
2868 cmdmod.split |= WSP_VERT;
2869 continue;
2870 }
2871 if (!checkforcmd(&p, "verbose", 4))
2872 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002873 if (!skip_only)
2874 {
2875 if (eap->verbose_save < 0)
2876 eap->verbose_save = p_verbose;
2877 if (vim_isdigit(*eap->cmd))
2878 p_verbose = atoi((char *)eap->cmd);
2879 else
2880 p_verbose = 1;
2881 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 eap->cmd = p;
2883 continue;
2884 }
2885 break;
2886 }
2887
2888 return OK;
2889}
2890
2891/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002892 * Free contents of "cmdmod".
2893 */
2894 static void
2895free_cmdmod(void)
2896{
2897 if (cmdmod.save_ei != NULL)
2898 {
2899 /* Restore 'eventignore' to the value before ":noautocmd". */
2900 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2901 OPT_FREE, SID_NONE);
2902 free_string_option(cmdmod.save_ei);
2903 }
2904
2905 if (cmdmod.filter_regmatch.regprog != NULL)
2906 vim_regfree(cmdmod.filter_regmatch.regprog);
2907}
2908
2909/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002910 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002911 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002912 * Return FAIL and set "errormsg" or return OK.
2913 */
2914 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002915parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002916{
2917 int address_count = 1;
2918 linenr_T lnum;
2919
2920 // Repeat for all ',' or ';' separated addresses.
2921 for (;;)
2922 {
2923 eap->line1 = eap->line2;
2924 switch (eap->addr_type)
2925 {
2926 case ADDR_LINES:
2927 // default is current line number
2928 eap->line2 = curwin->w_cursor.lnum;
2929 break;
2930 case ADDR_WINDOWS:
2931 eap->line2 = CURRENT_WIN_NR;
2932 break;
2933 case ADDR_ARGUMENTS:
2934 eap->line2 = curwin->w_arg_idx + 1;
2935 if (eap->line2 > ARGCOUNT)
2936 eap->line2 = ARGCOUNT;
2937 break;
2938 case ADDR_LOADED_BUFFERS:
2939 case ADDR_BUFFERS:
2940 eap->line2 = curbuf->b_fnum;
2941 break;
2942 case ADDR_TABS:
2943 eap->line2 = CURRENT_TAB_NR;
2944 break;
2945 case ADDR_TABS_RELATIVE:
2946 eap->line2 = 1;
2947 break;
2948#ifdef FEAT_QUICKFIX
2949 case ADDR_QUICKFIX:
2950 eap->line2 = qf_get_cur_valid_idx(eap);
2951 break;
2952#endif
2953 }
2954 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002955 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002956 eap->addr_count == 0, address_count++);
2957 if (eap->cmd == NULL) // error detected
2958 return FAIL;
2959 if (lnum == MAXLNUM)
2960 {
2961 if (*eap->cmd == '%') // '%' - all lines
2962 {
2963 ++eap->cmd;
2964 switch (eap->addr_type)
2965 {
2966 case ADDR_LINES:
2967 eap->line1 = 1;
2968 eap->line2 = curbuf->b_ml.ml_line_count;
2969 break;
2970 case ADDR_LOADED_BUFFERS:
2971 {
2972 buf_T *buf = firstbuf;
2973
2974 while (buf->b_next != NULL
2975 && buf->b_ml.ml_mfp == NULL)
2976 buf = buf->b_next;
2977 eap->line1 = buf->b_fnum;
2978 buf = lastbuf;
2979 while (buf->b_prev != NULL
2980 && buf->b_ml.ml_mfp == NULL)
2981 buf = buf->b_prev;
2982 eap->line2 = buf->b_fnum;
2983 break;
2984 }
2985 case ADDR_BUFFERS:
2986 eap->line1 = firstbuf->b_fnum;
2987 eap->line2 = lastbuf->b_fnum;
2988 break;
2989 case ADDR_WINDOWS:
2990 case ADDR_TABS:
2991 if (IS_USER_CMDIDX(eap->cmdidx))
2992 {
2993 eap->line1 = 1;
2994 eap->line2 = eap->addr_type == ADDR_WINDOWS
2995 ? LAST_WIN_NR : LAST_TAB_NR;
2996 }
2997 else
2998 {
2999 // there is no Vim command which uses '%' and
3000 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002 return FAIL;
3003 }
3004 break;
3005 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003006 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003007 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008 return FAIL;
3009 case ADDR_ARGUMENTS:
3010 if (ARGCOUNT == 0)
3011 eap->line1 = eap->line2 = 0;
3012 else
3013 {
3014 eap->line1 = 1;
3015 eap->line2 = ARGCOUNT;
3016 }
3017 break;
3018#ifdef FEAT_QUICKFIX
3019 case ADDR_QUICKFIX:
3020 eap->line1 = 1;
3021 eap->line2 = qf_get_size(eap);
3022 if (eap->line2 == 0)
3023 eap->line2 = 1;
3024 break;
3025#endif
3026 }
3027 ++eap->addr_count;
3028 }
3029 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3030 {
3031 pos_T *fp;
3032
3033 // '*' - visual area
3034 if (eap->addr_type != ADDR_LINES)
3035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003036 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 return FAIL;
3038 }
3039
3040 ++eap->cmd;
3041 if (!eap->skip)
3042 {
3043 fp = getmark('<', FALSE);
3044 if (check_mark(fp) == FAIL)
3045 return FAIL;
3046 eap->line1 = fp->lnum;
3047 fp = getmark('>', FALSE);
3048 if (check_mark(fp) == FAIL)
3049 return FAIL;
3050 eap->line2 = fp->lnum;
3051 ++eap->addr_count;
3052 }
3053 }
3054 }
3055 else
3056 eap->line2 = lnum;
3057 eap->addr_count++;
3058
3059 if (*eap->cmd == ';')
3060 {
3061 if (!eap->skip)
3062 {
3063 curwin->w_cursor.lnum = eap->line2;
3064 // don't leave the cursor on an illegal line or column
3065 check_cursor();
3066 }
3067 }
3068 else if (*eap->cmd != ',')
3069 break;
3070 ++eap->cmd;
3071 }
3072
3073 // One address given: set start and end lines.
3074 if (eap->addr_count == 1)
3075 {
3076 eap->line1 = eap->line2;
3077 // ... but only implicit: really no address given
3078 if (lnum == MAXLNUM)
3079 eap->addr_count = 0;
3080 }
3081 return OK;
3082}
3083
3084/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003085 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * If there is a match advance "pp" to the argument and return TRUE.
3087 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003088 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003089checkforcmd(
3090 char_u **pp, /* start of command */
3091 char *cmd, /* name of command */
3092 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 int i;
3095
3096 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003097 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 break;
3099 if (i >= len && !isalpha((*pp)[i]))
3100 {
3101 *pp = skipwhite(*pp + i);
3102 return TRUE;
3103 }
3104 return FALSE;
3105}
3106
3107/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003108 * Append "cmd" to the error message in IObuff.
3109 * Takes care of limiting the length and handling 0xa0, which would be
3110 * invisible otherwise.
3111 */
3112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003113append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003114{
3115 char_u *s = cmd;
3116 char_u *d;
3117
3118 STRCAT(IObuff, ": ");
3119 d = IObuff + STRLEN(IObuff);
3120 while (*s != NUL && d - IObuff < IOSIZE - 7)
3121 {
3122 if (
3123#ifdef FEAT_MBYTE
3124 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3125#endif
3126 *s == 0xa0)
3127 {
3128 s +=
3129#ifdef FEAT_MBYTE
3130 enc_utf8 ? 2 :
3131#endif
3132 1;
3133 STRCPY(d, "<a0>");
3134 d += 4;
3135 }
3136 else
3137 MB_COPY_CHAR(s, d);
3138 }
3139 *d = NUL;
3140}
3141
3142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003144 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 * Returns NULL for an ambiguous user command.
3148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003150find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 int len;
3153 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003154 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 /*
3157 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003158 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * - the 'k' command can directly be followed by any character.
3160 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003161 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003163 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 */
3165 p = eap->cmd;
3166 if (*p == 'k')
3167 {
3168 eap->cmdidx = CMD_k;
3169 ++p;
3170 }
3171 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003172 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3173 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 || p[1] == 'g'
3175 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3176 || p[1] == 'I'
3177 || (p[1] == 'r' && p[2] != 'e')))
3178 {
3179 eap->cmdidx = CMD_substitute;
3180 ++p;
3181 }
3182 else
3183 {
3184 while (ASCII_ISALPHA(*p))
3185 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003186 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003187 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003188 while (ASCII_ISALNUM(*p))
3189 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 /* check for non-alpha command */
3192 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3193 ++p;
3194 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003195 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3196 {
3197 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3198 * :delete with the 'l' flag. Same for 'p'. */
3199 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003200 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003201 break;
3202 if (i == len - 1)
3203 {
3204 --len;
3205 if (p[-1] == 'l')
3206 eap->flags |= EXFLAG_LIST;
3207 else
3208 eap->flags |= EXFLAG_PRINT;
3209 }
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003212 if (ASCII_ISLOWER(eap->cmd[0]))
3213 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003214 int c1 = eap->cmd[0];
3215 int c2 = eap->cmd[1];
3216
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003217 if (command_count != (int)CMD_SIZE)
3218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003219 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003220 getout(1);
3221 }
3222
3223 /* Use a precomputed index for fast look-up in cmdnames[]
3224 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003225 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3226 if (ASCII_ISLOWER(c2))
3227 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003230 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
3232 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3233 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3234 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3235 (size_t)len) == 0)
3236 {
3237#ifdef FEAT_EVAL
3238 if (full != NULL
3239 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3240 *full = TRUE;
3241#endif
3242 break;
3243 }
3244
3245#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003246 /* Look for a user defined command as a last resort. Let ":Print" be
3247 * overruled by a user defined command. */
3248 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3249 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 while (ASCII_ISALNUM(*p))
3253 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
3256#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 eap->cmdidx = CMD_SIZE;
3259 }
3260
3261 return p;
3262}
3263
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003264#ifdef FEAT_USR_CMDS
3265/*
3266 * Search for a user command that matches "eap->cmd".
3267 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3268 * Return a pointer to just after the command.
3269 * Return NULL if there is no matching command.
3270 */
3271 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003272find_ucmd(
3273 exarg_T *eap,
3274 char_u *p, /* end of the command (possibly including count) */
3275 int *full, /* set to TRUE for a full match */
3276 expand_T *xp, /* used for completion, NULL otherwise */
3277 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278{
3279 int len = (int)(p - eap->cmd);
3280 int j, k, matchlen = 0;
3281 ucmd_T *uc;
3282 int found = FALSE;
3283 int possible = FALSE;
3284 char_u *cp, *np; /* Point into typed cmd and test name */
3285 garray_T *gap;
3286 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3287 only full match global is accepted. */
3288
3289 /*
3290 * Look for buffer-local user commands first, then global ones.
3291 */
3292 gap = &curbuf->b_ucmds;
3293 for (;;)
3294 {
3295 for (j = 0; j < gap->ga_len; ++j)
3296 {
3297 uc = USER_CMD_GA(gap, j);
3298 cp = eap->cmd;
3299 np = uc->uc_name;
3300 k = 0;
3301 while (k < len && *np != NUL && *cp++ == *np++)
3302 k++;
3303 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3304 {
3305 /* If finding a second match, the command is ambiguous. But
3306 * not if a buffer-local command wasn't a full match and a
3307 * global command is a full match. */
3308 if (k == len && found && *np != NUL)
3309 {
3310 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003311 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003312 amb_local = TRUE;
3313 }
3314
3315 if (!found || (k == len && *np == NUL))
3316 {
3317 /* If we matched up to a digit, then there could
3318 * be another command including the digit that we
3319 * should use instead.
3320 */
3321 if (k == len)
3322 found = TRUE;
3323 else
3324 possible = TRUE;
3325
3326 if (gap == &ucmds)
3327 eap->cmdidx = CMD_USER;
3328 else
3329 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003330 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003331 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003332 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333
3334# ifdef FEAT_CMDL_COMPL
3335 if (compl != NULL)
3336 *compl = uc->uc_compl;
3337# ifdef FEAT_EVAL
3338 if (xp != NULL)
3339 {
3340 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003341 xp->xp_script_ctx = uc->uc_script_ctx;
3342 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003343 }
3344# endif
3345# endif
3346 /* Do not search for further abbreviations
3347 * if this is an exact match. */
3348 matchlen = k;
3349 if (k == len && *np == NUL)
3350 {
3351 if (full != NULL)
3352 *full = TRUE;
3353 amb_local = FALSE;
3354 break;
3355 }
3356 }
3357 }
3358 }
3359
3360 /* Stop if we found a full match or searched all. */
3361 if (j < gap->ga_len || gap == &ucmds)
3362 break;
3363 gap = &ucmds;
3364 }
3365
3366 /* Only found ambiguous matches. */
3367 if (amb_local)
3368 {
3369 if (xp != NULL)
3370 xp->xp_context = EXPAND_UNSUCCESSFUL;
3371 return NULL;
3372 }
3373
3374 /* The match we found may be followed immediately by a number. Move "p"
3375 * back to point to it. */
3376 if (found || possible)
3377 return p + (matchlen - len);
3378 return p;
3379}
3380#endif
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003383static struct cmdmod
3384{
3385 char *name;
3386 int minlen;
3387 int has_count; /* :123verbose :3tab */
3388} cmdmods[] = {
3389 {"aboveleft", 3, FALSE},
3390 {"belowright", 3, FALSE},
3391 {"botright", 2, FALSE},
3392 {"browse", 3, FALSE},
3393 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003394 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003395 {"hide", 3, FALSE},
3396 {"keepalt", 5, FALSE},
3397 {"keepjumps", 5, FALSE},
3398 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003399 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003400 {"leftabove", 5, FALSE},
3401 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003402 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003403 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003404 {"rightbelow", 6, FALSE},
3405 {"sandbox", 3, FALSE},
3406 {"silent", 3, FALSE},
3407 {"tab", 3, TRUE},
3408 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003409 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003410 {"verbose", 4, TRUE},
3411 {"vertical", 4, FALSE},
3412};
3413
3414/*
3415 * Return length of a command modifier (including optional count).
3416 * Return zero when it's not a modifier.
3417 */
3418 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003419modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003420{
3421 int i, j;
3422 char_u *p = cmd;
3423
3424 if (VIM_ISDIGIT(*cmd))
3425 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003426 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427 {
3428 for (j = 0; p[j] != NUL; ++j)
3429 if (p[j] != cmdmods[i].name[j])
3430 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003431 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003432 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003433 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003434 }
3435 return 0;
3436}
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438/*
3439 * Return > 0 if an Ex command "name" exists.
3440 * Return 2 if there is an exact match.
3441 * Return 3 if there is an ambiguous match.
3442 */
3443 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003444cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 exarg_T ea;
3447 int full = FALSE;
3448 int i;
3449 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003450 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
3452 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003453 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
3455 for (j = 0; name[j] != NUL; ++j)
3456 if (name[j] != cmdmods[i].name[j])
3457 break;
3458 if (name[j] == NUL && j >= cmdmods[i].minlen)
3459 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3460 }
3461
Bram Moolenaara9587612006-05-04 21:47:50 +00003462 /* Check built-in commands and user defined commands.
3463 * For ":2match" and ":3match" we need to skip the number. */
3464 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003466 p = find_command(&ea, &full);
3467 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003469 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3470 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003471 if (*skipwhite(p) != NUL)
3472 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3474}
3475#endif
3476
3477/*
3478 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3479 * we don't need/want deleted. Maybe this could be done better if we didn't
3480 * repeat all this stuff. The only problem is that they may not stay
3481 * perfectly compatible with each other, but then the command line syntax
3482 * probably won't change that much -- webb.
3483 */
3484 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003485set_one_cmd_context(
3486 expand_T *xp,
3487 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 char_u *p;
3490 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 int len = 0;
3492 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3494 int compl = EXPAND_NOTHING;
3495#endif
3496#ifdef FEAT_CMDL_COMPL
3497 int delim;
3498#endif
3499 int forceit = FALSE;
3500 int usefilter = FALSE; /* filter instead of file name */
3501
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003502 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 xp->xp_pattern = buff;
3504 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003505 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003508 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
3510 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3511 ;
3512 xp->xp_pattern = cmd;
3513
3514 if (*cmd == NUL)
3515 return NULL;
3516 if (*cmd == '"') /* ignore comment lines */
3517 {
3518 xp->xp_context = EXPAND_NOTHING;
3519 return NULL;
3520 }
3521
3522/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003523 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 xp->xp_pattern = cmd;
3527 if (*cmd == NUL)
3528 return NULL;
3529 if (*cmd == '"')
3530 {
3531 xp->xp_context = EXPAND_NOTHING;
3532 return NULL;
3533 }
3534
3535 if (*cmd == '|' || *cmd == '\n')
3536 return cmd + 1; /* There's another command */
3537
3538 /*
3539 * Isolate the command and search for it in the command table.
3540 * Exceptions:
3541 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003542 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3544 */
3545 if (*cmd == 'k' && cmd[1] != 'e')
3546 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003547 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 p = cmd + 1;
3549 }
3550 else
3551 {
3552 p = cmd;
3553 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3554 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003555 /* a user command may contain digits */
3556 if (ASCII_ISUPPER(cmd[0]))
3557 while (ASCII_ISALNUM(*p) || *p == '*')
3558 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003559 /* for python 3.x: ":py3*" commands completion */
3560 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003561 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003562 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003563 while (ASCII_ISALPHA(*p) || *p == '*')
3564 ++p;
3565 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003566 /* check for non-alpha command */
3567 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3568 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003569 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573 xp->xp_context = EXPAND_UNSUCCESSFUL;
3574 return NULL;
3575 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003577 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3578 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3579 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
3581
3582#ifdef FEAT_USR_CMDS
3583 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3585 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#endif
3587 }
3588
3589 /*
3590 * If the cursor is touching the command, and it ends in an alpha-numeric
3591 * character, complete the command name.
3592 */
3593 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3594 return NULL;
3595
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3599 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003600 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 p = cmd + 1;
3602 }
3603#ifdef FEAT_USR_CMDS
3604 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3605 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003606 ea.cmd = cmd;
3607 p = find_ucmd(&ea, p, NULL, xp,
3608# if defined(FEAT_CMDL_COMPL)
3609 &compl
3610# else
3611 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003613 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003614 if (p == NULL)
3615 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617#endif
3618 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 /* Not still touching the command and it was an illegal one */
3622 xp->xp_context = EXPAND_UNSUCCESSFUL;
3623 return NULL;
3624 }
3625
3626 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3627
3628 if (*p == '!') /* forced commands */
3629 {
3630 forceit = TRUE;
3631 ++p;
3632 }
3633
3634/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003635 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003637 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003638 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 arg = skipwhite(p);
3641
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003642 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 if (*arg == '>') /* append */
3645 {
3646 if (*++arg == '>')
3647 ++arg;
3648 arg = skipwhite(arg);
3649 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 ++arg;
3653 usefilter = TRUE;
3654 }
3655 }
3656
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003657 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 usefilter = forceit; /* :r! filter if forced */
3660 if (*arg == '!') /* :r !filter */
3661 {
3662 ++arg;
3663 usefilter = TRUE;
3664 }
3665 }
3666
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003667 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 while (*arg == *cmd) /* allow any number of '>' or '<' */
3670 ++arg;
3671 arg = skipwhite(arg);
3672 }
3673
3674 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003675 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
3677 /* Check if we're in the +command */
3678 p = arg + 1;
3679 arg = skip_cmd_arg(arg, FALSE);
3680
3681 /* Still touching the command after '+'? */
3682 if (*arg == NUL)
3683 return p;
3684
3685 /* Skip space(s) after +command to get to the real argument */
3686 arg = skipwhite(arg);
3687 }
3688
3689 /*
3690 * Check for '|' to separate commands and '"' to start comments.
3691 * Don't do this for ":read !cmd" and ":write !cmd".
3692 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003693 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
3695 p = arg;
3696 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003697 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 p += 2;
3699 while (*p)
3700 {
3701 if (*p == Ctrl_V)
3702 {
3703 if (p[1] != NUL)
3704 ++p;
3705 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003706 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 || *p == '|' || *p == '\n')
3708 {
3709 if (*(p - 1) != '\\')
3710 {
3711 if (*p == '|' || *p == '\n')
3712 return p + 1;
3713 return NULL; /* It's a comment */
3714 }
3715 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003716 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 }
3719
3720 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003721 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 vim_strchr((char_u *)"|\"", *arg) == NULL)
3723 return NULL;
3724
3725 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003726 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003728 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003729 while (*p && p < buff + len)
3730 {
3731 if (*p == ' ' || *p == TAB)
3732 {
3733 /* argument starts after a space */
3734 xp->xp_pattern = ++p;
3735 }
3736 else
3737 {
3738 if (*p == '\\' && *(p + 1) != NUL)
3739 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003740 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003741 }
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003744 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746 int c;
3747 int in_quote = FALSE;
3748 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 /*
3751 * Allow spaces within back-quotes to count as part of the argument
3752 * being expanded.
3753 */
3754 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003755 p = xp->xp_pattern;
3756 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003758#ifdef FEAT_MBYTE
3759 if (has_mbyte)
3760 c = mb_ptr2char(p);
3761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003763 c = *p;
3764 if (c == '\\' && p[1] != NUL)
3765 ++p;
3766 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
3768 if (!in_quote)
3769 {
3770 xp->xp_pattern = p;
3771 bow = p + 1;
3772 }
3773 in_quote = !in_quote;
3774 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003775 /* An argument can contain just about everything, except
3776 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003777 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003778#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003779 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003780#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003781 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003782 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003783 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003784 while (*p != NUL)
3785 {
3786#ifdef FEAT_MBYTE
3787 if (has_mbyte)
3788 c = mb_ptr2char(p);
3789 else
3790#endif
3791 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003792 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003793 break;
3794#ifdef FEAT_MBYTE
3795 if (has_mbyte)
3796 len = (*mb_ptr2len)(p);
3797 else
3798#endif
3799 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003800 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003801 }
3802 if (in_quote)
3803 bow = p;
3804 else
3805 xp->xp_pattern = p;
3806 p -= len;
3807 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003808 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810
3811 /*
3812 * If we are still inside the quotes, and we passed a space, just
3813 * expand from there.
3814 */
3815 if (bow != NULL && in_quote)
3816 xp->xp_pattern = bow;
3817 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003818
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003819 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003820 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003821 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003822#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003823 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003824#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003825 /* When still after the command name expand executables. */
3826 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003827 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 /* Check for environment variable */
3831 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003832#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 || *xp->xp_pattern == '%'
3834#endif
3835 )
3836 {
3837 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3838 if (!vim_isIDc(*p))
3839 break;
3840 if (*p == NUL)
3841 {
3842 xp->xp_context = EXPAND_ENV_VARS;
3843 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003844#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3845 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003846 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003847 compl = EXPAND_ENV_VARS;
3848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003851#if defined(FEAT_CMDL_COMPL)
3852 /* Check for user names */
3853 if (*xp->xp_pattern == '~')
3854 {
3855 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3856 ;
3857 /* Complete ~user only if it partially matches a user name.
3858 * A full match ~user<Tab> will be replaced by user's home
3859 * directory i.e. something like ~user<Tab> -> /home/user/ */
3860 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003861 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003862 {
3863 xp->xp_context = EXPAND_USER;
3864 ++xp->xp_pattern;
3865 }
3866 }
3867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869
3870/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003871 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003873 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003875 case CMD_find:
3876 case CMD_sfind:
3877 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003878 if (xp->xp_context == EXPAND_FILES)
3879 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003880 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_cd:
3882 case CMD_chdir:
3883 case CMD_lcd:
3884 case CMD_lchdir:
3885 if (xp->xp_context == EXPAND_FILES)
3886 xp->xp_context = EXPAND_DIRECTORIES;
3887 break;
3888 case CMD_help:
3889 xp->xp_context = EXPAND_HELP;
3890 xp->xp_pattern = arg;
3891 break;
3892
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003893 /* Command modifiers: return the argument.
3894 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003896 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 case CMD_belowright:
3898 case CMD_botright:
3899 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003900 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003901 case CMD_cdo:
3902 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003904 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 case CMD_folddoclosed:
3906 case CMD_folddoopen:
3907 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003908 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 case CMD_keepjumps:
3910 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003911 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003912 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003914 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003916 case CMD_noautocmd:
3917 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003919 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003921 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003922 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 case CMD_topleft:
3924 case CMD_verbose:
3925 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003926 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 return arg;
3928
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003929 case CMD_filter:
3930 if (*arg != NUL)
3931 arg = skip_vimgrep_pat(arg, NULL, NULL);
3932 if (arg == NULL || *arg == NUL)
3933 {
3934 xp->xp_context = EXPAND_NOTHING;
3935 return NULL;
3936 }
3937 return skipwhite(arg);
3938
Bram Moolenaar4f688582007-07-24 12:34:30 +00003939#ifdef FEAT_CMDL_COMPL
3940# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 case CMD_match:
3942 if (*arg == NUL || !ends_excmd(*arg))
3943 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003944 /* also complete "None" */
3945 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 arg = skipwhite(skiptowhite(arg));
3947 if (*arg != NUL)
3948 {
3949 xp->xp_context = EXPAND_NOTHING;
3950 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3951 }
3952 }
3953 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956/*
3957 * All completion for the +cmdline_compl feature goes here.
3958 */
3959
3960# ifdef FEAT_USR_CMDS
3961 case CMD_command:
3962 /* Check for attributes */
3963 while (*arg == '-')
3964 {
3965 arg++; /* Skip "-" */
3966 p = skiptowhite(arg);
3967 if (*p == NUL)
3968 {
3969 /* Cursor is still in the attribute */
3970 p = vim_strchr(arg, '=');
3971 if (p == NULL)
3972 {
3973 /* No "=", so complete attribute names */
3974 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3975 xp->xp_pattern = arg;
3976 return NULL;
3977 }
3978
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003979 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 * their arguments as well.
3981 */
3982 if (STRNICMP(arg, "complete", p - arg) == 0)
3983 {
3984 xp->xp_context = EXPAND_USER_COMPLETE;
3985 xp->xp_pattern = p + 1;
3986 return NULL;
3987 }
3988 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3989 {
3990 xp->xp_context = EXPAND_USER_NARGS;
3991 xp->xp_pattern = p + 1;
3992 return NULL;
3993 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003994 else if (STRNICMP(arg, "addr", p - arg) == 0)
3995 {
3996 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3997 xp->xp_pattern = p + 1;
3998 return NULL;
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return NULL;
4001 }
4002 arg = skipwhite(p);
4003 }
4004
4005 /* After the attributes comes the new command name */
4006 p = skiptowhite(arg);
4007 if (*p == NUL)
4008 {
4009 xp->xp_context = EXPAND_USER_COMMANDS;
4010 xp->xp_pattern = arg;
4011 break;
4012 }
4013
4014 /* And finally comes a normal command */
4015 return skipwhite(p);
4016
4017 case CMD_delcommand:
4018 xp->xp_context = EXPAND_USER_COMMANDS;
4019 xp->xp_pattern = arg;
4020 break;
4021# endif
4022
4023 case CMD_global:
4024 case CMD_vglobal:
4025 delim = *arg; /* get the delimiter */
4026 if (delim)
4027 ++arg; /* skip delimiter if there is one */
4028
4029 while (arg[0] != NUL && arg[0] != delim)
4030 {
4031 if (arg[0] == '\\' && arg[1] != NUL)
4032 ++arg;
4033 ++arg;
4034 }
4035 if (arg[0] != NUL)
4036 return arg + 1;
4037 break;
4038 case CMD_and:
4039 case CMD_substitute:
4040 delim = *arg;
4041 if (delim)
4042 {
4043 /* skip "from" part */
4044 ++arg;
4045 arg = skip_regexp(arg, delim, p_magic, NULL);
4046 }
4047 /* skip "to" part */
4048 while (arg[0] != NUL && arg[0] != delim)
4049 {
4050 if (arg[0] == '\\' && arg[1] != NUL)
4051 ++arg;
4052 ++arg;
4053 }
4054 if (arg[0] != NUL) /* skip delimiter */
4055 ++arg;
4056 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4057 ++arg;
4058 if (arg[0] != NUL)
4059 return arg;
4060 break;
4061 case CMD_isearch:
4062 case CMD_dsearch:
4063 case CMD_ilist:
4064 case CMD_dlist:
4065 case CMD_ijump:
4066 case CMD_psearch:
4067 case CMD_djump:
4068 case CMD_isplit:
4069 case CMD_dsplit:
4070 arg = skipwhite(skipdigits(arg)); /* skip count */
4071 if (*arg == '/') /* Match regexp, not just whole words */
4072 {
4073 for (++arg; *arg && *arg != '/'; arg++)
4074 if (*arg == '\\' && arg[1] != NUL)
4075 arg++;
4076 if (*arg)
4077 {
4078 arg = skipwhite(arg + 1);
4079
4080 /* Check for trailing illegal characters */
4081 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4082 xp->xp_context = EXPAND_NOTHING;
4083 else
4084 return arg;
4085 }
4086 }
4087 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 case CMD_autocmd:
4090 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004092 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 case CMD_set:
4095 set_context_in_set_cmd(xp, arg, 0);
4096 break;
4097 case CMD_setglobal:
4098 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4099 break;
4100 case CMD_setlocal:
4101 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4102 break;
4103 case CMD_tag:
4104 case CMD_stag:
4105 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004106 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 case CMD_tselect:
4108 case CMD_stselect:
4109 case CMD_ptselect:
4110 case CMD_tjump:
4111 case CMD_stjump:
4112 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004113 if (*p_wop != NUL)
4114 xp->xp_context = EXPAND_TAGS_LISTFILES;
4115 else
4116 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 xp->xp_pattern = arg;
4118 break;
4119 case CMD_augroup:
4120 xp->xp_context = EXPAND_AUGROUP;
4121 xp->xp_pattern = arg;
4122 break;
4123#ifdef FEAT_SYN_HL
4124 case CMD_syntax:
4125 set_context_in_syntax_cmd(xp, arg);
4126 break;
4127#endif
4128#ifdef FEAT_EVAL
4129 case CMD_let:
4130 case CMD_if:
4131 case CMD_elseif:
4132 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004133 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 case CMD_echo:
4135 case CMD_echon:
4136 case CMD_execute:
4137 case CMD_echomsg:
4138 case CMD_echoerr:
4139 case CMD_call:
4140 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004141 case CMD_cexpr:
4142 case CMD_caddexpr:
4143 case CMD_cgetexpr:
4144 case CMD_lexpr:
4145 case CMD_laddexpr:
4146 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004147 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 break;
4149
4150 case CMD_unlet:
4151 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4152 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 xp->xp_context = EXPAND_USER_VARS;
4155 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004156
4157 if (*xp->xp_pattern == '$')
4158 {
4159 xp->xp_context = EXPAND_ENV_VARS;
4160 ++xp->xp_pattern;
4161 }
4162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 break;
4164
4165 case CMD_function:
4166 case CMD_delfunction:
4167 xp->xp_context = EXPAND_USER_FUNC;
4168 xp->xp_pattern = arg;
4169 break;
4170
4171 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004172 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 break;
4174#endif
4175 case CMD_highlight:
4176 set_context_in_highlight_cmd(xp, arg);
4177 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004178#ifdef FEAT_CSCOPE
4179 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004180 case CMD_lcscope:
4181 case CMD_scscope:
4182 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004183 break;
4184#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004185#ifdef FEAT_SIGNS
4186 case CMD_sign:
4187 set_context_in_sign_cmd(xp, arg);
4188 break;
4189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 case CMD_bdelete:
4191 case CMD_bwipeout:
4192 case CMD_bunload:
4193 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4194 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004195 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 case CMD_buffer:
4197 case CMD_sbuffer:
4198 case CMD_checktime:
4199 xp->xp_context = EXPAND_BUFFERS;
4200 xp->xp_pattern = arg;
4201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202#ifdef FEAT_USR_CMDS
4203 case CMD_USER:
4204 case CMD_USER_BUF:
4205 if (compl != EXPAND_NOTHING)
4206 {
4207 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004208 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210# ifdef FEAT_MENU
4211 if (compl == EXPAND_MENUS)
4212 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4213# endif
4214 if (compl == EXPAND_COMMANDS)
4215 return arg;
4216 if (compl == EXPAND_MAPPINGS)
4217 return set_context_in_map_cmd(xp, (char_u *)"map",
4218 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004219 /* Find start of last argument. */
4220 p = arg;
4221 while (*p)
4222 {
4223 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004224 /* argument starts after a space */
4225 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004226 else if (*p == '\\' && *(p + 1) != NUL)
4227 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004228 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 xp->xp_pattern = arg;
4231 }
4232 xp->xp_context = compl;
4233 }
4234 break;
4235#endif
4236 case CMD_map: case CMD_noremap:
4237 case CMD_nmap: case CMD_nnoremap:
4238 case CMD_vmap: case CMD_vnoremap:
4239 case CMD_omap: case CMD_onoremap:
4240 case CMD_imap: case CMD_inoremap:
4241 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004242 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004243 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004244 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004245 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004247 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 case CMD_unmap:
4249 case CMD_nunmap:
4250 case CMD_vunmap:
4251 case CMD_ounmap:
4252 case CMD_iunmap:
4253 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004254 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004255 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004256 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004257 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004259 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004260 case CMD_mapclear:
4261 case CMD_nmapclear:
4262 case CMD_vmapclear:
4263 case CMD_omapclear:
4264 case CMD_imapclear:
4265 case CMD_cmapclear:
4266 case CMD_lmapclear:
4267 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004268 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004269 case CMD_xmapclear:
4270 xp->xp_context = EXPAND_MAPCLEAR;
4271 xp->xp_pattern = arg;
4272 break;
4273
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 case CMD_abbreviate: case CMD_noreabbrev:
4275 case CMD_cabbrev: case CMD_cnoreabbrev:
4276 case CMD_iabbrev: case CMD_inoreabbrev:
4277 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004278 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 case CMD_unabbreviate:
4280 case CMD_cunabbrev:
4281 case CMD_iunabbrev:
4282 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004283 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284#ifdef FEAT_MENU
4285 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4286 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4287 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4288 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4289 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4290 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4291 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004292 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 case CMD_tmenu: case CMD_tunmenu:
4294 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4295 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4296#endif
4297
4298 case CMD_colorscheme:
4299 xp->xp_context = EXPAND_COLORS;
4300 xp->xp_pattern = arg;
4301 break;
4302
4303 case CMD_compiler:
4304 xp->xp_context = EXPAND_COMPILER;
4305 xp->xp_pattern = arg;
4306 break;
4307
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004308 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004309 xp->xp_context = EXPAND_OWNSYNTAX;
4310 xp->xp_pattern = arg;
4311 break;
4312
4313 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004314 xp->xp_context = EXPAND_FILETYPE;
4315 xp->xp_pattern = arg;
4316 break;
4317
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004318 case CMD_packadd:
4319 xp->xp_context = EXPAND_PACKADD;
4320 xp->xp_pattern = arg;
4321 break;
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4324 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4325 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004326 p = skiptowhite(arg);
4327 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
4329 xp->xp_context = EXPAND_LANGUAGE;
4330 xp->xp_pattern = arg;
4331 }
4332 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004333 {
4334 if ( STRNCMP(arg, "messages", p - arg) == 0
4335 || STRNCMP(arg, "ctype", p - arg) == 0
4336 || STRNCMP(arg, "time", p - arg) == 0)
4337 {
4338 xp->xp_context = EXPAND_LOCALES;
4339 xp->xp_pattern = skipwhite(p);
4340 }
4341 else
4342 xp->xp_context = EXPAND_NOTHING;
4343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 break;
4345#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004346#if defined(FEAT_PROFILE)
4347 case CMD_profile:
4348 set_context_in_profile_cmd(xp, arg);
4349 break;
4350#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004351 case CMD_behave:
4352 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004353 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004356 case CMD_messages:
4357 xp->xp_context = EXPAND_MESSAGES;
4358 xp->xp_pattern = arg;
4359 break;
4360
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004361#if defined(FEAT_CMDHIST)
4362 case CMD_history:
4363 xp->xp_context = EXPAND_HISTORY;
4364 xp->xp_pattern = arg;
4365 break;
4366#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004367#if defined(FEAT_PROFILE)
4368 case CMD_syntime:
4369 xp->xp_context = EXPAND_SYNTIME;
4370 xp->xp_pattern = arg;
4371 break;
4372#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004373
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004374 case CMD_argdelete:
4375 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4376 arg = xp->xp_pattern + 1;
4377 xp->xp_context = EXPAND_ARGLIST;
4378 xp->xp_pattern = arg;
4379 break;
4380
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#endif /* FEAT_CMDL_COMPL */
4382
4383 default:
4384 break;
4385 }
4386 return NULL;
4387}
4388
4389/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004390 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 *
4392 * Backslashed delimiters after / or ? will be skipped, and commands will
4393 * not be expanded between /'s and ?'s or after "'".
4394 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004395 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 * Returns the "cmd" pointer advanced to beyond the range.
4397 */
4398 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004399skip_range(
4400 char_u *cmd,
4401 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004403 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004405 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004407 if (*cmd == '\\')
4408 {
4409 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4410 ++cmd;
4411 else
4412 break;
4413 }
4414 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 if (*++cmd == NUL && ctx != NULL)
4417 *ctx = EXPAND_NOTHING;
4418 }
4419 else if (*cmd == '/' || *cmd == '?')
4420 {
4421 delim = *cmd++;
4422 while (*cmd != NUL && *cmd != delim)
4423 if (*cmd++ == '\\' && *cmd != NUL)
4424 ++cmd;
4425 if (*cmd == NUL && ctx != NULL)
4426 *ctx = EXPAND_NOTHING;
4427 }
4428 if (*cmd != NUL)
4429 ++cmd;
4430 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004431
4432 /* Skip ":" and white space. */
4433 while (*cmd == ':')
4434 cmd = skipwhite(cmd + 1);
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 return cmd;
4437}
4438
4439/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004440 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 *
4442 * Set ptr to the next character after the part that was interpreted.
4443 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004444 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 *
4446 * Return MAXLNUM when no Ex address was found.
4447 */
4448 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004449get_address(
4450 exarg_T *eap UNUSED,
4451 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004452 int addr_type, // flag: one of ADDR_LINES, ...
4453 int skip, // only skip the address, don't use it
4454 int silent, // no errors or side effects
4455 int to_other_file, // flag: may jump to other file
4456 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
4458 int c;
4459 int i;
4460 long n;
4461 char_u *cmd;
4462 pos_T pos;
4463 pos_T *fp;
4464 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004465 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 cmd = skipwhite(*ptr);
4468 lnum = MAXLNUM;
4469 do
4470 {
4471 switch (*cmd)
4472 {
4473 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 ++cmd;
4475 switch (addr_type)
4476 {
4477 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 lnum = curwin->w_cursor.lnum;
4479 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004481 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 break;
4483 case ADDR_ARGUMENTS:
4484 lnum = curwin->w_arg_idx + 1;
4485 break;
4486 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004487 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 lnum = curbuf->b_fnum;
4489 break;
4490 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004491 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004493 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004494 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004495 cmd = NULL;
4496 goto error;
4497 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004498#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004499 case ADDR_QUICKFIX:
4500 lnum = qf_get_cur_valid_idx(eap);
4501 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004502#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 }
4504 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004507 ++cmd;
4508 switch (addr_type)
4509 {
4510 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 lnum = curbuf->b_ml.ml_line_count;
4512 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004514 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 break;
4516 case ADDR_ARGUMENTS:
4517 lnum = ARGCOUNT;
4518 break;
4519 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004520 buf = lastbuf;
4521 while (buf->b_ml.ml_mfp == NULL)
4522 {
4523 if (buf->b_prev == NULL)
4524 break;
4525 buf = buf->b_prev;
4526 }
4527 lnum = buf->b_fnum;
4528 break;
4529 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 lnum = lastbuf->b_fnum;
4531 break;
4532 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004533 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004534 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004535 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004536 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004537 cmd = NULL;
4538 goto error;
4539 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004540#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004541 case ADDR_QUICKFIX:
4542 lnum = qf_get_size(eap);
4543 if (lnum == 0)
4544 lnum = 1;
4545 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004546#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 }
4548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 if (*++cmd == NUL)
4552 {
4553 cmd = NULL;
4554 goto error;
4555 }
4556 if (addr_type != ADDR_LINES)
4557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004558 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004559 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004560 goto error;
4561 }
4562 if (skip)
4563 ++cmd;
4564 else
4565 {
4566 /* Only accept a mark in another file when it is
4567 * used by itself: ":'M". */
4568 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4569 ++cmd;
4570 if (fp == (pos_T *)-1)
4571 /* Jumped to another file. */
4572 lnum = curwin->w_cursor.lnum;
4573 else
4574 {
4575 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 cmd = NULL;
4578 goto error;
4579 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004580 lnum = fp->lnum;
4581 }
4582 }
4583 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
4585 case '/':
4586 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 c = *cmd++;
4588 if (addr_type != ADDR_LINES)
4589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004590 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004591 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004592 goto error;
4593 }
4594 if (skip) /* skip "/pat/" */
4595 {
4596 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4597 if (*cmd == c)
4598 ++cmd;
4599 }
4600 else
4601 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004602 int flags;
4603
4604 pos = curwin->w_cursor; // save curwin->w_cursor
4605
4606 // When '/' or '?' follows another address, start from
4607 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 if (lnum != MAXLNUM)
4609 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004610
4611 // Start a forward search at the end of the line (unless
4612 // before the first line).
4613 // Start a backward search at the start of the line.
4614 // This makes sure we never match in the current
4615 // line, and can match anywhere in the
4616 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004617 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004618 curwin->w_cursor.col = MAXCOL;
4619 else
4620 curwin->w_cursor.col = 0;
4621 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004622 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4623 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004624 {
4625 curwin->w_cursor = pos;
4626 cmd = NULL;
4627 goto error;
4628 }
4629 lnum = curwin->w_cursor.lnum;
4630 curwin->w_cursor = pos;
4631 /* adjust command string pointer */
4632 cmd += searchcmdlen;
4633 }
4634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
4636 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 ++cmd;
4638 if (addr_type != ADDR_LINES)
4639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004640 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004641 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004642 goto error;
4643 }
4644 if (*cmd == '&')
4645 i = RE_SUBST;
4646 else if (*cmd == '?' || *cmd == '/')
4647 i = RE_SEARCH;
4648 else
4649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004650 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004651 cmd = NULL;
4652 goto error;
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004655 if (!skip)
4656 {
4657 /*
4658 * When search follows another address, start from
4659 * there.
4660 */
4661 if (lnum != MAXLNUM)
4662 pos.lnum = lnum;
4663 else
4664 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004666 /*
4667 * Start the search just like for the above
4668 * do_search().
4669 */
4670 if (*cmd != '?')
4671 pos.col = MAXCOL;
4672 else
4673 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004674#ifdef FEAT_VIRTUALEDIT
4675 pos.coladd = 0;
4676#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004677 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004678 *cmd == '?' ? BACKWARD : FORWARD,
4679 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004680 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004681 lnum = pos.lnum;
4682 else
4683 {
4684 cmd = NULL;
4685 goto error;
4686 }
4687 }
4688 ++cmd;
4689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
4691 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004692 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4693 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695
4696 for (;;)
4697 {
4698 cmd = skipwhite(cmd);
4699 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4700 break;
4701
4702 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004703 {
4704 switch (addr_type)
4705 {
4706 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004707 /* "+1" is same as ".+1" */
4708 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004709 break;
4710 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004711 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004712 break;
4713 case ADDR_ARGUMENTS:
4714 lnum = curwin->w_arg_idx + 1;
4715 break;
4716 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004717 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004718 lnum = curbuf->b_fnum;
4719 break;
4720 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004721 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004722 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004723 case ADDR_TABS_RELATIVE:
4724 lnum = 1;
4725 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004726#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004727 case ADDR_QUICKFIX:
4728 lnum = qf_get_cur_valid_idx(eap);
4729 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004730#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004731 }
4732 }
4733
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (VIM_ISDIGIT(*cmd))
4735 i = '+'; /* "number" is same as "+number" */
4736 else
4737 i = *cmd++;
4738 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4739 n = 1;
4740 else
4741 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004742
4743 if (addr_type == ADDR_TABS_RELATIVE)
4744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004745 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004746 cmd = NULL;
4747 goto error;
4748 }
4749 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004750 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004751 lnum = compute_buffer_local_count(
4752 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004754 {
4755#ifdef FEAT_FOLDING
4756 /* Relative line addressing, need to adjust for folded lines
4757 * now, but only do it after the first address. */
4758 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4759 && address_count >= 2)
4760 (void)hasFolding(lnum, NULL, &lnum);
4761#endif
4762 if (i == '-')
4763 lnum -= n;
4764 else
4765 lnum += n;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 } while (*cmd == '/' || *cmd == '?');
4769
4770error:
4771 *ptr = cmd;
4772 return lnum;
4773}
4774
4775/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004776 * Get flags from an Ex command argument.
4777 */
4778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004779get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004780{
4781 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4782 {
4783 if (*eap->arg == 'l')
4784 eap->flags |= EXFLAG_LIST;
4785 else if (*eap->arg == 'p')
4786 eap->flags |= EXFLAG_PRINT;
4787 else
4788 eap->flags |= EXFLAG_NR;
4789 eap->arg = skipwhite(eap->arg + 1);
4790 }
4791}
4792
4793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 * Function called for command which is Not Implemented. NI!
4795 */
4796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004797ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
4799 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004800 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801}
4802
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004803#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804/*
4805 * Function called for script command which is Not Implemented. NI!
4806 * Skips over ":perl <<EOF" constructs.
4807 */
4808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004809ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 if (!eap->skip)
4812 ex_ni(eap);
4813 else
4814 vim_free(script_get(eap, eap->arg));
4815}
4816#endif
4817
4818/*
4819 * Check range in Ex command for validity.
4820 * Return NULL when valid, error message when invalid.
4821 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004822 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004823invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if ( eap->line1 < 0
4827 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004829 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830
4831 if (eap->argt & RANGE)
4832 {
4833 switch(eap->addr_type)
4834 {
4835 case ADDR_LINES:
4836 if (!(eap->argt & NOTADR)
4837 && eap->line2 > curbuf->b_ml.ml_line_count
4838#ifdef FEAT_DIFF
4839 + (eap->cmdidx == CMD_diffget)
4840#endif
4841 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004842 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 break;
4844 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004845 /* add 1 if ARGCOUNT is 0 */
4846 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004847 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004848 break;
4849 case ADDR_BUFFERS:
4850 if (eap->line1 < firstbuf->b_fnum
4851 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004853 break;
4854 case ADDR_LOADED_BUFFERS:
4855 buf = firstbuf;
4856 while (buf->b_ml.ml_mfp == NULL)
4857 {
4858 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004860 buf = buf->b_next;
4861 }
4862 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004863 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004864 buf = lastbuf;
4865 while (buf->b_ml.ml_mfp == NULL)
4866 {
4867 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004868 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004869 buf = buf->b_prev;
4870 }
4871 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004872 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004873 break;
4874 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004875 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004876 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004877 break;
4878 case ADDR_TABS:
4879 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004880 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004881 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004882 case ADDR_TABS_RELATIVE:
4883 /* Do nothing */
4884 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004885#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004886 case ADDR_QUICKFIX:
4887 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004888 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004889 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004890#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004891 }
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return NULL;
4894}
4895
4896/*
4897 * Correct the range for zero line number, if required.
4898 */
4899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004900correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4903 {
4904 if (eap->line1 == 0)
4905 eap->line1 = 1;
4906 if (eap->line2 == 0)
4907 eap->line2 = 1;
4908 }
4909}
4910
Bram Moolenaar748bf032005-02-02 23:04:36 +00004911#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004912/*
4913 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4914 * pattern. Otherwise return eap->arg.
4915 */
4916 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004917skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004918{
4919 char_u *p = eap->arg;
4920
Bram Moolenaara37420f2006-02-04 22:37:47 +00004921 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4922 || eap->cmdidx == CMD_vimgrepadd
4923 || eap->cmdidx == CMD_lvimgrepadd
4924 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004925 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004926 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004927 if (p == NULL)
4928 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004929 }
4930 return p;
4931}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004932
4933/*
4934 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4935 * in the command line, so that things like % get expanded.
4936 */
4937 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004938replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004939{
4940 char_u *new_cmdline;
4941 char_u *program;
4942 char_u *pos;
4943 char_u *ptr;
4944 int len;
4945 int i;
4946
4947 /*
4948 * Don't do it when ":vimgrep" is used for ":grep".
4949 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004950 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4951 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4952 || eap->cmdidx == CMD_grepadd
4953 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004954 && !grep_internal(eap->cmdidx))
4955 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004956 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4957 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004958 {
4959 if (*curbuf->b_p_gp == NUL)
4960 program = p_gp;
4961 else
4962 program = curbuf->b_p_gp;
4963 }
4964 else
4965 {
4966 if (*curbuf->b_p_mp == NUL)
4967 program = p_mp;
4968 else
4969 program = curbuf->b_p_mp;
4970 }
4971
4972 p = skipwhite(p);
4973
4974 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4975 {
4976 /* replace $* by given arguments */
4977 i = 1;
4978 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4979 ++i;
4980 len = (int)STRLEN(p);
4981 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4982 if (new_cmdline == NULL)
4983 return NULL; /* out of memory */
4984 ptr = new_cmdline;
4985 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4986 {
4987 i = (int)(pos - program);
4988 STRNCPY(ptr, program, i);
4989 STRCPY(ptr += i, p);
4990 ptr += len;
4991 program = pos + 2;
4992 }
4993 STRCPY(ptr, program);
4994 }
4995 else
4996 {
4997 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4998 if (new_cmdline == NULL)
4999 return NULL; /* out of memory */
5000 STRCPY(new_cmdline, program);
5001 STRCAT(new_cmdline, " ");
5002 STRCAT(new_cmdline, p);
5003 }
5004 msg_make(p);
5005
5006 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
5007 vim_free(*cmdlinep);
5008 *cmdlinep = new_cmdline;
5009 p = new_cmdline;
5010 }
5011 return p;
5012}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005013#endif
5014
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015/*
5016 * Expand file name in Ex command argument.
5017 * Return FAIL for failure, OK otherwise.
5018 */
5019 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005020expand_filename(
5021 exarg_T *eap,
5022 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005023 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 int has_wildcards; /* need to expand wildcards */
5026 char_u *repl;
5027 int srclen;
5028 char_u *p;
5029 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005030 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
Bram Moolenaar748bf032005-02-02 23:04:36 +00005032#ifdef FEAT_QUICKFIX
5033 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5034 p = skip_grep_pat(eap);
5035#else
5036 p = eap->arg;
5037#endif
5038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 /*
5040 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5041 * the file name contains a wildcard it should not cause expanding.
5042 * (it will be expanded anyway if there is a wildcard before replacing).
5043 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005044 has_wildcards = mch_has_wildcard(p);
5045 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005047#ifdef FEAT_EVAL
5048 /* Skip over `=expr`, wildcards in it are not expanded. */
5049 if (p[0] == '`' && p[1] == '=')
5050 {
5051 p += 2;
5052 (void)skip_expr(&p);
5053 if (*p == '`')
5054 ++p;
5055 continue;
5056 }
5057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 /*
5059 * Quick check if this cannot be the start of a special string.
5060 * Also removes backslash before '%', '#' and '<'.
5061 */
5062 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5063 {
5064 ++p;
5065 continue;
5066 }
5067
5068 /*
5069 * Try to find a match at this position.
5070 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005071 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5072 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (*errormsgp != NULL) /* error detected */
5074 return FAIL;
5075 if (repl == NULL) /* no match found */
5076 {
5077 p += srclen;
5078 continue;
5079 }
5080
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005081 /* Wildcards won't be expanded below, the replacement is taken
5082 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5083 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5084 {
5085 char_u *l = repl;
5086
5087 repl = expand_env_save(repl);
5088 vim_free(l);
5089 }
5090
Bram Moolenaar63b92542007-03-27 14:57:09 +00005091 /* Need to escape white space et al. with a backslash.
5092 * Don't do this for:
5093 * - replacement that already has been escaped: "##"
5094 * - shell commands (may have to use quotes instead).
5095 * - non-unix systems when there is a single argument (spaces don't
5096 * separate arguments then).
5097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005099 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 && eap->cmdidx != CMD_grep
5102 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005103 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005104 && eap->cmdidx != CMD_lgrep
5105 && eap->cmdidx != CMD_lgrepadd
5106 && eap->cmdidx != CMD_lmake
5107 && eap->cmdidx != CMD_make
5108 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109#ifndef UNIX
5110 && !(eap->argt & NOSPC)
5111#endif
5112 )
5113 {
5114 char_u *l;
5115#ifdef BACKSLASH_IN_FILENAME
5116 /* Don't escape a backslash here, because rem_backslash() doesn't
5117 * remove it later. */
5118 static char_u *nobslash = (char_u *)" \t\"|";
5119# define ESCAPE_CHARS nobslash
5120#else
5121# define ESCAPE_CHARS escape_chars
5122#endif
5123
5124 for (l = repl; *l; ++l)
5125 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5126 {
5127 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5128 if (l != NULL)
5129 {
5130 vim_free(repl);
5131 repl = l;
5132 }
5133 break;
5134 }
5135 }
5136
5137 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005138 if ((eap->usefilter || eap->cmdidx == CMD_bang
5139 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005140 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 char_u *l;
5143
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005144 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (l != NULL)
5146 {
5147 vim_free(repl);
5148 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 }
5151
5152 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5153 vim_free(repl);
5154 if (p == NULL)
5155 return FAIL;
5156 }
5157
5158 /*
5159 * One file argument: Expand wildcards.
5160 * Don't do this with ":r !command" or ":w !command".
5161 */
5162 if ((eap->argt & NOSPC) && !eap->usefilter)
5163 {
5164 /*
5165 * May do this twice:
5166 * 1. Replace environment variables.
5167 * 2. Replace any other wildcards, remove backslashes.
5168 */
5169 for (n = 1; n <= 2; ++n)
5170 {
5171 if (n == 2)
5172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 /*
5174 * Halve the number of backslashes (this is Vi compatible).
5175 * For Unix and OS/2, when wildcards are expanded, this is
5176 * done by ExpandOne() below.
5177 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005178#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (!has_wildcards)
5180#endif
5181 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183
5184 if (has_wildcards)
5185 {
5186 if (n == 1)
5187 {
5188 /*
5189 * First loop: May expand environment variables. This
5190 * can be done much faster with expand_env() than with
5191 * something else (e.g., calling a shell).
5192 * After expanding environment variables, check again
5193 * if there are still wildcards present.
5194 */
5195 if (vim_strchr(eap->arg, '$') != NULL
5196 || vim_strchr(eap->arg, '~') != NULL)
5197 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005198 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005199 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 has_wildcards = mch_has_wildcard(NameBuff);
5201 p = NameBuff;
5202 }
5203 else
5204 p = NULL;
5205 }
5206 else /* n == 2 */
5207 {
5208 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005209 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 ExpandInit(&xpc);
5212 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005213 if (p_wic)
5214 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005216 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (p == NULL)
5218 return FAIL;
5219 }
5220 if (p != NULL)
5221 {
5222 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5223 p, cmdlinep);
5224 if (n == 2) /* p came from ExpandOne() */
5225 vim_free(p);
5226 }
5227 }
5228 }
5229 }
5230 return OK;
5231}
5232
5233/*
5234 * Replace part of the command line, keeping eap->cmd, eap->arg and
5235 * eap->nextcmd correct.
5236 * "src" points to the part that is to be replaced, of length "srclen".
5237 * "repl" is the replacement string.
5238 * Returns a pointer to the character after the replaced string.
5239 * Returns NULL for failure.
5240 */
5241 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005242repl_cmdline(
5243 exarg_T *eap,
5244 char_u *src,
5245 int srclen,
5246 char_u *repl,
5247 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248{
5249 int len;
5250 int i;
5251 char_u *new_cmdline;
5252
5253 /*
5254 * The new command line is build in new_cmdline[].
5255 * First allocate it.
5256 * Careful: a "+cmd" argument may have been NUL terminated.
5257 */
5258 len = (int)STRLEN(repl);
5259 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005260 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5262 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5263 return NULL; /* out of memory! */
5264
5265 /*
5266 * Copy the stuff before the expanded part.
5267 * Copy the expanded stuff.
5268 * Copy what came after the expanded part.
5269 * Copy the next commands, if there are any.
5270 */
5271 i = (int)(src - *cmdlinep); /* length of part before match */
5272 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 mch_memmove(new_cmdline + i, repl, (size_t)len);
5275 i += len; /* remember the end of the string */
5276 STRCPY(new_cmdline + i, src + srclen);
5277 src = new_cmdline + i; /* remember where to continue */
5278
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005279 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
5281 i = (int)STRLEN(new_cmdline) + 1;
5282 STRCPY(new_cmdline + i, eap->nextcmd);
5283 eap->nextcmd = new_cmdline + i;
5284 }
5285 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5286 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5287 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5288 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5289 vim_free(*cmdlinep);
5290 *cmdlinep = new_cmdline;
5291
5292 return src;
5293}
5294
5295/*
5296 * Check for '|' to separate commands and '"' to start comments.
5297 */
5298 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005299separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
5301 char_u *p;
5302
Bram Moolenaar86b68352004-12-27 21:59:20 +00005303#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005304 p = skip_grep_pat(eap);
5305#else
5306 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005307#endif
5308
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005309 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
5311 if (*p == Ctrl_V)
5312 {
5313 if (eap->argt & (USECTRLV | XFILE))
5314 ++p; /* skip CTRL-V and next char */
5315 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005316 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005317 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (*p == NUL) /* stop at NUL after CTRL-V */
5319 break;
5320 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005321
5322#ifdef FEAT_EVAL
5323 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005324 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005325 {
5326 p += 2;
5327 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005328 }
5329#endif
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 /* Check for '"': start of comment or '|': next command */
5332 /* :@" and :*" do not start a comment!
5333 * :redir @" doesn't either. */
5334 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5335 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5336 || p != eap->arg)
5337 && (eap->cmdidx != CMD_redir
5338 || p != eap->arg + 1 || p[-1] != '@'))
5339 || *p == '|' || *p == '\n')
5340 {
5341 /*
5342 * We remove the '\' before the '|', unless USECTRLV is used
5343 * AND 'b' is present in 'cpoptions'.
5344 */
5345 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5346 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5347 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005348 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 --p;
5350 }
5351 else
5352 {
5353 eap->nextcmd = check_nextcmd(p);
5354 *p = NUL;
5355 break;
5356 }
5357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5361 del_trailing_spaces(eap->arg);
5362}
5363
5364/*
5365 * get + command from ex argument
5366 */
5367 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005368getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 char_u *arg = *argp;
5371 char_u *command = NULL;
5372
5373 if (*arg == '+') /* +[command] */
5374 {
5375 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005376 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 command = dollar_command;
5378 else
5379 {
5380 command = arg;
5381 arg = skip_cmd_arg(command, TRUE);
5382 if (*arg != NUL)
5383 *arg++ = NUL; /* terminate command with NUL */
5384 }
5385
5386 arg = skipwhite(arg); /* skip over spaces */
5387 *argp = arg;
5388 }
5389 return command;
5390}
5391
5392/*
5393 * Find end of "+command" argument. Skip over "\ " and "\\".
5394 */
5395 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396skip_cmd_arg(
5397 char_u *p,
5398 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
5400 while (*p && !vim_isspace(*p))
5401 {
5402 if (*p == '\\' && p[1] != NUL)
5403 {
5404 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005405 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 else
5407 ++p;
5408 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005409 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
5411 return p;
5412}
5413
Bram Moolenaar86676c92018-04-05 18:56:48 +02005414#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005415 int
5416get_bad_opt(char_u *p, exarg_T *eap)
5417{
5418 if (STRICMP(p, "keep") == 0)
5419 eap->bad_char = BAD_KEEP;
5420 else if (STRICMP(p, "drop") == 0)
5421 eap->bad_char = BAD_DROP;
5422 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5423 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005424 else
5425 return FAIL;
5426 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005427}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005428#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430/*
5431 * Get "++opt=arg" argument.
5432 * Return FAIL or OK.
5433 */
5434 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005435getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 char_u *arg = eap->arg + 2;
5438 int *pp = NULL;
5439#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005440 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 char_u *p;
5442#endif
5443
5444 /* ":edit ++[no]bin[ary] file" */
5445 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5446 {
5447 if (*arg == 'n')
5448 {
5449 arg += 2;
5450 eap->force_bin = FORCE_NOBIN;
5451 }
5452 else
5453 eap->force_bin = FORCE_BIN;
5454 if (!checkforcmd(&arg, "binary", 3))
5455 return FAIL;
5456 eap->arg = skipwhite(arg);
5457 return OK;
5458 }
5459
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005460 /* ":read ++edit file" */
5461 if (STRNCMP(arg, "edit", 4) == 0)
5462 {
5463 eap->read_edit = TRUE;
5464 eap->arg = skipwhite(arg + 4);
5465 return OK;
5466 }
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 if (STRNCMP(arg, "ff", 2) == 0)
5469 {
5470 arg += 2;
5471 pp = &eap->force_ff;
5472 }
5473 else if (STRNCMP(arg, "fileformat", 10) == 0)
5474 {
5475 arg += 10;
5476 pp = &eap->force_ff;
5477 }
5478#ifdef FEAT_MBYTE
5479 else if (STRNCMP(arg, "enc", 3) == 0)
5480 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005481 if (STRNCMP(arg, "encoding", 8) == 0)
5482 arg += 8;
5483 else
5484 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 pp = &eap->force_enc;
5486 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005487 else if (STRNCMP(arg, "bad", 3) == 0)
5488 {
5489 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005490 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492#endif
5493
5494 if (pp == NULL || *arg != '=')
5495 return FAIL;
5496
5497 ++arg;
5498 *pp = (int)(arg - eap->cmd);
5499 arg = skip_cmd_arg(arg, FALSE);
5500 eap->arg = skipwhite(arg);
5501 *arg = NUL;
5502
5503#ifdef FEAT_MBYTE
5504 if (pp == &eap->force_ff)
5505 {
5506#endif
5507 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5508 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005509 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510#ifdef FEAT_MBYTE
5511 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005512 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 /* Make 'fileencoding' lower case. */
5515 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5516 *p = TOLOWER_ASC(*p);
5517 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005518 else
5519 {
5520 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5521 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005522 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005523 return FAIL;
5524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#endif
5526
5527 return OK;
5528}
5529
5530/*
5531 * ":abbreviate" and friends.
5532 */
5533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005534ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 do_exmap(eap, TRUE); /* almost the same as mapping */
5537}
5538
5539/*
5540 * ":map" and friends.
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 /*
5546 * If we are sourcing .exrc or .vimrc in current directory we
5547 * print the mappings for security reasons.
5548 */
5549 if (secure)
5550 {
5551 secure = 2;
5552 msg_outtrans(eap->cmd);
5553 msg_putchar('\n');
5554 }
5555 do_exmap(eap, FALSE);
5556}
5557
5558/*
5559 * ":unmap" and friends.
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 do_exmap(eap, FALSE);
5565}
5566
5567/*
5568 * ":mapclear" and friends.
5569 */
5570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005571ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
5573 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5574}
5575
5576/*
5577 * ":abclear" and friends.
5578 */
5579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5583}
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005586ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005589 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 * directory for security reasons.
5591 */
5592 if (secure)
5593 {
5594 secure = 2;
5595 eap->errmsg = e_curdir;
5596 }
5597 else if (eap->cmdidx == CMD_autocmd)
5598 do_autocmd(eap->arg, eap->forceit);
5599 else
5600 do_augroup(eap->arg, eap->forceit);
5601}
5602
5603/*
5604 * ":doautocmd": Apply the automatic commands to the current buffer.
5605 */
5606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005607ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005609 char_u *arg = eap->arg;
5610 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005611 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005612
Bram Moolenaar1610d052016-06-09 22:53:01 +02005613 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5614 /* Only when there is no <nomodeline>. */
5615 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005616 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619/*
5620 * :[N]bunload[!] [N] [bufname] unload buffer
5621 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5622 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5623 */
5624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 eap->errmsg = do_bufdel(
5628 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5629 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5630 : DOBUF_UNLOAD, eap->arg,
5631 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5632}
5633
5634/*
5635 * :[N]buffer [N] to buffer N
5636 * :[N]sbuffer [N] to buffer N
5637 */
5638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 if (*eap->arg)
5642 eap->errmsg = e_trailing;
5643 else
5644 {
5645 if (eap->addr_count == 0) /* default is current buffer */
5646 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5647 else
5648 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005649 if (eap->do_ecmd_cmd != NULL)
5650 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652}
5653
5654/*
5655 * :[N]bmodified [N] to next mod. buffer
5656 * :[N]sbmodified [N] to next mod. buffer
5657 */
5658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005662 if (eap->do_ecmd_cmd != NULL)
5663 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
5666/*
5667 * :[N]bnext [N] to next buffer
5668 * :[N]sbnext [N] split and to next buffer
5669 */
5670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005674 if (eap->do_ecmd_cmd != NULL)
5675 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676}
5677
5678/*
5679 * :[N]bNext [N] to previous buffer
5680 * :[N]bprevious [N] to previous buffer
5681 * :[N]sbNext [N] split and to previous buffer
5682 * :[N]sbprevious [N] split and to previous buffer
5683 */
5684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005688 if (eap->do_ecmd_cmd != NULL)
5689 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690}
5691
5692/*
5693 * :brewind to first buffer
5694 * :bfirst to first buffer
5695 * :sbrewind split and to first buffer
5696 * :sbfirst split and to first buffer
5697 */
5698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005702 if (eap->do_ecmd_cmd != NULL)
5703 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704}
5705
5706/*
5707 * :blast to last buffer
5708 * :sblast split and to last buffer
5709 */
5710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005711ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005714 if (eap->do_ecmd_cmd != NULL)
5715 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005719ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 return (c == NUL || c == '|' || c == '"' || c == '\n');
5722}
5723
5724#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5725 || defined(PROTO)
5726/*
5727 * Return the next command, after the first '|' or '\n'.
5728 * Return NULL if not found.
5729 */
5730 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005731find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
5733 while (*p != '|' && *p != '\n')
5734 {
5735 if (*p == NUL)
5736 return NULL;
5737 ++p;
5738 }
5739 return (p + 1);
5740}
5741#endif
5742
5743/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005744 * Check if *p is a separator between Ex commands, skipping over white space.
5745 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 */
5747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005748check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005750 char_u *s = skipwhite(p);
5751
5752 if (*s == '|' || *s == '\n')
5753 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 else
5755 return NULL;
5756}
5757
5758/*
5759 * - if there are more files to edit
5760 * - and this is the last window
5761 * - and forceit not used
5762 * - and not repeated twice on a row
5763 * return FAIL and give error message if 'message' TRUE
5764 * return OK otherwise
5765 */
5766 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005767check_more(
5768 int message, /* when FALSE check only, no messages */
5769 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
5771 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5772
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005773 if (!forceit && only_one_window()
5774 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 {
5776 if (message)
5777 {
5778#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5779 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5780 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005781 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005783 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5784 NGETTEXT("%d more file to edit. Quit anyway?",
5785 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5787 return OK;
5788 return FAIL;
5789 }
5790#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005791 semsg(NGETTEXT("E173: %ld more file to edit",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005792 "E173: %ld more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 quitmore = 2; /* next try to quit is allowed */
5794 }
5795 return FAIL;
5796 }
5797 return OK;
5798}
5799
5800#ifdef FEAT_CMDL_COMPL
5801/*
5802 * Function given to ExpandGeneric() to obtain the list of command names.
5803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005805get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
5807 if (idx >= (int)CMD_SIZE)
5808# ifdef FEAT_USR_CMDS
5809 return get_user_command_name(idx);
5810# else
5811 return NULL;
5812# endif
5813 return cmdnames[idx].cmd_name;
5814}
5815#endif
5816
5817#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005819uc_add_command(
5820 char_u *name,
5821 size_t name_len,
5822 char_u *rep,
5823 long argt,
5824 long def,
5825 int flags,
5826 int compl,
5827 char_u *compl_arg,
5828 int addr_type,
5829 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 ucmd_T *cmd = NULL;
5832 char_u *p;
5833 int i;
5834 int cmp = 1;
5835 char_u *rep_buf = NULL;
5836 garray_T *gap;
5837
Bram Moolenaar9c102382006-05-03 21:26:49 +00005838 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (rep_buf == NULL)
5840 {
5841 /* Can't replace termcodes - try using the string as is */
5842 rep_buf = vim_strsave(rep);
5843
5844 /* Give up if out of memory */
5845 if (rep_buf == NULL)
5846 return FAIL;
5847 }
5848
5849 /* get address of growarray: global or in curbuf */
5850 if (flags & UC_BUFFER)
5851 {
5852 gap = &curbuf->b_ucmds;
5853 if (gap->ga_itemsize == 0)
5854 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5855 }
5856 else
5857 gap = &ucmds;
5858
5859 /* Search for the command in the already defined commands. */
5860 for (i = 0; i < gap->ga_len; ++i)
5861 {
5862 size_t len;
5863
5864 cmd = USER_CMD_GA(gap, i);
5865 len = STRLEN(cmd->uc_name);
5866 cmp = STRNCMP(name, cmd->uc_name, name_len);
5867 if (cmp == 0)
5868 {
5869 if (name_len < len)
5870 cmp = -1;
5871 else if (name_len > len)
5872 cmp = 1;
5873 }
5874
5875 if (cmp == 0)
5876 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005877 // Command can be replaced with "command!" and when sourcing the
5878 // same script again, but only once.
5879 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5880 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005882 semsg(_("E174: Command already exists: add ! to replace it: %s"),
Bram Moolenaar55d46912018-12-08 16:03:28 +01005883 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 goto fail;
5885 }
5886
Bram Moolenaard23a8232018-02-10 18:45:26 +01005887 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005888#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005889 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 break;
5892 }
5893
5894 /* Stop as soon as we pass the name to add */
5895 if (cmp < 0)
5896 break;
5897 }
5898
5899 /* Extend the array unless we're replacing an existing command */
5900 if (cmp != 0)
5901 {
5902 if (ga_grow(gap, 1) != OK)
5903 goto fail;
5904 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5905 goto fail;
5906
5907 cmd = USER_CMD_GA(gap, i);
5908 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5909
5910 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
5912 cmd->uc_name = p;
5913 }
5914
5915 cmd->uc_rep = rep_buf;
5916 cmd->uc_argt = argt;
5917 cmd->uc_def = def;
5918 cmd->uc_compl = compl;
5919#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005920 cmd->uc_script_ctx = current_sctx;
5921 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922# ifdef FEAT_CMDL_COMPL
5923 cmd->uc_compl_arg = compl_arg;
5924# endif
5925#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005926 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
5928 return OK;
5929
5930fail:
5931 vim_free(rep_buf);
5932#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5933 vim_free(compl_arg);
5934#endif
5935 return FAIL;
5936}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005939#if defined(FEAT_USR_CMDS)
5940static struct
5941{
5942 int expand;
5943 char *name;
5944} addr_type_complete[] =
5945{
5946 {ADDR_ARGUMENTS, "arguments"},
5947 {ADDR_LINES, "lines"},
5948 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5949 {ADDR_TABS, "tabs"},
5950 {ADDR_BUFFERS, "buffers"},
5951 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005952 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005953 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005954 {-1, NULL}
5955};
5956#endif
5957
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005958#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959/*
5960 * List of names for completion for ":command" with the EXPAND_ flag.
5961 * Must be alphabetical for completion.
5962 */
5963static struct
5964{
5965 int expand;
5966 char *name;
5967} command_complete[] =
5968{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005969 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005971 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005973 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005975 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005976#if defined(FEAT_CSCOPE)
5977 {EXPAND_CSCOPE, "cscope"},
5978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5980 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005981 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982#endif
5983 {EXPAND_DIRECTORIES, "dir"},
5984 {EXPAND_ENV_VARS, "environment"},
5985 {EXPAND_EVENTS, "event"},
5986 {EXPAND_EXPRESSION, "expression"},
5987 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005988 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005989 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 {EXPAND_FUNCTIONS, "function"},
5991 {EXPAND_HELP, "help"},
5992 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005993#if defined(FEAT_CMDHIST)
5994 {EXPAND_HISTORY, "history"},
5995#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005996#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005997 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005998 {EXPAND_LOCALES, "locale"},
5999#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02006000 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {EXPAND_MAPPINGS, "mapping"},
6002 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02006003 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006004 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006005#if defined(FEAT_PROFILE)
6006 {EXPAND_SYNTIME, "syntime"},
6007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006009 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006010 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006011#if defined(FEAT_SIGNS)
6012 {EXPAND_SIGN, "sign"},
6013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {EXPAND_TAGS, "tag"},
6015 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006016 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 {EXPAND_USER_VARS, "var"},
6018 {0, NULL}
6019};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006022#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006024uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 int i, j;
6027 int found = FALSE;
6028 ucmd_T *cmd;
6029 int len;
6030 long a;
6031 garray_T *gap;
6032
6033 gap = &curbuf->b_ucmds;
6034 for (;;)
6035 {
6036 for (i = 0; i < gap->ga_len; ++i)
6037 {
6038 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006039 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040
Bram Moolenaard29459b2016-08-26 22:29:11 +02006041 /* Skip commands which don't match the requested prefix and
6042 * commands filtered out. */
6043 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6044 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 continue;
6046
6047 /* Put out the title first time */
6048 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006049 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 found = TRUE;
6051 msg_putchar('\n');
6052 if (got_int)
6053 break;
6054
6055 /* Special cases */
6056 msg_putchar(a & BANG ? '!' : ' ');
6057 msg_putchar(a & REGSTR ? '"' : ' ');
6058 msg_putchar(gap != &ucmds ? 'b' : ' ');
6059 msg_putchar(' ');
6060
Bram Moolenaar8820b482017-03-16 17:23:31 +01006061 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 len = (int)STRLEN(cmd->uc_name) + 4;
6063
6064 do {
6065 msg_putchar(' ');
6066 ++len;
6067 } while (len < 16);
6068
6069 len = 0;
6070
6071 /* Arguments */
6072 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6073 {
6074 case 0: IObuff[len++] = '0'; break;
6075 case (EXTRA): IObuff[len++] = '*'; break;
6076 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6077 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6078 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6079 }
6080
6081 do {
6082 IObuff[len++] = ' ';
6083 } while (len < 5);
6084
6085 /* Range */
6086 if (a & (RANGE|COUNT))
6087 {
6088 if (a & COUNT)
6089 {
6090 /* -count=N */
6091 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6092 len += (int)STRLEN(IObuff + len);
6093 }
6094 else if (a & DFLALL)
6095 IObuff[len++] = '%';
6096 else if (cmd->uc_def >= 0)
6097 {
6098 /* -range=N */
6099 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6100 len += (int)STRLEN(IObuff + len);
6101 }
6102 else
6103 IObuff[len++] = '.';
6104 }
6105
6106 do {
6107 IObuff[len++] = ' ';
6108 } while (len < 11);
6109
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006110 /* Address Type */
6111 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6112 if (addr_type_complete[j].expand != ADDR_LINES
6113 && addr_type_complete[j].expand == cmd->uc_addr_type)
6114 {
6115 STRCPY(IObuff + len, addr_type_complete[j].name);
6116 len += (int)STRLEN(IObuff + len);
6117 break;
6118 }
6119
6120 do {
6121 IObuff[len++] = ' ';
6122 } while (len < 21);
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 /* Completion */
6125 for (j = 0; command_complete[j].expand != 0; ++j)
6126 if (command_complete[j].expand == cmd->uc_compl)
6127 {
6128 STRCPY(IObuff + len, command_complete[j].name);
6129 len += (int)STRLEN(IObuff + len);
6130 break;
6131 }
6132
6133 do {
6134 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006135 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
6137 IObuff[len] = '\0';
6138 msg_outtrans(IObuff);
6139
6140 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006141#ifdef FEAT_EVAL
6142 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006143 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 out_flush();
6146 ui_breakcheck();
6147 if (got_int)
6148 break;
6149 }
6150 if (gap == &ucmds || i < gap->ga_len)
6151 break;
6152 gap = &ucmds;
6153 }
6154
6155 if (!found)
6156 MSG(_("No user-defined commands found"));
6157}
6158
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006159 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006160uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6163 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6164 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6165 0xb9, 0x7f, 0};
6166 int i;
6167
6168 for (i = 0; fcmd[i]; ++i)
6169 IObuff[i] = fcmd[i] - 0x40;
6170 IObuff[i] = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006171 return (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172}
6173
6174 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006175uc_scan_attr(
6176 char_u *attr,
6177 size_t len,
6178 long *argt,
6179 long *def,
6180 int *flags,
6181 int *compl,
6182 char_u **compl_arg,
6183 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 char_u *p;
6186
6187 if (len == 0)
6188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006189 emsg(_("E175: No attribute specified"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 return FAIL;
6191 }
6192
6193 /* First, try the simple attributes (no arguments) */
6194 if (STRNICMP(attr, "bang", len) == 0)
6195 *argt |= BANG;
6196 else if (STRNICMP(attr, "buffer", len) == 0)
6197 *flags |= UC_BUFFER;
6198 else if (STRNICMP(attr, "register", len) == 0)
6199 *argt |= REGSTR;
6200 else if (STRNICMP(attr, "bar", len) == 0)
6201 *argt |= TRLBAR;
6202 else
6203 {
6204 int i;
6205 char_u *val = NULL;
6206 size_t vallen = 0;
6207 size_t attrlen = len;
6208
6209 /* Look for the attribute name - which is the part before any '=' */
6210 for (i = 0; i < (int)len; ++i)
6211 {
6212 if (attr[i] == '=')
6213 {
6214 val = &attr[i + 1];
6215 vallen = len - i - 1;
6216 attrlen = i;
6217 break;
6218 }
6219 }
6220
6221 if (STRNICMP(attr, "nargs", attrlen) == 0)
6222 {
6223 if (vallen == 1)
6224 {
6225 if (*val == '0')
6226 /* Do nothing - this is the default */;
6227 else if (*val == '1')
6228 *argt |= (EXTRA | NOSPC | NEEDARG);
6229 else if (*val == '*')
6230 *argt |= EXTRA;
6231 else if (*val == '?')
6232 *argt |= (EXTRA | NOSPC);
6233 else if (*val == '+')
6234 *argt |= (EXTRA | NEEDARG);
6235 else
6236 goto wrong_nargs;
6237 }
6238 else
6239 {
6240wrong_nargs:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006241 emsg(_("E176: Invalid number of arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 return FAIL;
6243 }
6244 }
6245 else if (STRNICMP(attr, "range", attrlen) == 0)
6246 {
6247 *argt |= RANGE;
6248 if (vallen == 1 && *val == '%')
6249 *argt |= DFLALL;
6250 else if (val != NULL)
6251 {
6252 p = val;
6253 if (*def >= 0)
6254 {
6255two_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006256 emsg(_("E177: Count cannot be specified twice"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 return FAIL;
6258 }
6259
6260 *def = getdigits(&p);
6261 *argt |= (ZEROR | NOTADR);
6262
6263 if (p != val + vallen || vallen == 0)
6264 {
6265invalid_count:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006266 emsg(_("E178: Invalid default value for count"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 return FAIL;
6268 }
6269 }
6270 }
6271 else if (STRNICMP(attr, "count", attrlen) == 0)
6272 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006273 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
6275 if (val != NULL)
6276 {
6277 p = val;
6278 if (*def >= 0)
6279 goto two_count;
6280
6281 *def = getdigits(&p);
6282
6283 if (p != val + vallen)
6284 goto invalid_count;
6285 }
6286
6287 if (*def < 0)
6288 *def = 0;
6289 }
6290 else if (STRNICMP(attr, "complete", attrlen) == 0)
6291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (val == NULL)
6293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006294 emsg(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 return FAIL;
6296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006298 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6299 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006302 else if (STRNICMP(attr, "addr", attrlen) == 0)
6303 {
6304 *argt |= RANGE;
6305 if (val == NULL)
6306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006307 emsg(_("E179: argument required for -addr"));
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006308 return FAIL;
6309 }
6310 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6311 == FAIL)
6312 return FAIL;
6313 if (addr_type_arg != ADDR_LINES)
6314 *argt |= (ZEROR | NOTADR) ;
6315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 else
6317 {
6318 char_u ch = attr[len];
6319 attr[len] = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006320 semsg(_("E181: Invalid attribute: %s"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 attr[len] = ch;
6322 return FAIL;
6323 }
6324 }
6325
6326 return OK;
6327}
6328
Bram Moolenaara850a712009-01-28 14:42:59 +00006329/*
6330 * ":command ..."
6331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006333ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 char_u *name;
6336 char_u *end;
6337 char_u *p;
6338 long argt = 0;
6339 long def = -1;
6340 int flags = 0;
6341 int compl = EXPAND_NOTHING;
6342 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006343 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006345 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 p = eap->arg;
6348
6349 /* Check for attributes */
6350 while (*p == '-')
6351 {
6352 ++p;
6353 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006354 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6355 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 == FAIL)
6357 return;
6358 p = skipwhite(end);
6359 }
6360
6361 /* Get the name (if any) and skip to the following argument */
6362 name = p;
6363 if (ASCII_ISALPHA(*p))
6364 while (ASCII_ISALNUM(*p))
6365 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006366 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006368 emsg(_("E182: Invalid command name"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 return;
6370 }
6371 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006372 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374 /* If there is nothing after the name, and no attributes were specified,
6375 * we are listing commands
6376 */
6377 p = skipwhite(end);
6378 if (!has_attr && ends_excmd(*p))
6379 {
6380 uc_list(name, end - name);
6381 }
6382 else if (!ASCII_ISUPPER(*name))
6383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006384 emsg(_("E183: User defined commands must start with an uppercase letter"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 return;
6386 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006387 else if ((name_len == 1 && *name == 'X')
6388 || (name_len <= 4
6389 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006391 emsg(_("E841: Reserved name, cannot be used for user defined command"));
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006392 return;
6393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 else
6395 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006396 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397}
6398
6399/*
6400 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 * Clear all user commands, global and for current buffer.
6402 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006403 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006404ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
6406 uc_clear(&ucmds);
6407 uc_clear(&curbuf->b_ucmds);
6408}
6409
6410/*
6411 * Clear all user commands for "gap".
6412 */
6413 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006414uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 int i;
6417 ucmd_T *cmd;
6418
6419 for (i = 0; i < gap->ga_len; ++i)
6420 {
6421 cmd = USER_CMD_GA(gap, i);
6422 vim_free(cmd->uc_name);
6423 vim_free(cmd->uc_rep);
6424# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6425 vim_free(cmd->uc_compl_arg);
6426# endif
6427 }
6428 ga_clear(gap);
6429}
6430
6431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 int i = 0;
6435 ucmd_T *cmd = NULL;
6436 int cmp = -1;
6437 garray_T *gap;
6438
6439 gap = &curbuf->b_ucmds;
6440 for (;;)
6441 {
6442 for (i = 0; i < gap->ga_len; ++i)
6443 {
6444 cmd = USER_CMD_GA(gap, i);
6445 cmp = STRCMP(eap->arg, cmd->uc_name);
6446 if (cmp <= 0)
6447 break;
6448 }
6449 if (gap == &ucmds || cmp == 0)
6450 break;
6451 gap = &ucmds;
6452 }
6453
6454 if (cmp != 0)
6455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006456 semsg(_("E184: No such user-defined command: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 return;
6458 }
6459
6460 vim_free(cmd->uc_name);
6461 vim_free(cmd->uc_rep);
6462# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6463 vim_free(cmd->uc_compl_arg);
6464# endif
6465
6466 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
6468 if (i < gap->ga_len)
6469 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6470}
6471
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006472/*
6473 * split and quote args for <f-args>
6474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006476uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 char_u *buf;
6479 char_u *p;
6480 char_u *q;
6481 int len;
6482
6483 /* Precalculate length */
6484 p = arg;
6485 len = 2; /* Initial and final quotes */
6486
6487 while (*p)
6488 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006489 if (p[0] == '\\' && p[1] == '\\')
6490 {
6491 len += 2;
6492 p += 2;
6493 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006494 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
6496 len += 1;
6497 p += 2;
6498 }
6499 else if (*p == '\\' || *p == '"')
6500 {
6501 len += 2;
6502 p += 1;
6503 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006504 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 p = skipwhite(p);
6507 if (*p == NUL)
6508 break;
6509 len += 3; /* "," */
6510 }
6511 else
6512 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006513#ifdef FEAT_MBYTE
6514 int charlen = (*mb_ptr2len)(p);
6515 len += charlen;
6516 p += charlen;
6517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 ++len;
6519 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522 }
6523
6524 buf = alloc(len + 1);
6525 if (buf == NULL)
6526 {
6527 *lenp = 0;
6528 return buf;
6529 }
6530
6531 p = arg;
6532 q = buf;
6533 *q++ = '"';
6534 while (*p)
6535 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006536 if (p[0] == '\\' && p[1] == '\\')
6537 {
6538 *q++ = '\\';
6539 *q++ = '\\';
6540 p += 2;
6541 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006542 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 {
6544 *q++ = p[1];
6545 p += 2;
6546 }
6547 else if (*p == '\\' || *p == '"')
6548 {
6549 *q++ = '\\';
6550 *q++ = *p++;
6551 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006552 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
6554 p = skipwhite(p);
6555 if (*p == NUL)
6556 break;
6557 *q++ = '"';
6558 *q++ = ',';
6559 *q++ = '"';
6560 }
6561 else
6562 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006563 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565 }
6566 *q++ = '"';
6567 *q = 0;
6568
6569 *lenp = len;
6570 return buf;
6571}
6572
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006573 static size_t
6574add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6575{
6576 size_t result;
6577
6578 result = STRLEN(mod_str);
6579 if (*multi_mods)
6580 result += 1;
6581 if (buf != NULL)
6582 {
6583 if (*multi_mods)
6584 STRCAT(buf, " ");
6585 STRCAT(buf, mod_str);
6586 }
6587
6588 *multi_mods = 1;
6589
6590 return result;
6591}
6592
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593/*
6594 * Check for a <> code in a user command.
6595 * "code" points to the '<'. "len" the length of the <> (inclusive).
6596 * "buf" is where the result is to be added.
6597 * "split_buf" points to a buffer used for splitting, caller should free it.
6598 * "split_len" is the length of what "split_buf" contains.
6599 * Returns the length of the replacement, which has been added to "buf".
6600 * Returns -1 if there was no match, and only the "<" has been copied.
6601 */
6602 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006603uc_check_code(
6604 char_u *code,
6605 size_t len,
6606 char_u *buf,
6607 ucmd_T *cmd, /* the user command we're expanding */
6608 exarg_T *eap, /* ex arguments */
6609 char_u **split_buf,
6610 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 size_t result = 0;
6613 char_u *p = code + 1;
6614 size_t l = len - 2;
6615 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006616 enum {
6617 ct_ARGS,
6618 ct_BANG,
6619 ct_COUNT,
6620 ct_LINE1,
6621 ct_LINE2,
6622 ct_RANGE,
6623 ct_MODS,
6624 ct_REGISTER,
6625 ct_LT,
6626 ct_NONE
6627 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6630 {
6631 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6632 p += 2;
6633 l -= 2;
6634 }
6635
Bram Moolenaar371d5402006-03-20 21:47:49 +00006636 ++l;
6637 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006639 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006641 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006643 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006645 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006647 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006649 else if (STRNICMP(p, "range>", l) == 0)
6650 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006651 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006653 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006655 else if (STRNICMP(p, "mods>", l) == 0)
6656 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
6658 switch (type)
6659 {
6660 case ct_ARGS:
6661 /* Simple case first */
6662 if (*eap->arg == NUL)
6663 {
6664 if (quote == 1)
6665 {
6666 result = 2;
6667 if (buf != NULL)
6668 STRCPY(buf, "''");
6669 }
6670 else
6671 result = 0;
6672 break;
6673 }
6674
6675 /* When specified there is a single argument don't split it.
6676 * Works for ":Cmd %" when % is "a b c". */
6677 if ((eap->argt & NOSPC) && quote == 2)
6678 quote = 1;
6679
6680 switch (quote)
6681 {
6682 case 0: /* No quoting, no splitting */
6683 result = STRLEN(eap->arg);
6684 if (buf != NULL)
6685 STRCPY(buf, eap->arg);
6686 break;
6687 case 1: /* Quote, but don't split */
6688 result = STRLEN(eap->arg) + 2;
6689 for (p = eap->arg; *p; ++p)
6690 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006691#ifdef FEAT_MBYTE
6692 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6693 /* DBCS can contain \ in a trail byte, skip the
6694 * double-byte character. */
6695 ++p;
6696 else
6697#endif
6698 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 ++result;
6700 }
6701
6702 if (buf != NULL)
6703 {
6704 *buf++ = '"';
6705 for (p = eap->arg; *p; ++p)
6706 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006707#ifdef FEAT_MBYTE
6708 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6709 /* DBCS can contain \ in a trail byte, copy the
6710 * double-byte character to avoid escaping. */
6711 *buf++ = *p++;
6712 else
6713#endif
6714 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 *buf++ = '\\';
6716 *buf++ = *p;
6717 }
6718 *buf = '"';
6719 }
6720
6721 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006722 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 /* This is hard, so only do it once, and cache the result */
6724 if (*split_buf == NULL)
6725 *split_buf = uc_split_args(eap->arg, split_len);
6726
6727 result = *split_len;
6728 if (buf != NULL && result != 0)
6729 STRCPY(buf, *split_buf);
6730
6731 break;
6732 }
6733 break;
6734
6735 case ct_BANG:
6736 result = eap->forceit ? 1 : 0;
6737 if (quote)
6738 result += 2;
6739 if (buf != NULL)
6740 {
6741 if (quote)
6742 *buf++ = '"';
6743 if (eap->forceit)
6744 *buf++ = '!';
6745 if (quote)
6746 *buf = '"';
6747 }
6748 break;
6749
6750 case ct_LINE1:
6751 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006752 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 case ct_COUNT:
6754 {
6755 char num_buf[20];
6756 long num = (type == ct_LINE1) ? eap->line1 :
6757 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006758 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6760 size_t num_len;
6761
6762 sprintf(num_buf, "%ld", num);
6763 num_len = STRLEN(num_buf);
6764 result = num_len;
6765
6766 if (quote)
6767 result += 2;
6768
6769 if (buf != NULL)
6770 {
6771 if (quote)
6772 *buf++ = '"';
6773 STRCPY(buf, num_buf);
6774 buf += num_len;
6775 if (quote)
6776 *buf = '"';
6777 }
6778
6779 break;
6780 }
6781
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006782 case ct_MODS:
6783 {
6784 int multi_mods = 0;
6785 typedef struct {
6786 int *varp;
6787 char *name;
6788 } mod_entry_T;
6789 static mod_entry_T mod_entries[] = {
6790#ifdef FEAT_BROWSE_CMD
6791 {&cmdmod.browse, "browse"},
6792#endif
6793#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6794 {&cmdmod.confirm, "confirm"},
6795#endif
6796 {&cmdmod.hide, "hide"},
6797 {&cmdmod.keepalt, "keepalt"},
6798 {&cmdmod.keepjumps, "keepjumps"},
6799 {&cmdmod.keepmarks, "keepmarks"},
6800 {&cmdmod.keeppatterns, "keeppatterns"},
6801 {&cmdmod.lockmarks, "lockmarks"},
6802 {&cmdmod.noswapfile, "noswapfile"},
6803 {NULL, NULL}
6804 };
6805 int i;
6806
6807 result = quote ? 2 : 0;
6808 if (buf != NULL)
6809 {
6810 if (quote)
6811 *buf++ = '"';
6812 *buf = '\0';
6813 }
6814
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006815 /* :aboveleft and :leftabove */
6816 if (cmdmod.split & WSP_ABOVE)
6817 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6818 /* :belowright and :rightbelow */
6819 if (cmdmod.split & WSP_BELOW)
6820 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6821 /* :botright */
6822 if (cmdmod.split & WSP_BOT)
6823 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006824
6825 /* the modifiers that are simple flags */
6826 for (i = 0; mod_entries[i].varp != NULL; ++i)
6827 if (*mod_entries[i].varp)
6828 result += add_cmd_modifier(buf, mod_entries[i].name,
6829 &multi_mods);
6830
6831 /* TODO: How to support :noautocmd? */
6832#ifdef HAVE_SANDBOX
6833 /* TODO: How to support :sandbox?*/
6834#endif
6835 /* :silent */
6836 if (msg_silent > 0)
6837 result += add_cmd_modifier(buf,
6838 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006839 /* :tab */
6840 if (cmdmod.tab > 0)
6841 result += add_cmd_modifier(buf, "tab", &multi_mods);
6842 /* :topleft */
6843 if (cmdmod.split & WSP_TOP)
6844 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006845 /* TODO: How to support :unsilent?*/
6846 /* :verbose */
6847 if (p_verbose > 0)
6848 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006849 /* :vertical */
6850 if (cmdmod.split & WSP_VERT)
6851 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006852 if (quote && buf != NULL)
6853 {
6854 buf += result - 2;
6855 *buf = '"';
6856 }
6857 break;
6858 }
6859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 case ct_REGISTER:
6861 result = eap->regname ? 1 : 0;
6862 if (quote)
6863 result += 2;
6864 if (buf != NULL)
6865 {
6866 if (quote)
6867 *buf++ = '\'';
6868 if (eap->regname)
6869 *buf++ = eap->regname;
6870 if (quote)
6871 *buf = '\'';
6872 }
6873 break;
6874
6875 case ct_LT:
6876 result = 1;
6877 if (buf != NULL)
6878 *buf = '<';
6879 break;
6880
6881 default:
6882 /* Not recognized: just copy the '<' and return -1. */
6883 result = (size_t)-1;
6884 if (buf != NULL)
6885 *buf = '<';
6886 break;
6887 }
6888
6889 return result;
6890}
6891
6892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006893do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 char_u *buf;
6896 char_u *p;
6897 char_u *q;
6898
6899 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006900 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006901 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 size_t len, totlen;
6903
6904 size_t split_len = 0;
6905 char_u *split_buf = NULL;
6906 ucmd_T *cmd;
6907#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006908 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909#endif
6910
6911 if (eap->cmdidx == CMD_USER)
6912 cmd = USER_CMD(eap->useridx);
6913 else
6914 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6915
6916 /*
6917 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006918 * First round: "buf" is NULL, compute length, allocate "buf".
6919 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 */
6921 buf = NULL;
6922 for (;;)
6923 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006924 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006925 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006927
6928 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006930 start = vim_strchr(p, '<');
6931 if (start != NULL)
6932 end = vim_strchr(start + 1, '>');
6933 if (buf != NULL)
6934 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006935 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6936 ;
6937 if (*ksp == K_SPECIAL
6938 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006939 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6940# ifdef FEAT_GUI
6941 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6942# endif
6943 ))
6944 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006945 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006946 * KS_SPECIAL KE_FILLER, like for mappings, but
6947 * do_cmdline() doesn't handle that, so convert it back.
6948 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6949 len = ksp - p;
6950 if (len > 0)
6951 {
6952 mch_memmove(q, p, len);
6953 q += len;
6954 }
6955 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6956 p = ksp + 3;
6957 continue;
6958 }
6959 }
6960
6961 /* break if there no <item> is found */
6962 if (start == NULL || end == NULL)
6963 break;
6964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 /* Include the '>' */
6966 ++end;
6967
6968 /* Take everything up to the '<' */
6969 len = start - p;
6970 if (buf == NULL)
6971 totlen += len;
6972 else
6973 {
6974 mch_memmove(q, p, len);
6975 q += len;
6976 }
6977
6978 len = uc_check_code(start, end - start, q, cmd, eap,
6979 &split_buf, &split_len);
6980 if (len == (size_t)-1)
6981 {
6982 /* no match, continue after '<' */
6983 p = start + 1;
6984 len = 1;
6985 }
6986 else
6987 p = end;
6988 if (buf == NULL)
6989 totlen += len;
6990 else
6991 q += len;
6992 }
6993 if (buf != NULL) /* second time here, finished */
6994 {
6995 STRCPY(q, p);
6996 break;
6997 }
6998
6999 totlen += STRLEN(p); /* Add on the trailing characters */
7000 buf = alloc((unsigned)(totlen + 1));
7001 if (buf == NULL)
7002 {
7003 vim_free(split_buf);
7004 return;
7005 }
7006 }
7007
7008#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007009 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010#endif
7011 (void)do_cmdline(buf, eap->getline, eap->cookie,
7012 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7013#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007014 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015#endif
7016 vim_free(buf);
7017 vim_free(split_buf);
7018}
7019
7020# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7021 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007022get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7025}
7026
7027/*
7028 * Function given to ExpandGeneric() to obtain the list of user command names.
7029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007031get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 if (idx < curbuf->b_ucmds.ga_len)
7034 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7035 idx -= curbuf->b_ucmds.ga_len;
7036 if (idx < ucmds.ga_len)
7037 return USER_CMD(idx)->uc_name;
7038 return NULL;
7039}
7040
7041/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007042 * Function given to ExpandGeneric() to obtain the list of user address type names.
7043 */
7044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007045get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007046{
7047 return (char_u *)addr_type_complete[idx].name;
7048}
7049
7050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 * Function given to ExpandGeneric() to obtain the list of user command
7052 * attributes.
7053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007055get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007058 {"addr", "bang", "bar", "buffer", "complete",
7059 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007061 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 return NULL;
7063 return (char_u *)user_cmd_flags[idx];
7064}
7065
7066/*
7067 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007070get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7073
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007074 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 return NULL;
7076 return (char_u *)user_cmd_nargs[idx];
7077}
7078
7079/*
7080 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007083get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 return (char_u *)command_complete[idx].name;
7086}
7087# endif /* FEAT_CMDL_COMPL */
7088
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007089/*
7090 * Parse address type argument
7091 */
7092 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007093parse_addr_type_arg(
7094 char_u *value,
7095 int vallen,
7096 long *argt,
7097 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007098{
7099 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007100
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007101 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7102 {
7103 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7104 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7105 if (a && b)
7106 {
7107 *addr_type_arg = addr_type_complete[i].expand;
7108 break;
7109 }
7110 }
7111
7112 if (addr_type_complete[i].expand == -1)
7113 {
7114 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007115
Bram Moolenaar1c465442017-03-12 20:10:05 +01007116 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007117 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007118 err[i] = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007119 semsg(_("E180: Invalid address type value: %s"), err);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007120 return FAIL;
7121 }
7122
7123 if (*addr_type_arg != ADDR_LINES)
7124 *argt |= NOTADR;
7125
7126 return OK;
7127}
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129#endif /* FEAT_USR_CMDS */
7130
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007131#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7132/*
7133 * Parse a completion argument "value[vallen]".
7134 * The detected completion goes in "*complp", argument type in "*argt".
7135 * When there is an argument, for function and user defined completion, it's
7136 * copied to allocated memory and stored in "*compl_arg".
7137 * Returns FAIL if something is wrong.
7138 */
7139 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007140parse_compl_arg(
7141 char_u *value,
7142 int vallen,
7143 int *complp,
7144 long *argt,
7145 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007146{
7147 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007148# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007149 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007150# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007151 int i;
7152 int valend = vallen;
7153
7154 /* Look for any argument part - which is the part after any ',' */
7155 for (i = 0; i < vallen; ++i)
7156 {
7157 if (value[i] == ',')
7158 {
7159 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007160# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007161 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007162# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007163 valend = i;
7164 break;
7165 }
7166 }
7167
7168 for (i = 0; command_complete[i].expand != 0; ++i)
7169 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007170 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007171 && STRNCMP(value, command_complete[i].name, valend) == 0)
7172 {
7173 *complp = command_complete[i].expand;
7174 if (command_complete[i].expand == EXPAND_BUFFERS)
7175 *argt |= BUFNAME;
7176 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7177 || command_complete[i].expand == EXPAND_FILES)
7178 *argt |= XFILE;
7179 break;
7180 }
7181 }
7182
7183 if (command_complete[i].expand == 0)
7184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007185 semsg(_("E180: Invalid complete value: %s"), value);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007186 return FAIL;
7187 }
7188
7189# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7190 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7191 && arg != NULL)
7192# else
7193 if (arg != NULL)
7194# endif
7195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007196 emsg(_("E468: Completion argument only allowed for custom completion"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007197 return FAIL;
7198 }
7199
7200# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7201 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7202 && arg == NULL)
7203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007204 emsg(_("E467: Custom completion requires a function argument"));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007205 return FAIL;
7206 }
7207
7208 if (arg != NULL)
7209 *compl_arg = vim_strnsave(arg, (int)arglen);
7210# endif
7211 return OK;
7212}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007213
7214 int
7215cmdcomplete_str_to_type(char_u *complete_str)
7216{
7217 int i;
7218
7219 for (i = 0; command_complete[i].expand != 0; ++i)
7220 if (STRCMP(complete_str, command_complete[i].name) == 0)
7221 return command_complete[i].expand;
7222
7223 return EXPAND_NOTHING;
7224}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007225#endif
7226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaare6850792010-05-14 15:28:44 +02007230 if (*eap->arg == NUL)
7231 {
7232#ifdef FEAT_EVAL
7233 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7234 char_u *p = NULL;
7235
7236 if (expr != NULL)
7237 {
7238 ++emsg_off;
7239 p = eval_to_string(expr, NULL, FALSE);
7240 --emsg_off;
7241 vim_free(expr);
7242 }
7243 if (p != NULL)
7244 {
7245 MSG(p);
7246 vim_free(p);
7247 }
7248 else
7249 MSG("default");
7250#else
7251 MSG(_("unknown"));
7252#endif
7253 }
7254 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007255 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256}
7257
7258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 if (*eap->arg == NUL && eap->cmd[2] == '!')
7262 MSG(_("Greetings, Vim user!"));
7263 do_highlight(eap->arg, eap->forceit, FALSE);
7264}
7265
7266
7267/*
7268 * Call this function if we thought we were going to exit, but we won't
7269 * (because of an error). May need to restore the terminal mode.
7270 */
7271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007272not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273{
7274 exiting = FALSE;
7275 settmode(TMODE_RAW);
7276}
7277
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007278 static int
7279before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7280{
7281 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7282
7283 /* Bail out when autocommands closed the window.
7284 * Refuse to quit when the buffer in the last window is being closed (can
7285 * only happen in autocommands). */
7286 if (!win_valid(wp)
7287 || curbuf_locked()
7288 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7289 return TRUE;
7290
7291 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7292 {
7293 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7294 /* Refuse to quit when locked or when the buffer in the last window is
7295 * being closed (can only happen in autocommands). */
7296 if (curbuf_locked()
7297 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7298 return TRUE;
7299 }
7300
7301 return FALSE;
7302}
7303
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007305 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007306 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 */
7308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007309ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007311 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313#ifdef FEAT_CMDWIN
7314 if (cmdwin_type != 0)
7315 {
7316 cmdwin_result = Ctrl_C;
7317 return;
7318 }
7319#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007320 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007321 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007322 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007323 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007324 return;
7325 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007326 if (eap->addr_count > 0)
7327 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007328 int wnr = eap->line2;
7329
7330 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7331 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007332 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007333 }
7334 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007335 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007336
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007337 /* Refuse to quit when locked. */
7338 if (curbuf_locked())
7339 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007340
7341 /* Trigger QuitPre and maybe ExitPre */
7342 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007343 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345#ifdef FEAT_NETBEANS_INTG
7346 netbeansForcedQuit = eap->forceit;
7347#endif
7348
7349 /*
7350 * If there are more files or windows we won't exit.
7351 */
7352 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7353 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007354 if ((!buf_hide(wp->w_buffer)
7355 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007356 | (eap->forceit ? CCGD_FORCEIT : 0)
7357 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007359 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
7361 not_exiting();
7362 }
7363 else
7364 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007365 /* quit last window
7366 * Note: only_one_window() returns true, even so a help window is
7367 * still open. In that case only quit, if no address has been
7368 * specified. Example:
7369 * :h|wincmd w|1q - don't quit
7370 * :h|wincmd w|q - quit
7371 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007372 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007374 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007375#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007379 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381}
7382
7383/*
7384 * ":cquit".
7385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007387ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
7389 getout(1); /* this does not always pass on the exit code to the Manx
7390 compiler. why? */
7391}
7392
7393/*
7394 * ":qall": try to quit all windows
7395 */
7396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007397ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399# ifdef FEAT_CMDWIN
7400 if (cmdwin_type != 0)
7401 {
7402 if (eap->forceit)
7403 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7404 else
7405 cmdwin_result = K_XF2;
7406 return;
7407 }
7408# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007409
7410 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007411 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007412 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007413 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007414 return;
7415 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007416
7417 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007418 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007421 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 getout(0);
7423 not_exiting();
7424}
7425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426/*
7427 * ":close": close current window, unless it is the last one
7428 */
7429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007430ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007432 win_T *win;
7433 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007434#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007436 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007438#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007439 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007440 {
7441 if (eap->addr_count == 0)
7442 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007443 else
7444 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007445 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007446 {
7447 winnr++;
7448 if (winnr == eap->line2)
7449 break;
7450 }
7451 if (win == NULL)
7452 win = lastwin;
7453 ex_win_close(eap->forceit, win, NULL);
7454 }
7455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456}
7457
Bram Moolenaar4033c552017-09-16 20:54:51 +02007458#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007459/*
7460 * ":pclose": Close any preview window.
7461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007463ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007464{
7465 win_T *win;
7466
Bram Moolenaar29323592016-07-24 22:04:11 +02007467 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007468 if (win->w_p_pvw)
7469 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007470 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007471 break;
7472 }
7473}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007474#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007475
Bram Moolenaarf740b292006-02-16 22:11:02 +00007476/*
7477 * Close window "win" and take care of handling closing the last window for a
7478 * modified buffer.
7479 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007481ex_win_close(
7482 int forceit,
7483 win_T *win,
7484 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 int need_hide;
7487 buf_T *buf = win->w_buffer;
7488
7489 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007490 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007492#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 if ((p_confirm || cmdmod.confirm) && p_write)
7494 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007495 bufref_T bufref;
7496
7497 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007499 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 return;
7501 need_hide = FALSE;
7502 }
7503 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007506 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 return;
7508 }
7509 }
7510
Bram Moolenaar4033c552017-09-16 20:54:51 +02007511#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007513#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007514
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007516 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007517 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007518 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007519 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520}
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007523 * Handle the argument for a tabpage related ex command.
7524 * Returns a tabpage number.
7525 * When an error is encountered then eap->errmsg is set.
7526 */
7527 static int
7528get_tabpage_arg(exarg_T *eap)
7529{
7530 int tab_number;
7531 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7532
7533 if (eap->arg && *eap->arg != NUL)
7534 {
7535 char_u *p = eap->arg;
7536 char_u *p_save;
7537 int relative = 0; /* argument +N/-N means: go to N places to the
7538 * right/left relative to the current position. */
7539
7540 if (*p == '-')
7541 {
7542 relative = -1;
7543 p++;
7544 }
7545 else if (*p == '+')
7546 {
7547 relative = 1;
7548 p++;
7549 }
7550
7551 p_save = p;
7552 tab_number = getdigits(&p);
7553
7554 if (relative == 0)
7555 {
7556 if (STRCMP(p, "$") == 0)
7557 tab_number = LAST_TAB_NR;
7558 else if (p == p_save || *p_save == '-' || *p != NUL
7559 || tab_number > LAST_TAB_NR)
7560 {
7561 /* No numbers as argument. */
7562 eap->errmsg = e_invarg;
7563 goto theend;
7564 }
7565 }
7566 else
7567 {
7568 if (*p_save == NUL)
7569 tab_number = 1;
7570 else if (p == p_save || *p_save == '-' || *p != NUL
7571 || tab_number == 0)
7572 {
7573 /* No numbers as argument. */
7574 eap->errmsg = e_invarg;
7575 goto theend;
7576 }
7577 tab_number = tab_number * relative + tabpage_index(curtab);
7578 if (!unaccept_arg0 && relative == -1)
7579 --tab_number;
7580 }
7581 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7582 eap->errmsg = e_invarg;
7583 }
7584 else if (eap->addr_count > 0)
7585 {
7586 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007587 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007588 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007589 tab_number = 0;
7590 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007591 else
7592 {
7593 tab_number = eap->line2;
7594 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7595 {
7596 --tab_number;
7597 if (tab_number < unaccept_arg0)
7598 eap->errmsg = e_invarg;
7599 }
7600 }
7601 }
7602 else
7603 {
7604 switch (eap->cmdidx)
7605 {
7606 case CMD_tabnext:
7607 tab_number = tabpage_index(curtab) + 1;
7608 if (tab_number > LAST_TAB_NR)
7609 tab_number = 1;
7610 break;
7611 case CMD_tabmove:
7612 tab_number = LAST_TAB_NR;
7613 break;
7614 default:
7615 tab_number = tabpage_index(curtab);
7616 }
7617 }
7618
7619theend:
7620 return tab_number;
7621}
7622
7623/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007624 * ":tabclose": close current tab page, unless it is the last one.
7625 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 */
7627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007628ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007630 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007631 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007632
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007633# ifdef FEAT_CMDWIN
7634 if (cmdwin_type != 0)
7635 cmdwin_result = K_IGNORE;
7636 else
7637# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007638 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007642 tab_number = get_tabpage_arg(eap);
7643 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007644 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007645 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007646 if (tp == NULL)
7647 {
7648 beep_flush();
7649 return;
7650 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007651 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007652 {
7653 tabpage_close_other(tp, eap->forceit);
7654 return;
7655 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007656 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007657 tabpage_close(eap->forceit);
7658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007660}
7661
7662/*
7663 * ":tabonly": close all tab pages except the current one
7664 */
7665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007666ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007667{
7668 tabpage_T *tp;
7669 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007670 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007671
7672# ifdef FEAT_CMDWIN
7673 if (cmdwin_type != 0)
7674 cmdwin_result = K_IGNORE;
7675 else
7676# endif
7677 if (first_tabpage->tp_next == NULL)
7678 MSG(_("Already only one tab page"));
7679 else
7680 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007681 tab_number = get_tabpage_arg(eap);
7682 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007683 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007684 goto_tabpage(tab_number);
7685 /* Repeat this up to a 1000 times, because autocommands may
7686 * mess up the lists. */
7687 for (done = 0; done < 1000; ++done)
7688 {
7689 FOR_ALL_TABPAGES(tp)
7690 if (tp->tp_topframe != topframe)
7691 {
7692 tabpage_close_other(tp, eap->forceit);
7693 /* if we failed to close it quit */
7694 if (valid_tabpage(tp))
7695 done = 1000;
7696 /* start over, "tp" is now invalid */
7697 break;
7698 }
7699 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007700 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007701 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007702 }
7703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705
7706/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007707 * Close the current tab page.
7708 */
7709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007710tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007711{
7712 /* First close all the windows but the current one. If that worked then
7713 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007714 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007715 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007716 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007717 ex_win_close(forceit, curwin, NULL);
7718# ifdef FEAT_GUI
7719 need_mouse_correct = TRUE;
7720# endif
7721}
7722
7723/*
7724 * Close tab page "tp", which is not the current tab page.
7725 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007726 * Also takes care of the tab pages line disappearing when closing the
7727 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007728 */
7729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007730tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007731{
7732 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007733 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007734 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007735
7736 /* Limit to 1000 windows, autocommands may add a window while we close
7737 * one. OK, so I'm paranoid... */
7738 while (++done < 1000)
7739 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007740 wp = tp->tp_firstwin;
7741 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007742
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007743 /* Autocommands may delete the tab page under our fingers and we may
7744 * fail to close a window with a modified buffer. */
7745 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007746 break;
7747 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007748
Bram Moolenaar29323592016-07-24 22:04:11 +02007749 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007750
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007751 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007752 if (h != tabline_height())
7753 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007754}
7755
7756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 * ":only".
7758 */
7759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007760ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007762 win_T *wp;
7763 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764# ifdef FEAT_GUI
7765 need_mouse_correct = TRUE;
7766# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007767 if (eap->addr_count > 0)
7768 {
7769 wnr = eap->line2;
7770 for (wp = firstwin; --wnr > 0; )
7771 {
7772 if (wp->w_next == NULL)
7773 break;
7774 else
7775 wp = wp->w_next;
7776 }
7777 win_goto(wp);
7778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 close_others(TRUE, eap->forceit);
7780}
7781
7782/*
7783 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007784 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007787ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 if (eap->addr_count == 0)
7790 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007791 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007795ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007797 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007798 if (!eap->skip)
7799 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007800#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007801 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007802#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007803 if (eap->addr_count == 0)
7804 win_close(curwin, FALSE); /* don't free buffer */
7805 else
7806 {
7807 int winnr = 0;
7808 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007809
Bram Moolenaar2256c992016-11-15 21:17:07 +01007810 FOR_ALL_WINDOWS(win)
7811 {
7812 winnr++;
7813 if (winnr == eap->line2)
7814 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007815 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007816 if (win == NULL)
7817 win = lastwin;
7818 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
7821}
7822
7823/*
7824 * ":stop" and ":suspend": Suspend Vim.
7825 */
7826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007827ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 /*
7830 * Disallow suspending for "rvim".
7831 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007832 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {
7834 if (!eap->forceit)
7835 autowrite_all();
7836 windgoto((int)Rows - 1, 0);
7837 out_char('\n');
7838 out_flush();
7839 stoptermcap();
7840 out_flush(); /* needed for SUN to restore xterm buffer */
7841#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007842 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843#endif
7844 ui_suspend(); /* call machine specific function */
7845#ifdef FEAT_TITLE
7846 maketitle();
7847 resettitle(); /* force updating the title */
7848#endif
7849 starttermcap();
7850 scroll_start(); /* scroll screen before redrawing */
7851 redraw_later_clear();
7852 shell_resized(); /* may have resized window */
7853 }
7854}
7855
7856/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007857 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 */
7859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007860ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862#ifdef FEAT_CMDWIN
7863 if (cmdwin_type != 0)
7864 {
7865 cmdwin_result = Ctrl_C;
7866 return;
7867 }
7868#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007869 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007870 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007871 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007872 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007873 return;
7874 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007875
7876 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007877 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878
7879 /*
7880 * if more files or windows we won't exit
7881 */
7882 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7883 exiting = TRUE;
7884 if ( ((eap->cmdidx == CMD_wq
7885 || curbufIsChanged())
7886 && do_write(eap) == FAIL)
7887 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007888 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
7890 not_exiting();
7891 }
7892 else
7893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007896 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897# ifdef FEAT_GUI
7898 need_mouse_correct = TRUE;
7899# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007900 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007901 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903}
7904
7905/*
7906 * ":print", ":list", ":number".
7907 */
7908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007909ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007911 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007912 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007913 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007915 for ( ;!got_int; ui_breakcheck())
7916 {
7917 print_line(eap->line1,
7918 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7919 || (eap->flags & EXFLAG_NR)),
7920 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7921 if (++eap->line1 > eap->line2)
7922 break;
7923 out_flush(); /* show one line at a time */
7924 }
7925 setpcmark();
7926 /* put cursor at last line */
7927 curwin->w_cursor.lnum = eap->line2;
7928 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932}
7933
7934#ifdef FEAT_BYTEOFF
7935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 goto_byte(eap->line2);
7939}
7940#endif
7941
7942/*
7943 * ":shell".
7944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007946ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 do_shell(NULL, 0);
7949}
7950
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007951#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007953static int drop_busy = FALSE;
7954static int drop_filec;
7955static char_u **drop_filev = NULL;
7956static int drop_split;
7957static void (*drop_callback)(void *);
7958static void *drop_cookie;
7959
7960 static void
7961handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963 exarg_T ea;
7964 int save_msg_scroll = msg_scroll;
7965
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007966 // Setting the argument list may cause screen updates and being called
7967 // recursively. Avoid that by setting drop_busy.
7968 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
7970 /* Check whether the current buffer is changed. If so, we will need
7971 * to split the current window or data could be lost.
7972 * We don't need to check if the 'hidden' option is set, as in this
7973 * case the buffer won't be lost.
7974 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007975 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {
7977 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007978 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 --emsg_off;
7980 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007981 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (win_split(0, 0) == FAIL)
7984 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007985 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
7987 /* When splitting the window, create a new alist. Otherwise the
7988 * existing one is overwritten. */
7989 alist_unlink(curwin->w_alist);
7990 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 }
7992
7993 /*
7994 * Set up the new argument list.
7995 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007996 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997
7998 /*
7999 * Move to the first file.
8000 */
8001 /* Fake up a minimal "next" command for do_argfile() */
8002 vim_memset(&ea, 0, sizeof(ea));
8003 ea.cmd = (char_u *)"next";
8004 do_argfile(&ea, 0);
8005
8006 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8007 * mode that is not needed here. */
8008 need_start_insertmode = FALSE;
8009
8010 /* Restore msg_scroll, otherwise a following command may cause scrolling
8011 * unexpectedly. The screen will be redrawn by the caller, thus
8012 * msg_scroll being set by displaying a message is irrelevant. */
8013 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008014
8015 if (drop_callback != NULL)
8016 drop_callback(drop_cookie);
8017
8018 drop_filev = NULL;
8019 drop_busy = FALSE;
8020}
8021
8022/*
8023 * Handle a file drop. The code is here because a drop is *nearly* like an
8024 * :args command, but not quite (we have a list of exact filenames, so we
8025 * don't want to (a) parse a command line, or (b) expand wildcards. So the
8026 * code is very similar to :args and hence needs access to a lot of the static
8027 * functions in this file.
8028 *
8029 * The "filev" list must have been allocated using alloc(), as should each item
8030 * in the list. This function takes over responsibility for freeing the "filev"
8031 * list.
8032 */
8033 void
8034handle_drop(
8035 int filec, // the number of files dropped
8036 char_u **filev, // the list of files dropped
8037 int split, // force splitting the window
8038 void (*callback)(void *), // to be called after setting the argument
8039 // list
8040 void *cookie) // argument for "callback" (allocated)
8041{
8042 // Cannot handle recursive drops, finish the pending one.
8043 if (drop_busy)
8044 {
8045 FreeWild(filec, filev);
8046 vim_free(cookie);
8047 return;
8048 }
8049
8050 // When calling handle_drop() more than once in a row we only use the last
8051 // one.
8052 if (drop_filev != NULL)
8053 {
8054 FreeWild(drop_filec, drop_filev);
8055 vim_free(drop_cookie);
8056 }
8057
8058 drop_filec = filec;
8059 drop_filev = filev;
8060 drop_split = split;
8061 drop_callback = callback;
8062 drop_cookie = cookie;
8063
8064 // Postpone this when:
8065 // - editing the command line
8066 // - not possible to change the current buffer
8067 // - updating the screen
8068 // As it may change buffers and window structures that are in use and cause
8069 // freed memory to be used.
8070 if (text_locked() || curbuf_locked() || updating_screen)
8071 return;
8072
8073 handle_drop_internal();
8074}
8075
8076/*
8077 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8078 * reset: Handle a postponed drop.
8079 */
8080 void
8081handle_any_postponed_drop(void)
8082{
8083 if (!drop_busy && drop_filev != NULL
8084 && !text_locked() && !curbuf_locked() && !updating_screen)
8085 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086}
8087#endif
8088
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089/*
8090 * Clear an argument list: free all file names and reset it to zero entries.
8091 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008093alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
8095 while (--al->al_ga.ga_len >= 0)
8096 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8097 ga_clear(&al->al_ga);
8098}
8099
8100/*
8101 * Init an argument list.
8102 */
8103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008104alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8107}
8108
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109/*
8110 * Remove a reference from an argument list.
8111 * Ignored when the argument list is the global one.
8112 * If the argument list is no longer used by any window, free it.
8113 */
8114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008115alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 if (al != &global_alist && --al->al_refcount <= 0)
8118 {
8119 alist_clear(al);
8120 vim_free(al);
8121 }
8122}
8123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124/*
8125 * Create a new argument list and use it for the current window.
8126 */
8127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008128alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
8130 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8131 if (curwin->w_alist == NULL)
8132 {
8133 curwin->w_alist = &global_alist;
8134 ++global_alist.al_refcount;
8135 }
8136 else
8137 {
8138 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008139 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 alist_init(curwin->w_alist);
8141 }
8142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008144#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145/*
8146 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008147 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8148 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 */
8150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008151alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
8153 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008154 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 char_u **new_arg_files;
8156 int new_arg_file_count;
8157 char_u *save_p_su = p_su;
8158 int i;
8159
8160 /* Don't use 'suffixes' here. This should work like the shell did the
8161 * expansion. Also, the vimrc file isn't read yet, thus the user
8162 * can't set the options. */
8163 p_su = empty_option;
8164 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8165 if (old_arg_files != NULL)
8166 {
8167 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008168 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8169 old_arg_count = GARGCOUNT;
8170 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008172 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 && new_arg_file_count > 0)
8174 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008175 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8176 TRUE, fnum_list, fnum_len);
8177 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
8180 p_su = save_p_su;
8181}
8182#endif
8183
8184/*
8185 * Set the argument list for the current window.
8186 * Takes over the allocated files[] and the allocated fnames in it.
8187 */
8188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008189alist_set(
8190 alist_T *al,
8191 int count,
8192 char_u **files,
8193 int use_curbuf,
8194 int *fnum_list,
8195 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196{
8197 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008198 static int recursive = 0;
8199
8200 if (recursive)
8201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008202 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008203 return;
8204 }
8205 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
8207 alist_clear(al);
8208 if (ga_grow(&al->al_ga, count) == OK)
8209 {
8210 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008211 {
8212 if (got_int)
8213 {
8214 /* When adding many buffers this can take a long time. Allow
8215 * interrupting here. */
8216 while (i < count)
8217 vim_free(files[i++]);
8218 break;
8219 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008220
8221 /* May set buffer name of a buffer previously used for the
8222 * argument list, so that it's re-used by alist_add. */
8223 if (fnum_list != NULL && i < fnum_len)
8224 buf_set_name(fnum_list[i], files[i]);
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008227 ui_breakcheck();
8228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 vim_free(files);
8230 }
8231 else
8232 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008235
8236 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237}
8238
8239/*
8240 * Add file "fname" to argument list "al".
8241 * "fname" must have been allocated and "al" must have been checked for room.
8242 */
8243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008244alist_add(
8245 alist_T *al,
8246 char_u *fname,
8247 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
8249 if (fname == NULL) /* don't add NULL file names */
8250 return;
8251#ifdef BACKSLASH_IN_FILENAME
8252 slash_adjust(fname);
8253#endif
8254 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8255 if (set_fnum > 0)
8256 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8257 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8258 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259}
8260
8261#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8262/*
8263 * Adjust slashes in file names. Called after 'shellslash' was set.
8264 */
8265 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008266alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267{
8268 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008270 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
8272 for (i = 0; i < GARGCOUNT; ++i)
8273 if (GARGLIST[i].ae_fname != NULL)
8274 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008275 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 if (wp->w_alist != &global_alist)
8277 for (i = 0; i < WARGCOUNT(wp); ++i)
8278 if (WARGLIST(wp)[i].ae_fname != NULL)
8279 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280}
8281#endif
8282
8283/*
8284 * ":preserve".
8285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008287ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008289 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 ml_preserve(curbuf, TRUE);
8291}
8292
8293/*
8294 * ":recover".
8295 */
8296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008297ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8300 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008301 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8302 | CCGD_MULTWIN
8303 | (eap->forceit ? CCGD_FORCEIT : 0)
8304 | CCGD_EXCMD)
8305
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 && (*eap->arg == NUL
8307 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8308 ml_recover();
8309 recoverymode = FALSE;
8310}
8311
8312/*
8313 * Command modifier used in a wrong way.
8314 */
8315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008316ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
8318 eap->errmsg = e_invcmd;
8319}
8320
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321/*
8322 * :sview [+command] file split window with new file, read-only
8323 * :split [[+command] file] split window with current or new file
8324 * :vsplit [[+command] file] split window vertically with current or new file
8325 * :new [[+command] file] split window with no or new file
8326 * :vnew [[+command] file] split vertically window with no or new file
8327 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008328 *
8329 * :tabedit open new Tab page with empty window
8330 * :tabedit [+command] file open new Tab page and edit "file"
8331 * :tabnew [[+command] file] just like :tabedit
8332 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 */
8334 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008335ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008337 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008338#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008340#endif
8341#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008343#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008344 int use_tab = eap->cmdidx == CMD_tabedit
8345 || eap->cmdidx == CMD_tabfind
8346 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
Bram Moolenaar4033c552017-09-16 20:54:51 +02008348#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
Bram Moolenaar4033c552017-09-16 20:54:51 +02008352#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008354 * quickfix windows. But it's OK when doing ":tab split". */
8355 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {
8357 if (eap->cmdidx == CMD_split)
8358 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (eap->cmdidx == CMD_vsplit)
8360 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363
Bram Moolenaar4033c552017-09-16 20:54:51 +02008364#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008365 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {
8367 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8368 FNAME_MESS, TRUE, curbuf->b_ffname);
8369 if (fname == NULL)
8370 goto theend;
8371 eap->arg = fname;
8372 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008373# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008374 else
8375# endif
8376#endif
8377#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 && eap->cmdidx != CMD_new)
8381 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008382 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008383# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008384 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008385# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008386 au_has_group((char_u *)"FileExplorer"))
8387 {
8388 /* No browsing supported but we do have the file explorer:
8389 * Edit the directory. */
8390 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8391 eap->arg = (char_u *)".";
8392 }
8393 else
8394 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008395 fname = do_browse(0, (char_u *)(use_tab
8396 ? _("Edit File in new tab page")
8397 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008399 if (fname == NULL)
8400 goto theend;
8401 eap->arg = fname;
8402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 }
8404 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008407 /*
8408 * Either open new tab page or split the window.
8409 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008410 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008411 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008412 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8413 : eap->addr_count == 0 ? 0
8414 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008415 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008416 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008417
8418 /* set the alternate buffer for the window we came from */
8419 if (curwin != old_curwin
8420 && win_valid(old_curwin)
8421 && old_curwin->w_buffer != curbuf
8422 && !cmdmod.keepalt)
8423 old_curwin->w_alt_fnum = curbuf->b_fnum;
8424 }
8425 }
8426 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 /* Reset 'scrollbind' when editing another file, but keep it when
8430 * doing ":split" without arguments. */
8431 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008432# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008436 {
8437 RESET_BINDING(curwin);
8438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 else
8440 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 do_exedit(eap, old_curwin);
8442 }
8443
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008444# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008448# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449theend:
8450 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008453
8454/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008455 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008456 */
8457 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008458tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008459{
8460 exarg_T ea;
8461
8462 vim_memset(&ea, 0, sizeof(ea));
8463 ea.cmdidx = CMD_tabnew;
8464 ea.cmd = (char_u *)"tabn";
8465 ea.arg = (char_u *)"";
8466 ex_splitview(&ea);
8467}
8468
8469/*
8470 * :tabnext command
8471 */
8472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008473ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008474{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008475 int tab_number;
8476
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008477 switch (eap->cmdidx)
8478 {
8479 case CMD_tabfirst:
8480 case CMD_tabrewind:
8481 goto_tabpage(1);
8482 break;
8483 case CMD_tablast:
8484 goto_tabpage(9999);
8485 break;
8486 case CMD_tabprevious:
8487 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008488 if (eap->arg && *eap->arg != NUL)
8489 {
8490 char_u *p = eap->arg;
8491 char_u *p_save = p;
8492
8493 tab_number = getdigits(&p);
8494 if (p == p_save || *p_save == '-' || *p != NUL
8495 || tab_number == 0)
8496 {
8497 /* No numbers as argument. */
8498 eap->errmsg = e_invarg;
8499 return;
8500 }
8501 }
8502 else
8503 {
8504 if (eap->addr_count == 0)
8505 tab_number = 1;
8506 else
8507 {
8508 tab_number = eap->line2;
8509 if (tab_number < 1)
8510 {
8511 eap->errmsg = e_invrange;
8512 return;
8513 }
8514 }
8515 }
8516 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008517 break;
8518 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008519 tab_number = get_tabpage_arg(eap);
8520 if (eap->errmsg == NULL)
8521 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008522 break;
8523 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008524}
8525
8526/*
8527 * :tabmove command
8528 */
8529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008530ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008531{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008532 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008533
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008534 tab_number = get_tabpage_arg(eap);
8535 if (eap->errmsg == NULL)
8536 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008537}
8538
8539/*
8540 * :tabs command: List tabs and their contents.
8541 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008543ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008544{
8545 tabpage_T *tp;
8546 win_T *wp;
8547 int tabcount = 1;
8548
8549 msg_start();
8550 msg_scroll = TRUE;
8551 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8552 {
8553 msg_putchar('\n');
8554 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008555 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008556 out_flush(); /* output one line at a time */
8557 ui_breakcheck();
8558
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008559 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008560 wp = firstwin;
8561 else
8562 wp = tp->tp_firstwin;
8563 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8564 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008565 msg_putchar('\n');
8566 msg_putchar(wp == curwin ? '>' : ' ');
8567 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008568 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8569 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008570 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008571 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008572 else
8573 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8574 IObuff, IOSIZE, TRUE);
8575 msg_outtrans(IObuff);
8576 out_flush(); /* output one line at a time */
8577 ui_breakcheck();
8578 }
8579 }
8580}
8581
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582/*
8583 * ":mode": Set screen mode.
8584 * If no argument given, just get the screen size and redraw.
8585 */
8586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008587ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
8589 if (*eap->arg == NUL)
8590 shell_resized();
8591 else
8592 mch_screenmode(eap->arg);
8593}
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595/*
8596 * ":resize".
8597 * set, increment or decrement current window height
8598 */
8599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008600ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 int n;
8603 win_T *wp = curwin;
8604
8605 if (eap->addr_count > 0)
8606 {
8607 n = eap->line2;
8608 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8609 ;
8610 }
8611
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008612# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 if (cmdmod.split & WSP_VERT)
8617 {
8618 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008619 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8621 n = 9999;
8622 win_setwidth_win((int)n, wp);
8623 }
8624 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {
8626 if (*eap->arg == '-' || *eap->arg == '+')
8627 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008628 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 n = 9999;
8630 win_setheight_win((int)n, wp);
8631 }
8632}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634/*
8635 * ":find [+command] <file>" command.
8636 */
8637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008638ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640#ifdef FEAT_SEARCHPATH
8641 char_u *fname;
8642 int count;
8643
8644 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8645 TRUE, curbuf->b_ffname);
8646 if (eap->addr_count > 0)
8647 {
8648 /* Repeat finding the file "count" times. This matters when it
8649 * appears several times in the path. */
8650 count = eap->line2;
8651 while (fname != NULL && --count > 0)
8652 {
8653 vim_free(fname);
8654 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8655 FALSE, curbuf->b_ffname);
8656 }
8657 }
8658
8659 if (fname != NULL)
8660 {
8661 eap->arg = fname;
8662#endif
8663 do_exedit(eap, NULL);
8664#ifdef FEAT_SEARCHPATH
8665 vim_free(fname);
8666 }
8667#endif
8668}
8669
8670/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008671 * ":open" simulation: for now just work like ":visual".
8672 */
8673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008674ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008675{
8676 regmatch_T regmatch;
8677 char_u *p;
8678
8679 curwin->w_cursor.lnum = eap->line2;
8680 beginline(BL_SOL | BL_FIX);
8681 if (*eap->arg == '/')
8682 {
8683 /* ":open /pattern/": put cursor in column found with pattern */
8684 ++eap->arg;
8685 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8686 *p = NUL;
8687 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8688 if (regmatch.regprog != NULL)
8689 {
8690 regmatch.rm_ic = p_ic;
8691 p = ml_get_curline();
8692 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008693 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008694 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008695 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008696 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008697 }
8698 /* Move to the NUL, ignore any other arguments. */
8699 eap->arg += STRLEN(eap->arg);
8700 }
8701 check_cursor();
8702
8703 eap->cmdidx = CMD_visual;
8704 do_exedit(eap, NULL);
8705}
8706
8707/*
8708 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 */
8710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 do_exedit(eap, NULL);
8714}
8715
8716/*
8717 * ":edit <file>" command and alikes.
8718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008720do_exedit(
8721 exarg_T *eap,
8722 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008726 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 /*
8729 * ":vi" command ends Ex mode.
8730 */
8731 if (exmode_active && (eap->cmdidx == CMD_visual
8732 || eap->cmdidx == CMD_view))
8733 {
8734 exmode_active = FALSE;
8735 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008736 {
8737 /* Special case: ":global/pat/visual\NLvi-commands" */
8738 if (global_busy)
8739 {
8740 int rd = RedrawingDisabled;
8741 int nwr = no_wait_return;
8742 int ms = msg_scroll;
8743#ifdef FEAT_GUI
8744 int he = hold_gui_events;
8745#endif
8746
8747 if (eap->nextcmd != NULL)
8748 {
8749 stuffReadbuff(eap->nextcmd);
8750 eap->nextcmd = NULL;
8751 }
8752
8753 if (exmode_was != EXMODE_VIM)
8754 settmode(TMODE_RAW);
8755 RedrawingDisabled = 0;
8756 no_wait_return = 0;
8757 need_wait_return = FALSE;
8758 msg_scroll = 0;
8759#ifdef FEAT_GUI
8760 hold_gui_events = 0;
8761#endif
8762 must_redraw = CLEAR;
8763
8764 main_loop(FALSE, TRUE);
8765
8766 RedrawingDisabled = rd;
8767 no_wait_return = nwr;
8768 msg_scroll = ms;
8769#ifdef FEAT_GUI
8770 hold_gui_events = he;
8771#endif
8772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 }
8776
8777 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008778 || eap->cmdidx == CMD_tabnew
8779 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008780 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008782 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 setpcmark();
8784 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008785 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8786 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008788 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 || *eap->arg != NUL
8790#ifdef FEAT_BROWSE
8791 || cmdmod.browse
8792#endif
8793 )
8794 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008795 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8796 * can bring us here, others are stopped earlier. */
8797 if (*eap->arg != NUL && curbuf_locked())
8798 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008799
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 n = readonlymode;
8801 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8802 readonlymode = TRUE;
8803 else if (eap->cmdidx == CMD_enew)
8804 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8805 empty buffer */
8806 setpcmark();
8807 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8808 NULL, eap,
8809 /* ":edit" goes to first line if Vi compatible */
8810 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8811 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8812 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008813 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008815 /* after a split we can use an existing buffer */
8816 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008818 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
8820 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 if (old_curwin != NULL)
8822 {
8823 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008824 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008826#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008827 cleanup_T cs;
8828
8829 /* Reset the error/interrupt/exception state here so that
8830 * aborting() returns FALSE when closing a window. */
8831 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008832#endif
8833#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008835#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008836 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008837
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008838#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008839 /* Restore the error/interrupt/exception state if not
8840 * discarded by a new aborting error, interrupt, or
8841 * uncaught exception. */
8842 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 }
8845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847 else if (readonlymode && curbuf->b_nwindows == 1)
8848 {
8849 /* When editing an already visited buffer, 'readonly' won't be set
8850 * but the previous value is kept. With ":view" and ":sview" we
8851 * want the file to be readonly, except when another window is
8852 * editing the same buffer. */
8853 curbuf->b_p_ro = TRUE;
8854 }
8855 readonlymode = n;
8856 }
8857 else
8858 {
8859 if (eap->do_ecmd_cmd != NULL)
8860 do_cmdline_cmd(eap->do_ecmd_cmd);
8861#ifdef FEAT_TITLE
8862 n = curwin->w_arg_idx_invalid;
8863#endif
8864 check_arg_idx(curwin);
8865#ifdef FEAT_TITLE
8866 if (n != curwin->w_arg_idx_invalid)
8867 maketitle();
8868#endif
8869 }
8870
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 /*
8872 * if ":split file" worked, set alternate file name in old window to new
8873 * file
8874 */
8875 if (old_curwin != NULL
8876 && *eap->arg != NUL
8877 && curwin != old_curwin
8878 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008879 && old_curwin->w_buffer != curbuf
8880 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882
8883 ex_no_reprint = TRUE;
8884}
8885
8886#ifndef FEAT_GUI
8887/*
8888 * ":gui" and ":gvim" when there is no GUI.
8889 */
8890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008891ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893 eap->errmsg = e_nogvim;
8894}
8895#endif
8896
8897#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008899ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
8901 gui_make_tearoff(eap->arg);
8902}
8903#endif
8904
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008905#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8906 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008908ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008910# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8911 if (gui.in_use)
8912 gui_make_popup(eap->arg, eap->forceit);
8913# ifdef FEAT_TERM_POPUP_MENU
8914 else
8915# endif
8916# endif
8917# ifdef FEAT_TERM_POPUP_MENU
8918 pum_make_popup(eap->arg, eap->forceit);
8919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920}
8921#endif
8922
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008924ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8927 MSG(_("No swap file"));
8928 else
8929 msg(curbuf->b_ml.ml_mfp->mf_fname);
8930}
8931
8932/*
8933 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8934 * offset.
8935 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008938ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008941 win_T *save_curwin = curwin;
8942 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 long topline;
8944 long y;
8945 linenr_T old_linenr = curwin->w_cursor.lnum;
8946
8947 setpcmark();
8948
8949 /*
8950 * determine max topline
8951 */
8952 if (curwin->w_p_scb)
8953 {
8954 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008955 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 {
8957 if (wp->w_p_scb && wp->w_buffer)
8958 {
8959 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8960 if (topline > y)
8961 topline = y;
8962 }
8963 }
8964 if (topline < 1)
8965 topline = 1;
8966 }
8967 else
8968 {
8969 topline = 1;
8970 }
8971
8972
8973 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008974 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008976 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 {
8978 if (curwin->w_p_scb)
8979 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008980 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 y = topline - curwin->w_topline;
8982 if (y > 0)
8983 scrollup(y, TRUE);
8984 else
8985 scrolldown(-y, TRUE);
8986 curwin->w_scbind_pos = topline;
8987 redraw_later(VALID);
8988 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 }
8991 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008992 curwin = save_curwin;
8993 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 if (curwin->w_p_scb)
8995 {
8996 did_syncbind = TRUE;
8997 checkpcmark();
8998 if (old_linenr != curwin->w_cursor.lnum)
8999 {
9000 char_u ctrl_o[2];
9001
9002 ctrl_o[0] = Ctrl_O;
9003 ctrl_o[1] = 0;
9004 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9005 }
9006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007}
9008
9009
9010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009011ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009013 int i;
9014 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9015 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017 if (eap->usefilter) /* :r!cmd */
9018 do_bang(1, eap, FALSE, FALSE, TRUE);
9019 else
9020 {
9021 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9022 return;
9023
9024#ifdef FEAT_BROWSE
9025 if (cmdmod.browse)
9026 {
9027 char_u *browseFile;
9028
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009029 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 NULL, NULL, NULL, curbuf);
9031 if (browseFile != NULL)
9032 {
9033 i = readfile(browseFile, NULL,
9034 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9035 vim_free(browseFile);
9036 }
9037 else
9038 i = OK;
9039 }
9040 else
9041#endif
9042 if (*eap->arg == NUL)
9043 {
9044 if (check_fname() == FAIL) /* check for no file name */
9045 return;
9046 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9047 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9048 }
9049 else
9050 {
9051 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9052 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9053 i = readfile(eap->arg, NULL,
9054 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9055
9056 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009057 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009059#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 if (!aborting())
9061#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009062 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 }
9064 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009065 {
9066 if (empty && exmode_active)
9067 {
9068 /* Delete the empty line that remains. Historically ex does
9069 * this but vi doesn't. */
9070 if (eap->line2 == 0)
9071 lnum = curbuf->b_ml.ml_line_count;
9072 else
9073 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009074 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009075 {
9076 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009077 if (curwin->w_cursor.lnum > 1
9078 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009079 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009080 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009081 }
9082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 }
9086}
9087
Bram Moolenaarea408852005-06-25 22:49:46 +00009088static char_u *prev_dir = NULL;
9089
9090#if defined(EXITFREE) || defined(PROTO)
9091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009092free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009093{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009094 VIM_CLEAR(prev_dir);
9095 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009096}
9097#endif
9098
Bram Moolenaarf4258302013-06-02 18:20:17 +02009099/*
9100 * Deal with the side effects of changing the current directory.
9101 * When "local" is TRUE then this was after an ":lcd" command.
9102 */
9103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009104post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009105{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009106 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009107 if (local)
9108 {
9109 /* If still in global directory, need to remember current
9110 * directory as global directory. */
9111 if (globaldir == NULL && prev_dir != NULL)
9112 globaldir = vim_strsave(prev_dir);
9113 /* Remember this local directory for the window. */
9114 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9115 curwin->w_localdir = vim_strsave(NameBuff);
9116 }
9117 else
9118 {
9119 /* We are now in the global directory, no need to remember its
9120 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009121 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009122 }
9123
9124 shorten_fnames(TRUE);
9125}
9126
Bram Moolenaarea408852005-06-25 22:49:46 +00009127
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128/*
9129 * ":cd", ":lcd", ":chdir" and ":lchdir".
9130 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009132ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009134 char_u *new_dir;
9135 char_u *tofree;
9136 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
9138 new_dir = eap->arg;
9139#if !defined(UNIX) && !defined(VMS)
9140 /* for non-UNIX ":cd" means: print current directory */
9141 if (*new_dir == NUL)
9142 ex_pwd(NULL);
9143 else
9144#endif
9145 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009146 if (allbuf_locked())
9147 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009148 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9149 && !eap->forceit)
9150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009151 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009152 return;
9153 }
9154
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 /* ":cd -": Change to previous directory */
9156 if (STRCMP(new_dir, "-") == 0)
9157 {
9158 if (prev_dir == NULL)
9159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009160 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 return;
9162 }
9163 new_dir = prev_dir;
9164 }
9165
9166 /* Save current directory for next ":cd -" */
9167 tofree = prev_dir;
9168 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9169 prev_dir = vim_strsave(NameBuff);
9170 else
9171 prev_dir = NULL;
9172
9173#if defined(UNIX) || defined(VMS)
9174 /* for UNIX ":cd" means: go to home directory */
9175 if (*new_dir == NUL)
9176 {
9177 /* use NameBuff for home directory name */
9178# ifdef VMS
9179 char_u *p;
9180
9181 p = mch_getenv((char_u *)"SYS$LOGIN");
9182 if (p == NULL || *p == NUL) /* empty is the same as not set */
9183 NameBuff[0] = NUL;
9184 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009185 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186# else
9187 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9188# endif
9189 new_dir = NameBuff;
9190 }
9191#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009192 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009193 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009194 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009195 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 else
9197 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009198 int is_local_chdir = eap->cmdidx == CMD_lcd
9199 || eap->cmdidx == CMD_lchdir;
9200
9201 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
9203 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009204 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009206
9207 if (dir_differs)
9208 apply_autocmds(EVENT_DIRCHANGED,
9209 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9210 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
9212 vim_free(tofree);
9213 }
9214}
9215
9216/*
9217 * ":pwd".
9218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009220ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221{
9222 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9223 {
9224#ifdef BACKSLASH_IN_FILENAME
9225 slash_adjust(NameBuff);
9226#endif
9227 msg(NameBuff);
9228 }
9229 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009230 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
9233/*
9234 * ":=".
9235 */
9236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009237ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009239 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009240 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241}
9242
9243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009244ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009246 int n;
9247 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248
9249 if (cursor_valid())
9250 {
9251 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9252 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009253 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009255
9256 len = eap->line2;
9257 switch (*eap->arg)
9258 {
9259 case 'm': break;
9260 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009261 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009262 }
9263 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264}
9265
9266/*
9267 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9268 */
9269 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009270do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271{
9272 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009273 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274
9275 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009276 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009277 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009279 wait_now = msec - done > 1000L ? 1000L : msec - done;
9280#ifdef FEAT_TIMERS
9281 {
9282 long due_time = check_due_timer();
9283
9284 if (due_time > 0 && due_time < wait_now)
9285 wait_now = due_time;
9286 }
9287#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009288#ifdef FEAT_JOB_CHANNEL
9289 if (has_any_channel() && wait_now > 100L)
9290 wait_now = 100L;
9291#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009292 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009293#ifdef FEAT_JOB_CHANNEL
9294 if (has_any_channel())
9295 ui_breakcheck_force(TRUE);
9296 else
9297#endif
9298 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009299#ifdef MESSAGE_QUEUE
9300 /* Process the netbeans and clientserver messages that may have been
9301 * received in the call to ui_breakcheck() when the GUI is in use. This
9302 * may occur when running a test case. */
9303 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009306
9307 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9308 // input buffer, otherwise a following call to input() fails.
9309 if (got_int)
9310 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311}
9312
9313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009314do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 int mode;
9317 char_u *cmdp;
9318
9319 cmdp = eap->cmd;
9320 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9321
9322 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9323 eap->arg, mode, isabbrev))
9324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009325 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009327 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 break;
9329 }
9330}
9331
9332/*
9333 * ":winsize" command (obsolete).
9334 */
9335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009336ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 int w, h;
9339 char_u *arg = eap->arg;
9340 char_u *p;
9341
9342 w = getdigits(&arg);
9343 arg = skipwhite(arg);
9344 p = arg;
9345 h = getdigits(&arg);
9346 if (*p != NUL && *arg == NUL)
9347 set_shellsize(w, h, TRUE);
9348 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009349 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350}
9351
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009353ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354{
9355 int xchar = NUL;
9356 char_u *p;
9357
9358 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9359 {
9360 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9361 if (eap->arg[1] == NUL)
9362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009363 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 return;
9365 }
9366 xchar = eap->arg[1];
9367 p = eap->arg + 2;
9368 }
9369 else
9370 p = eap->arg + 1;
9371
9372 eap->nextcmd = check_nextcmd(p);
9373 p = skipwhite(p);
9374 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009375 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009376 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 {
9378 /* Pass flags on for ":vertical wincmd ]". */
9379 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009380 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9382 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009383 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 }
9385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386
Bram Moolenaar843ee412004-06-30 16:16:41 +00009387#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388/*
9389 * ":winpos".
9390 */
9391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009392ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 int x, y;
9395 char_u *arg = eap->arg;
9396 char_u *p;
9397
9398 if (*arg == NUL)
9399 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009400# if defined(FEAT_GUI) || defined(MSWIN)
9401# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009403# else
9404 if (mch_get_winpos(&x, &y) != FAIL)
9405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 {
9407 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9408 msg(IObuff);
9409 }
9410 else
9411# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009412 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 }
9414 else
9415 {
9416 x = getdigits(&arg);
9417 arg = skipwhite(arg);
9418 p = arg;
9419 y = getdigits(&arg);
9420 if (*p == NUL || *arg != NUL)
9421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009422 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 return;
9424 }
9425# ifdef FEAT_GUI
9426 if (gui.in_use)
9427 gui_mch_set_winpos(x, y);
9428 else if (gui.starting)
9429 {
9430 /* Remember the coordinates for when the window is opened. */
9431 gui_win_x = x;
9432 gui_win_y = y;
9433 }
9434# ifdef HAVE_TGETENT
9435 else
9436# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009437# else
9438# ifdef MSWIN
9439 mch_set_winpos(x, y);
9440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441# endif
9442# ifdef HAVE_TGETENT
9443 if (*T_CWP)
9444 term_set_winpos(x, y);
9445# endif
9446 }
9447}
9448#endif
9449
9450/*
9451 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9452 */
9453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009454ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456 oparg_T oa;
9457
9458 clear_oparg(&oa);
9459 oa.regname = eap->regname;
9460 oa.start.lnum = eap->line1;
9461 oa.end.lnum = eap->line2;
9462 oa.line_count = eap->line2 - eap->line1 + 1;
9463 oa.motion_type = MLINE;
9464#ifdef FEAT_VIRTUALEDIT
9465 virtual_op = FALSE;
9466#endif
9467 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9468 {
9469 setpcmark();
9470 curwin->w_cursor.lnum = eap->line1;
9471 beginline(BL_SOL | BL_FIX);
9472 }
9473
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009474 if (VIsual_active)
9475 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009476
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 switch (eap->cmdidx)
9478 {
9479 case CMD_delete:
9480 oa.op_type = OP_DELETE;
9481 op_delete(&oa);
9482 break;
9483
9484 case CMD_yank:
9485 oa.op_type = OP_YANK;
9486 (void)op_yank(&oa, FALSE, TRUE);
9487 break;
9488
9489 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009490 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009492 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9493#else
9494 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009496 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 oa.op_type = OP_RSHIFT;
9498 else
9499 oa.op_type = OP_LSHIFT;
9500 op_shift(&oa, FALSE, eap->amount);
9501 break;
9502 }
9503#ifdef FEAT_VIRTUALEDIT
9504 virtual_op = MAYBE;
9505#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009506 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
9509/*
9510 * ":put".
9511 */
9512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009513ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 /* ":0put" works like ":1put!". */
9516 if (eap->line2 == 0)
9517 {
9518 eap->line2 = 1;
9519 eap->forceit = TRUE;
9520 }
9521 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009522 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9523 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524}
9525
9526/*
9527 * Handle ":copy" and ":move".
9528 */
9529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009530ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532 long n;
9533
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009534 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 if (eap->arg == NULL) /* error detected */
9536 {
9537 eap->nextcmd = NULL;
9538 return;
9539 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009540 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541
9542 /*
9543 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9544 */
9545 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009547 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 return;
9549 }
9550
9551 if (eap->cmdidx == CMD_move)
9552 {
9553 if (do_move(eap->line1, eap->line2, n) == FAIL)
9554 return;
9555 }
9556 else
9557 ex_copy(eap->line1, eap->line2, n);
9558 u_clearline();
9559 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009560 ex_may_print(eap);
9561}
9562
9563/*
9564 * Print the current line if flags were given to the Ex command.
9565 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009566 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009567ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009568{
9569 if (eap->flags != 0)
9570 {
9571 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9572 (eap->flags & EXFLAG_LIST));
9573 ex_no_reprint = TRUE;
9574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576
9577/*
9578 * ":smagic" and ":snomagic".
9579 */
9580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009581ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 int magic_save = p_magic;
9584
9585 p_magic = (eap->cmdidx == CMD_smagic);
9586 do_sub(eap);
9587 p_magic = magic_save;
9588}
9589
9590/*
9591 * ":join".
9592 */
9593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009594ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 curwin->w_cursor.lnum = eap->line1;
9597 if (eap->line1 == eap->line2)
9598 {
9599 if (eap->addr_count >= 2) /* :2,2join does nothing */
9600 return;
9601 if (eap->line2 == curbuf->b_ml.ml_line_count)
9602 {
9603 beep_flush();
9604 return;
9605 }
9606 ++eap->line2;
9607 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009608 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009610 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
9613/*
9614 * ":[addr]@r" or ":[addr]*r": execute register
9615 */
9616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009617ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009620 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621
9622 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009623 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
9625#ifdef USE_ON_FLY_SCROLL
9626 dont_scroll = TRUE; /* disallow scrolling here */
9627#endif
9628
9629 /* get the register name. No name means to use the previous one */
9630 c = *eap->arg;
9631 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9632 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009633 /* Put the register in the typeahead buffer with the "silent" flag. */
9634 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9635 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 else
9640 {
9641 int save_efr = exec_from_reg;
9642
9643 exec_from_reg = TRUE;
9644
9645 /*
9646 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009647 * Continue until the stuff buffer is empty and all added characters
9648 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009650 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9652
9653 exec_from_reg = save_efr;
9654 }
9655}
9656
9657/*
9658 * ":!".
9659 */
9660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009661ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9664}
9665
9666/*
9667 * ":undo".
9668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009670ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009672 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009673 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009674 else
9675 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676}
9677
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009678#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009680ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009681{
9682 char_u hash[UNDO_HASH_SIZE];
9683
9684 u_compute_hash(hash);
9685 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9686}
9687
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009689ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009690{
9691 char_u hash[UNDO_HASH_SIZE];
9692
9693 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009694 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009695}
9696#endif
9697
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698/*
9699 * ":redo".
9700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009702ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703{
9704 u_redo(1);
9705}
9706
9707/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009708 * ":earlier" and ":later".
9709 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009711ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009712{
9713 long count = 0;
9714 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009715 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009716 char_u *p = eap->arg;
9717
9718 if (*p == NUL)
9719 count = 1;
9720 else if (isdigit(*p))
9721 {
9722 count = getdigits(&p);
9723 switch (*p)
9724 {
9725 case 's': ++p; sec = TRUE; break;
9726 case 'm': ++p; sec = TRUE; count *= 60; break;
9727 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009728 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9729 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009730 }
9731 }
9732
9733 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009734 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009735 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009736 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9737 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009738}
9739
9740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 * ":redir": start/stop redirection.
9742 */
9743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009744ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745{
9746 char *mode;
9747 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009748 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
Bram Moolenaarba768492016-07-08 15:32:54 +02009750#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009751 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009753 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009754 return;
9755 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009756#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009757
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (STRICMP(eap->arg, "END") == 0)
9759 close_redir();
9760 else
9761 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009762 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009764 ++arg;
9765 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009767 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 mode = "a";
9769 }
9770 else
9771 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009772 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773
9774 close_redir();
9775
9776 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009777 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 if (fname == NULL)
9779 return;
9780#ifdef FEAT_BROWSE
9781 if (cmdmod.browse)
9782 {
9783 char_u *browseFile;
9784
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009785 browseFile = do_browse(BROWSE_SAVE,
9786 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009787 fname, NULL, NULL,
9788 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 if (browseFile == NULL)
9790 return; /* operation cancelled */
9791 vim_free(fname);
9792 fname = browseFile;
9793 eap->forceit = TRUE; /* since dialog already asked */
9794 }
9795#endif
9796
9797 redir_fd = open_exfile(fname, eap->forceit, mode);
9798 vim_free(fname);
9799 }
9800#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009801 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 {
9803 /* redirect to a register a-z (resp. A-Z for appending) */
9804 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009805 ++arg;
9806 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009808 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009809 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009811 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009813 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009814 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009815 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009816 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009818 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009819 if (*arg == '>')
9820 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009821 /* Make register empty when not using @A-@Z and the
9822 * command is valid. */
9823 if (*arg == NUL && !isupper(redir_reg))
9824 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009826 }
9827 if (*arg != NUL)
9828 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009829 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009830 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009831 }
9832 }
9833 else if (*arg == '=' && arg[1] == '>')
9834 {
9835 int append;
9836
9837 /* redirect to a variable */
9838 close_redir();
9839 arg += 2;
9840
9841 if (*arg == '>')
9842 {
9843 ++arg;
9844 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 }
9846 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009847 append = FALSE;
9848
9849 if (var_redir_start(skipwhite(arg), append) == OK)
9850 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 }
9852#endif
9853
9854 /* TODO: redirect to a buffer */
9855
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009857 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009859
9860 /* Make sure redirection is not off. Can happen for cmdline completion
9861 * that indirectly invokes a command to catch its output. */
9862 if (redir_fd != NULL
9863#ifdef FEAT_EVAL
9864 || redir_reg || redir_vname
9865#endif
9866 )
9867 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868}
9869
9870/*
9871 * ":redraw": force redraw
9872 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009874ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875{
9876 int r = RedrawingDisabled;
9877 int p = p_lz;
9878
9879 RedrawingDisabled = 0;
9880 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009881 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009883 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884#ifdef FEAT_TITLE
9885 if (need_maketitle)
9886 maketitle();
9887#endif
9888 RedrawingDisabled = r;
9889 p_lz = p;
9890
9891 /* Reset msg_didout, so that a message that's there is overwritten. */
9892 msg_didout = FALSE;
9893 msg_col = 0;
9894
Bram Moolenaar56667a52013-07-17 11:54:28 +02009895 /* No need to wait after an intentional redraw. */
9896 need_wait_return = FALSE;
9897
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 out_flush();
9899}
9900
9901/*
9902 * ":redrawstatus": force redraw of status line(s)
9903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009905ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 int r = RedrawingDisabled;
9908 int p = p_lz;
9909
9910 RedrawingDisabled = 0;
9911 p_lz = FALSE;
9912 if (eap->forceit)
9913 status_redraw_all();
9914 else
9915 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009916 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 RedrawingDisabled = r;
9918 p_lz = p;
9919 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920}
9921
Bram Moolenaare12bab32019-01-08 22:02:56 +01009922/*
9923 * ":redrawtabline": force redraw of the tabline
9924 */
9925 static void
9926ex_redrawtabline(exarg_T *eap UNUSED)
9927{
9928 int r = RedrawingDisabled;
9929 int p = p_lz;
9930
9931 RedrawingDisabled = 0;
9932 p_lz = FALSE;
9933
9934 draw_tabline();
9935
9936 RedrawingDisabled = r;
9937 p_lz = p;
9938 out_flush();
9939}
9940
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009942close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
9944 if (redir_fd != NULL)
9945 {
9946 fclose(redir_fd);
9947 redir_fd = NULL;
9948 }
9949#ifdef FEAT_EVAL
9950 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009951 if (redir_vname)
9952 {
9953 var_redir_stop();
9954 redir_vname = 0;
9955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956#endif
9957}
9958
9959#if defined(FEAT_SESSION) && defined(USE_CRNL)
9960# define MKSESSION_NL
9961static int mksession_nl = FALSE; /* use NL only in put_eol() */
9962#endif
9963
9964/*
9965 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9966 */
9967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009968ex_mkrc(
9969 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
9971 FILE *fd;
9972 int failed = FALSE;
9973 char_u *fname;
9974#ifdef FEAT_BROWSE
9975 char_u *browseFile = NULL;
9976#endif
9977#ifdef FEAT_SESSION
9978 int view_session = FALSE;
9979 int using_vdir = FALSE; /* using 'viewdir'? */
9980 char_u *viewFile = NULL;
9981 unsigned *flagp;
9982#endif
9983
9984 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9985 {
9986#ifdef FEAT_SESSION
9987 view_session = TRUE;
9988#else
9989 ex_ni(eap);
9990 return;
9991#endif
9992 }
9993
9994#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009995 /* Use the short file name until ":lcd" is used. We also don't use the
9996 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009997 did_lcd = FALSE;
9998
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
10000 if (eap->cmdidx == CMD_mkview
10001 && (*eap->arg == NUL
10002 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
10003 {
10004 eap->forceit = TRUE;
10005 fname = get_view_file(*eap->arg);
10006 if (fname == NULL)
10007 return;
10008 viewFile = fname;
10009 using_vdir = TRUE;
10010 }
10011 else
10012#endif
10013 if (*eap->arg != NUL)
10014 fname = eap->arg;
10015 else if (eap->cmdidx == CMD_mkvimrc)
10016 fname = (char_u *)VIMRC_FILE;
10017#ifdef FEAT_SESSION
10018 else if (eap->cmdidx == CMD_mksession)
10019 fname = (char_u *)SESSION_FILE;
10020#endif
10021 else
10022 fname = (char_u *)EXRC_FILE;
10023
10024#ifdef FEAT_BROWSE
10025 if (cmdmod.browse)
10026 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +000010027 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028# ifdef FEAT_SESSION
10029 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10030 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10031# endif
10032 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010033 fname, (char_u *)"vim", NULL,
10034 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (browseFile == NULL)
10036 goto theend;
10037 fname = browseFile;
10038 eap->forceit = TRUE; /* since dialog already asked */
10039 }
10040#endif
10041
10042#if defined(FEAT_SESSION) && defined(vim_mkdir)
10043 /* When using 'viewdir' may have to create the directory. */
10044 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010045 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046#endif
10047
10048 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10049 if (fd != NULL)
10050 {
10051#ifdef FEAT_SESSION
10052 if (eap->cmdidx == CMD_mkview)
10053 flagp = &vop_flags;
10054 else
10055 flagp = &ssop_flags;
10056#endif
10057
10058#ifdef MKSESSION_NL
10059 /* "unix" in 'sessionoptions': use NL line separator */
10060 if (view_session && (*flagp & SSOP_UNIX))
10061 mksession_nl = TRUE;
10062#endif
10063
10064 /* Write the version command for :mkvimrc */
10065 if (eap->cmdidx == CMD_mkvimrc)
10066 (void)put_line(fd, "version 6.0");
10067
10068#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010069 if (eap->cmdidx == CMD_mksession)
10070 {
10071 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10072 failed = TRUE;
10073 }
10074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 if (eap->cmdidx != CMD_mkview)
10076#endif
10077 {
10078 /* Write setting 'compatible' first, because it has side effects.
10079 * For that same reason only do it when needed. */
10080 if (p_cp)
10081 (void)put_line(fd, "if !&cp | set cp | endif");
10082 else
10083 (void)put_line(fd, "if &cp | set nocp | endif");
10084 }
10085
10086#ifdef FEAT_SESSION
10087 if (!view_session
10088 || (eap->cmdidx == CMD_mksession
10089 && (*flagp & SSOP_OPTIONS)))
10090#endif
10091 failed |= (makemap(fd, NULL) == FAIL
10092 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10093
10094#ifdef FEAT_SESSION
10095 if (!failed && view_session)
10096 {
10097 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10098 failed = TRUE;
10099 if (eap->cmdidx == CMD_mksession)
10100 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010101 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102
Bram Moolenaard9462e32011-04-11 21:35:11 +020010103 dirnow = alloc(MAXPATHL);
10104 if (dirnow == NULL)
10105 failed = TRUE;
10106 else
10107 {
10108 /*
10109 * Change to session file's dir.
10110 */
10111 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010113 *dirnow = NUL;
10114 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10115 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010116 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010117 shorten_fnames(TRUE);
10118 }
10119 else if (*dirnow != NUL
10120 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10121 {
10122 if (mch_chdir((char *)globaldir) == 0)
10123 shorten_fnames(TRUE);
10124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaard9462e32011-04-11 21:35:11 +020010126 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127
Bram Moolenaard9462e32011-04-11 21:35:11 +020010128 /* restore original dir */
10129 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010131 {
10132 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010133 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +020010134 shorten_fnames(TRUE);
10135 }
10136 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 }
10138 }
10139 else
10140 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010141 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10142 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 }
10144 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10145 == FAIL)
10146 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +010010147# ifdef FEAT_SEARCH_EXTRA
10148 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
10149 failed = TRUE;
10150# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010151 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10152 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010153 if (eap->cmdidx == CMD_mksession)
10154 {
10155 if (put_line(fd, "unlet SessionLoad") == FAIL)
10156 failed = TRUE;
10157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 }
10159#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010160 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10161 failed = TRUE;
10162
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 failed |= fclose(fd);
10164
10165 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010166 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10168 else if (eap->cmdidx == CMD_mksession)
10169 {
10170 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010171 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172
Bram Moolenaard9462e32011-04-11 21:35:11 +020010173 tbuf = alloc(MAXPATHL);
10174 if (tbuf != NULL)
10175 {
10176 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10177 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10178 vim_free(tbuf);
10179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 }
10181#endif
10182#ifdef MKSESSION_NL
10183 mksession_nl = FALSE;
10184#endif
10185 }
10186
10187#ifdef FEAT_BROWSE
10188theend:
10189 vim_free(browseFile);
10190#endif
10191#ifdef FEAT_SESSION
10192 vim_free(viewFile);
10193#endif
10194}
10195
Bram Moolenaardf177f62005-02-22 08:39:57 +000010196#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10197 || defined(PROTO)
10198 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010199vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010200{
10201 if (vim_mkdir(name, prot) != 0)
10202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010203 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +000010204 return FAIL;
10205 }
10206 return OK;
10207}
10208#endif
10209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210/*
10211 * Open a file for writing for an Ex command, with some checks.
10212 * Return file descriptor, or NULL on failure.
10213 */
10214 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010215open_exfile(
10216 char_u *fname,
10217 int forceit,
10218 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219{
10220 FILE *fd;
10221
10222#ifdef UNIX
10223 /* with Unix it is possible to open a directory */
10224 if (mch_isdir(fname))
10225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010226 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 return NULL;
10228 }
10229#endif
10230 if (!forceit && *mode != 'a' && vim_fexists(fname))
10231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010232 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 return NULL;
10234 }
10235
10236 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010237 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
10239 return fd;
10240}
10241
10242/*
10243 * ":mark" and ":k".
10244 */
10245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010246ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247{
10248 pos_T pos;
10249
10250 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010251 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010253 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 else
10255 {
10256 pos = curwin->w_cursor; /* save curwin->w_cursor */
10257 curwin->w_cursor.lnum = eap->line2;
10258 beginline(BL_WHITE | BL_FIX);
10259 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010260 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10262 }
10263}
10264
10265/*
10266 * Update w_topline, w_leftcol and the cursor position.
10267 */
10268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010269update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
10271 check_cursor(); /* put cursor on valid line */
10272 update_topline();
10273 if (!curwin->w_p_wrap)
10274 validate_cursor();
10275 update_curswant();
10276}
10277
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010279 * Save the current State and go to Normal mode.
10280 * Return TRUE if the typeahead could be saved.
10281 */
10282 int
10283save_current_state(save_state_T *sst)
10284{
10285 sst->save_msg_scroll = msg_scroll;
10286 sst->save_restart_edit = restart_edit;
10287 sst->save_msg_didout = msg_didout;
10288 sst->save_State = State;
10289 sst->save_insertmode = p_im;
10290 sst->save_finish_op = finish_op;
10291 sst->save_opcount = opcount;
10292
10293 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10294 restart_edit = 0; /* don't go to Insert mode */
10295 p_im = FALSE; /* don't use 'insertmode' */
10296
10297 /*
10298 * Save the current typeahead. This is required to allow using ":normal"
10299 * from an event handler and makes sure we don't hang when the argument
10300 * ends with half a command.
10301 */
10302 save_typeahead(&sst->tabuf);
10303 return sst->tabuf.typebuf_valid;
10304}
10305
10306 void
10307restore_current_state(save_state_T *sst)
10308{
10309 /* Restore the previous typeahead. */
10310 restore_typeahead(&sst->tabuf);
10311
10312 msg_scroll = sst->save_msg_scroll;
10313 restart_edit = sst->save_restart_edit;
10314 p_im = sst->save_insertmode;
10315 finish_op = sst->save_finish_op;
10316 opcount = sst->save_opcount;
10317 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10318
10319 /* Restore the state (needed when called from a function executed for
10320 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10321 State = sst->save_State;
10322#ifdef CURSOR_SHAPE
10323 ui_cursor_shape(); /* may show different cursor shape */
10324#endif
10325}
10326
10327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 * ":normal[!] {commands}": Execute normal mode commands.
10329 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010331ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010333 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334#ifdef FEAT_MBYTE
10335 char_u *arg = NULL;
10336 int l;
10337 char_u *p;
10338#endif
10339
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010340 if (ex_normal_lock > 0)
10341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010342 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010343 return;
10344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 if (ex_normal_busy >= p_mmd)
10346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010347 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 return;
10349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350
10351#ifdef FEAT_MBYTE
10352 /*
10353 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10354 * this for the K_SPECIAL leading byte, otherwise special keys will not
10355 * work.
10356 */
10357 if (has_mbyte)
10358 {
10359 int len = 0;
10360
10361 /* Count the number of characters to be escaped. */
10362 for (p = eap->arg; *p != NUL; ++p)
10363 {
10364# ifdef FEAT_GUI
10365 if (*p == CSI) /* leadbyte CSI */
10366 len += 2;
10367# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010368 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10370# ifdef FEAT_GUI
10371 || *p == CSI
10372# endif
10373 )
10374 len += 2;
10375 }
10376 if (len > 0)
10377 {
10378 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10379 if (arg != NULL)
10380 {
10381 len = 0;
10382 for (p = eap->arg; *p != NUL; ++p)
10383 {
10384 arg[len++] = *p;
10385# ifdef FEAT_GUI
10386 if (*p == CSI)
10387 {
10388 arg[len++] = KS_EXTRA;
10389 arg[len++] = (int)KE_CSI;
10390 }
10391# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010392 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 {
10394 arg[len++] = *++p;
10395 if (*p == K_SPECIAL)
10396 {
10397 arg[len++] = KS_SPECIAL;
10398 arg[len++] = KE_FILLER;
10399 }
10400# ifdef FEAT_GUI
10401 else if (*p == CSI)
10402 {
10403 arg[len++] = KS_EXTRA;
10404 arg[len++] = (int)KE_CSI;
10405 }
10406# endif
10407 }
10408 arg[len] = NUL;
10409 }
10410 }
10411 }
10412 }
10413#endif
10414
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010415 ++ex_normal_busy;
10416 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 {
10418 /*
10419 * Repeat the :normal command for each line in the range. When no
10420 * range given, execute it just once, without positioning the cursor
10421 * first.
10422 */
10423 do
10424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 if (eap->addr_count != 0)
10426 {
10427 curwin->w_cursor.lnum = eap->line1++;
10428 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010429 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 }
10431
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010432 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433#ifdef FEAT_MBYTE
10434 arg != NULL ? arg :
10435#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010436 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 }
10438 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10439 }
10440
10441 /* Might not return to the main loop when in an event handler. */
10442 update_topline_cursor();
10443
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010444 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010446#ifdef FEAT_MOUSE
10447 setmouse();
10448#endif
10449#ifdef CURSOR_SHAPE
10450 ui_cursor_shape(); /* may show different cursor shape */
10451#endif
10452
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453#ifdef FEAT_MBYTE
10454 vim_free(arg);
10455#endif
10456}
10457
10458/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010459 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 */
10461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010462ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010464 if (eap->forceit)
10465 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010466 /* cursor line can be zero on startup */
10467 if (!curwin->w_cursor.lnum)
10468 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010469 coladvance((colnr_T)MAXCOL);
10470 curwin->w_curswant = MAXCOL;
10471 curwin->w_set_curswant = FALSE;
10472 }
10473
Bram Moolenaara40c5002005-01-09 21:16:21 +000010474 /* Ignore the command when already in Insert mode. Inserting an
10475 * expression register that invokes a function can do this. */
10476 if (State & INSERT)
10477 return;
10478
Bram Moolenaar64969662005-12-14 21:59:55 +000010479 if (eap->cmdidx == CMD_startinsert)
10480 restart_edit = 'a';
10481 else if (eap->cmdidx == CMD_startreplace)
10482 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010484 restart_edit = 'V';
10485
10486 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010488 if (eap->cmdidx == CMD_startinsert)
10489 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 curwin->w_curswant = 0; /* avoid MAXCOL */
10491 }
10492}
10493
10494/*
10495 * ":stopinsert"
10496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010498ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499{
10500 restart_edit = 0;
10501 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010502 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010505/*
10506 * Execute normal mode command "cmd".
10507 * "remap" can be REMAP_NONE or REMAP_YES.
10508 */
10509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010510exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010511{
Bram Moolenaar25281632016-01-21 23:32:32 +010010512 /* Stuff the argument into the typeahead buffer. */
10513 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010514 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010515}
Bram Moolenaar25281632016-01-21 23:32:32 +010010516
Bram Moolenaar25281632016-01-21 23:32:32 +010010517/*
10518 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010519 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010520 */
10521 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010522exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010523{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010524 oparg_T oa;
10525
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010526 clear_oparg(&oa);
10527 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010528 while ((!stuff_empty()
10529 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10530 || (use_vpeekc && vpeekc() != NUL))
10531 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010532 {
10533 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010534#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010535 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010536 && oa.op_type == OP_NOP && oa.regname == NUL
10537 && !VIsual_active)
10538 {
10539 /* If terminal_loop() returns OK we got a key that is handled
10540 * in Normal model. With FAIL we first need to position the
10541 * cursor and the screen needs to be redrawn. */
10542 if (terminal_loop(TRUE) == OK)
10543 normal_cmd(&oa, TRUE);
10544 }
10545 else
10546#endif
10547 /* execute a Normal mode cmd */
10548 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010549 }
10550}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010551
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552#ifdef FEAT_FIND_ID
10553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010554ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555{
10556 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10557 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10558 (linenr_T)1, (linenr_T)MAXLNUM);
10559}
10560
Bram Moolenaar4033c552017-09-16 20:54:51 +020010561#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562/*
10563 * ":psearch"
10564 */
10565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010566ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
10568 g_do_tagpreview = p_pvh;
10569 ex_findpat(eap);
10570 g_do_tagpreview = 0;
10571}
10572#endif
10573
10574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010575ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576{
10577 int whole = TRUE;
10578 long n;
10579 char_u *p;
10580 int action;
10581
10582 switch (cmdnames[eap->cmdidx].cmd_name[2])
10583 {
10584 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10585 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10586 action = ACTION_GOTO;
10587 else
10588 action = ACTION_SHOW;
10589 break;
10590 case 'i': /* ":ilist" and ":dlist" */
10591 action = ACTION_SHOW_ALL;
10592 break;
10593 case 'u': /* ":ijump" and ":djump" */
10594 action = ACTION_GOTO;
10595 break;
10596 default: /* ":isplit" and ":dsplit" */
10597 action = ACTION_SPLIT;
10598 break;
10599 }
10600
10601 n = 1;
10602 if (vim_isdigit(*eap->arg)) /* get count */
10603 {
10604 n = getdigits(&eap->arg);
10605 eap->arg = skipwhite(eap->arg);
10606 }
10607 if (*eap->arg == '/') /* Match regexp, not just whole words */
10608 {
10609 whole = FALSE;
10610 ++eap->arg;
10611 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10612 if (*p)
10613 {
10614 *p++ = NUL;
10615 p = skipwhite(p);
10616
10617 /* Check for trailing illegal characters */
10618 if (!ends_excmd(*p))
10619 eap->errmsg = e_trailing;
10620 else
10621 eap->nextcmd = check_nextcmd(p);
10622 }
10623 }
10624 if (!eap->skip)
10625 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10626 whole, !eap->forceit,
10627 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10628 n, action, eap->line1, eap->line2);
10629}
10630#endif
10631
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632
Bram Moolenaar4033c552017-09-16 20:54:51 +020010633#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634/*
10635 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10636 */
10637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010638ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010640 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10642}
10643
10644/*
10645 * ":pedit"
10646 */
10647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010648ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
10650 win_T *curwin_save = curwin;
10651
10652 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010653 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010654 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 do_exedit(eap, NULL);
10656 keep_help_flag = FALSE;
10657 if (curwin != curwin_save && win_valid(curwin_save))
10658 {
10659 /* Return cursor to where we were */
10660 validate_cursor();
10661 redraw_later(VALID);
10662 win_enter(curwin_save, TRUE);
10663 }
10664 g_do_tagpreview = 0;
10665}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667
10668/*
10669 * ":stag", ":stselect" and ":stjump".
10670 */
10671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010672ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
10674 postponed_split = -1;
10675 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010676 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10678 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010679 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681
10682/*
10683 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10684 */
10685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010686ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10689}
10690
10691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010692ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
10694 int cmd;
10695
10696 switch (name[1])
10697 {
10698 case 'j': cmd = DT_JUMP; /* ":tjump" */
10699 break;
10700 case 's': cmd = DT_SELECT; /* ":tselect" */
10701 break;
10702 case 'p': cmd = DT_PREV; /* ":tprevious" */
10703 break;
10704 case 'N': cmd = DT_PREV; /* ":tNext" */
10705 break;
10706 case 'n': cmd = DT_NEXT; /* ":tnext" */
10707 break;
10708 case 'o': cmd = DT_POP; /* ":pop" */
10709 break;
10710 case 'f': /* ":tfirst" */
10711 case 'r': cmd = DT_FIRST; /* ":trewind" */
10712 break;
10713 case 'l': cmd = DT_LAST; /* ":tlast" */
10714 break;
10715 default: /* ":tag" */
10716#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010717 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010719 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 return;
10721 }
10722#endif
10723 cmd = DT_TAG;
10724 break;
10725 }
10726
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010727 if (name[0] == 'l')
10728 {
10729#ifndef FEAT_QUICKFIX
10730 ex_ni(eap);
10731 return;
10732#else
10733 cmd = DT_LTAG;
10734#endif
10735 }
10736
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10738 eap->forceit, TRUE);
10739}
10740
10741/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010742 * Check "str" for starting with a special cmdline variable.
10743 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10744 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10745 */
10746 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010747find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010748{
10749 int len;
10750 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010751 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010752 "%",
10753#define SPEC_PERC 0
10754 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010755#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010756 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010757#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010758 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010759#define SPEC_CCWORD (SPEC_CWORD + 1)
10760 "<cexpr>", /* expr under cursor */
10761#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010762 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010763#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010764 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010765#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010766 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010767#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010768 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010769#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010770 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010771#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010772 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010773#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010774 "<sflnum>", /* script file line number */
10775#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010776#ifdef FEAT_CLIENTSERVER
10777 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010778# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010779#endif
10780 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010781
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010782 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010783 {
10784 len = (int)STRLEN(spec_str[i]);
10785 if (STRNCMP(src, spec_str[i], len) == 0)
10786 {
10787 *usedlen = len;
10788 return i;
10789 }
10790 }
10791 return -1;
10792}
10793
10794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 * Evaluate cmdline variables.
10796 *
10797 * change '%' to curbuf->b_ffname
10798 * '#' to curwin->w_altfile
10799 * '<cword>' to word under the cursor
10800 * '<cWORD>' to WORD under the cursor
10801 * '<cfile>' to path name under the cursor
10802 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010803 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 * '<afile>' to file name for autocommand
10805 * '<abuf>' to buffer number for autocommand
10806 * '<amatch>' to matching name for autocommand
10807 *
10808 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10809 * "" for error without a message) and NULL is returned.
10810 * Returns an allocated string if a valid match was found.
10811 * Returns NULL if no match was found. "usedlen" then still contains the
10812 * number of characters to skip.
10813 */
10814 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010815eval_vars(
10816 char_u *src, /* pointer into commandline */
10817 char_u *srcstart, /* beginning of valid memory for src */
10818 int *usedlen, /* characters after src that are used */
10819 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010820 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010821 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010822 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823{
10824 int i;
10825 char_u *s;
10826 char_u *result;
10827 char_u *resultbuf = NULL;
10828 int resultlen;
10829 buf_T *buf;
10830 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10831 int spec_idx;
10832#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010833 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 int skip_mod = FALSE;
10835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837
10838 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010839 if (escaped != NULL)
10840 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841
10842 /*
10843 * Check if there is something to do.
10844 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010845 spec_idx = find_cmdline_var(src, usedlen);
10846 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 {
10848 *usedlen = 1;
10849 return NULL;
10850 }
10851
10852 /*
10853 * Skip when preceded with a backslash "\%" and "\#".
10854 * Note: In "\\%" the % is also not recognized!
10855 */
10856 if (src > srcstart && src[-1] == '\\')
10857 {
10858 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010859 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 return NULL;
10861 }
10862
10863 /*
10864 * word or WORD under cursor
10865 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010866 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10867 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010869 resultlen = find_ident_under_cursor(&result,
10870 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10871 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10872 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 if (resultlen == 0)
10874 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010875 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 return NULL;
10877 }
10878 }
10879
10880 /*
10881 * '#': Alternate file name
10882 * '%': Current file name
10883 * File name under the cursor
10884 * File name for autocommand
10885 * and following modifiers
10886 */
10887 else
10888 {
10889 switch (spec_idx)
10890 {
10891 case SPEC_PERC: /* '%': current file */
10892 if (curbuf->b_fname == NULL)
10893 {
10894 result = (char_u *)"";
10895 valid = 0; /* Must have ":p:h" to be valid */
10896 }
10897 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010900#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010901 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010902#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 break;
10905
10906 case SPEC_HASH: /* '#' or "#99": alternate file */
10907 if (src[1] == '#') /* "##": the argument list */
10908 {
10909 result = arg_all();
10910 resultbuf = result;
10911 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010912 if (escaped != NULL)
10913 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914#ifdef FEAT_MODIFY_FNAME
10915 skip_mod = TRUE;
10916#endif
10917 break;
10918 }
10919 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010920 if (*s == '<') /* "#<99" uses v:oldfiles */
10921 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010923 if (s == src + 2 && src[1] == '-')
10924 /* just a minus sign, don't skip over it */
10925 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 *usedlen = (int)(s - src); /* length of what we expand */
10927
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010928 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010930 if (*usedlen < 2)
10931 {
10932 /* Should we give an error message for #<text? */
10933 *usedlen = 1;
10934 return NULL;
10935 }
10936#ifdef FEAT_EVAL
10937 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10938 (long)i);
10939 if (result == NULL)
10940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010941 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +000010942 return NULL;
10943 }
10944#else
10945 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 }
10949 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010950 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010951 if (i == 0 && src[1] == '<' && *usedlen > 1)
10952 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010953 buf = buflist_findnr(i);
10954 if (buf == NULL)
10955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010956 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +000010957 return NULL;
10958 }
10959 if (lnump != NULL)
10960 *lnump = ECMD_LAST;
10961 if (buf->b_fname == NULL)
10962 {
10963 result = (char_u *)"";
10964 valid = 0; /* Must have ":p:h" to be valid */
10965 }
10966 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010967 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010968 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010969#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010970 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010971#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010972 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 break;
10975
10976#ifdef FEAT_SEARCHPATH
10977 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010978 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 if (result == NULL)
10980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010981 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 return NULL;
10983 }
10984 resultbuf = result; /* remember allocated string */
10985 break;
10986#endif
10987
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 case SPEC_AFILE: /* file name for autocommand */
10989 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010990 if (result != NULL && !autocmd_fname_full)
10991 {
10992 /* Still need to turn the fname into a full path. It is
10993 * postponed to avoid a delay when <afile> is not used. */
10994 autocmd_fname_full = TRUE;
10995 result = FullName_save(autocmd_fname, FALSE);
10996 vim_free(autocmd_fname);
10997 autocmd_fname = result;
10998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 if (result == NULL)
11000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011001 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 return NULL;
11003 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000011004 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 break;
11006
11007 case SPEC_ABUF: /* buffer number for autocommand */
11008 if (autocmd_bufnr <= 0)
11009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011010 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 return NULL;
11012 }
11013 sprintf((char *)strbuf, "%d", autocmd_bufnr);
11014 result = strbuf;
11015 break;
11016
11017 case SPEC_AMATCH: /* match name for autocommand */
11018 result = autocmd_match;
11019 if (result == NULL)
11020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011021 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 return NULL;
11023 }
11024 break;
11025
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 case SPEC_SFILE: /* file name for ":so" command */
11027 result = sourcing_name;
11028 if (result == NULL)
11029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011030 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 return NULL;
11032 }
11033 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011034
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011035 case SPEC_SLNUM: /* line in file for ":so" command */
11036 if (sourcing_name == NULL || sourcing_lnum == 0)
11037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011038 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011039 return NULL;
11040 }
11041 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11042 result = strbuf;
11043 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011044
11045#ifdef FEAT_EVAL
11046 case SPEC_SFLNUM: /* line in script file */
11047 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011049 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011050 return NULL;
11051 }
11052 sprintf((char *)strbuf, "%ld",
11053 (long)(current_sctx.sc_lnum + sourcing_lnum));
11054 result = strbuf;
11055 break;
11056#endif
11057
11058#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011060 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11061 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 result = strbuf;
11063 break;
11064#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011065
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011066 default:
11067 result = (char_u *)""; /* avoid gcc warning */
11068 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 }
11070
11071 resultlen = (int)STRLEN(result); /* length of new string */
11072 if (src[*usedlen] == '<') /* remove the file name extension */
11073 {
11074 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 resultlen = (int)(s - result);
11077 }
11078#ifdef FEAT_MODIFY_FNAME
11079 else if (!skip_mod)
11080 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011081 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 &resultlen);
11083 if (result == NULL)
11084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011085 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 return NULL;
11087 }
11088 }
11089#endif
11090 }
11091
11092 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11093 {
11094 if (valid != VALID_HEAD + VALID_PATH)
11095 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011096 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011098 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 result = NULL;
11100 }
11101 else
11102 result = vim_strnsave(result, resultlen);
11103 vim_free(resultbuf);
11104 return result;
11105}
11106
11107/*
11108 * Concatenate all files in the argument list, separated by spaces, and return
11109 * it in one allocated string.
11110 * Spaces and backslashes in the file names are escaped with a backslash.
11111 * Returns NULL when out of memory.
11112 */
11113 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011114arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
11116 int len;
11117 int idx;
11118 char_u *retval = NULL;
11119 char_u *p;
11120
11121 /*
11122 * Do this loop two times:
11123 * first time: compute the total length
11124 * second time: concatenate the names
11125 */
11126 for (;;)
11127 {
11128 len = 0;
11129 for (idx = 0; idx < ARGCOUNT; ++idx)
11130 {
11131 p = alist_name(&ARGLIST[idx]);
11132 if (p != NULL)
11133 {
11134 if (len > 0)
11135 {
11136 /* insert a space in between names */
11137 if (retval != NULL)
11138 retval[len] = ' ';
11139 ++len;
11140 }
11141 for ( ; *p != NUL; ++p)
11142 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011143 if (*p == ' '
11144#ifndef BACKSLASH_IN_FILENAME
11145 || *p == '\\'
11146#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011147 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 {
11149 /* insert a backslash */
11150 if (retval != NULL)
11151 retval[len] = '\\';
11152 ++len;
11153 }
11154 if (retval != NULL)
11155 retval[len] = *p;
11156 ++len;
11157 }
11158 }
11159 }
11160
11161 /* second time: break here */
11162 if (retval != NULL)
11163 {
11164 retval[len] = NUL;
11165 break;
11166 }
11167
11168 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011169 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 if (retval == NULL)
11171 break;
11172 }
11173
11174 return retval;
11175}
11176
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177/*
11178 * Expand the <sfile> string in "arg".
11179 *
11180 * Returns an allocated string, or NULL for any error.
11181 */
11182 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011183expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011185 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 int len;
11187 char_u *result;
11188 char_u *newres;
11189 char_u *repl;
11190 int srclen;
11191 char_u *p;
11192
11193 result = vim_strsave(arg);
11194 if (result == NULL)
11195 return NULL;
11196
11197 for (p = result; *p; )
11198 {
11199 if (STRNCMP(p, "<sfile>", 7) != 0)
11200 ++p;
11201 else
11202 {
11203 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011204 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 if (errormsg != NULL)
11206 {
11207 if (*errormsg)
11208 emsg(errormsg);
11209 vim_free(result);
11210 return NULL;
11211 }
11212 if (repl == NULL) /* no match (cannot happen) */
11213 {
11214 p += srclen;
11215 continue;
11216 }
11217 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11218 newres = alloc(len);
11219 if (newres == NULL)
11220 {
11221 vim_free(repl);
11222 vim_free(result);
11223 return NULL;
11224 }
11225 mch_memmove(newres, result, (size_t)(p - result));
11226 STRCPY(newres + (p - result), repl);
11227 len = (int)STRLEN(newres);
11228 STRCAT(newres, p + srclen);
11229 vim_free(repl);
11230 vim_free(result);
11231 result = newres;
11232 p = newres + len; /* continue after the match */
11233 }
11234 }
11235
11236 return result;
11237}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238
11239#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011240static int ses_winsizes(FILE *fd, int restore_size,
11241 win_T *tab_firstwin);
11242static int ses_win_rec(FILE *fd, frame_T *fr);
11243static frame_T *ses_skipframe(frame_T *fr);
11244static int ses_do_frame(frame_T *fr);
11245static int ses_do_win(win_T *wp);
11246static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11247static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011248static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249
11250/*
11251 * Write openfile commands for the current buffers to an .exrc file.
11252 * Return FAIL on error, OK otherwise.
11253 */
11254 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011255makeopens(
11256 FILE *fd,
11257 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258{
11259 buf_T *buf;
11260 int only_save_windows = TRUE;
11261 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 int restore_size = TRUE;
11263 win_T *wp;
11264 char_u *sname;
11265 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011266 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011267 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011268 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011269 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011270 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011271 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
11273 if (ssop_flags & SSOP_BUFFERS)
11274 only_save_windows = FALSE; /* Save ALL buffers */
11275
11276 /*
11277 * Begin by setting the this_session variable, and then other
11278 * sessionable variables.
11279 */
11280#ifdef FEAT_EVAL
11281 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11282 return FAIL;
11283 if (ssop_flags & SSOP_GLOBALS)
11284 if (store_session_globals(fd) == FAIL)
11285 return FAIL;
11286#endif
11287
11288 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011289 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 */
11291 if (put_line(fd, "silent only") == FAIL)
11292 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011293 if ((ssop_flags & SSOP_TABPAGES)
11294 && put_line(fd, "silent tabonly") == FAIL)
11295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296
11297 /*
11298 * Now a :cd command to the session directory or the current directory
11299 */
11300 if (ssop_flags & SSOP_SESDIR)
11301 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011302 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11303 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 return FAIL;
11305 }
11306 else if (ssop_flags & SSOP_CURDIR)
11307 {
11308 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11309 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011310 || fputs("cd ", fd) < 0
11311 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11312 || put_eol(fd) == FAIL)
11313 {
11314 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 vim_free(sname);
11318 }
11319
11320 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011321 * If there is an empty, unnamed buffer we will wipe it out later.
11322 * Remember the buffer number.
11323 */
11324 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11325 return FAIL;
11326 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11327 return FAIL;
11328 if (put_line(fd, "endif") == FAIL)
11329 return FAIL;
11330
11331 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 * Now save the current files, current buffer first.
11333 */
11334 if (put_line(fd, "set shortmess=aoO") == FAIL)
11335 return FAIL;
11336
11337 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011338 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 {
11340 if (!(only_save_windows && buf->b_nwindows == 0)
11341 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011342#ifdef FEAT_TERMINAL
11343 /* skip terminal buffers: finished ones are not useful, others
11344 * will be resurrected and result in a new buffer */
11345 && !bt_terminal(buf)
11346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 && buf->b_fname != NULL
11348 && buf->b_p_bl)
11349 {
11350 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11351 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011352 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 return FAIL;
11354 }
11355 }
11356
11357 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011358 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11360 return FAIL;
11361
11362 if (ssop_flags & SSOP_RESIZE)
11363 {
11364 /* Note: after the restore we still check it worked!*/
11365 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11366 || put_eol(fd) == FAIL)
11367 return FAIL;
11368 }
11369
11370#ifdef FEAT_GUI
11371 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11372 {
11373 int x, y;
11374
11375 if (gui_mch_get_winpos(&x, &y) == OK)
11376 {
11377 /* Note: after the restore we still check it worked!*/
11378 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11379 return FAIL;
11380 }
11381 }
11382#endif
11383
11384 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011385 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11386 * will be displayed when creating the next tab. That resizes the windows
11387 * in the first tab, which may cause problems. Set 'showtabline' to 2
11388 * temporarily to avoid that.
11389 */
11390 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11391 {
11392 if (put_line(fd, "set stal=2") == FAIL)
11393 return FAIL;
11394 restore_stal = TRUE;
11395 }
11396
11397 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011398 * May repeat putting Windows for each tab, when "tabpages" is in
11399 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011400 * Don't use goto_tabpage(), it may change directory and trigger
11401 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011403 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011404 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011405 if ((ssop_flags & SSOP_TABPAGES))
11406 {
11407 int num_tabs;
11408
11409 /*
11410 * Similar to ses_win_rec() below, populate the tab pages first so
11411 * later local options won't be copied to the new tabs.
11412 */
11413 for (tabnr = 1; ; ++tabnr)
11414 {
11415 tabpage_T *tp = find_tabpage(tabnr);
11416
11417 if (tp == NULL) /* done all tab pages */
11418 break;
11419
11420 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11421 return FAIL;
11422 }
11423
11424 num_tabs = tabnr - 1;
11425 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11426 || put_eol(fd) == FAIL))
11427 return FAIL;
11428 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011429 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011431 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011432 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011433
Bram Moolenaar18144c82006-04-12 21:52:12 +000011434 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011436 tabpage_T *tp = find_tabpage(tabnr);
11437
11438 if (tp == NULL)
11439 break; /* done all tab pages */
11440 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011441 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011442 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011443 tab_topframe = topframe;
11444 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011445 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011446 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011447 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011448 tab_topframe = tp->tp_topframe;
11449 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011450 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011451 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453
Bram Moolenaar18144c82006-04-12 21:52:12 +000011454 /*
11455 * Before creating the window layout, try loading one file. If this
11456 * is aborted we don't end up with a number of useless windows.
11457 * This may have side effects! (e.g., compressed or network file).
11458 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011459 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011460 {
11461 if (ses_do_win(wp)
11462 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011463 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011464#ifdef FEAT_QUICKFIX
11465 && !bt_nofile(wp->w_buffer)
11466#endif
11467 )
11468 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011469 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11470 return FAIL;
11471 need_tabnext = FALSE;
11472
11473 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011474 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11475 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011476 return FAIL;
11477 if (!wp->w_arg_idx_invalid)
11478 edited_win = wp;
11479 break;
11480 }
11481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011483 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011484 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011485 return FAIL;
11486
Bram Moolenaar18144c82006-04-12 21:52:12 +000011487 /*
11488 * Save current window layout.
11489 */
11490 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011492 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011494 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11495 return FAIL;
11496 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11497 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498
Bram Moolenaar18144c82006-04-12 21:52:12 +000011499 /*
11500 * Check if window sizes can be restored (no windows omitted).
11501 * Remember the window number of the current window after restoring.
11502 */
11503 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011504 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011505 {
11506 if (ses_do_win(wp))
11507 ++nr;
11508 else
11509 restore_size = FALSE;
11510 if (curwin == wp)
11511 cnr = nr;
11512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513
Bram Moolenaar18144c82006-04-12 21:52:12 +000011514 /* Go to the first window. */
11515 if (put_line(fd, "wincmd t") == FAIL)
11516 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011517
Bram Moolenaar18144c82006-04-12 21:52:12 +000011518 /*
11519 * If more than one window, see if sizes can be restored.
11520 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11521 * resized when moving between windows.
11522 * Do this before restoring the view, so that the topline and the
11523 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011524 * winminheight and winminwidth need to be set to avoid an error if the
11525 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011526 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011527 if (put_line(fd, "set winminheight=0") == FAIL
11528 || put_line(fd, "set winheight=1") == FAIL
11529 || put_line(fd, "set winminwidth=0") == FAIL
11530 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011531 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011532 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534
Bram Moolenaar18144c82006-04-12 21:52:12 +000011535 /*
11536 * Restore the view of the window (options, file, cursor, etc.).
11537 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011538 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011539 {
11540 if (!ses_do_win(wp))
11541 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011542 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11543 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011544 return FAIL;
11545 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11546 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011547 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011548 }
11549
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011550 /* The argument index in the first tab page is zero, need to set it in
11551 * each window. For further tab pages it's the window where we do
11552 * "tabedit". */
11553 cur_arg_idx = next_arg_idx;
11554
Bram Moolenaar18144c82006-04-12 21:52:12 +000011555 /*
11556 * Restore cursor to the current window if it's not the first one.
11557 */
11558 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11559 || put_eol(fd) == FAIL))
11560 return FAIL;
11561
11562 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011563 * Restore window sizes again after jumping around in windows, because
11564 * the current window has a minimum size while others may not.
11565 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011566 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011567 return FAIL;
11568
Bram Moolenaar18144c82006-04-12 21:52:12 +000011569 /* Don't continue in another tab page when doing only the current one
11570 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011571 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011572 break;
11573 }
11574
11575 if (ssop_flags & SSOP_TABPAGES)
11576 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011577 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11578 || put_eol(fd) == FAIL)
11579 return FAIL;
11580 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011581 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11582 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011583
Bram Moolenaar9c102382006-05-03 21:26:49 +000011584 /*
11585 * Wipe out an empty unnamed buffer we started in.
11586 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011587 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011588 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011589 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011590 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011591 return FAIL;
11592 if (put_line(fd, "endif") == FAIL)
11593 return FAIL;
11594 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11595 return FAIL;
11596
11597 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11598 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11599 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11600 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011601 /* Re-apply 'winminheight' and 'winminwidth'. */
11602 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11603 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605
11606 /*
11607 * Lastly, execute the x.vim file if it exists.
11608 */
11609 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11610 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011611 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 || put_line(fd, "endif") == FAIL)
11613 return FAIL;
11614
11615 return OK;
11616}
11617
11618 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011619ses_winsizes(
11620 FILE *fd,
11621 int restore_size,
11622 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623{
11624 int n = 0;
11625 win_T *wp;
11626
11627 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11628 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011629 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 {
11631 if (!ses_do_win(wp))
11632 continue;
11633 ++n;
11634
11635 /* restore height when not full height */
11636 if (wp->w_height + wp->w_status_height < topframe->fr_height
11637 && (fprintf(fd,
11638 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11639 n, (long)wp->w_height, Rows / 2, Rows) < 0
11640 || put_eol(fd) == FAIL))
11641 return FAIL;
11642
11643 /* restore width when not full width */
11644 if (wp->w_width < Columns && (fprintf(fd,
11645 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11646 n, (long)wp->w_width, Columns / 2, Columns) < 0
11647 || put_eol(fd) == FAIL))
11648 return FAIL;
11649 }
11650 }
11651 else
11652 {
11653 /* Just equalise window sizes */
11654 if (put_line(fd, "wincmd =") == FAIL)
11655 return FAIL;
11656 }
11657 return OK;
11658}
11659
11660/*
11661 * Write commands to "fd" to recursively create windows for frame "fr",
11662 * horizontally and vertically split.
11663 * After the commands the last window in the frame is the current window.
11664 * Returns FAIL when writing the commands to "fd" fails.
11665 */
11666 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011667ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668{
11669 frame_T *frc;
11670 int count = 0;
11671
11672 if (fr->fr_layout != FR_LEAF)
11673 {
11674 /* Find first frame that's not skipped and then create a window for
11675 * each following one (first frame is already there). */
11676 frc = ses_skipframe(fr->fr_child);
11677 if (frc != NULL)
11678 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11679 {
11680 /* Make window as big as possible so that we have lots of room
11681 * to split. */
11682 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11683 || put_line(fd, fr->fr_layout == FR_COL
11684 ? "split" : "vsplit") == FAIL)
11685 return FAIL;
11686 ++count;
11687 }
11688
11689 /* Go back to the first window. */
11690 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11691 ? "%dwincmd k" : "%dwincmd h", count) < 0
11692 || put_eol(fd) == FAIL))
11693 return FAIL;
11694
11695 /* Recursively create frames/windows in each window of this column or
11696 * row. */
11697 frc = ses_skipframe(fr->fr_child);
11698 while (frc != NULL)
11699 {
11700 ses_win_rec(fd, frc);
11701 frc = ses_skipframe(frc->fr_next);
11702 /* Go to next window. */
11703 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11704 return FAIL;
11705 }
11706 }
11707 return OK;
11708}
11709
11710/*
11711 * Skip frames that don't contain windows we want to save in the Session.
11712 * Returns NULL when there none.
11713 */
11714 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011715ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 frame_T *frc;
11718
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011719 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 if (ses_do_frame(frc))
11721 break;
11722 return frc;
11723}
11724
11725/*
11726 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11727 * the Session.
11728 */
11729 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011730ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731{
11732 frame_T *frc;
11733
11734 if (fr->fr_layout == FR_LEAF)
11735 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011736 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 if (ses_do_frame(frc))
11738 return TRUE;
11739 return FALSE;
11740}
11741
11742/*
11743 * Return non-zero if window "wp" is to be stored in the Session.
11744 */
11745 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011746ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011748#ifdef FEAT_TERMINAL
11749 if (bt_terminal(wp->w_buffer))
11750 return !term_is_finished(wp->w_buffer)
11751 && (ssop_flags & SSOP_TERMINAL)
11752 && term_should_restore(wp->w_buffer);
11753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 if (wp->w_buffer->b_fname == NULL
11755#ifdef FEAT_QUICKFIX
11756 /* When 'buftype' is "nofile" can't restore the window contents. */
11757 || bt_nofile(wp->w_buffer)
11758#endif
11759 )
11760 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011761 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 return (ssop_flags & SSOP_HELP);
11763 return TRUE;
11764}
11765
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011766 static int
11767put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11768{
11769 int r;
11770
11771 if (wp->w_curswant == MAXCOL)
11772 r = fprintf(fd, "%snormal! $", spaces);
11773 else
11774 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11775 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11776}
11777
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778/*
11779 * Write commands to "fd" to restore the view of a window.
11780 * Caller must make sure 'scrolloff' is zero.
11781 */
11782 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011783put_view(
11784 FILE *fd,
11785 win_T *wp,
11786 int add_edit, /* add ":edit" command to view */
11787 unsigned *flagp, /* vop_flags or ssop_flags */
11788 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011789 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790{
11791 win_T *save_curwin;
11792 int f;
11793 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011794 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795
11796 /* Always restore cursor position for ":mksession". For ":mkview" only
11797 * when 'viewoptions' contains "cursor". */
11798 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11799
11800 /*
11801 * Local argument list.
11802 */
11803 if (wp->w_alist == &global_alist)
11804 {
11805 if (put_line(fd, "argglobal") == FAIL)
11806 return FAIL;
11807 }
11808 else
11809 {
11810 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11811 flagp == &vop_flags
11812 || !(*flagp & SSOP_CURDIR)
11813 || wp->w_localdir != NULL, flagp) == FAIL)
11814 return FAIL;
11815 }
11816
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011817 /* Only when part of a session: restore the argument index. Some
11818 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011819 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011820 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011822 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 || put_eol(fd) == FAIL)
11824 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011825 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 }
11827
11828 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011829 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011831# ifdef FEAT_TERMINAL
11832 if (bt_terminal(wp->w_buffer))
11833 {
11834 if (term_write_session(fd, wp) == FAIL)
11835 return FAIL;
11836 }
11837 else
11838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 /*
11840 * Load the file.
11841 */
11842 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011843# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 )
11847 {
11848 /*
11849 * Editing a file in this buffer: use ":edit file".
11850 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011851 *
11852 * Note, if a buffer for that file already exists, use :badd to
11853 * edit that buffer, to not lose folding information (:edit resets
11854 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011856 if (fputs("if bufexists('", fd) < 0
11857 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11858 || fputs("') | buffer ", fd) < 0
11859 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11860 || fputs(" | else | edit ", fd) < 0
11861 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11862 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011863 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 return FAIL;
11865 }
11866 else
11867 {
11868 /* No file in this buffer, just make it empty. */
11869 if (put_line(fd, "enew") == FAIL)
11870 return FAIL;
11871#ifdef FEAT_QUICKFIX
11872 if (wp->w_buffer->b_ffname != NULL)
11873 {
11874 /* The buffer does have a name, but it's not a file name. */
11875 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011876 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 return FAIL;
11878 }
11879#endif
11880 do_cursor = FALSE;
11881 }
11882 }
11883
11884 /*
11885 * Local mappings and abbreviations.
11886 */
11887 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11888 && makemap(fd, wp->w_buffer) == FAIL)
11889 return FAIL;
11890
11891 /*
11892 * Local options. Need to go to the window temporarily.
11893 * Store only local values when using ":mkview" and when ":mksession" is
11894 * used and 'sessionoptions' doesn't include "options".
11895 * Some folding options are always stored when "folds" is included,
11896 * otherwise the folds would not be restored correctly.
11897 */
11898 save_curwin = curwin;
11899 curwin = wp;
11900 curbuf = curwin->w_buffer;
11901 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11902 f = makeset(fd, OPT_LOCAL,
11903 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11904#ifdef FEAT_FOLDING
11905 else if (*flagp & SSOP_FOLDS)
11906 f = makefoldset(fd);
11907#endif
11908 else
11909 f = OK;
11910 curwin = save_curwin;
11911 curbuf = curwin->w_buffer;
11912 if (f == FAIL)
11913 return FAIL;
11914
11915#ifdef FEAT_FOLDING
11916 /*
11917 * Save Folds when 'buftype' is empty and for help files.
11918 */
11919 if ((*flagp & SSOP_FOLDS)
11920 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011921 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 {
11923 if (put_folds(fd, wp) == FAIL)
11924 return FAIL;
11925 }
11926#endif
11927
11928 /*
11929 * Set the cursor after creating folds, since that moves the cursor.
11930 */
11931 if (do_cursor)
11932 {
11933
11934 /* Restore the cursor line in the file and relatively in the
11935 * window. Don't use "G", it changes the jumplist. */
11936 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11937 (long)wp->w_cursor.lnum,
11938 (long)(wp->w_cursor.lnum - wp->w_topline),
11939 (long)wp->w_height / 2, (long)wp->w_height) < 0
11940 || put_eol(fd) == FAIL
11941 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11942 || put_line(fd, "exe s:l") == FAIL
11943 || put_line(fd, "normal! zt") == FAIL
11944 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11945 || put_eol(fd) == FAIL)
11946 return FAIL;
11947 /* Restore the cursor column and left offset when not wrapping. */
11948 if (wp->w_cursor.col == 0)
11949 {
11950 if (put_line(fd, "normal! 0") == FAIL)
11951 return FAIL;
11952 }
11953 else
11954 {
11955 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11956 {
11957 if (fprintf(fd,
11958 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011959 (long)wp->w_virtcol + 1,
11960 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 (long)wp->w_width / 2, (long)wp->w_width) < 0
11962 || put_eol(fd) == FAIL
11963 || put_line(fd, "if s:c > 0") == FAIL
11964 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011965 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11966 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 || put_eol(fd) == FAIL
11968 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011969 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 || put_line(fd, "endif") == FAIL)
11971 return FAIL;
11972 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011973 else if (put_view_curpos(fd, wp, "") == FAIL)
11974 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 }
11976 }
11977
11978 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011979 * Local directory, if the current flag is not view options or the "curdir"
11980 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011982 if (wp->w_localdir != NULL
11983 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 {
11985 if (fputs("lcd ", fd) < 0
11986 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11987 || put_eol(fd) == FAIL)
11988 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011989 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 }
11991
11992 return OK;
11993}
11994
11995/*
11996 * Write an argument list to the session file.
11997 * Returns FAIL if writing fails.
11998 */
11999 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012000ses_arglist(
12001 FILE *fd,
12002 char *cmd,
12003 garray_T *gap,
12004 int fullname, /* TRUE: use full path name */
12005 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006{
12007 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012008 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 char_u *s;
12010
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012011 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
12012 return FAIL;
12013 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 return FAIL;
12015 for (i = 0; i < gap->ga_len; ++i)
12016 {
12017 /* NULL file names are skipped (only happens when out of memory). */
12018 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
12019 if (s != NULL)
12020 {
12021 if (fullname)
12022 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012023 buf = alloc(MAXPATHL);
12024 if (buf != NULL)
12025 {
12026 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
12027 s = buf;
12028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010012030 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012031 || ses_put_fname(fd, s, flagp) == FAIL
12032 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020012033 {
12034 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012036 }
12037 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 }
12039 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012040 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041}
12042
12043/*
12044 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012045 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 * Returns FAIL if writing fails.
12047 */
12048 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012049ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050{
12051 char_u *name;
12052
12053 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012054 * the session file will be sourced.
12055 * Don't do this for ":mkview", we don't know the current directory.
12056 * Don't do this after ":lcd", we don't keep track of what the current
12057 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 if (buf->b_sfname != NULL
12059 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012060 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012061#ifdef FEAT_AUTOCHDIR
12062 && !p_acd
12063#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012064 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 name = buf->b_sfname;
12066 else
12067 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012068 if (ses_put_fname(fd, name, flagp) == FAIL
12069 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 return FAIL;
12071 return OK;
12072}
12073
12074/*
12075 * Write a file name to the session file.
12076 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12077 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012078 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 */
12080 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012081ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082{
12083 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012084 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086
12087 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012088 if (sname == NULL)
12089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012091 if (*flagp & SSOP_SLASH)
12092 {
12093 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012094 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012095 if (*p == '\\')
12096 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012098
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012099 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012100 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012102 if (p == NULL)
12103 return FAIL;
12104
12105 /* write the result */
12106 if (fputs((char *)p, fd) < 0)
12107 retval = FAIL;
12108
12109 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 return retval;
12111}
12112
12113/*
12114 * ":loadview [nr]"
12115 */
12116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012117ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118{
12119 char_u *fname;
12120
12121 fname = get_view_file(*eap->arg);
12122 if (fname != NULL)
12123 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012124 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 vim_free(fname);
12126 }
12127}
12128
12129/*
12130 * Get the name of the view file for the current buffer.
12131 */
12132 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012133get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134{
12135 int len = 0;
12136 char_u *p, *s;
12137 char_u *retval;
12138 char_u *sname;
12139
12140 if (curbuf->b_ffname == NULL)
12141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012142 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 return NULL;
12144 }
12145 sname = home_replace_save(NULL, curbuf->b_ffname);
12146 if (sname == NULL)
12147 return NULL;
12148
12149 /*
12150 * We want a file name without separators, because we're not going to make
12151 * a directory.
12152 * "normal" path separator -> "=+"
12153 * "=" -> "=="
12154 * ":" path separator -> "=-"
12155 */
12156 for (p = sname; *p; ++p)
12157 if (*p == '=' || vim_ispathsep(*p))
12158 ++len;
12159 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12160 if (retval != NULL)
12161 {
12162 STRCPY(retval, p_vdir);
12163 add_pathsep(retval);
12164 s = retval + STRLEN(retval);
12165 for (p = sname; *p; ++p)
12166 {
12167 if (*p == '=')
12168 {
12169 *s++ = '=';
12170 *s++ = '=';
12171 }
12172 else if (vim_ispathsep(*p))
12173 {
12174 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012175#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 if (*p == ':')
12177 *s++ = '-';
12178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012180 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 }
12182 else
12183 *s++ = *p;
12184 }
12185 *s++ = '=';
12186 *s++ = c;
12187 STRCPY(s, ".vim");
12188 }
12189
12190 vim_free(sname);
12191 return retval;
12192}
12193
12194#endif /* FEAT_SESSION */
12195
12196/*
12197 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12198 * Return FAIL for a write error.
12199 */
12200 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012201put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202{
12203 if (
12204#ifdef USE_CRNL
12205 (
12206# ifdef MKSESSION_NL
12207 !mksession_nl &&
12208# endif
12209 (putc('\r', fd) < 0)) ||
12210#endif
12211 (putc('\n', fd) < 0))
12212 return FAIL;
12213 return OK;
12214}
12215
12216/*
12217 * Write a line to "fd".
12218 * Return FAIL for a write error.
12219 */
12220 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012221put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
12223 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12224 return FAIL;
12225 return OK;
12226}
12227
12228#ifdef FEAT_VIMINFO
12229/*
12230 * ":rviminfo" and ":wviminfo".
12231 */
12232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012233ex_viminfo(
12234 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235{
12236 char_u *save_viminfo;
12237
12238 save_viminfo = p_viminfo;
12239 if (*p_viminfo == NUL)
12240 p_viminfo = (char_u *)"'100";
12241 if (eap->cmdidx == CMD_rviminfo)
12242 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012243 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12244 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012245 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 }
12247 else
12248 write_viminfo(eap->arg, eap->forceit);
12249 p_viminfo = save_viminfo;
12250}
12251#endif
12252
12253#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012254/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012255 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012256 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012259dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 if (fname == NULL)
12262 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012263 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264}
12265#endif
12266
12267/*
12268 * ":behave {mswin,xterm}"
12269 */
12270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012271ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272{
12273 if (STRCMP(eap->arg, "mswin") == 0)
12274 {
12275 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12276 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12277 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12278 set_option_value((char_u *)"keymodel", 0L,
12279 (char_u *)"startsel,stopsel", 0);
12280 }
12281 else if (STRCMP(eap->arg, "xterm") == 0)
12282 {
12283 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12284 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12285 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12286 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12287 }
12288 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012289 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290}
12291
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012292#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12293/*
12294 * Function given to ExpandGeneric() to obtain the possible arguments of the
12295 * ":behave {mswin,xterm}" command.
12296 */
12297 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012298get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012299{
12300 if (idx == 0)
12301 return (char_u *)"mswin";
12302 if (idx == 1)
12303 return (char_u *)"xterm";
12304 return NULL;
12305}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012306
12307/*
12308 * Function given to ExpandGeneric() to obtain the possible arguments of the
12309 * ":messages {clear}" command.
12310 */
12311 char_u *
12312get_messages_arg(expand_T *xp UNUSED, int idx)
12313{
12314 if (idx == 0)
12315 return (char_u *)"clear";
12316 return NULL;
12317}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012318#endif
12319
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012320 char_u *
12321get_mapclear_arg(expand_T *xp UNUSED, int idx)
12322{
12323 if (idx == 0)
12324 return (char_u *)"<buffer>";
12325 return NULL;
12326}
12327
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328static int filetype_detect = FALSE;
12329static int filetype_plugin = FALSE;
12330static int filetype_indent = FALSE;
12331
12332/*
12333 * ":filetype [plugin] [indent] {on,off,detect}"
12334 * on: Load the filetype.vim file to install autocommands for file types.
12335 * off: Load the ftoff.vim file to remove all autocommands for file types.
12336 * plugin on: load filetype.vim and ftplugin.vim
12337 * plugin off: load ftplugof.vim
12338 * indent on: load filetype.vim and indent.vim
12339 * indent off: load indoff.vim
12340 */
12341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012342ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343{
12344 char_u *arg = eap->arg;
12345 int plugin = FALSE;
12346 int indent = FALSE;
12347
12348 if (*eap->arg == NUL)
12349 {
12350 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012351 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 filetype_detect ? "ON" : "OFF",
12353 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12354 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12355 return;
12356 }
12357
12358 /* Accept "plugin" and "indent" in any order. */
12359 for (;;)
12360 {
12361 if (STRNCMP(arg, "plugin", 6) == 0)
12362 {
12363 plugin = TRUE;
12364 arg = skipwhite(arg + 6);
12365 continue;
12366 }
12367 if (STRNCMP(arg, "indent", 6) == 0)
12368 {
12369 indent = TRUE;
12370 arg = skipwhite(arg + 6);
12371 continue;
12372 }
12373 break;
12374 }
12375 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12376 {
12377 if (*arg == 'o' || !filetype_detect)
12378 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012379 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 filetype_detect = TRUE;
12381 if (plugin)
12382 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012383 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 filetype_plugin = TRUE;
12385 }
12386 if (indent)
12387 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012388 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 filetype_indent = TRUE;
12390 }
12391 }
12392 if (*arg == 'd')
12393 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012394 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012395 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 }
12397 }
12398 else if (STRCMP(arg, "off") == 0)
12399 {
12400 if (plugin || indent)
12401 {
12402 if (plugin)
12403 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012404 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 filetype_plugin = FALSE;
12406 }
12407 if (indent)
12408 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012409 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 filetype_indent = FALSE;
12411 }
12412 }
12413 else
12414 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012415 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 filetype_detect = FALSE;
12417 }
12418 }
12419 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012420 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421}
12422
12423/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012424 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 */
12426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012427ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428{
12429 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012430 {
12431 char_u *arg = eap->arg;
12432
12433 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12434 arg += 9;
12435
12436 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12437 if (arg != eap->arg)
12438 did_filetype = FALSE;
12439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012443ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444{
12445#ifdef FEAT_DIGRAPHS
12446 if (*eap->arg != NUL)
12447 putdigraph(eap->arg);
12448 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012449 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012451 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452#endif
12453}
12454
12455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012456ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457{
12458 int flags = 0;
12459
12460 if (eap->cmdidx == CMD_setlocal)
12461 flags = OPT_LOCAL;
12462 else if (eap->cmdidx == CMD_setglobal)
12463 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012464#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 if (cmdmod.browse && flags == 0)
12466 ex_options(eap);
12467 else
12468#endif
12469 (void)do_set(eap->arg, flags);
12470}
12471
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012472#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12473 void
12474set_no_hlsearch(int flag)
12475{
12476 no_hlsearch = flag;
12477# ifdef FEAT_EVAL
12478 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12479# endif
12480}
12481
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482/*
12483 * ":nohlsearch"
12484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012486ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012488 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012489 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490}
12491
12492/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012493 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494 * Sets nextcmd to the start of the next command, if any. Also called when
12495 * skipping commands to find the next command.
12496 */
12497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012498ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499{
12500 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012501 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 char_u *end;
12503 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012504 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012505
12506 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012507 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012508 else
12509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012510 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012511 return;
12512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513
12514 /* First clear any old pattern. */
12515 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012516 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517
12518 if (ends_excmd(*eap->arg))
12519 end = eap->arg;
12520 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012521 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 end = eap->arg + 4;
12523 else
12524 {
12525 p = skiptowhite(eap->arg);
12526 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012527 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 p = skipwhite(p);
12529 if (*p == NUL)
12530 {
12531 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012532 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012533 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534 return;
12535 }
12536 end = skip_regexp(p + 1, *p, TRUE, NULL);
12537 if (!eap->skip)
12538 {
12539 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12540 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012541 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 eap->errmsg = e_trailing;
12543 return;
12544 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012545 if (*end != *p)
12546 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012547 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012548 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012549 return;
12550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551
12552 c = *end;
12553 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012554 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012555 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012556 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557 }
12558 }
12559 eap->nextcmd = find_nextcmd(end);
12560}
12561#endif
12562
12563#ifdef FEAT_CRYPT
12564/*
12565 * ":X": Get crypt key
12566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012568ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012570 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012571 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572}
12573#endif
12574
12575#ifdef FEAT_FOLDING
12576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012577ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578{
12579 if (foldManualAllowed(TRUE))
12580 foldCreate(eap->line1, eap->line2);
12581}
12582
12583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012584ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585{
12586 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12587 eap->forceit, FALSE);
12588}
12589
12590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012591ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592{
12593 linenr_T lnum;
12594
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012595#ifdef FEAT_CLIPBOARD
12596 start_global_changes();
12597#endif
12598
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 /* First set the marks for all lines closed/open. */
12600 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12601 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12602 ml_setmarked(lnum);
12603
12604 /* Execute the command on the marked lines. */
12605 global_exe(eap->arg);
12606 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012607#ifdef FEAT_CLIPBOARD
12608 end_global_changes();
12609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610}
12611#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012612
Bram Moolenaar39665952018-08-15 20:59:48 +020012613#ifdef FEAT_QUICKFIX
12614/*
12615 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12616 * instead of a quickfix command.
12617 */
12618 int
12619is_loclist_cmd(int cmdidx)
12620{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012621 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012622 return FALSE;
12623 return cmdnames[cmdidx].cmd_name[0] == 'l';
12624}
12625#endif
12626
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012627# if defined(FEAT_TIMERS) || defined(PROTO)
12628 int
12629get_pressedreturn(void)
12630{
12631 return ex_pressedreturn;
12632}
12633
12634 void
12635set_pressedreturn(int val)
12636{
12637 ex_pressedreturn = val;
12638}
12639#endif