blob: aad818294ed1611701f4a766536ccaae6ebd6417 [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 Moolenaarf28dbce2016-01-29 22:03:47 +0100129static char_u *invalid_range(exarg_T *eap);
130static 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);
299static void close_redir(void);
300static void ex_mkrc(exarg_T *eap);
301static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100303static char_u *uc_fun_cmd(void);
304static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306static void ex_startinsert(exarg_T *eap);
307static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_checkpath(exarg_T *eap);
310static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#else
312# define ex_findpat ex_ni
313# define ex_checkpath ex_ni
314#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200315#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#else
318# define ex_psearch ex_ni
319#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_tag(exarg_T *eap);
321static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#ifndef FEAT_EVAL
323# define ex_scriptnames ex_ni
324# define ex_finish ex_ni
325# define ex_echo ex_ni
326# define ex_echohl ex_ni
327# define ex_execute ex_ni
328# define ex_call ex_ni
329# define ex_if ex_ni
330# define ex_endif ex_ni
331# define ex_else ex_ni
332# define ex_while ex_ni
333# define ex_continue ex_ni
334# define ex_break ex_ni
335# define ex_endwhile ex_ni
336# define ex_throw ex_ni
337# define ex_try ex_ni
338# define ex_catch ex_ni
339# define ex_finally ex_ni
340# define ex_endtry ex_ni
341# define ex_endfunction ex_ni
342# define ex_let ex_ni
343# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000344# define ex_lockvar ex_ni
345# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346# define ex_function ex_ni
347# define ex_delfunction ex_ni
348# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000349# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100351static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static int makeopens(FILE *fd, char_u *dirnow);
354static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
355static void ex_loadview(exarg_T *eap);
356static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000357static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#else
359# define ex_loadview ex_ni
360#endif
361#ifndef FEAT_EVAL
362# define ex_compiler ex_ni
363#endif
364#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100365static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366#else
367# define ex_viminfo ex_ni
368#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100369static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100370static void ex_filetype(exarg_T *eap);
371static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000373# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_diffpatch ex_ni
375# define ex_diffgetput ex_ni
376# define ex_diffsplit ex_ni
377# define ex_diffthis ex_ni
378# define ex_diffupdate ex_ni
379#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100380static void ex_digraphs(exarg_T *eap);
381static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100382#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383# define ex_options ex_ni
384#endif
385#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100386static void ex_nohlsearch(exarg_T *eap);
387static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#else
389# define ex_nohlsearch ex_ni
390# define ex_match ex_ni
391#endif
392#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100393static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#else
395# define ex_X ex_ni
396#endif
397#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100398static void ex_fold(exarg_T *eap);
399static void ex_foldopen(exarg_T *eap);
400static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#else
402# define ex_fold ex_ni
403# define ex_foldopen ex_ni
404# define ex_folddo ex_ni
405#endif
406#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
407 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
408# define ex_language ex_ni
409#endif
410#ifndef FEAT_SIGNS
411# define ex_sign ex_ni
412#endif
413#ifndef FEAT_SUN_WORKSHOP
414# define ex_wsverb ex_ni
415#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000416#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200417# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000418# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200419# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422#ifndef FEAT_EVAL
423# define ex_debug ex_ni
424# define ex_breakadd ex_ni
425# define ex_debuggreedy ex_ni
426# define ex_breakdel ex_ni
427# define ex_breaklist ex_ni
428#endif
429
430#ifndef FEAT_CMDHIST
431# define ex_history ex_ni
432#endif
433#ifndef FEAT_JUMPLIST
434# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200435# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# define ex_changes ex_ni
437#endif
438
Bram Moolenaar05159a02005-02-26 23:04:13 +0000439#ifndef FEAT_PROFILE
440# define ex_profile ex_ni
441#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200442#ifndef FEAT_TERMINAL
443# define ex_terminal ex_ni
444#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000445
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446/*
447 * Declare cmdnames[].
448 */
449#define DO_DECLARE_EXCMD
450#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200451#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453static char_u dollar_command[2] = {'$', 0};
454
455
456#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000457/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458typedef struct
459{
460 char_u *line; /* command line */
461 linenr_T lnum; /* sourcing_lnum of the line */
462} wcmd_T;
463
464/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000465 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000467 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000469struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 garray_T *lines_gap; /* growarray with line info */
472 int current_line; /* last read line from growarray */
473 int repeating; /* TRUE when looping a second time */
474 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100475 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 void *cookie;
477};
478
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100479static char_u *get_loop_line(int c, void *cookie, int indent);
480static int store_loop_line(garray_T *gap, char_u *line);
481static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000482
483/* Struct to save a few things while debugging. Used in do_cmdline() only. */
484struct dbg_stuff
485{
486 int trylevel;
487 int force_abort;
488 except_T *caught_stack;
489 char_u *vv_exception;
490 char_u *vv_throwpoint;
491 int did_emsg;
492 int got_int;
493 int did_throw;
494 int need_rethrow;
495 int check_cstack;
496 except_T *current_exception;
497};
498
Bram Moolenaared203462004-06-16 11:19:22 +0000499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100500save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000501{
502 dsp->trylevel = trylevel; trylevel = 0;
503 dsp->force_abort = force_abort; force_abort = FALSE;
504 dsp->caught_stack = caught_stack; caught_stack = NULL;
505 dsp->vv_exception = v_exception(NULL);
506 dsp->vv_throwpoint = v_throwpoint(NULL);
507
508 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
509 dsp->did_emsg = did_emsg; did_emsg = FALSE;
510 dsp->got_int = got_int; got_int = FALSE;
511 dsp->did_throw = did_throw; did_throw = FALSE;
512 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
513 dsp->check_cstack = check_cstack; check_cstack = FALSE;
514 dsp->current_exception = current_exception; current_exception = NULL;
515}
516
517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100518restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000519{
520 suppress_errthrow = FALSE;
521 trylevel = dsp->trylevel;
522 force_abort = dsp->force_abort;
523 caught_stack = dsp->caught_stack;
524 (void)v_exception(dsp->vv_exception);
525 (void)v_throwpoint(dsp->vv_throwpoint);
526 did_emsg = dsp->did_emsg;
527 got_int = dsp->got_int;
528 did_throw = dsp->did_throw;
529 need_rethrow = dsp->need_rethrow;
530 check_cstack = dsp->check_cstack;
531 current_exception = dsp->current_exception;
532}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#endif
534
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535/*
536 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
537 * command is given.
538 */
539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100540do_exmode(
541 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543 int save_msg_scroll;
544 int prev_msg_row;
545 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100546 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547
548 if (improved)
549 exmode_active = EXMODE_VIM;
550 else
551 exmode_active = EXMODE_NORMAL;
552 State = NORMAL;
553
554 /* When using ":global /pat/ visual" and then "Q" we return to continue
555 * the :global command. */
556 if (global_busy)
557 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
559 save_msg_scroll = msg_scroll;
560 ++RedrawingDisabled; /* don't redisplay the window */
561 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#ifdef FEAT_GUI
563 /* Ignore scrollbar and mouse events in Ex mode */
564 ++hold_gui_events;
565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
568 while (exmode_active)
569 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000570 /* Check for a ":normal" command and no more characters left. */
571 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
572 {
573 exmode_active = FALSE;
574 break;
575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 msg_scroll = TRUE;
577 need_wait_return = FALSE;
578 ex_pressedreturn = FALSE;
579 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100580 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 prev_msg_row = msg_row;
582 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (improved)
584 {
585 cmdline_row = msg_row;
586 do_cmdline(NULL, getexline, NULL, 0);
587 }
588 else
589 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
590 lines_left = Rows - 1;
591
Bram Moolenaardf177f62005-02-22 08:39:57 +0000592 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100593 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000595 if (curbuf->b_ml.ml_flags & ML_EMPTY)
596 EMSG(_(e_emptybuf));
597 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000599 if (ex_pressedreturn)
600 {
601 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000602 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000603 msg_row = prev_msg_row;
604 if (prev_msg_row == Rows - 1)
605 msg_row--;
606 }
607 msg_col = 0;
608 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
609 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000612 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
613 {
614 if (curbuf->b_ml.ml_flags & ML_EMPTY)
615 EMSG(_(e_emptybuf));
616 else
617 EMSG(_("E501: At end-of-file"));
618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
620
621#ifdef FEAT_GUI
622 --hold_gui_events;
623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 --RedrawingDisabled;
625 --no_wait_return;
626 update_screen(CLEAR);
627 need_wait_return = FALSE;
628 msg_scroll = save_msg_scroll;
629}
630
631/*
632 * Execute a simple command line. Used for translated commands like "*".
633 */
634 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100635do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 return do_cmdline(cmd, NULL, NULL,
638 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
639}
640
641/*
642 * do_cmdline(): execute one Ex command line
643 *
644 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100645 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 * 2. Split up in parts separated with '|'.
647 *
648 * This function can be called recursively!
649 *
650 * flags:
651 * DOCMD_VERBOSE - The command will be included in the error message.
652 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100653 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 * DOCMD_KEYTYPED - Don't reset KeyTyped.
655 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
656 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
657 *
658 * return FAIL if cmdline could not be executed, OK otherwise
659 */
660 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100661do_cmdline(
662 char_u *cmdline,
663 char_u *(*fgetline)(int, void *, int),
664 void *cookie, /* argument for fgetline() */
665 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
667 char_u *next_cmdline; /* next cmd to execute */
668 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100669 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 static int recursive = 0; /* recursive depth */
671 int msg_didout_before_start = 0;
672 int count = 0; /* line number count */
673 int did_inc = FALSE; /* incremented RedrawingDisabled */
674 int retval = OK;
675#ifdef FEAT_EVAL
676 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000677 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 int current_line = 0; /* active line in lines_ga */
679 char_u *fname = NULL; /* function or script name */
680 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
681 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000682 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 int initial_trylevel;
684 struct msglist **saved_msg_list = NULL;
685 struct msglist *private_msg_list;
686
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100687 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100688 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000692 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695# define cmd_cookie cookie
696#endif
697 static int call_depth = 0; /* recursiveness */
698
699#ifdef FEAT_EVAL
700 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
701 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000702 * This ensures that the do_errthrow() call in do_one_cmd() does not
703 * combine the messages stored by an earlier invocation of do_one_cmd()
704 * with the command name of the later one. This would happen when
705 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 saved_msg_list = msg_list;
707 msg_list = &private_msg_list;
708 private_msg_list = NULL;
709#endif
710
711 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100712 * here. The value of 200 allows nested function calls, ":source", etc.
713 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100714 if (call_depth >= 200
715#ifdef FEAT_EVAL
716 && call_depth >= p_mfd
717#endif
718 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
720 EMSG(_("E169: Command too recursive"));
721#ifdef FEAT_EVAL
722 /* When converting to an exception, we do not include the command name
723 * since this is not an error of the specific command. */
724 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
725 msg_list = saved_msg_list;
726#endif
727 return FAIL;
728 }
729 ++call_depth;
730
731#ifdef FEAT_EVAL
732 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000733 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 cstack.cs_trylevel = 0;
735 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000736 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
738
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100742 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000743 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 ++ex_nesting_level;
745
746 /* Get the function or script name and the address where the next breakpoint
747 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000748 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
750 fname = func_name(real_cookie);
751 breakpoint = func_breakpoint(real_cookie);
752 dbg_tick = func_dbg_tick(real_cookie);
753 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 fname = sourcing_name;
757 breakpoint = source_breakpoint(real_cookie);
758 dbg_tick = source_dbg_tick(real_cookie);
759 }
760
761 /*
762 * Initialize "force_abort" and "suppress_errthrow" at the top level.
763 */
764 if (!recursive)
765 {
766 force_abort = FALSE;
767 suppress_errthrow = FALSE;
768 }
769
770 /*
771 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000772 * exception handling (used when debugging). Otherwise clear it to avoid
773 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000775 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000776 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000777 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200778 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 initial_trylevel = trylevel;
781
782 /*
783 * "did_throw" will be set to TRUE when an exception is being thrown.
784 */
785 did_throw = FALSE;
786#endif
787 /*
788 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * If force_abort is set, we cancel everything.
791 */
792 did_emsg = FALSE;
793
794 /*
795 * KeyTyped is only set when calling vgetc(). Reset it here when not
796 * calling vgetc() (sourced command lines).
797 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100798 if (!(flags & DOCMD_KEYTYPED)
799 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 KeyTyped = FALSE;
801
802 /*
803 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000804 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * - for multiple commands on one line, separated with '|'
806 * - when repeating until there are no more lines (for ":source")
807 */
808 next_cmdline = cmdline;
809 do
810 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813#endif
814
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000815 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (next_cmdline == NULL
817#ifdef FEAT_EVAL
818 && !force_abort
819 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
822 )
823 did_emsg = FALSE;
824
825 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * 3. If a line is given: Make a copy, so we can mess with it.
829 */
830
831#ifdef FEAT_EVAL
832 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000833 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 /* Each '|' separated command is stored separately in lines_ga, to
836 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100837 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 /* Check if a function has returned or, unless it has an unclosed
840 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000844 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 func_line_end(real_cookie);
846# endif
847 if (func_has_ended(real_cookie))
848 {
849 retval = FAIL;
850 break;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000854 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 script_line_end();
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
862 retval = FAIL;
863 break;
864 }
865
866 /* If breakpoints have been added/deleted need to check for it. */
867 if (breakpoint != NULL && dbg_tick != NULL
868 && *dbg_tick != debug_tick)
869 {
870 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 fname, sourcing_lnum);
873 *dbg_tick = debug_tick;
874 }
875
876 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
877 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
878
879 /* Did we encounter a breakpoint? */
880 if (breakpoint != NULL && *breakpoint != 0
881 && *breakpoint <= sourcing_lnum)
882 {
883 dbg_breakpoint(fname, sourcing_lnum);
884 /* Find next breakpoint. */
885 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 fname, sourcing_lnum);
888 *dbg_tick = debug_tick;
889 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000891 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 {
893 if (getline_is_func)
894 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896 script_line_start();
897 }
898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000903 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100904 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * below, so that it stores lines in or reads them from
906 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000907 * while/for loop. */
908 cmd_getline = get_loop_line;
909 cmd_cookie = (void *)&cmd_loop_cookie;
910 cmd_loop_cookie.lines_gap = &lines_ga;
911 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000913 cmd_loop_cookie.cookie = cookie;
914 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916 else
917 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100918 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 cmd_cookie = cookie;
920 }
921#endif
922
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100923 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 if (next_cmdline == NULL)
925 {
926 /*
927 * Need to set msg_didout for the first line after an ":if",
928 * otherwise the ":if" will be overwritten.
929 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100930 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100932 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933#ifdef FEAT_EVAL
934 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
935#else
936 0
937#endif
938 )) == NULL)
939 {
940 /* Don't call wait_return for aborted command line. The NULL
941 * returned for the end of a sourced file or executed function
942 * doesn't do this. */
943 if (KeyTyped && !(flags & DOCMD_REPEAT))
944 need_wait_return = FALSE;
945 retval = FAIL;
946 break;
947 }
948 used_getline = TRUE;
949
950 /*
951 * Keep the first typed line. Clear it when more lines are typed.
952 */
953 if (flags & DOCMD_KEEPLINE)
954 {
955 vim_free(repeat_cmdline);
956 if (count == 0)
957 repeat_cmdline = vim_strsave(next_cmdline);
958 else
959 repeat_cmdline = NULL;
960 }
961 }
962
963 /* 3. Make a copy of the command so we can mess with it. */
964 else if (cmdline_copy == NULL)
965 {
966 next_cmdline = vim_strsave(next_cmdline);
967 if (next_cmdline == NULL)
968 {
969 EMSG(_(e_outofmem));
970 retval = FAIL;
971 break;
972 }
973 }
974 cmdline_copy = next_cmdline;
975
976#ifdef FEAT_EVAL
977 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000978 * Save the current line when inside a ":while" or ":for", and when
979 * the command looks like a ":while" or ":for", because we may need it
980 * later. When there is a '|' and another command, it is stored
981 * separately, because we need to be able to jump back to it from an
982 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 if (current_line == lines_ga.ga_len
985 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
989 retval = FAIL;
990 break;
991 }
992 }
993 did_endif = FALSE;
994#endif
995
996 if (count++ == 0)
997 {
998 /*
999 * All output from the commands is put below each other, without
1000 * waiting for a return. Don't do this when executing commands
1001 * from a script or when being called recursive (e.g. for ":e
1002 * +command file").
1003 */
1004 if (!(flags & DOCMD_NOWAIT) && !recursive)
1005 {
1006 msg_didout_before_start = msg_didout;
1007 msg_didany = FALSE; /* no output yet */
1008 msg_start();
1009 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001010 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 ++RedrawingDisabled;
1012 did_inc = TRUE;
1013 }
1014 }
1015
1016 if (p_verbose >= 15 && sourcing_name != NULL)
1017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001019 verbose_enter_scroll();
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 smsg((char_u *)_("line %ld: %s"),
1022 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001023 if (msg_silent == 0)
1024 msg_puts((char_u *)"\n"); /* don't overwrite this */
1025
1026 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 --no_wait_return;
1028 }
1029
1030 /*
1031 * 2. Execute one '|' separated command.
1032 * do_one_cmd() will return NULL if there is no trailing '|'.
1033 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1034 */
1035 ++recursive;
1036 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1037#ifdef FEAT_EVAL
1038 &cstack,
1039#endif
1040 cmd_getline, cmd_cookie);
1041 --recursive;
1042
1043#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (cmd_cookie == (void *)&cmd_loop_cookie)
1045 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#endif
1049
1050 if (next_cmdline == NULL)
1051 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001052 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#ifdef FEAT_CMDHIST
1054 /*
1055 * If the command was typed, remember it for the ':' register.
1056 * Do this AFTER executing the command to make :@: work.
1057 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001058 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 && new_last_cmdline != NULL)
1060 {
1061 vim_free(last_cmdline);
1062 last_cmdline = new_last_cmdline;
1063 new_last_cmdline = NULL;
1064 }
1065#endif
1066 }
1067 else
1068 {
1069 /* need to copy the command after the '|' to cmdline_copy, for the
1070 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001071 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 next_cmdline = cmdline_copy;
1073 }
1074
1075
1076#ifdef FEAT_EVAL
1077 /* reset did_emsg for a function that is not aborted by an error */
1078 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001079 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 && !func_has_abort(real_cookie))
1081 did_emsg = FALSE;
1082
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 ++current_line;
1086
1087 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001088 * An ":endwhile", ":endfor" and ":continue" is handled here.
1089 * If we were executing commands, jump back to the ":while" or
1090 * ":for".
1091 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 /* Jump back to the matching ":while" or ":for". Be careful
1098 * not to use a cs_line[] from an entry that isn't a ":while"
1099 * or ":for": It would make "current_line" invalid and can
1100 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (!did_emsg && !got_int && !did_throw
1102 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 && (cstack.cs_flags[cstack.cs_idx]
1104 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 && cstack.cs_line[cstack.cs_idx] >= 0
1106 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1107 {
1108 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 /* remember we jumped there */
1110 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 line_breakcheck(); /* check if CTRL-C typed */
1112
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 /* Check for the next breakpoint at or after the ":while"
1114 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 if (breakpoint != NULL)
1116 {
1117 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001118 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 fname,
1120 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1121 *dbg_tick = debug_tick;
1122 }
1123 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001128 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1129 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 }
1132
1133 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1140 }
1141 }
1142
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001143 /* Check for the next breakpoint after a watchexpression */
1144 if (breakpoint != NULL && has_watchexpr())
1145 {
1146 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1147 *dbg_tick = debug_tick;
1148 }
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 /*
1151 * When not inside any ":while" loop, clear remembered lines.
1152 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 if (lines_ga.ga_len > 0)
1156 {
1157 sourcing_lnum =
1158 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1159 free_cmdlines(&lines_ga);
1160 }
1161 current_line = 0;
1162 }
1163
1164 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1166 * being restored at the ":endtry". Reset them here and set the
1167 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1168 * This includes the case where a missing ":endif", ":endwhile" or
1169 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 cstack.cs_lflags &= ~CSL_HAD_FINA;
1174 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1175 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 did_throw ? (void *)current_exception : NULL);
1177 did_emsg = got_int = did_throw = FALSE;
1178 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1179 }
1180
1181 /* Update global "trylevel" for recursive calls to do_cmdline() from
1182 * within this loop. */
1183 trylevel = initial_trylevel + cstack.cs_trylevel;
1184
1185 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001186 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 * files) is aborted because of an error, an interrupt, or an uncaught
1188 * exception, cancel everything. If it is left normally, reset
1189 * force_abort to get the non-EH compatible abortion behavior for
1190 * the rest of the script.
1191 */
1192 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1193 force_abort = FALSE;
1194
1195 /* Convert an interrupt to an exception if appropriate. */
1196 (void)do_intthrow(&cstack);
1197#endif /* FEAT_EVAL */
1198
1199 }
1200 /*
1201 * Continue executing command lines when:
1202 * - no CTRL-C typed, no aborting error, no exception thrown or try
1203 * conditionals need to be checked for executing finally clauses or
1204 * catching an interrupt exception
1205 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 * looping for ":source" command or function call.
1208 */
1209 while (!((got_int
1210#ifdef FEAT_EVAL
1211 || (did_emsg && force_abort) || did_throw
1212#endif
1213 )
1214#ifdef FEAT_EVAL
1215 && cstack.cs_trylevel == 0
1216#endif
1217 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001218 && !(did_emsg
1219#ifdef FEAT_EVAL
1220 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001221 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001222 * the :endtry to be missed. */
1223 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1224#endif
1225 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001226 && (getline_equal(fgetline, cookie, getexmodeline)
1227 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 && (next_cmdline != NULL
1229#ifdef FEAT_EVAL
1230 || cstack.cs_idx >= 0
1231#endif
1232 || (flags & DOCMD_REPEAT)));
1233
1234 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001235 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236#ifdef FEAT_EVAL
1237 free_cmdlines(&lines_ga);
1238 ga_clear(&lines_ga);
1239
1240 if (cstack.cs_idx >= 0)
1241 {
1242 /*
1243 * If a sourced file or executed function ran to its end, report the
1244 * unclosed conditional.
1245 */
1246 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001247 && ((getline_equal(fgetline, cookie, getsourceline)
1248 && !source_finished(fgetline, cookie))
1249 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 && !func_has_ended(real_cookie))))
1251 {
1252 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1253 EMSG(_(e_endtry));
1254 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1255 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001256 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1257 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 else
1259 EMSG(_(e_endif));
1260 }
1261
1262 /*
1263 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1264 * ":endtry" in a sourced file or executed function. If the try
1265 * conditional is in its finally clause, ignore anything pending.
1266 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001267 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 */
1269 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001270 {
1271 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1272
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001273 if (idx >= 0)
1274 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001275 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1276 &cstack.cs_looplevel);
1277 }
1278 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 trylevel = initial_trylevel;
1280 }
1281
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001282 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1283 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001285 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 ? (char_u *)"endfunction" : (char_u *)NULL);
1287
1288 if (trylevel == 0)
1289 {
1290 /*
1291 * When an exception is being thrown out of the outermost try
1292 * conditional, discard the uncaught exception, disable the conversion
1293 * of interrupts or errors to exceptions, and ensure that no more
1294 * commands are executed.
1295 */
1296 if (did_throw)
1297 {
1298 void *p = NULL;
1299 char_u *saved_sourcing_name;
1300 int saved_sourcing_lnum;
1301 struct msglist *messages = NULL, *next;
1302
1303 /*
1304 * If the uncaught exception is a user exception, report it as an
1305 * error. If it is an error exception, display the saved error
1306 * message now. For an interrupt exception, do nothing; the
1307 * interrupt message is given elsewhere.
1308 */
1309 switch (current_exception->type)
1310 {
1311 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001312 vim_snprintf((char *)IObuff, IOSIZE,
1313 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 current_exception->value);
1315 p = vim_strsave(IObuff);
1316 break;
1317 case ET_ERROR:
1318 messages = current_exception->messages;
1319 current_exception->messages = NULL;
1320 break;
1321 case ET_INTERRUPT:
1322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324
1325 saved_sourcing_name = sourcing_name;
1326 saved_sourcing_lnum = sourcing_lnum;
1327 sourcing_name = current_exception->throw_name;
1328 sourcing_lnum = current_exception->throw_lnum;
1329 current_exception->throw_name = NULL;
1330
1331 discard_current_exception(); /* uses IObuff if 'verbose' */
1332 suppress_errthrow = TRUE;
1333 force_abort = TRUE;
1334
1335 if (messages != NULL)
1336 {
1337 do
1338 {
1339 next = messages->next;
1340 emsg(messages->msg);
1341 vim_free(messages->msg);
1342 vim_free(messages);
1343 messages = next;
1344 }
1345 while (messages != NULL);
1346 }
1347 else if (p != NULL)
1348 {
1349 emsg(p);
1350 vim_free(p);
1351 }
1352 vim_free(sourcing_name);
1353 sourcing_name = saved_sourcing_name;
1354 sourcing_lnum = saved_sourcing_lnum;
1355 }
1356
1357 /*
1358 * On an interrupt or an aborting error not converted to an exception,
1359 * disable the conversion of errors to exceptions. (Interrupts are not
1360 * converted any more, here.) This enables also the interrupt message
1361 * when force_abort is set and did_emsg unset in case of an interrupt
1362 * from a finally clause after an error.
1363 */
1364 else if (got_int || (did_emsg && force_abort))
1365 suppress_errthrow = TRUE;
1366 }
1367
1368 /*
1369 * The current cstack will be freed when do_cmdline() returns. An uncaught
1370 * exception will have to be rethrown in the previous cstack. If a function
1371 * has just returned or a script file was just finished and the previous
1372 * cstack belongs to the same function or, respectively, script file, it
1373 * will have to be checked for finally clauses to be executed due to the
1374 * ":return" or ":finish". This is done in do_one_cmd().
1375 */
1376 if (did_throw)
1377 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001378 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001380 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 && ex_nesting_level > func_level(real_cookie) + 1))
1382 {
1383 if (!did_throw)
1384 check_cstack = TRUE;
1385 }
1386 else
1387 {
1388 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001389 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 --ex_nesting_level;
1391 /*
1392 * Go to debug mode when returning from a function in which we are
1393 * single-stepping.
1394 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001395 if ((getline_equal(fgetline, cookie, getsourceline)
1396 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001398 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 ? (char_u *)_("End of sourced file")
1400 : (char_u *)_("End of function"));
1401 }
1402
1403 /*
1404 * Restore the exception environment (done after returning from the
1405 * debugger).
1406 */
1407 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001408 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 msg_list = saved_msg_list;
1411#endif /* FEAT_EVAL */
1412
1413 /*
1414 * If there was too much output to fit on the command line, ask the user to
1415 * hit return before redrawing the screen. With the ":global" command we do
1416 * this only once after the command is finished.
1417 */
1418 if (did_inc)
1419 {
1420 --RedrawingDisabled;
1421 --no_wait_return;
1422 msg_scroll = FALSE;
1423
1424 /*
1425 * When just finished an ":if"-":else" which was typed, no need to
1426 * wait for hit-return. Also for an error situation.
1427 */
1428 if (retval == FAIL
1429#ifdef FEAT_EVAL
1430 || (did_endif && KeyTyped && !did_emsg)
1431#endif
1432 )
1433 {
1434 need_wait_return = FALSE;
1435 msg_didany = FALSE; /* don't wait when restarting edit */
1436 }
1437 else if (need_wait_return)
1438 {
1439 /*
1440 * The msg_start() above clears msg_didout. The wait_return we do
1441 * here should not overwrite the command that may be shown before
1442 * doing that.
1443 */
1444 msg_didout |= msg_didout_before_start;
1445 wait_return(FALSE);
1446 }
1447 }
1448
Bram Moolenaar2a942252012-11-28 23:03:07 +01001449#ifdef FEAT_EVAL
1450 did_endif = FALSE; /* in case do_cmdline used recursively */
1451#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 /*
1453 * Reset if_level, in case a sourced script file contains more ":if" than
1454 * ":endif" (could be ":if x | foo | endif").
1455 */
1456 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001457#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 --call_depth;
1460 return retval;
1461}
1462
1463#ifdef FEAT_EVAL
1464/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001465 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
1467 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001468get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001470 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 wcmd_T *wp;
1472 char_u *line;
1473
1474 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1475 {
1476 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001477 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001479 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (cp->getline == NULL)
1481 line = getcmdline(c, 0L, indent);
1482 else
1483 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001484 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 ++cp->current_line;
1486
1487 return line;
1488 }
1489
1490 KeyTyped = FALSE;
1491 ++cp->current_line;
1492 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1493 sourcing_lnum = wp->lnum;
1494 return vim_strsave(wp->line);
1495}
1496
1497/*
1498 * Store a line in "gap" so that a ":while" loop can execute it again.
1499 */
1500 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001501store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 if (ga_grow(gap, 1) == FAIL)
1504 return FAIL;
1505 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1506 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1507 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 return OK;
1509}
1510
1511/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001512 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
1514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 while (gap->ga_len > 0)
1518 {
1519 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1520 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
1522}
1523#endif
1524
1525/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001526 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1527 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001530getline_equal(
1531 char_u *(*fgetline)(int, void *, int),
1532 void *cookie UNUSED, /* argument for fgetline() */
1533 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
1535#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001536 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001540 * function that's originally used to obtain the lines. This may be
1541 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001542 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 cp = (struct loop_cookie *)cookie;
1544 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
1546 gp = cp->getline;
1547 cp = cp->cookie;
1548 }
1549 return gp == func;
1550#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#endif
1553}
1554
1555#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1556/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001557 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 * getline function. Otherwise return "cookie".
1559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001561getline_cookie(
1562 char_u *(*fgetline)(int, void *, int) UNUSED,
1563 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001566 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar89d40322006-08-29 15:30:07 +00001569 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001570 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001572 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 cp = (struct loop_cookie *)cookie;
1574 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 gp = cp->getline;
1577 cp = cp->cookie;
1578 }
1579 return cp;
1580# else
1581 return cookie;
1582# endif
1583}
1584#endif
1585
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001586
1587/*
1588 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1589 * ":bwipeout", etc.
1590 * Returns the buffer number.
1591 */
1592 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001593compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594{
1595 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001596 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001597 int count = offset;
1598
1599 buf = firstbuf;
1600 while (buf->b_next != NULL && buf->b_fnum < lnum)
1601 buf = buf->b_next;
1602 while (count != 0)
1603 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001604 count += (offset < 0) ? 1 : -1;
1605 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1606 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001608 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001609 if (addr_type == ADDR_LOADED_BUFFERS)
1610 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001611 while (buf->b_ml.ml_mfp == NULL)
1612 {
1613 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1614 if (nextbuf == NULL)
1615 break;
1616 buf = nextbuf;
1617 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001619 /* we might have gone too far, last buffer is not loadedd */
1620 if (addr_type == ADDR_LOADED_BUFFERS)
1621 while (buf->b_ml.ml_mfp == NULL)
1622 {
1623 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1624 if (nextbuf == NULL)
1625 break;
1626 buf = nextbuf;
1627 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 return buf->b_fnum;
1629}
1630
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001632current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001633{
1634 win_T *wp;
1635 int nr = 0;
1636
Bram Moolenaar29323592016-07-24 22:04:11 +02001637 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001638 {
1639 ++nr;
1640 if (wp == win)
1641 break;
1642 }
1643 return nr;
1644}
1645
1646 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001647current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648{
1649 tabpage_T *tp;
1650 int nr = 0;
1651
Bram Moolenaar29323592016-07-24 22:04:11 +02001652 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001653 {
1654 ++nr;
1655 if (tp == tab)
1656 break;
1657 }
1658 return nr;
1659}
1660
1661# define CURRENT_WIN_NR current_win_nr(curwin)
1662# define LAST_WIN_NR current_win_nr(NULL)
1663# define CURRENT_TAB_NR current_tab_nr(curtab)
1664# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666/*
1667 * Execute one Ex command.
1668 *
1669 * If 'sourcing' is TRUE, the command will be included in the error message.
1670 *
1671 * 1. skip comment lines and leading space
1672 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001673 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001675 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 * 6. parse arguments
1677 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001679 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 *
1681 * This function may be called recursively!
1682 */
1683#if (_MSC_VER == 1200)
1684/*
Bram Moolenaared203462004-06-16 11:19:22 +00001685 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001687 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#endif
1689 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001690do_one_cmd(
1691 char_u **cmdlinep,
1692 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001694 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001696 char_u *(*fgetline)(int, void *, int),
1697 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 char_u *p;
1700 linenr_T lnum;
1701 long n;
1702 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001703 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001705 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 cmdmod_T save_cmdmod;
1707 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
1710 vim_memset(&ea, 0, sizeof(ea));
1711 ea.line1 = 1;
1712 ea.line2 = 1;
1713#ifdef FEAT_EVAL
1714 ++ex_nesting_level;
1715#endif
1716
Bram Moolenaar21691f82012-12-05 19:13:18 +01001717 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (quitmore
1719#ifdef FEAT_EVAL
1720 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001721 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001722#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001723 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001724 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 --quitmore;
1726
1727 /*
1728 * Reset browse, confirm, etc.. They are restored when returning, for
1729 * recursive calls.
1730 */
1731 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001733 /* "#!anything" is handled like a comment. */
1734 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1735 goto doend;
1736
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001737/*
1738 * 1. Skip comment lines and leading white space and colons.
1739 * 2. Handle command modifiers.
1740 */
1741 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001743 ea.cmdlinep = cmdlinep;
1744 ea.getline = fgetline;
1745 ea.cookie = cookie;
1746#ifdef FEAT_EVAL
1747 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001749 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001750 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001752 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754#ifdef FEAT_EVAL
1755 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1756 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1757#else
1758 ea.skip = (if_level > 0);
1759#endif
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001762 * 3. Skip over the range to find the command. Let "p" point to after it.
1763 *
1764 * We need the command to know what kind of range it uses.
1765 */
1766 cmd = ea.cmd;
1767 ea.cmd = skip_range(ea.cmd, NULL);
1768 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1769 ea.cmd = skipwhite(ea.cmd + 1);
1770 p = find_command(&ea, NULL);
1771
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001772#ifdef FEAT_EVAL
1773# ifdef FEAT_PROFILE
1774 // Count this line for profiling if skip is TRUE.
1775 if (do_profiling == PROF_YES
1776 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1777 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1778 {
1779 int skip = did_emsg || got_int || did_throw;
1780
1781 if (ea.cmdidx == CMD_catch)
1782 skip = !skip && !(cstack->cs_idx >= 0
1783 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1784 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1785 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1786 skip = skip || !(cstack->cs_idx >= 0
1787 && !(cstack->cs_flags[cstack->cs_idx]
1788 & (CSF_ACTIVE | CSF_TRUE)));
1789 else if (ea.cmdidx == CMD_finally)
1790 skip = FALSE;
1791 else if (ea.cmdidx != CMD_endif
1792 && ea.cmdidx != CMD_endfor
1793 && ea.cmdidx != CMD_endtry
1794 && ea.cmdidx != CMD_endwhile)
1795 skip = ea.skip;
1796
1797 if (!skip)
1798 {
1799 if (getline_equal(fgetline, cookie, get_func_line))
1800 func_line_exec(getline_cookie(fgetline, cookie));
1801 else if (getline_equal(fgetline, cookie, getsourceline))
1802 script_line_exec();
1803 }
1804 }
1805# endif
1806
1807 /* May go to debug mode. If this happens and the ">quit" debug command is
1808 * used, throw an interrupt exception and skip the next command. */
1809 dbg_check_breakpoint(&ea);
1810 if (!ea.skip && got_int)
1811 {
1812 ea.skip = TRUE;
1813 (void)do_intthrow(cstack);
1814 }
1815#endif
1816
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001817/*
1818 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 *
1820 * where 'addr' is:
1821 *
1822 * % (entire file)
1823 * $ [+-NUM]
1824 * 'x [+-NUM] (where x denotes a currently defined mark)
1825 * . [+-NUM]
1826 * [+-NUM]..
1827 * NUM
1828 *
1829 * The ea.cmd pointer is updated to point to the first character following the
1830 * range spec. If an initial address is found, but no second, the upper bound
1831 * is equal to the lower.
1832 */
1833
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001834 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001835 if (!IS_USER_CMDIDX(ea.cmdidx))
1836 {
1837 if (ea.cmdidx != CMD_SIZE)
1838 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1839 else
1840 ea.addr_type = ADDR_LINES;
1841
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001842 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001843 if (ea.cmdidx == CMD_wincmd && p != NULL)
1844 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001845 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001846
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001847 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001848 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001849 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001852 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 */
1854
1855 /*
1856 * Skip ':' and any white space
1857 */
1858 ea.cmd = skipwhite(ea.cmd);
1859 while (*ea.cmd == ':')
1860 ea.cmd = skipwhite(ea.cmd + 1);
1861
1862 /*
1863 * If we got a line, but no command, then go to the line.
1864 * If we find a '|' or '\n' we set ea.nextcmd.
1865 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866 if (*ea.cmd == NUL || *ea.cmd == '"'
1867 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 /*
1870 * strange vi behaviour:
1871 * ":3" jumps to line 3
1872 * ":3|..." prints line 3
1873 * ":|" prints current line
1874 */
1875 if (ea.skip) /* skip this if inside :if */
1876 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001877 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 ea.cmdidx = CMD_print;
1880 ea.argt = RANGE+COUNT+TRLBAR;
1881 if ((errormsg = invalid_range(&ea)) == NULL)
1882 {
1883 correct_range(&ea);
1884 ex_print(&ea);
1885 }
1886 }
1887 else if (ea.addr_count != 0)
1888 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001889 if (ea.line2 > curbuf->b_ml.ml_line_count)
1890 {
1891 /* With '-' in 'cpoptions' a line number past the file is an
1892 * error, otherwise put it at the end of the file. */
1893 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1894 ea.line2 = -1;
1895 else
1896 ea.line2 = curbuf->b_ml.ml_line_count;
1897 }
1898
1899 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001900 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 else
1902 {
1903 if (ea.line2 == 0)
1904 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 else
1906 curwin->w_cursor.lnum = ea.line2;
1907 beginline(BL_SOL | BL_FIX);
1908 }
1909 }
1910 goto doend;
1911 }
1912
Bram Moolenaard5005162014-08-22 23:05:54 +02001913 /* If this looks like an undefined user command and there are CmdUndefined
1914 * autocommands defined, trigger the matching autocommands. */
1915 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1916 && ASCII_ISUPPER(*ea.cmd)
1917 && has_cmdundefined())
1918 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001919 int ret;
1920
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001921 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001922 while (ASCII_ISALNUM(*p))
1923 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001924 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001925 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1926 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001927 /* If the autocommands did something and didn't cause an error, try
1928 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001929 p = (ret
1930#ifdef FEAT_EVAL
1931 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001932#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001933 ) ? find_command(&ea, NULL) : ea.cmd;
1934 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef FEAT_USR_CMDS
1937 if (p == NULL)
1938 {
1939 if (!ea.skip)
1940 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
1941 goto doend;
1942 }
1943 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001944 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1945 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 errormsg = uc_fun_cmd();
1948 goto doend;
1949 }
1950#endif
1951 if (ea.cmdidx == CMD_SIZE)
1952 {
1953 if (!ea.skip)
1954 {
1955 STRCPY(IObuff, _("E492: Not an editor command"));
1956 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001957 {
1958 /* If the modifier was parsed OK the error must be in the
1959 * following command */
1960 if (after_modifier != NULL)
1961 append_command(after_modifier);
1962 else
1963 append_command(*cmdlinep);
1964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001966 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 goto doend;
1969 }
1970
Bram Moolenaar958636c2014-10-21 20:01:58 +02001971 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1972 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001973#ifdef HAVE_EX_SCRIPT_NI
1974 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1975#endif
1976 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978#ifndef FEAT_EVAL
1979 /*
1980 * When the expression evaluation is disabled, recognize the ":if" and
1981 * ":endif" commands and ignore everything in between it.
1982 */
1983 if (ea.cmdidx == CMD_if)
1984 ++if_level;
1985 if (if_level)
1986 {
1987 if (ea.cmdidx == CMD_endif)
1988 --if_level;
1989 goto doend;
1990 }
1991
1992#endif
1993
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001994 /* forced commands */
1995 if (*p == '!' && ea.cmdidx != CMD_substitute
1996 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 ++p;
1999 ea.forceit = TRUE;
2000 }
2001 else
2002 ea.forceit = FALSE;
2003
2004/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002005 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002007 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002008 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 if (!ea.skip)
2011 {
2012#ifdef HAVE_SANDBOX
2013 if (sandbox != 0 && !(ea.argt & SBOXOK))
2014 {
2015 /* Command not allowed in sandbox. */
2016 errormsg = (char_u *)_(e_sandbox);
2017 goto doend;
2018 }
2019#endif
2020 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2021 {
2022 /* Command not allowed in non-'modifiable' buffer */
2023 errormsg = (char_u *)_(e_modifiable);
2024 goto doend;
2025 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002026
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002027 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002028 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002030 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002031 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 goto doend;
2033 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002034
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002035 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002036 * Do allow ":checktime" (it is postponed).
2037 * Do allow ":edit" (check for an argument later).
2038 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002040 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002041 && ea.cmdidx != CMD_edit
2042 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002043 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 && curbuf_locked())
2045 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2048 {
2049 /* no range allowed */
2050 errormsg = (char_u *)_(e_norange);
2051 goto doend;
2052 }
2053 }
2054
2055 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2056 {
2057 errormsg = (char_u *)_(e_nobang);
2058 goto doend;
2059 }
2060
2061 /*
2062 * Don't complain about the range if it is not used
2063 * (could happen if line_count is accidentally set to 0).
2064 */
2065 if (!ea.skip && !ni)
2066 {
2067 /*
2068 * If the range is backwards, ask for confirmation and, if given, swap
2069 * ea.line1 & ea.line2 so it's forwards again.
2070 * When global command is busy, don't ask, will fail below.
2071 */
2072 if (!global_busy && ea.line1 > ea.line2)
2073 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002074 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002076 if (sourcing || exmode_active)
2077 {
2078 errormsg = (char_u *)_("E493: Backwards range given");
2079 goto doend;
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (ask_yesno((char_u *)
2082 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002083 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 lnum = ea.line1;
2086 ea.line1 = ea.line2;
2087 ea.line2 = lnum;
2088 }
2089 if ((errormsg = invalid_range(&ea)) != NULL)
2090 goto doend;
2091 }
2092
2093 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2094 ea.line2 = 1;
2095
2096 correct_range(&ea);
2097
2098#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002099 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2100 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 /* Put the first line at the start of a closed fold, put the last line
2103 * at the end of a closed fold. */
2104 (void)hasFolding(ea.line1, &ea.line1, NULL);
2105 (void)hasFolding(ea.line2, NULL, &ea.line2);
2106 }
2107#endif
2108
2109#ifdef FEAT_QUICKFIX
2110 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002111 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 * option here, so things like % get expanded.
2113 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002114 p = replace_makeprg(&ea, p, cmdlinep);
2115 if (p == NULL)
2116 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
2118
2119 /*
2120 * Skip to start of argument.
2121 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2122 */
2123 if (ea.cmdidx == CMD_bang)
2124 ea.arg = p;
2125 else
2126 ea.arg = skipwhite(p);
2127
Bram Moolenaar379fb762018-08-30 15:58:28 +02002128 // ":file" cannot be run with an argument when "curbuf_lock" is set
2129 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2130 goto doend;
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 /*
2133 * Check for "++opt=val" argument.
2134 * Must be first, allow ":w ++enc=utf8 !cmd"
2135 */
2136 if (ea.argt & ARGOPT)
2137 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2138 if (getargopt(&ea) == FAIL && !ni)
2139 {
2140 errormsg = (char_u *)_(e_invarg);
2141 goto doend;
2142 }
2143
2144 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2145 {
2146 if (*ea.arg == '>') /* append */
2147 {
2148 if (*++ea.arg != '>') /* typed wrong */
2149 {
2150 errormsg = (char_u *)_("E494: Use w or w>>");
2151 goto doend;
2152 }
2153 ea.arg = skipwhite(ea.arg + 1);
2154 ea.append = TRUE;
2155 }
2156 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2157 {
2158 ++ea.arg;
2159 ea.usefilter = TRUE;
2160 }
2161 }
2162
2163 if (ea.cmdidx == CMD_read)
2164 {
2165 if (ea.forceit)
2166 {
2167 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2168 ea.forceit = FALSE;
2169 }
2170 else if (*ea.arg == '!') /* :r !filter */
2171 {
2172 ++ea.arg;
2173 ea.usefilter = TRUE;
2174 }
2175 }
2176
2177 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2178 {
2179 ea.amount = 1;
2180 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2181 {
2182 ++ea.arg;
2183 ++ea.amount;
2184 }
2185 ea.arg = skipwhite(ea.arg);
2186 }
2187
2188 /*
2189 * Check for "+command" argument, before checking for next command.
2190 * Don't do this for ":read !cmd" and ":write !cmd".
2191 */
2192 if ((ea.argt & EDITCMD) && !ea.usefilter)
2193 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2194
2195 /*
2196 * Check for '|' to separate commands and '"' to start comments.
2197 * Don't do this for ":read !cmd" and ":write !cmd".
2198 */
2199 if ((ea.argt & TRLBAR) && !ea.usefilter)
2200 separate_nextcmd(&ea);
2201
2202 /*
2203 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002204 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2205 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002207 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002208 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002209 || ea.cmdidx == CMD_global
2210 || ea.cmdidx == CMD_vglobal
2211 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 for (p = ea.arg; *p; ++p)
2214 {
2215 /* Remove one backslash before a newline, so that it's possible to
2216 * pass a newline to the shell and also a newline that is preceded
2217 * with a backslash. This makes it impossible to end a shell
2218 * command in a backslash, but that doesn't appear useful.
2219 * Halving the number of backslashes is incompatible with previous
2220 * versions. */
2221 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002222 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 else if (*p == '\n')
2224 {
2225 ea.nextcmd = p + 1;
2226 *p = NUL;
2227 break;
2228 }
2229 }
2230 }
2231
2232 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2233 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002234 buf_T *buf;
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002237 switch (ea.addr_type)
2238 {
2239 case ADDR_LINES:
2240 ea.line2 = curbuf->b_ml.ml_line_count;
2241 break;
2242 case ADDR_LOADED_BUFFERS:
2243 buf = firstbuf;
2244 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2245 buf = buf->b_next;
2246 ea.line1 = buf->b_fnum;
2247 buf = lastbuf;
2248 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2249 buf = buf->b_prev;
2250 ea.line2 = buf->b_fnum;
2251 break;
2252 case ADDR_BUFFERS:
2253 ea.line1 = firstbuf->b_fnum;
2254 ea.line2 = lastbuf->b_fnum;
2255 break;
2256 case ADDR_WINDOWS:
2257 ea.line2 = LAST_WIN_NR;
2258 break;
2259 case ADDR_TABS:
2260 ea.line2 = LAST_TAB_NR;
2261 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002262 case ADDR_TABS_RELATIVE:
2263 ea.line2 = 1;
2264 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002265 case ADDR_ARGUMENTS:
2266 if (ARGCOUNT == 0)
2267 ea.line1 = ea.line2 = 0;
2268 else
2269 ea.line2 = ARGCOUNT;
2270 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002271#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002272 case ADDR_QUICKFIX:
2273 ea.line2 = qf_get_size(&ea);
2274 if (ea.line2 == 0)
2275 ea.line2 = 1;
2276 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002277#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 /* accept numbered register only when no count allowed (:put) */
2282 if ( (ea.argt & REGSTR)
2283 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002284 /* Do not allow register = for user commands */
2285 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2287 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002288#ifndef FEAT_CLIPBOARD
2289 /* check these explicitly for a more specific error message */
2290 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002292 errormsg = (char_u *)_(e_invalidreg);
2293 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002296 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2297 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002298 {
2299 ea.regname = *ea.arg++;
2300#ifdef FEAT_EVAL
2301 /* for '=' register: accept the rest of the line as an expression */
2302 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2303 {
2304 set_expr_line(vim_strsave(ea.arg));
2305 ea.arg += STRLEN(ea.arg);
2306 }
2307#endif
2308 ea.arg = skipwhite(ea.arg);
2309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311
2312 /*
2313 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2314 * count, it's a buffer name.
2315 */
2316 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2317 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002318 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 n = getdigits(&ea.arg);
2321 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002322 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 errormsg = (char_u *)_(e_zerocount);
2325 goto doend;
2326 }
2327 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2328 {
2329 ea.line2 = n;
2330 if (ea.addr_count == 0)
2331 ea.addr_count = 1;
2332 }
2333 else
2334 {
2335 ea.line1 = ea.line2;
2336 ea.line2 += n - 1;
2337 ++ea.addr_count;
2338 /*
2339 * Be vi compatible: no error message for out of range.
2340 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002341 if (ea.addr_type == ADDR_LINES
2342 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 ea.line2 = curbuf->b_ml.ml_line_count;
2344 }
2345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002346
2347 /*
2348 * Check for flags: 'l', 'p' and '#'.
2349 */
2350 if (ea.argt & EXFLAGS)
2351 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002353 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002354 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
2356 errormsg = (char_u *)_(e_trailing);
2357 goto doend;
2358 }
2359
2360 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2361 {
2362 errormsg = (char_u *)_(e_argreq);
2363 goto doend;
2364 }
2365
2366#ifdef FEAT_EVAL
2367 /*
2368 * Skip the command when it's not going to be executed.
2369 * The commands like :if, :endif, etc. always need to be executed.
2370 * Also make an exception for commands that handle a trailing command
2371 * themselves.
2372 */
2373 if (ea.skip)
2374 {
2375 switch (ea.cmdidx)
2376 {
2377 /* commands that need evaluation */
2378 case CMD_while:
2379 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002380 case CMD_for:
2381 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_if:
2383 case CMD_elseif:
2384 case CMD_else:
2385 case CMD_endif:
2386 case CMD_try:
2387 case CMD_catch:
2388 case CMD_finally:
2389 case CMD_endtry:
2390 case CMD_function:
2391 break;
2392
2393 /* Commands that handle '|' themselves. Check: A command should
2394 * either have the TRLBAR flag, appear in this list or appear in
2395 * the list at ":help :bar". */
2396 case CMD_aboveleft:
2397 case CMD_and:
2398 case CMD_belowright:
2399 case CMD_botright:
2400 case CMD_browse:
2401 case CMD_call:
2402 case CMD_confirm:
2403 case CMD_delfunction:
2404 case CMD_djump:
2405 case CMD_dlist:
2406 case CMD_dsearch:
2407 case CMD_dsplit:
2408 case CMD_echo:
2409 case CMD_echoerr:
2410 case CMD_echomsg:
2411 case CMD_echon:
2412 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002413 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_help:
2415 case CMD_hide:
2416 case CMD_ijump:
2417 case CMD_ilist:
2418 case CMD_isearch:
2419 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002420 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_keepjumps:
2422 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002423 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_leftabove:
2425 case CMD_let:
2426 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002427 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002429 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002430 case CMD_noautocmd:
2431 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_perl:
2433 case CMD_psearch:
2434 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002435 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002436 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_return:
2438 case CMD_rightbelow:
2439 case CMD_ruby:
2440 case CMD_silent:
2441 case CMD_smagic:
2442 case CMD_snomagic:
2443 case CMD_substitute:
2444 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002445 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_tcl:
2447 case CMD_throw:
2448 case CMD_tilde:
2449 case CMD_topleft:
2450 case CMD_unlet:
2451 case CMD_verbose:
2452 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002453 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 break;
2455
2456 default: goto doend;
2457 }
2458 }
2459#endif
2460
2461 if (ea.argt & XFILE)
2462 {
2463 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2464 goto doend;
2465 }
2466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 /*
2468 * Accept buffer name. Cannot be used at the same time with a buffer
2469 * number. Don't do this for a user command.
2470 */
2471 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002472 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 /*
2475 * :bdelete, :bwipeout and :bunload take several arguments, separated
2476 * by spaces: find next space (skipping over escaped characters).
2477 * The others take one argument: ignore trailing spaces.
2478 */
2479 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2480 || ea.cmdidx == CMD_bunload)
2481 p = skiptowhite_esc(ea.arg);
2482 else
2483 {
2484 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002485 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 --p;
2487 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002488 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2489 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (ea.line2 < 0) /* failed */
2491 goto doend;
2492 ea.addr_count = 1;
2493 ea.arg = skipwhite(p);
2494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar2be57332018-02-13 18:05:18 +01002496 /* The :try command saves the emsg_silent flag, reset it here when
2497 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002498 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002499 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002500 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002501 if (emsg_silent < 0)
2502 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002503 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002504 }
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002507 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002511 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
2513 /*
2514 * Execute a user-defined command.
2515 */
2516 do_ucmd(&ea);
2517 }
2518 else
2519#endif
2520 {
2521 /*
2522 * Call the function to execute the command.
2523 */
2524 ea.errmsg = NULL;
2525 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2526 if (ea.errmsg != NULL)
2527 errormsg = (char_u *)_(ea.errmsg);
2528 }
2529
2530#ifdef FEAT_EVAL
2531 /*
2532 * If the command just executed called do_cmdline(), any throw or ":return"
2533 * or ":finish" encountered there must also check the cstack of the still
2534 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2535 * exception, or reanimate a returned function or finished script file and
2536 * return or finish it again.
2537 */
2538 if (need_rethrow)
2539 do_throw(cstack);
2540 else if (check_cstack)
2541 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002542 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002544 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 && current_func_returned())
2546 do_return(&ea, TRUE, FALSE, NULL);
2547 }
2548 need_rethrow = check_cstack = FALSE;
2549#endif
2550
2551doend:
2552 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002555 curwin->w_cursor.col = 0;
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2559 {
2560 if (sourcing)
2561 {
2562 if (errormsg != IObuff)
2563 {
2564 STRCPY(IObuff, errormsg);
2565 errormsg = IObuff;
2566 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002567 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 emsg(errormsg);
2570 }
2571#ifdef FEAT_EVAL
2572 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002573 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2574 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575#endif
2576
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 if (ea.verbose_save >= 0)
2578 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002579
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002580 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 cmdmod = save_cmdmod;
2582
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 /* messages could be enabled for a serious error, need to check if the
2586 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002587 if (!did_emsg || msg_silent > ea.save_msg_silent)
2588 msg_silent = ea.save_msg_silent;
2589 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (emsg_silent < 0)
2591 emsg_silent = 0;
2592 /* Restore msg_scroll, it's set by file I/O commands, even when no
2593 * message is actually displayed. */
2594 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002595
2596 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2597 * somewhere in the line. Put it back in the first column. */
2598 if (redirecting())
2599 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002602#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002604 --sandbox;
2605#endif
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2608 ea.nextcmd = NULL;
2609
2610#ifdef FEAT_EVAL
2611 --ex_nesting_level;
2612#endif
2613
2614 return ea.nextcmd;
2615}
2616#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002617 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#endif
2619
2620/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 * Parse and skip over command modifiers:
2622 * - update eap->cmd
2623 * - store flags in "cmdmod".
2624 * - Set ex_pressedreturn for an empty command line.
2625 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002626 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 * - set p_verbose for ":verbose"
2628 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002629 * When "skip_only" is TRUE the global variables are not changed, except for
2630 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002631 * Return FAIL when the command is not to be executed.
2632 * May set "errormsg" to an error message.
2633 */
2634 int
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002635parse_command_modifiers(exarg_T *eap, char_u **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002636{
2637 char_u *p;
2638
2639 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2640 eap->verbose_save = -1;
2641 eap->save_msg_silent = -1;
2642
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002643 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 for (;;)
2645 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2647 ++eap->cmd;
2648
2649 /* in ex mode, an empty line works like :+ */
2650 if (*eap->cmd == NUL && exmode_active
2651 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2652 || getline_equal(eap->getline, eap->cookie, getexline))
2653 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2654 {
2655 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002656 if (!skip_only)
2657 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002658 }
2659
2660 /* ignore comment and empty lines */
2661 if (*eap->cmd == '"')
2662 return FAIL;
2663 if (*eap->cmd == NUL)
2664 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002665 if (!skip_only)
2666 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 return FAIL;
2668 }
2669
Bram Moolenaareffed932018-08-14 13:38:17 +02002670 p = skip_range(eap->cmd, NULL);
2671 switch (*p)
2672 {
2673 /* When adding an entry, also modify cmd_exists(). */
2674 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2675 break;
2676 cmdmod.split |= WSP_ABOVE;
2677 continue;
2678
2679 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2680 {
2681 cmdmod.split |= WSP_BELOW;
2682 continue;
2683 }
2684 if (checkforcmd(&eap->cmd, "browse", 3))
2685 {
2686#ifdef FEAT_BROWSE_CMD
2687 cmdmod.browse = TRUE;
2688#endif
2689 continue;
2690 }
2691 if (!checkforcmd(&eap->cmd, "botright", 2))
2692 break;
2693 cmdmod.split |= WSP_BOT;
2694 continue;
2695
2696 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2697 break;
2698#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2699 cmdmod.confirm = TRUE;
2700#endif
2701 continue;
2702
2703 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2704 {
2705 cmdmod.keepmarks = TRUE;
2706 continue;
2707 }
2708 if (checkforcmd(&eap->cmd, "keepalt", 5))
2709 {
2710 cmdmod.keepalt = TRUE;
2711 continue;
2712 }
2713 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2714 {
2715 cmdmod.keeppatterns = TRUE;
2716 continue;
2717 }
2718 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2719 break;
2720 cmdmod.keepjumps = TRUE;
2721 continue;
2722
2723 case 'f': /* only accept ":filter {pat} cmd" */
2724 {
2725 char_u *reg_pat;
2726
2727 if (!checkforcmd(&p, "filter", 4)
2728 || *p == NUL || ends_excmd(*p))
2729 break;
2730 if (*p == '!')
2731 {
2732 cmdmod.filter_force = TRUE;
2733 p = skipwhite(p + 1);
2734 if (*p == NUL || ends_excmd(*p))
2735 break;
2736 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002737 if (skip_only)
2738 p = skip_vimgrep_pat(p, NULL, NULL);
2739 else
2740 // NOTE: This puts a NUL after the pattern.
2741 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002742 if (p == NULL || *p == NUL)
2743 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002744 if (!skip_only)
2745 {
2746 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002748 if (cmdmod.filter_regmatch.regprog == NULL)
2749 break;
2750 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 eap->cmd = p;
2752 continue;
2753 }
2754
2755 /* ":hide" and ":hide | cmd" are not modifiers */
2756 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2757 || *p == NUL || ends_excmd(*p))
2758 break;
2759 eap->cmd = p;
2760 cmdmod.hide = TRUE;
2761 continue;
2762
2763 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2764 {
2765 cmdmod.lockmarks = TRUE;
2766 continue;
2767 }
2768
2769 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2770 break;
2771 cmdmod.split |= WSP_ABOVE;
2772 continue;
2773
2774 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2775 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 {
2778 /* Set 'eventignore' to "all". Restore the
2779 * existing option value later. */
2780 cmdmod.save_ei = vim_strsave(p_ei);
2781 set_string_option_direct((char_u *)"ei", -1,
2782 (char_u *)"all", OPT_FREE, SID_NONE);
2783 }
2784 continue;
2785 }
2786 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2787 break;
2788 cmdmod.noswapfile = TRUE;
2789 continue;
2790
2791 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2792 break;
2793 cmdmod.split |= WSP_BELOW;
2794 continue;
2795
2796 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2797 {
2798#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (!skip_only)
2800 {
2801 if (!eap->did_sandbox)
2802 ++sandbox;
2803 eap->did_sandbox = TRUE;
2804 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002805#endif
2806 continue;
2807 }
2808 if (!checkforcmd(&eap->cmd, "silent", 3))
2809 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 if (!skip_only)
2811 {
2812 if (eap->save_msg_silent == -1)
2813 eap->save_msg_silent = msg_silent;
2814 ++msg_silent;
2815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2817 {
2818 /* ":silent!", but not "silent !cmd" */
2819 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 {
2822 ++emsg_silent;
2823 ++eap->did_esilent;
2824 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 }
2826 continue;
2827
2828 case 't': if (checkforcmd(&p, "tab", 3))
2829 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002830 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002832 long tabnr = get_address(eap, &eap->cmd,
2833 ADDR_TABS, eap->skip,
2834 skip_only, FALSE, 1);
2835 if (tabnr == MAXLNUM)
2836 cmdmod.tab = tabpage_index(curtab) + 1;
2837 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002839 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2840 {
2841 *errormsg = (char_u *)_(e_invrange);
2842 return FAIL;
2843 }
2844 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
2847 eap->cmd = p;
2848 continue;
2849 }
2850 if (!checkforcmd(&eap->cmd, "topleft", 2))
2851 break;
2852 cmdmod.split |= WSP_TOP;
2853 continue;
2854
2855 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2856 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002857 if (!skip_only)
2858 {
2859 if (eap->save_msg_silent == -1)
2860 eap->save_msg_silent = msg_silent;
2861 msg_silent = 0;
2862 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 continue;
2864
2865 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2866 {
2867 cmdmod.split |= WSP_VERT;
2868 continue;
2869 }
2870 if (!checkforcmd(&p, "verbose", 4))
2871 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002872 if (!skip_only)
2873 {
2874 if (eap->verbose_save < 0)
2875 eap->verbose_save = p_verbose;
2876 if (vim_isdigit(*eap->cmd))
2877 p_verbose = atoi((char *)eap->cmd);
2878 else
2879 p_verbose = 1;
2880 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 eap->cmd = p;
2882 continue;
2883 }
2884 break;
2885 }
2886
2887 return OK;
2888}
2889
2890/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002891 * Free contents of "cmdmod".
2892 */
2893 static void
2894free_cmdmod(void)
2895{
2896 if (cmdmod.save_ei != NULL)
2897 {
2898 /* Restore 'eventignore' to the value before ":noautocmd". */
2899 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2900 OPT_FREE, SID_NONE);
2901 free_string_option(cmdmod.save_ei);
2902 }
2903
2904 if (cmdmod.filter_regmatch.regprog != NULL)
2905 vim_regfree(cmdmod.filter_regmatch.regprog);
2906}
2907
2908/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002910 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002911 * Return FAIL and set "errormsg" or return OK.
2912 */
2913 int
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002914parse_cmd_address(exarg_T *eap, char_u **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915{
2916 int address_count = 1;
2917 linenr_T lnum;
2918
2919 // Repeat for all ',' or ';' separated addresses.
2920 for (;;)
2921 {
2922 eap->line1 = eap->line2;
2923 switch (eap->addr_type)
2924 {
2925 case ADDR_LINES:
2926 // default is current line number
2927 eap->line2 = curwin->w_cursor.lnum;
2928 break;
2929 case ADDR_WINDOWS:
2930 eap->line2 = CURRENT_WIN_NR;
2931 break;
2932 case ADDR_ARGUMENTS:
2933 eap->line2 = curwin->w_arg_idx + 1;
2934 if (eap->line2 > ARGCOUNT)
2935 eap->line2 = ARGCOUNT;
2936 break;
2937 case ADDR_LOADED_BUFFERS:
2938 case ADDR_BUFFERS:
2939 eap->line2 = curbuf->b_fnum;
2940 break;
2941 case ADDR_TABS:
2942 eap->line2 = CURRENT_TAB_NR;
2943 break;
2944 case ADDR_TABS_RELATIVE:
2945 eap->line2 = 1;
2946 break;
2947#ifdef FEAT_QUICKFIX
2948 case ADDR_QUICKFIX:
2949 eap->line2 = qf_get_cur_valid_idx(eap);
2950 break;
2951#endif
2952 }
2953 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002954 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002955 eap->addr_count == 0, address_count++);
2956 if (eap->cmd == NULL) // error detected
2957 return FAIL;
2958 if (lnum == MAXLNUM)
2959 {
2960 if (*eap->cmd == '%') // '%' - all lines
2961 {
2962 ++eap->cmd;
2963 switch (eap->addr_type)
2964 {
2965 case ADDR_LINES:
2966 eap->line1 = 1;
2967 eap->line2 = curbuf->b_ml.ml_line_count;
2968 break;
2969 case ADDR_LOADED_BUFFERS:
2970 {
2971 buf_T *buf = firstbuf;
2972
2973 while (buf->b_next != NULL
2974 && buf->b_ml.ml_mfp == NULL)
2975 buf = buf->b_next;
2976 eap->line1 = buf->b_fnum;
2977 buf = lastbuf;
2978 while (buf->b_prev != NULL
2979 && buf->b_ml.ml_mfp == NULL)
2980 buf = buf->b_prev;
2981 eap->line2 = buf->b_fnum;
2982 break;
2983 }
2984 case ADDR_BUFFERS:
2985 eap->line1 = firstbuf->b_fnum;
2986 eap->line2 = lastbuf->b_fnum;
2987 break;
2988 case ADDR_WINDOWS:
2989 case ADDR_TABS:
2990 if (IS_USER_CMDIDX(eap->cmdidx))
2991 {
2992 eap->line1 = 1;
2993 eap->line2 = eap->addr_type == ADDR_WINDOWS
2994 ? LAST_WIN_NR : LAST_TAB_NR;
2995 }
2996 else
2997 {
2998 // there is no Vim command which uses '%' and
2999 // ADDR_WINDOWS or ADDR_TABS
3000 *errormsg = (char_u *)_(e_invrange);
3001 return FAIL;
3002 }
3003 break;
3004 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003005 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003006 *errormsg = (char_u *)_(e_invrange);
3007 return FAIL;
3008 case ADDR_ARGUMENTS:
3009 if (ARGCOUNT == 0)
3010 eap->line1 = eap->line2 = 0;
3011 else
3012 {
3013 eap->line1 = 1;
3014 eap->line2 = ARGCOUNT;
3015 }
3016 break;
3017#ifdef FEAT_QUICKFIX
3018 case ADDR_QUICKFIX:
3019 eap->line1 = 1;
3020 eap->line2 = qf_get_size(eap);
3021 if (eap->line2 == 0)
3022 eap->line2 = 1;
3023 break;
3024#endif
3025 }
3026 ++eap->addr_count;
3027 }
3028 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3029 {
3030 pos_T *fp;
3031
3032 // '*' - visual area
3033 if (eap->addr_type != ADDR_LINES)
3034 {
3035 *errormsg = (char_u *)_(e_invrange);
3036 return FAIL;
3037 }
3038
3039 ++eap->cmd;
3040 if (!eap->skip)
3041 {
3042 fp = getmark('<', FALSE);
3043 if (check_mark(fp) == FAIL)
3044 return FAIL;
3045 eap->line1 = fp->lnum;
3046 fp = getmark('>', FALSE);
3047 if (check_mark(fp) == FAIL)
3048 return FAIL;
3049 eap->line2 = fp->lnum;
3050 ++eap->addr_count;
3051 }
3052 }
3053 }
3054 else
3055 eap->line2 = lnum;
3056 eap->addr_count++;
3057
3058 if (*eap->cmd == ';')
3059 {
3060 if (!eap->skip)
3061 {
3062 curwin->w_cursor.lnum = eap->line2;
3063 // don't leave the cursor on an illegal line or column
3064 check_cursor();
3065 }
3066 }
3067 else if (*eap->cmd != ',')
3068 break;
3069 ++eap->cmd;
3070 }
3071
3072 // One address given: set start and end lines.
3073 if (eap->addr_count == 1)
3074 {
3075 eap->line1 = eap->line2;
3076 // ... but only implicit: really no address given
3077 if (lnum == MAXLNUM)
3078 eap->addr_count = 0;
3079 }
3080 return OK;
3081}
3082
3083/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003084 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * If there is a match advance "pp" to the argument and return TRUE.
3086 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003087 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088checkforcmd(
3089 char_u **pp, /* start of command */
3090 char *cmd, /* name of command */
3091 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 int i;
3094
3095 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003096 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 break;
3098 if (i >= len && !isalpha((*pp)[i]))
3099 {
3100 *pp = skipwhite(*pp + i);
3101 return TRUE;
3102 }
3103 return FALSE;
3104}
3105
3106/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003107 * Append "cmd" to the error message in IObuff.
3108 * Takes care of limiting the length and handling 0xa0, which would be
3109 * invisible otherwise.
3110 */
3111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003112append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003113{
3114 char_u *s = cmd;
3115 char_u *d;
3116
3117 STRCAT(IObuff, ": ");
3118 d = IObuff + STRLEN(IObuff);
3119 while (*s != NUL && d - IObuff < IOSIZE - 7)
3120 {
3121 if (
3122#ifdef FEAT_MBYTE
3123 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3124#endif
3125 *s == 0xa0)
3126 {
3127 s +=
3128#ifdef FEAT_MBYTE
3129 enc_utf8 ? 2 :
3130#endif
3131 1;
3132 STRCPY(d, "<a0>");
3133 d += 4;
3134 }
3135 else
3136 MB_COPY_CHAR(s, d);
3137 }
3138 *d = NUL;
3139}
3140
3141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003143 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003145 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 * Returns NULL for an ambiguous user command.
3147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003149find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 int len;
3152 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003153 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 /*
3156 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003157 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 * - the 'k' command can directly be followed by any character.
3159 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003160 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003162 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 */
3164 p = eap->cmd;
3165 if (*p == 'k')
3166 {
3167 eap->cmdidx = CMD_k;
3168 ++p;
3169 }
3170 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003171 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3172 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 || p[1] == 'g'
3174 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3175 || p[1] == 'I'
3176 || (p[1] == 'r' && p[2] != 'e')))
3177 {
3178 eap->cmdidx = CMD_substitute;
3179 ++p;
3180 }
3181 else
3182 {
3183 while (ASCII_ISALPHA(*p))
3184 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003185 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003186 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003187 while (ASCII_ISALNUM(*p))
3188 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 /* check for non-alpha command */
3191 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3192 ++p;
3193 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003194 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3195 {
3196 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3197 * :delete with the 'l' flag. Same for 'p'. */
3198 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003199 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003200 break;
3201 if (i == len - 1)
3202 {
3203 --len;
3204 if (p[-1] == 'l')
3205 eap->flags |= EXFLAG_LIST;
3206 else
3207 eap->flags |= EXFLAG_PRINT;
3208 }
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003211 if (ASCII_ISLOWER(eap->cmd[0]))
3212 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003213 int c1 = eap->cmd[0];
3214 int c2 = eap->cmd[1];
3215
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003216 if (command_count != (int)CMD_SIZE)
3217 {
3218 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3219 getout(1);
3220 }
3221
3222 /* Use a precomputed index for fast look-up in cmdnames[]
3223 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003224 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3225 if (ASCII_ISLOWER(c2))
3226 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003229 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3232 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3233 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3234 (size_t)len) == 0)
3235 {
3236#ifdef FEAT_EVAL
3237 if (full != NULL
3238 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3239 *full = TRUE;
3240#endif
3241 break;
3242 }
3243
3244#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003245 /* Look for a user defined command as a last resort. Let ":Print" be
3246 * overruled by a user defined command. */
3247 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3248 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 while (ASCII_ISALNUM(*p))
3252 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 }
3255#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 eap->cmdidx = CMD_SIZE;
3258 }
3259
3260 return p;
3261}
3262
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263#ifdef FEAT_USR_CMDS
3264/*
3265 * Search for a user command that matches "eap->cmd".
3266 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3267 * Return a pointer to just after the command.
3268 * Return NULL if there is no matching command.
3269 */
3270 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271find_ucmd(
3272 exarg_T *eap,
3273 char_u *p, /* end of the command (possibly including count) */
3274 int *full, /* set to TRUE for a full match */
3275 expand_T *xp, /* used for completion, NULL otherwise */
3276 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277{
3278 int len = (int)(p - eap->cmd);
3279 int j, k, matchlen = 0;
3280 ucmd_T *uc;
3281 int found = FALSE;
3282 int possible = FALSE;
3283 char_u *cp, *np; /* Point into typed cmd and test name */
3284 garray_T *gap;
3285 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3286 only full match global is accepted. */
3287
3288 /*
3289 * Look for buffer-local user commands first, then global ones.
3290 */
3291 gap = &curbuf->b_ucmds;
3292 for (;;)
3293 {
3294 for (j = 0; j < gap->ga_len; ++j)
3295 {
3296 uc = USER_CMD_GA(gap, j);
3297 cp = eap->cmd;
3298 np = uc->uc_name;
3299 k = 0;
3300 while (k < len && *np != NUL && *cp++ == *np++)
3301 k++;
3302 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3303 {
3304 /* If finding a second match, the command is ambiguous. But
3305 * not if a buffer-local command wasn't a full match and a
3306 * global command is a full match. */
3307 if (k == len && found && *np != NUL)
3308 {
3309 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003310 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003311 amb_local = TRUE;
3312 }
3313
3314 if (!found || (k == len && *np == NUL))
3315 {
3316 /* If we matched up to a digit, then there could
3317 * be another command including the digit that we
3318 * should use instead.
3319 */
3320 if (k == len)
3321 found = TRUE;
3322 else
3323 possible = TRUE;
3324
3325 if (gap == &ucmds)
3326 eap->cmdidx = CMD_USER;
3327 else
3328 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003329 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003331 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003332
3333# ifdef FEAT_CMDL_COMPL
3334 if (compl != NULL)
3335 *compl = uc->uc_compl;
3336# ifdef FEAT_EVAL
3337 if (xp != NULL)
3338 {
3339 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003340 xp->xp_script_ctx = uc->uc_script_ctx;
3341 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 }
3343# endif
3344# endif
3345 /* Do not search for further abbreviations
3346 * if this is an exact match. */
3347 matchlen = k;
3348 if (k == len && *np == NUL)
3349 {
3350 if (full != NULL)
3351 *full = TRUE;
3352 amb_local = FALSE;
3353 break;
3354 }
3355 }
3356 }
3357 }
3358
3359 /* Stop if we found a full match or searched all. */
3360 if (j < gap->ga_len || gap == &ucmds)
3361 break;
3362 gap = &ucmds;
3363 }
3364
3365 /* Only found ambiguous matches. */
3366 if (amb_local)
3367 {
3368 if (xp != NULL)
3369 xp->xp_context = EXPAND_UNSUCCESSFUL;
3370 return NULL;
3371 }
3372
3373 /* The match we found may be followed immediately by a number. Move "p"
3374 * back to point to it. */
3375 if (found || possible)
3376 return p + (matchlen - len);
3377 return p;
3378}
3379#endif
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003382static struct cmdmod
3383{
3384 char *name;
3385 int minlen;
3386 int has_count; /* :123verbose :3tab */
3387} cmdmods[] = {
3388 {"aboveleft", 3, FALSE},
3389 {"belowright", 3, FALSE},
3390 {"botright", 2, FALSE},
3391 {"browse", 3, FALSE},
3392 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003393 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003394 {"hide", 3, FALSE},
3395 {"keepalt", 5, FALSE},
3396 {"keepjumps", 5, FALSE},
3397 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003398 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003399 {"leftabove", 5, FALSE},
3400 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003401 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003402 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 {"rightbelow", 6, FALSE},
3404 {"sandbox", 3, FALSE},
3405 {"silent", 3, FALSE},
3406 {"tab", 3, TRUE},
3407 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003408 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003409 {"verbose", 4, TRUE},
3410 {"vertical", 4, FALSE},
3411};
3412
3413/*
3414 * Return length of a command modifier (including optional count).
3415 * Return zero when it's not a modifier.
3416 */
3417 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003418modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003419{
3420 int i, j;
3421 char_u *p = cmd;
3422
3423 if (VIM_ISDIGIT(*cmd))
3424 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003425 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003426 {
3427 for (j = 0; p[j] != NUL; ++j)
3428 if (p[j] != cmdmods[i].name[j])
3429 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003430 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003431 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003432 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003433 }
3434 return 0;
3435}
3436
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437/*
3438 * Return > 0 if an Ex command "name" exists.
3439 * Return 2 if there is an exact match.
3440 * Return 3 if there is an ambiguous match.
3441 */
3442 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003443cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 exarg_T ea;
3446 int full = FALSE;
3447 int i;
3448 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003449 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003452 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 for (j = 0; name[j] != NUL; ++j)
3455 if (name[j] != cmdmods[i].name[j])
3456 break;
3457 if (name[j] == NUL && j >= cmdmods[i].minlen)
3458 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3459 }
3460
Bram Moolenaara9587612006-05-04 21:47:50 +00003461 /* Check built-in commands and user defined commands.
3462 * For ":2match" and ":3match" we need to skip the number. */
3463 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003465 p = find_command(&ea, &full);
3466 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003468 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3469 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003470 if (*skipwhite(p) != NUL)
3471 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3473}
3474#endif
3475
3476/*
3477 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3478 * we don't need/want deleted. Maybe this could be done better if we didn't
3479 * repeat all this stuff. The only problem is that they may not stay
3480 * perfectly compatible with each other, but then the command line syntax
3481 * probably won't change that much -- webb.
3482 */
3483 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003484set_one_cmd_context(
3485 expand_T *xp,
3486 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 char_u *p;
3489 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 int len = 0;
3491 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3493 int compl = EXPAND_NOTHING;
3494#endif
3495#ifdef FEAT_CMDL_COMPL
3496 int delim;
3497#endif
3498 int forceit = FALSE;
3499 int usefilter = FALSE; /* filter instead of file name */
3500
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003501 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 xp->xp_pattern = buff;
3503 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003507 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
3509 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3510 ;
3511 xp->xp_pattern = cmd;
3512
3513 if (*cmd == NUL)
3514 return NULL;
3515 if (*cmd == '"') /* ignore comment lines */
3516 {
3517 xp->xp_context = EXPAND_NOTHING;
3518 return NULL;
3519 }
3520
3521/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003522 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 */
3524 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 xp->xp_pattern = cmd;
3526 if (*cmd == NUL)
3527 return NULL;
3528 if (*cmd == '"')
3529 {
3530 xp->xp_context = EXPAND_NOTHING;
3531 return NULL;
3532 }
3533
3534 if (*cmd == '|' || *cmd == '\n')
3535 return cmd + 1; /* There's another command */
3536
3537 /*
3538 * Isolate the command and search for it in the command table.
3539 * Exceptions:
3540 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003541 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3543 */
3544 if (*cmd == 'k' && cmd[1] != 'e')
3545 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 p = cmd + 1;
3548 }
3549 else
3550 {
3551 p = cmd;
3552 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3553 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003554 /* a user command may contain digits */
3555 if (ASCII_ISUPPER(cmd[0]))
3556 while (ASCII_ISALNUM(*p) || *p == '*')
3557 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003558 /* for python 3.x: ":py3*" commands completion */
3559 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003560 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003561 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003562 while (ASCII_ISALPHA(*p) || *p == '*')
3563 ++p;
3564 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003565 /* check for non-alpha command */
3566 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3567 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 xp->xp_context = EXPAND_UNSUCCESSFUL;
3573 return NULL;
3574 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003576 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3577 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3578 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 break;
3580
3581#ifdef FEAT_USR_CMDS
3582 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3584 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#endif
3586 }
3587
3588 /*
3589 * If the cursor is touching the command, and it ends in an alpha-numeric
3590 * character, complete the command name.
3591 */
3592 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3593 return NULL;
3594
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3598 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 p = cmd + 1;
3601 }
3602#ifdef FEAT_USR_CMDS
3603 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3604 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 ea.cmd = cmd;
3606 p = find_ucmd(&ea, p, NULL, xp,
3607# if defined(FEAT_CMDL_COMPL)
3608 &compl
3609# else
3610 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003613 if (p == NULL)
3614 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616#endif
3617 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
3620 /* Not still touching the command and it was an illegal one */
3621 xp->xp_context = EXPAND_UNSUCCESSFUL;
3622 return NULL;
3623 }
3624
3625 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3626
3627 if (*p == '!') /* forced commands */
3628 {
3629 forceit = TRUE;
3630 ++p;
3631 }
3632
3633/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003634 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003636 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003637 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 arg = skipwhite(p);
3640
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003641 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 if (*arg == '>') /* append */
3644 {
3645 if (*++arg == '>')
3646 ++arg;
3647 arg = skipwhite(arg);
3648 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 ++arg;
3652 usefilter = TRUE;
3653 }
3654 }
3655
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003656 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 usefilter = forceit; /* :r! filter if forced */
3659 if (*arg == '!') /* :r !filter */
3660 {
3661 ++arg;
3662 usefilter = TRUE;
3663 }
3664 }
3665
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 while (*arg == *cmd) /* allow any number of '>' or '<' */
3669 ++arg;
3670 arg = skipwhite(arg);
3671 }
3672
3673 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003674 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676 /* Check if we're in the +command */
3677 p = arg + 1;
3678 arg = skip_cmd_arg(arg, FALSE);
3679
3680 /* Still touching the command after '+'? */
3681 if (*arg == NUL)
3682 return p;
3683
3684 /* Skip space(s) after +command to get to the real argument */
3685 arg = skipwhite(arg);
3686 }
3687
3688 /*
3689 * Check for '|' to separate commands and '"' to start comments.
3690 * Don't do this for ":read !cmd" and ":write !cmd".
3691 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003692 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 p = arg;
3695 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003696 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 p += 2;
3698 while (*p)
3699 {
3700 if (*p == Ctrl_V)
3701 {
3702 if (p[1] != NUL)
3703 ++p;
3704 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003705 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 || *p == '|' || *p == '\n')
3707 {
3708 if (*(p - 1) != '\\')
3709 {
3710 if (*p == '|' || *p == '\n')
3711 return p + 1;
3712 return NULL; /* It's a comment */
3713 }
3714 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003715 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 }
3718
3719 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003720 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 vim_strchr((char_u *)"|\"", *arg) == NULL)
3722 return NULL;
3723
3724 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003725 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003727 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003728 while (*p && p < buff + len)
3729 {
3730 if (*p == ' ' || *p == TAB)
3731 {
3732 /* argument starts after a space */
3733 xp->xp_pattern = ++p;
3734 }
3735 else
3736 {
3737 if (*p == '\\' && *(p + 1) != NUL)
3738 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003739 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003740 }
3741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003743 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003745 int c;
3746 int in_quote = FALSE;
3747 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
3749 /*
3750 * Allow spaces within back-quotes to count as part of the argument
3751 * being expanded.
3752 */
3753 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003754 p = xp->xp_pattern;
3755 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003757#ifdef FEAT_MBYTE
3758 if (has_mbyte)
3759 c = mb_ptr2char(p);
3760 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003762 c = *p;
3763 if (c == '\\' && p[1] != NUL)
3764 ++p;
3765 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
3767 if (!in_quote)
3768 {
3769 xp->xp_pattern = p;
3770 bow = p + 1;
3771 }
3772 in_quote = !in_quote;
3773 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003774 /* An argument can contain just about everything, except
3775 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003776 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003777#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003778 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003779#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003780 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003781 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003782 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003783 while (*p != NUL)
3784 {
3785#ifdef FEAT_MBYTE
3786 if (has_mbyte)
3787 c = mb_ptr2char(p);
3788 else
3789#endif
3790 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003791 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003792 break;
3793#ifdef FEAT_MBYTE
3794 if (has_mbyte)
3795 len = (*mb_ptr2len)(p);
3796 else
3797#endif
3798 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003799 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003800 }
3801 if (in_quote)
3802 bow = p;
3803 else
3804 xp->xp_pattern = p;
3805 p -= len;
3806 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003807 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809
3810 /*
3811 * If we are still inside the quotes, and we passed a space, just
3812 * expand from there.
3813 */
3814 if (bow != NULL && in_quote)
3815 xp->xp_pattern = bow;
3816 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003817
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003818 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003819 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003820 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003821#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003822 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003823#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003824 /* When still after the command name expand executables. */
3825 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003826 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 /* Check for environment variable */
3830 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003831#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 || *xp->xp_pattern == '%'
3833#endif
3834 )
3835 {
3836 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3837 if (!vim_isIDc(*p))
3838 break;
3839 if (*p == NUL)
3840 {
3841 xp->xp_context = EXPAND_ENV_VARS;
3842 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003843#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3844 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003845 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003846 compl = EXPAND_ENV_VARS;
3847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003850#if defined(FEAT_CMDL_COMPL)
3851 /* Check for user names */
3852 if (*xp->xp_pattern == '~')
3853 {
3854 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3855 ;
3856 /* Complete ~user only if it partially matches a user name.
3857 * A full match ~user<Tab> will be replaced by user's home
3858 * directory i.e. something like ~user<Tab> -> /home/user/ */
3859 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003860 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003861 {
3862 xp->xp_context = EXPAND_USER;
3863 ++xp->xp_pattern;
3864 }
3865 }
3866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868
3869/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003870 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003872 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003874 case CMD_find:
3875 case CMD_sfind:
3876 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003877 if (xp->xp_context == EXPAND_FILES)
3878 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003879 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_cd:
3881 case CMD_chdir:
3882 case CMD_lcd:
3883 case CMD_lchdir:
3884 if (xp->xp_context == EXPAND_FILES)
3885 xp->xp_context = EXPAND_DIRECTORIES;
3886 break;
3887 case CMD_help:
3888 xp->xp_context = EXPAND_HELP;
3889 xp->xp_pattern = arg;
3890 break;
3891
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003892 /* Command modifiers: return the argument.
3893 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003895 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_belowright:
3897 case CMD_botright:
3898 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003899 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003900 case CMD_cdo:
3901 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003903 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 case CMD_folddoclosed:
3905 case CMD_folddoopen:
3906 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003907 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_keepjumps:
3909 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003910 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003911 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003913 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003915 case CMD_noautocmd:
3916 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003918 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003920 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003921 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 case CMD_topleft:
3923 case CMD_verbose:
3924 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003925 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return arg;
3927
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003928 case CMD_filter:
3929 if (*arg != NUL)
3930 arg = skip_vimgrep_pat(arg, NULL, NULL);
3931 if (arg == NULL || *arg == NUL)
3932 {
3933 xp->xp_context = EXPAND_NOTHING;
3934 return NULL;
3935 }
3936 return skipwhite(arg);
3937
Bram Moolenaar4f688582007-07-24 12:34:30 +00003938#ifdef FEAT_CMDL_COMPL
3939# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 case CMD_match:
3941 if (*arg == NUL || !ends_excmd(*arg))
3942 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003943 /* also complete "None" */
3944 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 arg = skipwhite(skiptowhite(arg));
3946 if (*arg != NUL)
3947 {
3948 xp->xp_context = EXPAND_NOTHING;
3949 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3950 }
3951 }
3952 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955/*
3956 * All completion for the +cmdline_compl feature goes here.
3957 */
3958
3959# ifdef FEAT_USR_CMDS
3960 case CMD_command:
3961 /* Check for attributes */
3962 while (*arg == '-')
3963 {
3964 arg++; /* Skip "-" */
3965 p = skiptowhite(arg);
3966 if (*p == NUL)
3967 {
3968 /* Cursor is still in the attribute */
3969 p = vim_strchr(arg, '=');
3970 if (p == NULL)
3971 {
3972 /* No "=", so complete attribute names */
3973 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3974 xp->xp_pattern = arg;
3975 return NULL;
3976 }
3977
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003978 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 * their arguments as well.
3980 */
3981 if (STRNICMP(arg, "complete", p - arg) == 0)
3982 {
3983 xp->xp_context = EXPAND_USER_COMPLETE;
3984 xp->xp_pattern = p + 1;
3985 return NULL;
3986 }
3987 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3988 {
3989 xp->xp_context = EXPAND_USER_NARGS;
3990 xp->xp_pattern = p + 1;
3991 return NULL;
3992 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003993 else if (STRNICMP(arg, "addr", p - arg) == 0)
3994 {
3995 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3996 xp->xp_pattern = p + 1;
3997 return NULL;
3998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return NULL;
4000 }
4001 arg = skipwhite(p);
4002 }
4003
4004 /* After the attributes comes the new command name */
4005 p = skiptowhite(arg);
4006 if (*p == NUL)
4007 {
4008 xp->xp_context = EXPAND_USER_COMMANDS;
4009 xp->xp_pattern = arg;
4010 break;
4011 }
4012
4013 /* And finally comes a normal command */
4014 return skipwhite(p);
4015
4016 case CMD_delcommand:
4017 xp->xp_context = EXPAND_USER_COMMANDS;
4018 xp->xp_pattern = arg;
4019 break;
4020# endif
4021
4022 case CMD_global:
4023 case CMD_vglobal:
4024 delim = *arg; /* get the delimiter */
4025 if (delim)
4026 ++arg; /* skip delimiter if there is one */
4027
4028 while (arg[0] != NUL && arg[0] != delim)
4029 {
4030 if (arg[0] == '\\' && arg[1] != NUL)
4031 ++arg;
4032 ++arg;
4033 }
4034 if (arg[0] != NUL)
4035 return arg + 1;
4036 break;
4037 case CMD_and:
4038 case CMD_substitute:
4039 delim = *arg;
4040 if (delim)
4041 {
4042 /* skip "from" part */
4043 ++arg;
4044 arg = skip_regexp(arg, delim, p_magic, NULL);
4045 }
4046 /* skip "to" part */
4047 while (arg[0] != NUL && arg[0] != delim)
4048 {
4049 if (arg[0] == '\\' && arg[1] != NUL)
4050 ++arg;
4051 ++arg;
4052 }
4053 if (arg[0] != NUL) /* skip delimiter */
4054 ++arg;
4055 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4056 ++arg;
4057 if (arg[0] != NUL)
4058 return arg;
4059 break;
4060 case CMD_isearch:
4061 case CMD_dsearch:
4062 case CMD_ilist:
4063 case CMD_dlist:
4064 case CMD_ijump:
4065 case CMD_psearch:
4066 case CMD_djump:
4067 case CMD_isplit:
4068 case CMD_dsplit:
4069 arg = skipwhite(skipdigits(arg)); /* skip count */
4070 if (*arg == '/') /* Match regexp, not just whole words */
4071 {
4072 for (++arg; *arg && *arg != '/'; arg++)
4073 if (*arg == '\\' && arg[1] != NUL)
4074 arg++;
4075 if (*arg)
4076 {
4077 arg = skipwhite(arg + 1);
4078
4079 /* Check for trailing illegal characters */
4080 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4081 xp->xp_context = EXPAND_NOTHING;
4082 else
4083 return arg;
4084 }
4085 }
4086 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 case CMD_autocmd:
4089 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004091 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 case CMD_set:
4094 set_context_in_set_cmd(xp, arg, 0);
4095 break;
4096 case CMD_setglobal:
4097 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4098 break;
4099 case CMD_setlocal:
4100 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4101 break;
4102 case CMD_tag:
4103 case CMD_stag:
4104 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004105 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 case CMD_tselect:
4107 case CMD_stselect:
4108 case CMD_ptselect:
4109 case CMD_tjump:
4110 case CMD_stjump:
4111 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004112 if (*p_wop != NUL)
4113 xp->xp_context = EXPAND_TAGS_LISTFILES;
4114 else
4115 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 xp->xp_pattern = arg;
4117 break;
4118 case CMD_augroup:
4119 xp->xp_context = EXPAND_AUGROUP;
4120 xp->xp_pattern = arg;
4121 break;
4122#ifdef FEAT_SYN_HL
4123 case CMD_syntax:
4124 set_context_in_syntax_cmd(xp, arg);
4125 break;
4126#endif
4127#ifdef FEAT_EVAL
4128 case CMD_let:
4129 case CMD_if:
4130 case CMD_elseif:
4131 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004132 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 case CMD_echo:
4134 case CMD_echon:
4135 case CMD_execute:
4136 case CMD_echomsg:
4137 case CMD_echoerr:
4138 case CMD_call:
4139 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004140 case CMD_cexpr:
4141 case CMD_caddexpr:
4142 case CMD_cgetexpr:
4143 case CMD_lexpr:
4144 case CMD_laddexpr:
4145 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004146 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 break;
4148
4149 case CMD_unlet:
4150 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4151 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 xp->xp_context = EXPAND_USER_VARS;
4154 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004155
4156 if (*xp->xp_pattern == '$')
4157 {
4158 xp->xp_context = EXPAND_ENV_VARS;
4159 ++xp->xp_pattern;
4160 }
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 break;
4163
4164 case CMD_function:
4165 case CMD_delfunction:
4166 xp->xp_context = EXPAND_USER_FUNC;
4167 xp->xp_pattern = arg;
4168 break;
4169
4170 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004171 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 break;
4173#endif
4174 case CMD_highlight:
4175 set_context_in_highlight_cmd(xp, arg);
4176 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004177#ifdef FEAT_CSCOPE
4178 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004179 case CMD_lcscope:
4180 case CMD_scscope:
4181 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004182 break;
4183#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004184#ifdef FEAT_SIGNS
4185 case CMD_sign:
4186 set_context_in_sign_cmd(xp, arg);
4187 break;
4188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 case CMD_bdelete:
4190 case CMD_bwipeout:
4191 case CMD_bunload:
4192 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4193 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004194 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 case CMD_buffer:
4196 case CMD_sbuffer:
4197 case CMD_checktime:
4198 xp->xp_context = EXPAND_BUFFERS;
4199 xp->xp_pattern = arg;
4200 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#ifdef FEAT_USR_CMDS
4202 case CMD_USER:
4203 case CMD_USER_BUF:
4204 if (compl != EXPAND_NOTHING)
4205 {
4206 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004207 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
4209# ifdef FEAT_MENU
4210 if (compl == EXPAND_MENUS)
4211 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4212# endif
4213 if (compl == EXPAND_COMMANDS)
4214 return arg;
4215 if (compl == EXPAND_MAPPINGS)
4216 return set_context_in_map_cmd(xp, (char_u *)"map",
4217 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004218 /* Find start of last argument. */
4219 p = arg;
4220 while (*p)
4221 {
4222 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004223 /* argument starts after a space */
4224 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004225 else if (*p == '\\' && *(p + 1) != NUL)
4226 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004227 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 xp->xp_pattern = arg;
4230 }
4231 xp->xp_context = compl;
4232 }
4233 break;
4234#endif
4235 case CMD_map: case CMD_noremap:
4236 case CMD_nmap: case CMD_nnoremap:
4237 case CMD_vmap: case CMD_vnoremap:
4238 case CMD_omap: case CMD_onoremap:
4239 case CMD_imap: case CMD_inoremap:
4240 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004241 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004242 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004243 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004244 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004246 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 case CMD_unmap:
4248 case CMD_nunmap:
4249 case CMD_vunmap:
4250 case CMD_ounmap:
4251 case CMD_iunmap:
4252 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004253 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004254 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004255 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004256 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004258 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004259 case CMD_mapclear:
4260 case CMD_nmapclear:
4261 case CMD_vmapclear:
4262 case CMD_omapclear:
4263 case CMD_imapclear:
4264 case CMD_cmapclear:
4265 case CMD_lmapclear:
4266 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004267 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004268 case CMD_xmapclear:
4269 xp->xp_context = EXPAND_MAPCLEAR;
4270 xp->xp_pattern = arg;
4271 break;
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 case CMD_abbreviate: case CMD_noreabbrev:
4274 case CMD_cabbrev: case CMD_cnoreabbrev:
4275 case CMD_iabbrev: case CMD_inoreabbrev:
4276 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004277 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 case CMD_unabbreviate:
4279 case CMD_cunabbrev:
4280 case CMD_iunabbrev:
4281 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004282 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#ifdef FEAT_MENU
4284 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4285 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4286 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4287 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4288 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4289 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4290 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004291 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 case CMD_tmenu: case CMD_tunmenu:
4293 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4294 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4295#endif
4296
4297 case CMD_colorscheme:
4298 xp->xp_context = EXPAND_COLORS;
4299 xp->xp_pattern = arg;
4300 break;
4301
4302 case CMD_compiler:
4303 xp->xp_context = EXPAND_COMPILER;
4304 xp->xp_pattern = arg;
4305 break;
4306
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004307 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004308 xp->xp_context = EXPAND_OWNSYNTAX;
4309 xp->xp_pattern = arg;
4310 break;
4311
4312 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004313 xp->xp_context = EXPAND_FILETYPE;
4314 xp->xp_pattern = arg;
4315 break;
4316
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004317 case CMD_packadd:
4318 xp->xp_context = EXPAND_PACKADD;
4319 xp->xp_pattern = arg;
4320 break;
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4323 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4324 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004325 p = skiptowhite(arg);
4326 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
4328 xp->xp_context = EXPAND_LANGUAGE;
4329 xp->xp_pattern = arg;
4330 }
4331 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004332 {
4333 if ( STRNCMP(arg, "messages", p - arg) == 0
4334 || STRNCMP(arg, "ctype", p - arg) == 0
4335 || STRNCMP(arg, "time", p - arg) == 0)
4336 {
4337 xp->xp_context = EXPAND_LOCALES;
4338 xp->xp_pattern = skipwhite(p);
4339 }
4340 else
4341 xp->xp_context = EXPAND_NOTHING;
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 break;
4344#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004345#if defined(FEAT_PROFILE)
4346 case CMD_profile:
4347 set_context_in_profile_cmd(xp, arg);
4348 break;
4349#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004350 case CMD_behave:
4351 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004352 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004355 case CMD_messages:
4356 xp->xp_context = EXPAND_MESSAGES;
4357 xp->xp_pattern = arg;
4358 break;
4359
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004360#if defined(FEAT_CMDHIST)
4361 case CMD_history:
4362 xp->xp_context = EXPAND_HISTORY;
4363 xp->xp_pattern = arg;
4364 break;
4365#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004366#if defined(FEAT_PROFILE)
4367 case CMD_syntime:
4368 xp->xp_context = EXPAND_SYNTIME;
4369 xp->xp_pattern = arg;
4370 break;
4371#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004372
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004373 case CMD_argdelete:
4374 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4375 arg = xp->xp_pattern + 1;
4376 xp->xp_context = EXPAND_ARGLIST;
4377 xp->xp_pattern = arg;
4378 break;
4379
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380#endif /* FEAT_CMDL_COMPL */
4381
4382 default:
4383 break;
4384 }
4385 return NULL;
4386}
4387
4388/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004389 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 *
4391 * Backslashed delimiters after / or ? will be skipped, and commands will
4392 * not be expanded between /'s and ?'s or after "'".
4393 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004394 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 * Returns the "cmd" pointer advanced to beyond the range.
4396 */
4397 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004398skip_range(
4399 char_u *cmd,
4400 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004402 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004404 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004406 if (*cmd == '\\')
4407 {
4408 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4409 ++cmd;
4410 else
4411 break;
4412 }
4413 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
4415 if (*++cmd == NUL && ctx != NULL)
4416 *ctx = EXPAND_NOTHING;
4417 }
4418 else if (*cmd == '/' || *cmd == '?')
4419 {
4420 delim = *cmd++;
4421 while (*cmd != NUL && *cmd != delim)
4422 if (*cmd++ == '\\' && *cmd != NUL)
4423 ++cmd;
4424 if (*cmd == NUL && ctx != NULL)
4425 *ctx = EXPAND_NOTHING;
4426 }
4427 if (*cmd != NUL)
4428 ++cmd;
4429 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004430
4431 /* Skip ":" and white space. */
4432 while (*cmd == ':')
4433 cmd = skipwhite(cmd + 1);
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 return cmd;
4436}
4437
4438/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004439 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 *
4441 * Set ptr to the next character after the part that was interpreted.
4442 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004443 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 *
4445 * Return MAXLNUM when no Ex address was found.
4446 */
4447 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004448get_address(
4449 exarg_T *eap UNUSED,
4450 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004451 int addr_type, // flag: one of ADDR_LINES, ...
4452 int skip, // only skip the address, don't use it
4453 int silent, // no errors or side effects
4454 int to_other_file, // flag: may jump to other file
4455 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
4457 int c;
4458 int i;
4459 long n;
4460 char_u *cmd;
4461 pos_T pos;
4462 pos_T *fp;
4463 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004464 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 cmd = skipwhite(*ptr);
4467 lnum = MAXLNUM;
4468 do
4469 {
4470 switch (*cmd)
4471 {
4472 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004473 ++cmd;
4474 switch (addr_type)
4475 {
4476 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 lnum = curwin->w_cursor.lnum;
4478 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004480 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 break;
4482 case ADDR_ARGUMENTS:
4483 lnum = curwin->w_arg_idx + 1;
4484 break;
4485 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004486 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 lnum = curbuf->b_fnum;
4488 break;
4489 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004490 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004492 case ADDR_TABS_RELATIVE:
4493 EMSG(_(e_invrange));
4494 cmd = NULL;
4495 goto error;
4496 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004497#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004498 case ADDR_QUICKFIX:
4499 lnum = qf_get_cur_valid_idx(eap);
4500 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004501#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004502 }
4503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 ++cmd;
4507 switch (addr_type)
4508 {
4509 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 lnum = curbuf->b_ml.ml_line_count;
4511 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004513 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 break;
4515 case ADDR_ARGUMENTS:
4516 lnum = ARGCOUNT;
4517 break;
4518 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004519 buf = lastbuf;
4520 while (buf->b_ml.ml_mfp == NULL)
4521 {
4522 if (buf->b_prev == NULL)
4523 break;
4524 buf = buf->b_prev;
4525 }
4526 lnum = buf->b_fnum;
4527 break;
4528 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 lnum = lastbuf->b_fnum;
4530 break;
4531 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004532 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004534 case ADDR_TABS_RELATIVE:
4535 EMSG(_(e_invrange));
4536 cmd = NULL;
4537 goto error;
4538 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004539#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004540 case ADDR_QUICKFIX:
4541 lnum = qf_get_size(eap);
4542 if (lnum == 0)
4543 lnum = 1;
4544 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004545#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004546 }
4547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 if (*++cmd == NUL)
4551 {
4552 cmd = NULL;
4553 goto error;
4554 }
4555 if (addr_type != ADDR_LINES)
4556 {
4557 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004558 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 goto error;
4560 }
4561 if (skip)
4562 ++cmd;
4563 else
4564 {
4565 /* Only accept a mark in another file when it is
4566 * used by itself: ":'M". */
4567 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4568 ++cmd;
4569 if (fp == (pos_T *)-1)
4570 /* Jumped to another file. */
4571 lnum = curwin->w_cursor.lnum;
4572 else
4573 {
4574 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
4576 cmd = NULL;
4577 goto error;
4578 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 lnum = fp->lnum;
4580 }
4581 }
4582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
4584 case '/':
4585 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004586 c = *cmd++;
4587 if (addr_type != ADDR_LINES)
4588 {
4589 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004590 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 goto error;
4592 }
4593 if (skip) /* skip "/pat/" */
4594 {
4595 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4596 if (*cmd == c)
4597 ++cmd;
4598 }
4599 else
4600 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004601 int flags;
4602
4603 pos = curwin->w_cursor; // save curwin->w_cursor
4604
4605 // When '/' or '?' follows another address, start from
4606 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004607 if (lnum != MAXLNUM)
4608 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004609
4610 // Start a forward search at the end of the line (unless
4611 // before the first line).
4612 // Start a backward search at the start of the line.
4613 // This makes sure we never match in the current
4614 // line, and can match anywhere in the
4615 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004616 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 curwin->w_cursor.col = MAXCOL;
4618 else
4619 curwin->w_cursor.col = 0;
4620 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004621 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4622 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 {
4624 curwin->w_cursor = pos;
4625 cmd = NULL;
4626 goto error;
4627 }
4628 lnum = curwin->w_cursor.lnum;
4629 curwin->w_cursor = pos;
4630 /* adjust command string pointer */
4631 cmd += searchcmdlen;
4632 }
4633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004636 ++cmd;
4637 if (addr_type != ADDR_LINES)
4638 {
4639 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004640 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004641 goto error;
4642 }
4643 if (*cmd == '&')
4644 i = RE_SUBST;
4645 else if (*cmd == '?' || *cmd == '/')
4646 i = RE_SEARCH;
4647 else
4648 {
4649 EMSG(_(e_backslash));
4650 cmd = NULL;
4651 goto error;
4652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004654 if (!skip)
4655 {
4656 /*
4657 * When search follows another address, start from
4658 * there.
4659 */
4660 if (lnum != MAXLNUM)
4661 pos.lnum = lnum;
4662 else
4663 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004665 /*
4666 * Start the search just like for the above
4667 * do_search().
4668 */
4669 if (*cmd != '?')
4670 pos.col = MAXCOL;
4671 else
4672 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004673#ifdef FEAT_VIRTUALEDIT
4674 pos.coladd = 0;
4675#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004676 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004677 *cmd == '?' ? BACKWARD : FORWARD,
4678 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004679 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004680 lnum = pos.lnum;
4681 else
4682 {
4683 cmd = NULL;
4684 goto error;
4685 }
4686 }
4687 ++cmd;
4688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004691 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4692 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694
4695 for (;;)
4696 {
4697 cmd = skipwhite(cmd);
4698 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4699 break;
4700
4701 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004702 {
4703 switch (addr_type)
4704 {
4705 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004706 /* "+1" is same as ".+1" */
4707 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004708 break;
4709 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004710 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004711 break;
4712 case ADDR_ARGUMENTS:
4713 lnum = curwin->w_arg_idx + 1;
4714 break;
4715 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004716 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004717 lnum = curbuf->b_fnum;
4718 break;
4719 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004720 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004721 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004722 case ADDR_TABS_RELATIVE:
4723 lnum = 1;
4724 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004725#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004726 case ADDR_QUICKFIX:
4727 lnum = qf_get_cur_valid_idx(eap);
4728 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004729#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004730 }
4731 }
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 if (VIM_ISDIGIT(*cmd))
4734 i = '+'; /* "number" is same as "+number" */
4735 else
4736 i = *cmd++;
4737 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4738 n = 1;
4739 else
4740 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004741
4742 if (addr_type == ADDR_TABS_RELATIVE)
4743 {
4744 EMSG(_(e_invrange));
4745 cmd = NULL;
4746 goto error;
4747 }
4748 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004749 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004750 lnum = compute_buffer_local_count(
4751 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004753 {
4754#ifdef FEAT_FOLDING
4755 /* Relative line addressing, need to adjust for folded lines
4756 * now, but only do it after the first address. */
4757 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4758 && address_count >= 2)
4759 (void)hasFolding(lnum, NULL, &lnum);
4760#endif
4761 if (i == '-')
4762 lnum -= n;
4763 else
4764 lnum += n;
4765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767 } while (*cmd == '/' || *cmd == '?');
4768
4769error:
4770 *ptr = cmd;
4771 return lnum;
4772}
4773
4774/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004775 * Get flags from an Ex command argument.
4776 */
4777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004778get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004779{
4780 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4781 {
4782 if (*eap->arg == 'l')
4783 eap->flags |= EXFLAG_LIST;
4784 else if (*eap->arg == 'p')
4785 eap->flags |= EXFLAG_PRINT;
4786 else
4787 eap->flags |= EXFLAG_NR;
4788 eap->arg = skipwhite(eap->arg + 1);
4789 }
4790}
4791
4792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 * Function called for command which is Not Implemented. NI!
4794 */
4795 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004796ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 if (!eap->skip)
4799 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4800}
4801
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004802#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803/*
4804 * Function called for script command which is Not Implemented. NI!
4805 * Skips over ":perl <<EOF" constructs.
4806 */
4807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004808ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
4810 if (!eap->skip)
4811 ex_ni(eap);
4812 else
4813 vim_free(script_get(eap, eap->arg));
4814}
4815#endif
4816
4817/*
4818 * Check range in Ex command for validity.
4819 * Return NULL when valid, error message when invalid.
4820 */
4821 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004822invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004824 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if ( eap->line1 < 0
4826 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004827 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004829
4830 if (eap->argt & RANGE)
4831 {
4832 switch(eap->addr_type)
4833 {
4834 case ADDR_LINES:
4835 if (!(eap->argt & NOTADR)
4836 && eap->line2 > curbuf->b_ml.ml_line_count
4837#ifdef FEAT_DIFF
4838 + (eap->cmdidx == CMD_diffget)
4839#endif
4840 )
4841 return (char_u *)_(e_invrange);
4842 break;
4843 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004844 /* add 1 if ARGCOUNT is 0 */
4845 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004846 return (char_u *)_(e_invrange);
4847 break;
4848 case ADDR_BUFFERS:
4849 if (eap->line1 < firstbuf->b_fnum
4850 || eap->line2 > lastbuf->b_fnum)
4851 return (char_u *)_(e_invrange);
4852 break;
4853 case ADDR_LOADED_BUFFERS:
4854 buf = firstbuf;
4855 while (buf->b_ml.ml_mfp == NULL)
4856 {
4857 if (buf->b_next == NULL)
4858 return (char_u *)_(e_invrange);
4859 buf = buf->b_next;
4860 }
4861 if (eap->line1 < buf->b_fnum)
4862 return (char_u *)_(e_invrange);
4863 buf = lastbuf;
4864 while (buf->b_ml.ml_mfp == NULL)
4865 {
4866 if (buf->b_prev == NULL)
4867 return (char_u *)_(e_invrange);
4868 buf = buf->b_prev;
4869 }
4870 if (eap->line2 > buf->b_fnum)
4871 return (char_u *)_(e_invrange);
4872 break;
4873 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004874 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004875 return (char_u *)_(e_invrange);
4876 break;
4877 case ADDR_TABS:
4878 if (eap->line2 > LAST_TAB_NR)
4879 return (char_u *)_(e_invrange);
4880 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004881 case ADDR_TABS_RELATIVE:
4882 /* Do nothing */
4883 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004884#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004885 case ADDR_QUICKFIX:
4886 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4887 return (char_u *)_(e_invrange);
4888 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004889#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004890 }
4891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return NULL;
4893}
4894
4895/*
4896 * Correct the range for zero line number, if required.
4897 */
4898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004899correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900{
4901 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4902 {
4903 if (eap->line1 == 0)
4904 eap->line1 = 1;
4905 if (eap->line2 == 0)
4906 eap->line2 = 1;
4907 }
4908}
4909
Bram Moolenaar748bf032005-02-02 23:04:36 +00004910#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004911/*
4912 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4913 * pattern. Otherwise return eap->arg.
4914 */
4915 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004916skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004917{
4918 char_u *p = eap->arg;
4919
Bram Moolenaara37420f2006-02-04 22:37:47 +00004920 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4921 || eap->cmdidx == CMD_vimgrepadd
4922 || eap->cmdidx == CMD_lvimgrepadd
4923 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004924 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004925 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004926 if (p == NULL)
4927 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004928 }
4929 return p;
4930}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004931
4932/*
4933 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4934 * in the command line, so that things like % get expanded.
4935 */
4936 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004937replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004938{
4939 char_u *new_cmdline;
4940 char_u *program;
4941 char_u *pos;
4942 char_u *ptr;
4943 int len;
4944 int i;
4945
4946 /*
4947 * Don't do it when ":vimgrep" is used for ":grep".
4948 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004949 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4950 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4951 || eap->cmdidx == CMD_grepadd
4952 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004953 && !grep_internal(eap->cmdidx))
4954 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004955 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4956 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004957 {
4958 if (*curbuf->b_p_gp == NUL)
4959 program = p_gp;
4960 else
4961 program = curbuf->b_p_gp;
4962 }
4963 else
4964 {
4965 if (*curbuf->b_p_mp == NUL)
4966 program = p_mp;
4967 else
4968 program = curbuf->b_p_mp;
4969 }
4970
4971 p = skipwhite(p);
4972
4973 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4974 {
4975 /* replace $* by given arguments */
4976 i = 1;
4977 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4978 ++i;
4979 len = (int)STRLEN(p);
4980 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4981 if (new_cmdline == NULL)
4982 return NULL; /* out of memory */
4983 ptr = new_cmdline;
4984 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4985 {
4986 i = (int)(pos - program);
4987 STRNCPY(ptr, program, i);
4988 STRCPY(ptr += i, p);
4989 ptr += len;
4990 program = pos + 2;
4991 }
4992 STRCPY(ptr, program);
4993 }
4994 else
4995 {
4996 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4997 if (new_cmdline == NULL)
4998 return NULL; /* out of memory */
4999 STRCPY(new_cmdline, program);
5000 STRCAT(new_cmdline, " ");
5001 STRCAT(new_cmdline, p);
5002 }
5003 msg_make(p);
5004
5005 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
5006 vim_free(*cmdlinep);
5007 *cmdlinep = new_cmdline;
5008 p = new_cmdline;
5009 }
5010 return p;
5011}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005012#endif
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014/*
5015 * Expand file name in Ex command argument.
5016 * Return FAIL for failure, OK otherwise.
5017 */
5018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005019expand_filename(
5020 exarg_T *eap,
5021 char_u **cmdlinep,
5022 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 int has_wildcards; /* need to expand wildcards */
5025 char_u *repl;
5026 int srclen;
5027 char_u *p;
5028 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005029 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
Bram Moolenaar748bf032005-02-02 23:04:36 +00005031#ifdef FEAT_QUICKFIX
5032 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5033 p = skip_grep_pat(eap);
5034#else
5035 p = eap->arg;
5036#endif
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 /*
5039 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5040 * the file name contains a wildcard it should not cause expanding.
5041 * (it will be expanded anyway if there is a wildcard before replacing).
5042 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005043 has_wildcards = mch_has_wildcard(p);
5044 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005046#ifdef FEAT_EVAL
5047 /* Skip over `=expr`, wildcards in it are not expanded. */
5048 if (p[0] == '`' && p[1] == '=')
5049 {
5050 p += 2;
5051 (void)skip_expr(&p);
5052 if (*p == '`')
5053 ++p;
5054 continue;
5055 }
5056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 /*
5058 * Quick check if this cannot be the start of a special string.
5059 * Also removes backslash before '%', '#' and '<'.
5060 */
5061 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5062 {
5063 ++p;
5064 continue;
5065 }
5066
5067 /*
5068 * Try to find a match at this position.
5069 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005070 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5071 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (*errormsgp != NULL) /* error detected */
5073 return FAIL;
5074 if (repl == NULL) /* no match found */
5075 {
5076 p += srclen;
5077 continue;
5078 }
5079
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005080 /* Wildcards won't be expanded below, the replacement is taken
5081 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5082 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5083 {
5084 char_u *l = repl;
5085
5086 repl = expand_env_save(repl);
5087 vim_free(l);
5088 }
5089
Bram Moolenaar63b92542007-03-27 14:57:09 +00005090 /* Need to escape white space et al. with a backslash.
5091 * Don't do this for:
5092 * - replacement that already has been escaped: "##"
5093 * - shell commands (may have to use quotes instead).
5094 * - non-unix systems when there is a single argument (spaces don't
5095 * separate arguments then).
5096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005098 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 && eap->cmdidx != CMD_grep
5101 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005102 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005103 && eap->cmdidx != CMD_lgrep
5104 && eap->cmdidx != CMD_lgrepadd
5105 && eap->cmdidx != CMD_lmake
5106 && eap->cmdidx != CMD_make
5107 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108#ifndef UNIX
5109 && !(eap->argt & NOSPC)
5110#endif
5111 )
5112 {
5113 char_u *l;
5114#ifdef BACKSLASH_IN_FILENAME
5115 /* Don't escape a backslash here, because rem_backslash() doesn't
5116 * remove it later. */
5117 static char_u *nobslash = (char_u *)" \t\"|";
5118# define ESCAPE_CHARS nobslash
5119#else
5120# define ESCAPE_CHARS escape_chars
5121#endif
5122
5123 for (l = repl; *l; ++l)
5124 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5125 {
5126 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5127 if (l != NULL)
5128 {
5129 vim_free(repl);
5130 repl = l;
5131 }
5132 break;
5133 }
5134 }
5135
5136 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005137 if ((eap->usefilter || eap->cmdidx == CMD_bang
5138 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005139 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 char_u *l;
5142
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005143 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (l != NULL)
5145 {
5146 vim_free(repl);
5147 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149 }
5150
5151 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5152 vim_free(repl);
5153 if (p == NULL)
5154 return FAIL;
5155 }
5156
5157 /*
5158 * One file argument: Expand wildcards.
5159 * Don't do this with ":r !command" or ":w !command".
5160 */
5161 if ((eap->argt & NOSPC) && !eap->usefilter)
5162 {
5163 /*
5164 * May do this twice:
5165 * 1. Replace environment variables.
5166 * 2. Replace any other wildcards, remove backslashes.
5167 */
5168 for (n = 1; n <= 2; ++n)
5169 {
5170 if (n == 2)
5171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 /*
5173 * Halve the number of backslashes (this is Vi compatible).
5174 * For Unix and OS/2, when wildcards are expanded, this is
5175 * done by ExpandOne() below.
5176 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005177#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (!has_wildcards)
5179#endif
5180 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182
5183 if (has_wildcards)
5184 {
5185 if (n == 1)
5186 {
5187 /*
5188 * First loop: May expand environment variables. This
5189 * can be done much faster with expand_env() than with
5190 * something else (e.g., calling a shell).
5191 * After expanding environment variables, check again
5192 * if there are still wildcards present.
5193 */
5194 if (vim_strchr(eap->arg, '$') != NULL
5195 || vim_strchr(eap->arg, '~') != NULL)
5196 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005197 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005198 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 has_wildcards = mch_has_wildcard(NameBuff);
5200 p = NameBuff;
5201 }
5202 else
5203 p = NULL;
5204 }
5205 else /* n == 2 */
5206 {
5207 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005208 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
5210 ExpandInit(&xpc);
5211 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005212 if (p_wic)
5213 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005215 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (p == NULL)
5217 return FAIL;
5218 }
5219 if (p != NULL)
5220 {
5221 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5222 p, cmdlinep);
5223 if (n == 2) /* p came from ExpandOne() */
5224 vim_free(p);
5225 }
5226 }
5227 }
5228 }
5229 return OK;
5230}
5231
5232/*
5233 * Replace part of the command line, keeping eap->cmd, eap->arg and
5234 * eap->nextcmd correct.
5235 * "src" points to the part that is to be replaced, of length "srclen".
5236 * "repl" is the replacement string.
5237 * Returns a pointer to the character after the replaced string.
5238 * Returns NULL for failure.
5239 */
5240 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005241repl_cmdline(
5242 exarg_T *eap,
5243 char_u *src,
5244 int srclen,
5245 char_u *repl,
5246 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247{
5248 int len;
5249 int i;
5250 char_u *new_cmdline;
5251
5252 /*
5253 * The new command line is build in new_cmdline[].
5254 * First allocate it.
5255 * Careful: a "+cmd" argument may have been NUL terminated.
5256 */
5257 len = (int)STRLEN(repl);
5258 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005259 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5261 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5262 return NULL; /* out of memory! */
5263
5264 /*
5265 * Copy the stuff before the expanded part.
5266 * Copy the expanded stuff.
5267 * Copy what came after the expanded part.
5268 * Copy the next commands, if there are any.
5269 */
5270 i = (int)(src - *cmdlinep); /* length of part before match */
5271 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 mch_memmove(new_cmdline + i, repl, (size_t)len);
5274 i += len; /* remember the end of the string */
5275 STRCPY(new_cmdline + i, src + srclen);
5276 src = new_cmdline + i; /* remember where to continue */
5277
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005278 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 i = (int)STRLEN(new_cmdline) + 1;
5281 STRCPY(new_cmdline + i, eap->nextcmd);
5282 eap->nextcmd = new_cmdline + i;
5283 }
5284 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5285 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5286 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5287 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5288 vim_free(*cmdlinep);
5289 *cmdlinep = new_cmdline;
5290
5291 return src;
5292}
5293
5294/*
5295 * Check for '|' to separate commands and '"' to start comments.
5296 */
5297 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005298separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
5300 char_u *p;
5301
Bram Moolenaar86b68352004-12-27 21:59:20 +00005302#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005303 p = skip_grep_pat(eap);
5304#else
5305 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005306#endif
5307
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005308 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 if (*p == Ctrl_V)
5311 {
5312 if (eap->argt & (USECTRLV | XFILE))
5313 ++p; /* skip CTRL-V and next char */
5314 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005315 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005316 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (*p == NUL) /* stop at NUL after CTRL-V */
5318 break;
5319 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005320
5321#ifdef FEAT_EVAL
5322 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005323 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005324 {
5325 p += 2;
5326 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005327 }
5328#endif
5329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 /* Check for '"': start of comment or '|': next command */
5331 /* :@" and :*" do not start a comment!
5332 * :redir @" doesn't either. */
5333 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5334 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5335 || p != eap->arg)
5336 && (eap->cmdidx != CMD_redir
5337 || p != eap->arg + 1 || p[-1] != '@'))
5338 || *p == '|' || *p == '\n')
5339 {
5340 /*
5341 * We remove the '\' before the '|', unless USECTRLV is used
5342 * AND 'b' is present in 'cpoptions'.
5343 */
5344 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5345 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5346 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005347 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 --p;
5349 }
5350 else
5351 {
5352 eap->nextcmd = check_nextcmd(p);
5353 *p = NUL;
5354 break;
5355 }
5356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5360 del_trailing_spaces(eap->arg);
5361}
5362
5363/*
5364 * get + command from ex argument
5365 */
5366 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005367getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 char_u *arg = *argp;
5370 char_u *command = NULL;
5371
5372 if (*arg == '+') /* +[command] */
5373 {
5374 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005375 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 command = dollar_command;
5377 else
5378 {
5379 command = arg;
5380 arg = skip_cmd_arg(command, TRUE);
5381 if (*arg != NUL)
5382 *arg++ = NUL; /* terminate command with NUL */
5383 }
5384
5385 arg = skipwhite(arg); /* skip over spaces */
5386 *argp = arg;
5387 }
5388 return command;
5389}
5390
5391/*
5392 * Find end of "+command" argument. Skip over "\ " and "\\".
5393 */
5394 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005395skip_cmd_arg(
5396 char_u *p,
5397 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 while (*p && !vim_isspace(*p))
5400 {
5401 if (*p == '\\' && p[1] != NUL)
5402 {
5403 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005404 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 else
5406 ++p;
5407 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005408 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410 return p;
5411}
5412
Bram Moolenaar86676c92018-04-05 18:56:48 +02005413#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005414 int
5415get_bad_opt(char_u *p, exarg_T *eap)
5416{
5417 if (STRICMP(p, "keep") == 0)
5418 eap->bad_char = BAD_KEEP;
5419 else if (STRICMP(p, "drop") == 0)
5420 eap->bad_char = BAD_DROP;
5421 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5422 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005423 else
5424 return FAIL;
5425 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005426}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005427#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005428
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429/*
5430 * Get "++opt=arg" argument.
5431 * Return FAIL or OK.
5432 */
5433 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 char_u *arg = eap->arg + 2;
5437 int *pp = NULL;
5438#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005439 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 char_u *p;
5441#endif
5442
5443 /* ":edit ++[no]bin[ary] file" */
5444 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5445 {
5446 if (*arg == 'n')
5447 {
5448 arg += 2;
5449 eap->force_bin = FORCE_NOBIN;
5450 }
5451 else
5452 eap->force_bin = FORCE_BIN;
5453 if (!checkforcmd(&arg, "binary", 3))
5454 return FAIL;
5455 eap->arg = skipwhite(arg);
5456 return OK;
5457 }
5458
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005459 /* ":read ++edit file" */
5460 if (STRNCMP(arg, "edit", 4) == 0)
5461 {
5462 eap->read_edit = TRUE;
5463 eap->arg = skipwhite(arg + 4);
5464 return OK;
5465 }
5466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 if (STRNCMP(arg, "ff", 2) == 0)
5468 {
5469 arg += 2;
5470 pp = &eap->force_ff;
5471 }
5472 else if (STRNCMP(arg, "fileformat", 10) == 0)
5473 {
5474 arg += 10;
5475 pp = &eap->force_ff;
5476 }
5477#ifdef FEAT_MBYTE
5478 else if (STRNCMP(arg, "enc", 3) == 0)
5479 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005480 if (STRNCMP(arg, "encoding", 8) == 0)
5481 arg += 8;
5482 else
5483 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 pp = &eap->force_enc;
5485 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005486 else if (STRNCMP(arg, "bad", 3) == 0)
5487 {
5488 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005489 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491#endif
5492
5493 if (pp == NULL || *arg != '=')
5494 return FAIL;
5495
5496 ++arg;
5497 *pp = (int)(arg - eap->cmd);
5498 arg = skip_cmd_arg(arg, FALSE);
5499 eap->arg = skipwhite(arg);
5500 *arg = NUL;
5501
5502#ifdef FEAT_MBYTE
5503 if (pp == &eap->force_ff)
5504 {
5505#endif
5506 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5507 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005508 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509#ifdef FEAT_MBYTE
5510 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005511 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
5513 /* Make 'fileencoding' lower case. */
5514 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5515 *p = TOLOWER_ASC(*p);
5516 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005517 else
5518 {
5519 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5520 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005521 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005522 return FAIL;
5523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#endif
5525
5526 return OK;
5527}
5528
5529/*
5530 * ":abbreviate" and friends.
5531 */
5532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 do_exmap(eap, TRUE); /* almost the same as mapping */
5536}
5537
5538/*
5539 * ":map" and friends.
5540 */
5541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 /*
5545 * If we are sourcing .exrc or .vimrc in current directory we
5546 * print the mappings for security reasons.
5547 */
5548 if (secure)
5549 {
5550 secure = 2;
5551 msg_outtrans(eap->cmd);
5552 msg_putchar('\n');
5553 }
5554 do_exmap(eap, FALSE);
5555}
5556
5557/*
5558 * ":unmap" and friends.
5559 */
5560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 do_exmap(eap, FALSE);
5564}
5565
5566/*
5567 * ":mapclear" and friends.
5568 */
5569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005570ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5573}
5574
5575/*
5576 * ":abclear" and friends.
5577 */
5578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005579ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5582}
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005588 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 * directory for security reasons.
5590 */
5591 if (secure)
5592 {
5593 secure = 2;
5594 eap->errmsg = e_curdir;
5595 }
5596 else if (eap->cmdidx == CMD_autocmd)
5597 do_autocmd(eap->arg, eap->forceit);
5598 else
5599 do_augroup(eap->arg, eap->forceit);
5600}
5601
5602/*
5603 * ":doautocmd": Apply the automatic commands to the current buffer.
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005608 char_u *arg = eap->arg;
5609 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005610 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005611
Bram Moolenaar1610d052016-06-09 22:53:01 +02005612 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5613 /* Only when there is no <nomodeline>. */
5614 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005615 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618/*
5619 * :[N]bunload[!] [N] [bufname] unload buffer
5620 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5621 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5622 */
5623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 eap->errmsg = do_bufdel(
5627 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5628 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5629 : DOBUF_UNLOAD, eap->arg,
5630 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5631}
5632
5633/*
5634 * :[N]buffer [N] to buffer N
5635 * :[N]sbuffer [N] to buffer N
5636 */
5637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 if (*eap->arg)
5641 eap->errmsg = e_trailing;
5642 else
5643 {
5644 if (eap->addr_count == 0) /* default is current buffer */
5645 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5646 else
5647 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005648 if (eap->do_ecmd_cmd != NULL)
5649 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651}
5652
5653/*
5654 * :[N]bmodified [N] to next mod. buffer
5655 * :[N]sbmodified [N] to next mod. buffer
5656 */
5657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005658ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005661 if (eap->do_ecmd_cmd != NULL)
5662 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663}
5664
5665/*
5666 * :[N]bnext [N] to next buffer
5667 * :[N]sbnext [N] split and to next buffer
5668 */
5669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005670ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005673 if (eap->do_ecmd_cmd != NULL)
5674 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675}
5676
5677/*
5678 * :[N]bNext [N] to previous buffer
5679 * :[N]bprevious [N] to previous buffer
5680 * :[N]sbNext [N] split and to previous buffer
5681 * :[N]sbprevious [N] split and to previous buffer
5682 */
5683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005684ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005687 if (eap->do_ecmd_cmd != NULL)
5688 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689}
5690
5691/*
5692 * :brewind to first buffer
5693 * :bfirst to first buffer
5694 * :sbrewind split and to first buffer
5695 * :sbfirst split and to first buffer
5696 */
5697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005701 if (eap->do_ecmd_cmd != NULL)
5702 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703}
5704
5705/*
5706 * :blast to last buffer
5707 * :sblast split and to last buffer
5708 */
5709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005710ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005713 if (eap->do_ecmd_cmd != NULL)
5714 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
5720 return (c == NUL || c == '|' || c == '"' || c == '\n');
5721}
5722
5723#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5724 || defined(PROTO)
5725/*
5726 * Return the next command, after the first '|' or '\n'.
5727 * Return NULL if not found.
5728 */
5729 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005730find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 while (*p != '|' && *p != '\n')
5733 {
5734 if (*p == NUL)
5735 return NULL;
5736 ++p;
5737 }
5738 return (p + 1);
5739}
5740#endif
5741
5742/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005743 * Check if *p is a separator between Ex commands, skipping over white space.
5744 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 */
5746 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005747check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005749 char_u *s = skipwhite(p);
5750
5751 if (*s == '|' || *s == '\n')
5752 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 else
5754 return NULL;
5755}
5756
5757/*
5758 * - if there are more files to edit
5759 * - and this is the last window
5760 * - and forceit not used
5761 * - and not repeated twice on a row
5762 * return FAIL and give error message if 'message' TRUE
5763 * return OK otherwise
5764 */
5765 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005766check_more(
5767 int message, /* when FALSE check only, no messages */
5768 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5771
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005772 if (!forceit && only_one_window()
5773 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (message)
5776 {
5777#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5778 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5779 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005780 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005782 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5783 NGETTEXT("%d more file to edit. Quit anyway?",
5784 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5786 return OK;
5787 return FAIL;
5788 }
5789#endif
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005790 EMSGN(NGETTEXT("E173: %ld more file to edit",
5791 "E173: %ld more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 quitmore = 2; /* next try to quit is allowed */
5793 }
5794 return FAIL;
5795 }
5796 return OK;
5797}
5798
5799#ifdef FEAT_CMDL_COMPL
5800/*
5801 * Function given to ExpandGeneric() to obtain the list of command names.
5802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005804get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 if (idx >= (int)CMD_SIZE)
5807# ifdef FEAT_USR_CMDS
5808 return get_user_command_name(idx);
5809# else
5810 return NULL;
5811# endif
5812 return cmdnames[idx].cmd_name;
5813}
5814#endif
5815
5816#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005818uc_add_command(
5819 char_u *name,
5820 size_t name_len,
5821 char_u *rep,
5822 long argt,
5823 long def,
5824 int flags,
5825 int compl,
5826 char_u *compl_arg,
5827 int addr_type,
5828 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 ucmd_T *cmd = NULL;
5831 char_u *p;
5832 int i;
5833 int cmp = 1;
5834 char_u *rep_buf = NULL;
5835 garray_T *gap;
5836
Bram Moolenaar9c102382006-05-03 21:26:49 +00005837 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 if (rep_buf == NULL)
5839 {
5840 /* Can't replace termcodes - try using the string as is */
5841 rep_buf = vim_strsave(rep);
5842
5843 /* Give up if out of memory */
5844 if (rep_buf == NULL)
5845 return FAIL;
5846 }
5847
5848 /* get address of growarray: global or in curbuf */
5849 if (flags & UC_BUFFER)
5850 {
5851 gap = &curbuf->b_ucmds;
5852 if (gap->ga_itemsize == 0)
5853 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5854 }
5855 else
5856 gap = &ucmds;
5857
5858 /* Search for the command in the already defined commands. */
5859 for (i = 0; i < gap->ga_len; ++i)
5860 {
5861 size_t len;
5862
5863 cmd = USER_CMD_GA(gap, i);
5864 len = STRLEN(cmd->uc_name);
5865 cmp = STRNCMP(name, cmd->uc_name, name_len);
5866 if (cmp == 0)
5867 {
5868 if (name_len < len)
5869 cmp = -1;
5870 else if (name_len > len)
5871 cmp = 1;
5872 }
5873
5874 if (cmp == 0)
5875 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005876 // Command can be replaced with "command!" and when sourcing the
5877 // same script again, but only once.
5878 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
5879 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
Bram Moolenaar55d46912018-12-08 16:03:28 +01005881 EMSG2(_("E174: Command already exists: add ! to replace it: %s"),
5882 name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 goto fail;
5884 }
5885
Bram Moolenaard23a8232018-02-10 18:45:26 +01005886 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005887#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005888 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 break;
5891 }
5892
5893 /* Stop as soon as we pass the name to add */
5894 if (cmp < 0)
5895 break;
5896 }
5897
5898 /* Extend the array unless we're replacing an existing command */
5899 if (cmp != 0)
5900 {
5901 if (ga_grow(gap, 1) != OK)
5902 goto fail;
5903 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5904 goto fail;
5905
5906 cmd = USER_CMD_GA(gap, i);
5907 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5908
5909 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
5911 cmd->uc_name = p;
5912 }
5913
5914 cmd->uc_rep = rep_buf;
5915 cmd->uc_argt = argt;
5916 cmd->uc_def = def;
5917 cmd->uc_compl = compl;
5918#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005919 cmd->uc_script_ctx = current_sctx;
5920 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921# ifdef FEAT_CMDL_COMPL
5922 cmd->uc_compl_arg = compl_arg;
5923# endif
5924#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005925 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 return OK;
5928
5929fail:
5930 vim_free(rep_buf);
5931#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5932 vim_free(compl_arg);
5933#endif
5934 return FAIL;
5935}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005938#if defined(FEAT_USR_CMDS)
5939static struct
5940{
5941 int expand;
5942 char *name;
5943} addr_type_complete[] =
5944{
5945 {ADDR_ARGUMENTS, "arguments"},
5946 {ADDR_LINES, "lines"},
5947 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5948 {ADDR_TABS, "tabs"},
5949 {ADDR_BUFFERS, "buffers"},
5950 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005951 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005952 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005953 {-1, NULL}
5954};
5955#endif
5956
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005957#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958/*
5959 * List of names for completion for ":command" with the EXPAND_ flag.
5960 * Must be alphabetical for completion.
5961 */
5962static struct
5963{
5964 int expand;
5965 char *name;
5966} command_complete[] =
5967{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005968 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005970 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005972 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005974 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005975#if defined(FEAT_CSCOPE)
5976 {EXPAND_CSCOPE, "cscope"},
5977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5979 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005980 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981#endif
5982 {EXPAND_DIRECTORIES, "dir"},
5983 {EXPAND_ENV_VARS, "environment"},
5984 {EXPAND_EVENTS, "event"},
5985 {EXPAND_EXPRESSION, "expression"},
5986 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005987 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005988 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 {EXPAND_FUNCTIONS, "function"},
5990 {EXPAND_HELP, "help"},
5991 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005992#if defined(FEAT_CMDHIST)
5993 {EXPAND_HISTORY, "history"},
5994#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005995#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005996 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005997 {EXPAND_LOCALES, "locale"},
5998#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005999 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {EXPAND_MAPPINGS, "mapping"},
6001 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02006002 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006003 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006004#if defined(FEAT_PROFILE)
6005 {EXPAND_SYNTIME, "syntime"},
6006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006008 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006009 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006010#if defined(FEAT_SIGNS)
6011 {EXPAND_SIGN, "sign"},
6012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {EXPAND_TAGS, "tag"},
6014 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006015 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 {EXPAND_USER_VARS, "var"},
6017 {0, NULL}
6018};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006021#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
6025 int i, j;
6026 int found = FALSE;
6027 ucmd_T *cmd;
6028 int len;
6029 long a;
6030 garray_T *gap;
6031
6032 gap = &curbuf->b_ucmds;
6033 for (;;)
6034 {
6035 for (i = 0; i < gap->ga_len; ++i)
6036 {
6037 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006038 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039
Bram Moolenaard29459b2016-08-26 22:29:11 +02006040 /* Skip commands which don't match the requested prefix and
6041 * commands filtered out. */
6042 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6043 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 continue;
6045
6046 /* Put out the title first time */
6047 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006048 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 found = TRUE;
6050 msg_putchar('\n');
6051 if (got_int)
6052 break;
6053
6054 /* Special cases */
6055 msg_putchar(a & BANG ? '!' : ' ');
6056 msg_putchar(a & REGSTR ? '"' : ' ');
6057 msg_putchar(gap != &ucmds ? 'b' : ' ');
6058 msg_putchar(' ');
6059
Bram Moolenaar8820b482017-03-16 17:23:31 +01006060 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 len = (int)STRLEN(cmd->uc_name) + 4;
6062
6063 do {
6064 msg_putchar(' ');
6065 ++len;
6066 } while (len < 16);
6067
6068 len = 0;
6069
6070 /* Arguments */
6071 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6072 {
6073 case 0: IObuff[len++] = '0'; break;
6074 case (EXTRA): IObuff[len++] = '*'; break;
6075 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6076 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6077 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6078 }
6079
6080 do {
6081 IObuff[len++] = ' ';
6082 } while (len < 5);
6083
6084 /* Range */
6085 if (a & (RANGE|COUNT))
6086 {
6087 if (a & COUNT)
6088 {
6089 /* -count=N */
6090 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6091 len += (int)STRLEN(IObuff + len);
6092 }
6093 else if (a & DFLALL)
6094 IObuff[len++] = '%';
6095 else if (cmd->uc_def >= 0)
6096 {
6097 /* -range=N */
6098 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6099 len += (int)STRLEN(IObuff + len);
6100 }
6101 else
6102 IObuff[len++] = '.';
6103 }
6104
6105 do {
6106 IObuff[len++] = ' ';
6107 } while (len < 11);
6108
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006109 /* Address Type */
6110 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6111 if (addr_type_complete[j].expand != ADDR_LINES
6112 && addr_type_complete[j].expand == cmd->uc_addr_type)
6113 {
6114 STRCPY(IObuff + len, addr_type_complete[j].name);
6115 len += (int)STRLEN(IObuff + len);
6116 break;
6117 }
6118
6119 do {
6120 IObuff[len++] = ' ';
6121 } while (len < 21);
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 /* Completion */
6124 for (j = 0; command_complete[j].expand != 0; ++j)
6125 if (command_complete[j].expand == cmd->uc_compl)
6126 {
6127 STRCPY(IObuff + len, command_complete[j].name);
6128 len += (int)STRLEN(IObuff + len);
6129 break;
6130 }
6131
6132 do {
6133 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006134 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136 IObuff[len] = '\0';
6137 msg_outtrans(IObuff);
6138
6139 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006140#ifdef FEAT_EVAL
6141 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006142 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 out_flush();
6145 ui_breakcheck();
6146 if (got_int)
6147 break;
6148 }
6149 if (gap == &ucmds || i < gap->ga_len)
6150 break;
6151 gap = &ucmds;
6152 }
6153
6154 if (!found)
6155 MSG(_("No user-defined commands found"));
6156}
6157
6158 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006159uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6162 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6163 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6164 0xb9, 0x7f, 0};
6165 int i;
6166
6167 for (i = 0; fcmd[i]; ++i)
6168 IObuff[i] = fcmd[i] - 0x40;
6169 IObuff[i] = 0;
6170 return IObuff;
6171}
6172
6173 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006174uc_scan_attr(
6175 char_u *attr,
6176 size_t len,
6177 long *argt,
6178 long *def,
6179 int *flags,
6180 int *compl,
6181 char_u **compl_arg,
6182 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
6184 char_u *p;
6185
6186 if (len == 0)
6187 {
6188 EMSG(_("E175: No attribute specified"));
6189 return FAIL;
6190 }
6191
6192 /* First, try the simple attributes (no arguments) */
6193 if (STRNICMP(attr, "bang", len) == 0)
6194 *argt |= BANG;
6195 else if (STRNICMP(attr, "buffer", len) == 0)
6196 *flags |= UC_BUFFER;
6197 else if (STRNICMP(attr, "register", len) == 0)
6198 *argt |= REGSTR;
6199 else if (STRNICMP(attr, "bar", len) == 0)
6200 *argt |= TRLBAR;
6201 else
6202 {
6203 int i;
6204 char_u *val = NULL;
6205 size_t vallen = 0;
6206 size_t attrlen = len;
6207
6208 /* Look for the attribute name - which is the part before any '=' */
6209 for (i = 0; i < (int)len; ++i)
6210 {
6211 if (attr[i] == '=')
6212 {
6213 val = &attr[i + 1];
6214 vallen = len - i - 1;
6215 attrlen = i;
6216 break;
6217 }
6218 }
6219
6220 if (STRNICMP(attr, "nargs", attrlen) == 0)
6221 {
6222 if (vallen == 1)
6223 {
6224 if (*val == '0')
6225 /* Do nothing - this is the default */;
6226 else if (*val == '1')
6227 *argt |= (EXTRA | NOSPC | NEEDARG);
6228 else if (*val == '*')
6229 *argt |= EXTRA;
6230 else if (*val == '?')
6231 *argt |= (EXTRA | NOSPC);
6232 else if (*val == '+')
6233 *argt |= (EXTRA | NEEDARG);
6234 else
6235 goto wrong_nargs;
6236 }
6237 else
6238 {
6239wrong_nargs:
6240 EMSG(_("E176: Invalid number of arguments"));
6241 return FAIL;
6242 }
6243 }
6244 else if (STRNICMP(attr, "range", attrlen) == 0)
6245 {
6246 *argt |= RANGE;
6247 if (vallen == 1 && *val == '%')
6248 *argt |= DFLALL;
6249 else if (val != NULL)
6250 {
6251 p = val;
6252 if (*def >= 0)
6253 {
6254two_count:
6255 EMSG(_("E177: Count cannot be specified twice"));
6256 return FAIL;
6257 }
6258
6259 *def = getdigits(&p);
6260 *argt |= (ZEROR | NOTADR);
6261
6262 if (p != val + vallen || vallen == 0)
6263 {
6264invalid_count:
6265 EMSG(_("E178: Invalid default value for count"));
6266 return FAIL;
6267 }
6268 }
6269 }
6270 else if (STRNICMP(attr, "count", attrlen) == 0)
6271 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006272 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 if (val != NULL)
6275 {
6276 p = val;
6277 if (*def >= 0)
6278 goto two_count;
6279
6280 *def = getdigits(&p);
6281
6282 if (p != val + vallen)
6283 goto invalid_count;
6284 }
6285
6286 if (*def < 0)
6287 *def = 0;
6288 }
6289 else if (STRNICMP(attr, "complete", attrlen) == 0)
6290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (val == NULL)
6292 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006293 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 return FAIL;
6295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006297 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6298 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006301 else if (STRNICMP(attr, "addr", attrlen) == 0)
6302 {
6303 *argt |= RANGE;
6304 if (val == NULL)
6305 {
6306 EMSG(_("E179: argument required for -addr"));
6307 return FAIL;
6308 }
6309 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6310 == FAIL)
6311 return FAIL;
6312 if (addr_type_arg != ADDR_LINES)
6313 *argt |= (ZEROR | NOTADR) ;
6314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 else
6316 {
6317 char_u ch = attr[len];
6318 attr[len] = '\0';
6319 EMSG2(_("E181: Invalid attribute: %s"), attr);
6320 attr[len] = ch;
6321 return FAIL;
6322 }
6323 }
6324
6325 return OK;
6326}
6327
Bram Moolenaara850a712009-01-28 14:42:59 +00006328/*
6329 * ":command ..."
6330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 char_u *name;
6335 char_u *end;
6336 char_u *p;
6337 long argt = 0;
6338 long def = -1;
6339 int flags = 0;
6340 int compl = EXPAND_NOTHING;
6341 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006342 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006344 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346 p = eap->arg;
6347
6348 /* Check for attributes */
6349 while (*p == '-')
6350 {
6351 ++p;
6352 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006353 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6354 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 == FAIL)
6356 return;
6357 p = skipwhite(end);
6358 }
6359
6360 /* Get the name (if any) and skip to the following argument */
6361 name = p;
6362 if (ASCII_ISALPHA(*p))
6363 while (ASCII_ISALNUM(*p))
6364 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006365 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
6367 EMSG(_("E182: Invalid command name"));
6368 return;
6369 }
6370 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006371 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373 /* If there is nothing after the name, and no attributes were specified,
6374 * we are listing commands
6375 */
6376 p = skipwhite(end);
6377 if (!has_attr && ends_excmd(*p))
6378 {
6379 uc_list(name, end - name);
6380 }
6381 else if (!ASCII_ISUPPER(*name))
6382 {
6383 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6384 return;
6385 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006386 else if ((name_len == 1 && *name == 'X')
6387 || (name_len <= 4
6388 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6389 {
6390 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6391 return;
6392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 else
6394 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006395 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396}
6397
6398/*
6399 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 * Clear all user commands, global and for current buffer.
6401 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006402 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 uc_clear(&ucmds);
6406 uc_clear(&curbuf->b_ucmds);
6407}
6408
6409/*
6410 * Clear all user commands for "gap".
6411 */
6412 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006413uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 int i;
6416 ucmd_T *cmd;
6417
6418 for (i = 0; i < gap->ga_len; ++i)
6419 {
6420 cmd = USER_CMD_GA(gap, i);
6421 vim_free(cmd->uc_name);
6422 vim_free(cmd->uc_rep);
6423# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6424 vim_free(cmd->uc_compl_arg);
6425# endif
6426 }
6427 ga_clear(gap);
6428}
6429
6430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006431ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 int i = 0;
6434 ucmd_T *cmd = NULL;
6435 int cmp = -1;
6436 garray_T *gap;
6437
6438 gap = &curbuf->b_ucmds;
6439 for (;;)
6440 {
6441 for (i = 0; i < gap->ga_len; ++i)
6442 {
6443 cmd = USER_CMD_GA(gap, i);
6444 cmp = STRCMP(eap->arg, cmd->uc_name);
6445 if (cmp <= 0)
6446 break;
6447 }
6448 if (gap == &ucmds || cmp == 0)
6449 break;
6450 gap = &ucmds;
6451 }
6452
6453 if (cmp != 0)
6454 {
6455 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6456 return;
6457 }
6458
6459 vim_free(cmd->uc_name);
6460 vim_free(cmd->uc_rep);
6461# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6462 vim_free(cmd->uc_compl_arg);
6463# endif
6464
6465 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 if (i < gap->ga_len)
6468 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6469}
6470
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006471/*
6472 * split and quote args for <f-args>
6473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006475uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 char_u *buf;
6478 char_u *p;
6479 char_u *q;
6480 int len;
6481
6482 /* Precalculate length */
6483 p = arg;
6484 len = 2; /* Initial and final quotes */
6485
6486 while (*p)
6487 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006488 if (p[0] == '\\' && p[1] == '\\')
6489 {
6490 len += 2;
6491 p += 2;
6492 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006493 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 len += 1;
6496 p += 2;
6497 }
6498 else if (*p == '\\' || *p == '"')
6499 {
6500 len += 2;
6501 p += 1;
6502 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006503 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
6505 p = skipwhite(p);
6506 if (*p == NUL)
6507 break;
6508 len += 3; /* "," */
6509 }
6510 else
6511 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006512#ifdef FEAT_MBYTE
6513 int charlen = (*mb_ptr2len)(p);
6514 len += charlen;
6515 p += charlen;
6516#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 ++len;
6518 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
6521 }
6522
6523 buf = alloc(len + 1);
6524 if (buf == NULL)
6525 {
6526 *lenp = 0;
6527 return buf;
6528 }
6529
6530 p = arg;
6531 q = buf;
6532 *q++ = '"';
6533 while (*p)
6534 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006535 if (p[0] == '\\' && p[1] == '\\')
6536 {
6537 *q++ = '\\';
6538 *q++ = '\\';
6539 p += 2;
6540 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006541 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
6543 *q++ = p[1];
6544 p += 2;
6545 }
6546 else if (*p == '\\' || *p == '"')
6547 {
6548 *q++ = '\\';
6549 *q++ = *p++;
6550 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006551 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
6553 p = skipwhite(p);
6554 if (*p == NUL)
6555 break;
6556 *q++ = '"';
6557 *q++ = ',';
6558 *q++ = '"';
6559 }
6560 else
6561 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006562 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 }
6564 }
6565 *q++ = '"';
6566 *q = 0;
6567
6568 *lenp = len;
6569 return buf;
6570}
6571
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006572 static size_t
6573add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6574{
6575 size_t result;
6576
6577 result = STRLEN(mod_str);
6578 if (*multi_mods)
6579 result += 1;
6580 if (buf != NULL)
6581 {
6582 if (*multi_mods)
6583 STRCAT(buf, " ");
6584 STRCAT(buf, mod_str);
6585 }
6586
6587 *multi_mods = 1;
6588
6589 return result;
6590}
6591
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592/*
6593 * Check for a <> code in a user command.
6594 * "code" points to the '<'. "len" the length of the <> (inclusive).
6595 * "buf" is where the result is to be added.
6596 * "split_buf" points to a buffer used for splitting, caller should free it.
6597 * "split_len" is the length of what "split_buf" contains.
6598 * Returns the length of the replacement, which has been added to "buf".
6599 * Returns -1 if there was no match, and only the "<" has been copied.
6600 */
6601 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006602uc_check_code(
6603 char_u *code,
6604 size_t len,
6605 char_u *buf,
6606 ucmd_T *cmd, /* the user command we're expanding */
6607 exarg_T *eap, /* ex arguments */
6608 char_u **split_buf,
6609 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 size_t result = 0;
6612 char_u *p = code + 1;
6613 size_t l = len - 2;
6614 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006615 enum {
6616 ct_ARGS,
6617 ct_BANG,
6618 ct_COUNT,
6619 ct_LINE1,
6620 ct_LINE2,
6621 ct_RANGE,
6622 ct_MODS,
6623 ct_REGISTER,
6624 ct_LT,
6625 ct_NONE
6626 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
6628 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6629 {
6630 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6631 p += 2;
6632 l -= 2;
6633 }
6634
Bram Moolenaar371d5402006-03-20 21:47:49 +00006635 ++l;
6636 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006638 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006640 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006642 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006644 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006646 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006648 else if (STRNICMP(p, "range>", l) == 0)
6649 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006650 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006652 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006654 else if (STRNICMP(p, "mods>", l) == 0)
6655 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
6657 switch (type)
6658 {
6659 case ct_ARGS:
6660 /* Simple case first */
6661 if (*eap->arg == NUL)
6662 {
6663 if (quote == 1)
6664 {
6665 result = 2;
6666 if (buf != NULL)
6667 STRCPY(buf, "''");
6668 }
6669 else
6670 result = 0;
6671 break;
6672 }
6673
6674 /* When specified there is a single argument don't split it.
6675 * Works for ":Cmd %" when % is "a b c". */
6676 if ((eap->argt & NOSPC) && quote == 2)
6677 quote = 1;
6678
6679 switch (quote)
6680 {
6681 case 0: /* No quoting, no splitting */
6682 result = STRLEN(eap->arg);
6683 if (buf != NULL)
6684 STRCPY(buf, eap->arg);
6685 break;
6686 case 1: /* Quote, but don't split */
6687 result = STRLEN(eap->arg) + 2;
6688 for (p = eap->arg; *p; ++p)
6689 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006690#ifdef FEAT_MBYTE
6691 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6692 /* DBCS can contain \ in a trail byte, skip the
6693 * double-byte character. */
6694 ++p;
6695 else
6696#endif
6697 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 ++result;
6699 }
6700
6701 if (buf != NULL)
6702 {
6703 *buf++ = '"';
6704 for (p = eap->arg; *p; ++p)
6705 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006706#ifdef FEAT_MBYTE
6707 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6708 /* DBCS can contain \ in a trail byte, copy the
6709 * double-byte character to avoid escaping. */
6710 *buf++ = *p++;
6711 else
6712#endif
6713 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 *buf++ = '\\';
6715 *buf++ = *p;
6716 }
6717 *buf = '"';
6718 }
6719
6720 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006721 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 /* This is hard, so only do it once, and cache the result */
6723 if (*split_buf == NULL)
6724 *split_buf = uc_split_args(eap->arg, split_len);
6725
6726 result = *split_len;
6727 if (buf != NULL && result != 0)
6728 STRCPY(buf, *split_buf);
6729
6730 break;
6731 }
6732 break;
6733
6734 case ct_BANG:
6735 result = eap->forceit ? 1 : 0;
6736 if (quote)
6737 result += 2;
6738 if (buf != NULL)
6739 {
6740 if (quote)
6741 *buf++ = '"';
6742 if (eap->forceit)
6743 *buf++ = '!';
6744 if (quote)
6745 *buf = '"';
6746 }
6747 break;
6748
6749 case ct_LINE1:
6750 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006751 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 case ct_COUNT:
6753 {
6754 char num_buf[20];
6755 long num = (type == ct_LINE1) ? eap->line1 :
6756 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006757 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6759 size_t num_len;
6760
6761 sprintf(num_buf, "%ld", num);
6762 num_len = STRLEN(num_buf);
6763 result = num_len;
6764
6765 if (quote)
6766 result += 2;
6767
6768 if (buf != NULL)
6769 {
6770 if (quote)
6771 *buf++ = '"';
6772 STRCPY(buf, num_buf);
6773 buf += num_len;
6774 if (quote)
6775 *buf = '"';
6776 }
6777
6778 break;
6779 }
6780
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006781 case ct_MODS:
6782 {
6783 int multi_mods = 0;
6784 typedef struct {
6785 int *varp;
6786 char *name;
6787 } mod_entry_T;
6788 static mod_entry_T mod_entries[] = {
6789#ifdef FEAT_BROWSE_CMD
6790 {&cmdmod.browse, "browse"},
6791#endif
6792#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6793 {&cmdmod.confirm, "confirm"},
6794#endif
6795 {&cmdmod.hide, "hide"},
6796 {&cmdmod.keepalt, "keepalt"},
6797 {&cmdmod.keepjumps, "keepjumps"},
6798 {&cmdmod.keepmarks, "keepmarks"},
6799 {&cmdmod.keeppatterns, "keeppatterns"},
6800 {&cmdmod.lockmarks, "lockmarks"},
6801 {&cmdmod.noswapfile, "noswapfile"},
6802 {NULL, NULL}
6803 };
6804 int i;
6805
6806 result = quote ? 2 : 0;
6807 if (buf != NULL)
6808 {
6809 if (quote)
6810 *buf++ = '"';
6811 *buf = '\0';
6812 }
6813
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006814 /* :aboveleft and :leftabove */
6815 if (cmdmod.split & WSP_ABOVE)
6816 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6817 /* :belowright and :rightbelow */
6818 if (cmdmod.split & WSP_BELOW)
6819 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6820 /* :botright */
6821 if (cmdmod.split & WSP_BOT)
6822 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006823
6824 /* the modifiers that are simple flags */
6825 for (i = 0; mod_entries[i].varp != NULL; ++i)
6826 if (*mod_entries[i].varp)
6827 result += add_cmd_modifier(buf, mod_entries[i].name,
6828 &multi_mods);
6829
6830 /* TODO: How to support :noautocmd? */
6831#ifdef HAVE_SANDBOX
6832 /* TODO: How to support :sandbox?*/
6833#endif
6834 /* :silent */
6835 if (msg_silent > 0)
6836 result += add_cmd_modifier(buf,
6837 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006838 /* :tab */
6839 if (cmdmod.tab > 0)
6840 result += add_cmd_modifier(buf, "tab", &multi_mods);
6841 /* :topleft */
6842 if (cmdmod.split & WSP_TOP)
6843 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006844 /* TODO: How to support :unsilent?*/
6845 /* :verbose */
6846 if (p_verbose > 0)
6847 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006848 /* :vertical */
6849 if (cmdmod.split & WSP_VERT)
6850 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006851 if (quote && buf != NULL)
6852 {
6853 buf += result - 2;
6854 *buf = '"';
6855 }
6856 break;
6857 }
6858
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 case ct_REGISTER:
6860 result = eap->regname ? 1 : 0;
6861 if (quote)
6862 result += 2;
6863 if (buf != NULL)
6864 {
6865 if (quote)
6866 *buf++ = '\'';
6867 if (eap->regname)
6868 *buf++ = eap->regname;
6869 if (quote)
6870 *buf = '\'';
6871 }
6872 break;
6873
6874 case ct_LT:
6875 result = 1;
6876 if (buf != NULL)
6877 *buf = '<';
6878 break;
6879
6880 default:
6881 /* Not recognized: just copy the '<' and return -1. */
6882 result = (size_t)-1;
6883 if (buf != NULL)
6884 *buf = '<';
6885 break;
6886 }
6887
6888 return result;
6889}
6890
6891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006892do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 char_u *buf;
6895 char_u *p;
6896 char_u *q;
6897
6898 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006899 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006900 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 size_t len, totlen;
6902
6903 size_t split_len = 0;
6904 char_u *split_buf = NULL;
6905 ucmd_T *cmd;
6906#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006907 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908#endif
6909
6910 if (eap->cmdidx == CMD_USER)
6911 cmd = USER_CMD(eap->useridx);
6912 else
6913 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6914
6915 /*
6916 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006917 * First round: "buf" is NULL, compute length, allocate "buf".
6918 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 */
6920 buf = NULL;
6921 for (;;)
6922 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006923 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006924 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006926
6927 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006929 start = vim_strchr(p, '<');
6930 if (start != NULL)
6931 end = vim_strchr(start + 1, '>');
6932 if (buf != NULL)
6933 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006934 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6935 ;
6936 if (*ksp == K_SPECIAL
6937 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006938 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6939# ifdef FEAT_GUI
6940 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6941# endif
6942 ))
6943 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006944 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006945 * KS_SPECIAL KE_FILLER, like for mappings, but
6946 * do_cmdline() doesn't handle that, so convert it back.
6947 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6948 len = ksp - p;
6949 if (len > 0)
6950 {
6951 mch_memmove(q, p, len);
6952 q += len;
6953 }
6954 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6955 p = ksp + 3;
6956 continue;
6957 }
6958 }
6959
6960 /* break if there no <item> is found */
6961 if (start == NULL || end == NULL)
6962 break;
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 /* Include the '>' */
6965 ++end;
6966
6967 /* Take everything up to the '<' */
6968 len = start - p;
6969 if (buf == NULL)
6970 totlen += len;
6971 else
6972 {
6973 mch_memmove(q, p, len);
6974 q += len;
6975 }
6976
6977 len = uc_check_code(start, end - start, q, cmd, eap,
6978 &split_buf, &split_len);
6979 if (len == (size_t)-1)
6980 {
6981 /* no match, continue after '<' */
6982 p = start + 1;
6983 len = 1;
6984 }
6985 else
6986 p = end;
6987 if (buf == NULL)
6988 totlen += len;
6989 else
6990 q += len;
6991 }
6992 if (buf != NULL) /* second time here, finished */
6993 {
6994 STRCPY(q, p);
6995 break;
6996 }
6997
6998 totlen += STRLEN(p); /* Add on the trailing characters */
6999 buf = alloc((unsigned)(totlen + 1));
7000 if (buf == NULL)
7001 {
7002 vim_free(split_buf);
7003 return;
7004 }
7005 }
7006
7007#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007008 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009#endif
7010 (void)do_cmdline(buf, eap->getline, eap->cookie,
7011 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7012#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007013 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014#endif
7015 vim_free(buf);
7016 vim_free(split_buf);
7017}
7018
7019# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7020 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007021get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7024}
7025
7026/*
7027 * Function given to ExpandGeneric() to obtain the list of user command names.
7028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007030get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 if (idx < curbuf->b_ucmds.ga_len)
7033 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7034 idx -= curbuf->b_ucmds.ga_len;
7035 if (idx < ucmds.ga_len)
7036 return USER_CMD(idx)->uc_name;
7037 return NULL;
7038}
7039
7040/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007041 * Function given to ExpandGeneric() to obtain the list of user address type names.
7042 */
7043 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007044get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007045{
7046 return (char_u *)addr_type_complete[idx].name;
7047}
7048
7049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 * Function given to ExpandGeneric() to obtain the list of user command
7051 * attributes.
7052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007054get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007057 {"addr", "bang", "bar", "buffer", "complete",
7058 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007060 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 return NULL;
7062 return (char_u *)user_cmd_flags[idx];
7063}
7064
7065/*
7066 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007069get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7072
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007073 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 return NULL;
7075 return (char_u *)user_cmd_nargs[idx];
7076}
7077
7078/*
7079 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007082get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
7084 return (char_u *)command_complete[idx].name;
7085}
7086# endif /* FEAT_CMDL_COMPL */
7087
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007088/*
7089 * Parse address type argument
7090 */
7091 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007092parse_addr_type_arg(
7093 char_u *value,
7094 int vallen,
7095 long *argt,
7096 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007097{
7098 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007099
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007100 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7101 {
7102 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7103 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7104 if (a && b)
7105 {
7106 *addr_type_arg = addr_type_complete[i].expand;
7107 break;
7108 }
7109 }
7110
7111 if (addr_type_complete[i].expand == -1)
7112 {
7113 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007114
Bram Moolenaar1c465442017-03-12 20:10:05 +01007115 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007116 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007117 err[i] = NUL;
7118 EMSG2(_("E180: Invalid address type value: %s"), err);
7119 return FAIL;
7120 }
7121
7122 if (*addr_type_arg != ADDR_LINES)
7123 *argt |= NOTADR;
7124
7125 return OK;
7126}
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128#endif /* FEAT_USR_CMDS */
7129
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007130#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7131/*
7132 * Parse a completion argument "value[vallen]".
7133 * The detected completion goes in "*complp", argument type in "*argt".
7134 * When there is an argument, for function and user defined completion, it's
7135 * copied to allocated memory and stored in "*compl_arg".
7136 * Returns FAIL if something is wrong.
7137 */
7138 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007139parse_compl_arg(
7140 char_u *value,
7141 int vallen,
7142 int *complp,
7143 long *argt,
7144 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007145{
7146 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007147# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007148 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007149# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007150 int i;
7151 int valend = vallen;
7152
7153 /* Look for any argument part - which is the part after any ',' */
7154 for (i = 0; i < vallen; ++i)
7155 {
7156 if (value[i] == ',')
7157 {
7158 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007159# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007160 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007161# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007162 valend = i;
7163 break;
7164 }
7165 }
7166
7167 for (i = 0; command_complete[i].expand != 0; ++i)
7168 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007169 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007170 && STRNCMP(value, command_complete[i].name, valend) == 0)
7171 {
7172 *complp = command_complete[i].expand;
7173 if (command_complete[i].expand == EXPAND_BUFFERS)
7174 *argt |= BUFNAME;
7175 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7176 || command_complete[i].expand == EXPAND_FILES)
7177 *argt |= XFILE;
7178 break;
7179 }
7180 }
7181
7182 if (command_complete[i].expand == 0)
7183 {
7184 EMSG2(_("E180: Invalid complete value: %s"), value);
7185 return FAIL;
7186 }
7187
7188# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7189 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7190 && arg != NULL)
7191# else
7192 if (arg != NULL)
7193# endif
7194 {
7195 EMSG(_("E468: Completion argument only allowed for custom completion"));
7196 return FAIL;
7197 }
7198
7199# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7200 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7201 && arg == NULL)
7202 {
7203 EMSG(_("E467: Custom completion requires a function argument"));
7204 return FAIL;
7205 }
7206
7207 if (arg != NULL)
7208 *compl_arg = vim_strnsave(arg, (int)arglen);
7209# endif
7210 return OK;
7211}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007212
7213 int
7214cmdcomplete_str_to_type(char_u *complete_str)
7215{
7216 int i;
7217
7218 for (i = 0; command_complete[i].expand != 0; ++i)
7219 if (STRCMP(complete_str, command_complete[i].name) == 0)
7220 return command_complete[i].expand;
7221
7222 return EXPAND_NOTHING;
7223}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007224#endif
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007227ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
Bram Moolenaare6850792010-05-14 15:28:44 +02007229 if (*eap->arg == NUL)
7230 {
7231#ifdef FEAT_EVAL
7232 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7233 char_u *p = NULL;
7234
7235 if (expr != NULL)
7236 {
7237 ++emsg_off;
7238 p = eval_to_string(expr, NULL, FALSE);
7239 --emsg_off;
7240 vim_free(expr);
7241 }
7242 if (p != NULL)
7243 {
7244 MSG(p);
7245 vim_free(p);
7246 }
7247 else
7248 MSG("default");
7249#else
7250 MSG(_("unknown"));
7251#endif
7252 }
7253 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007254 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255}
7256
7257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007258ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
7260 if (*eap->arg == NUL && eap->cmd[2] == '!')
7261 MSG(_("Greetings, Vim user!"));
7262 do_highlight(eap->arg, eap->forceit, FALSE);
7263}
7264
7265
7266/*
7267 * Call this function if we thought we were going to exit, but we won't
7268 * (because of an error). May need to restore the terminal mode.
7269 */
7270 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007271not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272{
7273 exiting = FALSE;
7274 settmode(TMODE_RAW);
7275}
7276
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007277 static int
7278before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7279{
7280 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7281
7282 /* Bail out when autocommands closed the window.
7283 * Refuse to quit when the buffer in the last window is being closed (can
7284 * only happen in autocommands). */
7285 if (!win_valid(wp)
7286 || curbuf_locked()
7287 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7288 return TRUE;
7289
7290 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7291 {
7292 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7293 /* Refuse to quit when locked or when the buffer in the last window is
7294 * being closed (can only happen in autocommands). */
7295 if (curbuf_locked()
7296 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7297 return TRUE;
7298 }
7299
7300 return FALSE;
7301}
7302
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007304 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007305 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 */
7307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007308ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007310 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#ifdef FEAT_CMDWIN
7313 if (cmdwin_type != 0)
7314 {
7315 cmdwin_result = Ctrl_C;
7316 return;
7317 }
7318#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007319 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007320 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007321 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007322 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007323 return;
7324 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007325 if (eap->addr_count > 0)
7326 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007327 int wnr = eap->line2;
7328
7329 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7330 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007331 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007332 }
7333 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007334 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007335
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007336 /* Refuse to quit when locked. */
7337 if (curbuf_locked())
7338 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007339
7340 /* Trigger QuitPre and maybe ExitPre */
7341 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007342 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
7344#ifdef FEAT_NETBEANS_INTG
7345 netbeansForcedQuit = eap->forceit;
7346#endif
7347
7348 /*
7349 * If there are more files or windows we won't exit.
7350 */
7351 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7352 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007353 if ((!buf_hide(wp->w_buffer)
7354 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007355 | (eap->forceit ? CCGD_FORCEIT : 0)
7356 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007358 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
7360 not_exiting();
7361 }
7362 else
7363 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007364 /* quit last window
7365 * Note: only_one_window() returns true, even so a help window is
7366 * still open. In that case only quit, if no address has been
7367 * specified. Example:
7368 * :h|wincmd w|1q - don't quit
7369 * :h|wincmd w|q - quit
7370 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007371 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007373 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007374#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007378 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 }
7380}
7381
7382/*
7383 * ":cquit".
7384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007386ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 getout(1); /* this does not always pass on the exit code to the Manx
7389 compiler. why? */
7390}
7391
7392/*
7393 * ":qall": try to quit all windows
7394 */
7395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007396ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398# ifdef FEAT_CMDWIN
7399 if (cmdwin_type != 0)
7400 {
7401 if (eap->forceit)
7402 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7403 else
7404 cmdwin_result = K_XF2;
7405 return;
7406 }
7407# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007408
7409 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007410 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007411 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007412 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007413 return;
7414 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007415
7416 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007417 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007420 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 getout(0);
7422 not_exiting();
7423}
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425/*
7426 * ":close": close current window, unless it is the last one
7427 */
7428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007429ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007431 win_T *win;
7432 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007433#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007435 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007437#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007438 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007439 {
7440 if (eap->addr_count == 0)
7441 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007442 else
7443 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007444 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007445 {
7446 winnr++;
7447 if (winnr == eap->line2)
7448 break;
7449 }
7450 if (win == NULL)
7451 win = lastwin;
7452 ex_win_close(eap->forceit, win, NULL);
7453 }
7454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}
7456
Bram Moolenaar4033c552017-09-16 20:54:51 +02007457#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458/*
7459 * ":pclose": Close any preview window.
7460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007462ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463{
7464 win_T *win;
7465
Bram Moolenaar29323592016-07-24 22:04:11 +02007466 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467 if (win->w_p_pvw)
7468 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007469 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007470 break;
7471 }
7472}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007473#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007474
Bram Moolenaarf740b292006-02-16 22:11:02 +00007475/*
7476 * Close window "win" and take care of handling closing the last window for a
7477 * modified buffer.
7478 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007480ex_win_close(
7481 int forceit,
7482 win_T *win,
7483 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 int need_hide;
7486 buf_T *buf = win->w_buffer;
7487
7488 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007489 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007491#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if ((p_confirm || cmdmod.confirm) && p_write)
7493 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007494 bufref_T bufref;
7495
7496 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007498 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 return;
7500 need_hide = FALSE;
7501 }
7502 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007505 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 return;
7507 }
7508 }
7509
Bram Moolenaar4033c552017-09-16 20:54:51 +02007510#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007512#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007513
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007515 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007516 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007517 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007518 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519}
7520
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007522 * Handle the argument for a tabpage related ex command.
7523 * Returns a tabpage number.
7524 * When an error is encountered then eap->errmsg is set.
7525 */
7526 static int
7527get_tabpage_arg(exarg_T *eap)
7528{
7529 int tab_number;
7530 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7531
7532 if (eap->arg && *eap->arg != NUL)
7533 {
7534 char_u *p = eap->arg;
7535 char_u *p_save;
7536 int relative = 0; /* argument +N/-N means: go to N places to the
7537 * right/left relative to the current position. */
7538
7539 if (*p == '-')
7540 {
7541 relative = -1;
7542 p++;
7543 }
7544 else if (*p == '+')
7545 {
7546 relative = 1;
7547 p++;
7548 }
7549
7550 p_save = p;
7551 tab_number = getdigits(&p);
7552
7553 if (relative == 0)
7554 {
7555 if (STRCMP(p, "$") == 0)
7556 tab_number = LAST_TAB_NR;
7557 else if (p == p_save || *p_save == '-' || *p != NUL
7558 || tab_number > LAST_TAB_NR)
7559 {
7560 /* No numbers as argument. */
7561 eap->errmsg = e_invarg;
7562 goto theend;
7563 }
7564 }
7565 else
7566 {
7567 if (*p_save == NUL)
7568 tab_number = 1;
7569 else if (p == p_save || *p_save == '-' || *p != NUL
7570 || tab_number == 0)
7571 {
7572 /* No numbers as argument. */
7573 eap->errmsg = e_invarg;
7574 goto theend;
7575 }
7576 tab_number = tab_number * relative + tabpage_index(curtab);
7577 if (!unaccept_arg0 && relative == -1)
7578 --tab_number;
7579 }
7580 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7581 eap->errmsg = e_invarg;
7582 }
7583 else if (eap->addr_count > 0)
7584 {
7585 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007586 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007587 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007588 tab_number = 0;
7589 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007590 else
7591 {
7592 tab_number = eap->line2;
7593 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7594 {
7595 --tab_number;
7596 if (tab_number < unaccept_arg0)
7597 eap->errmsg = e_invarg;
7598 }
7599 }
7600 }
7601 else
7602 {
7603 switch (eap->cmdidx)
7604 {
7605 case CMD_tabnext:
7606 tab_number = tabpage_index(curtab) + 1;
7607 if (tab_number > LAST_TAB_NR)
7608 tab_number = 1;
7609 break;
7610 case CMD_tabmove:
7611 tab_number = LAST_TAB_NR;
7612 break;
7613 default:
7614 tab_number = tabpage_index(curtab);
7615 }
7616 }
7617
7618theend:
7619 return tab_number;
7620}
7621
7622/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623 * ":tabclose": close current tab page, unless it is the last one.
7624 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 */
7626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007627ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007629 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007630 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007631
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007632# ifdef FEAT_CMDWIN
7633 if (cmdwin_type != 0)
7634 cmdwin_result = K_IGNORE;
7635 else
7636# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007638 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007641 tab_number = get_tabpage_arg(eap);
7642 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007643 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007644 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007645 if (tp == NULL)
7646 {
7647 beep_flush();
7648 return;
7649 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007650 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007651 {
7652 tabpage_close_other(tp, eap->forceit);
7653 return;
7654 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007655 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007656 tabpage_close(eap->forceit);
7657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007659}
7660
7661/*
7662 * ":tabonly": close all tab pages except the current one
7663 */
7664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007665ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007666{
7667 tabpage_T *tp;
7668 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007669 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007670
7671# ifdef FEAT_CMDWIN
7672 if (cmdwin_type != 0)
7673 cmdwin_result = K_IGNORE;
7674 else
7675# endif
7676 if (first_tabpage->tp_next == NULL)
7677 MSG(_("Already only one tab page"));
7678 else
7679 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007680 tab_number = get_tabpage_arg(eap);
7681 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007682 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007683 goto_tabpage(tab_number);
7684 /* Repeat this up to a 1000 times, because autocommands may
7685 * mess up the lists. */
7686 for (done = 0; done < 1000; ++done)
7687 {
7688 FOR_ALL_TABPAGES(tp)
7689 if (tp->tp_topframe != topframe)
7690 {
7691 tabpage_close_other(tp, eap->forceit);
7692 /* if we failed to close it quit */
7693 if (valid_tabpage(tp))
7694 done = 1000;
7695 /* start over, "tp" is now invalid */
7696 break;
7697 }
7698 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007699 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007700 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007701 }
7702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704
7705/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007706 * Close the current tab page.
7707 */
7708 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007709tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007710{
7711 /* First close all the windows but the current one. If that worked then
7712 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007713 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007714 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007715 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007716 ex_win_close(forceit, curwin, NULL);
7717# ifdef FEAT_GUI
7718 need_mouse_correct = TRUE;
7719# endif
7720}
7721
7722/*
7723 * Close tab page "tp", which is not the current tab page.
7724 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007725 * Also takes care of the tab pages line disappearing when closing the
7726 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007727 */
7728 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007729tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007730{
7731 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007732 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007733 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007734
7735 /* Limit to 1000 windows, autocommands may add a window while we close
7736 * one. OK, so I'm paranoid... */
7737 while (++done < 1000)
7738 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007739 wp = tp->tp_firstwin;
7740 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007741
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007742 /* Autocommands may delete the tab page under our fingers and we may
7743 * fail to close a window with a modified buffer. */
7744 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007745 break;
7746 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007747
Bram Moolenaar29323592016-07-24 22:04:11 +02007748 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007749
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007750 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007751 if (h != tabline_height())
7752 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007753}
7754
7755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * ":only".
7757 */
7758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007759ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007761 win_T *wp;
7762 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763# ifdef FEAT_GUI
7764 need_mouse_correct = TRUE;
7765# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007766 if (eap->addr_count > 0)
7767 {
7768 wnr = eap->line2;
7769 for (wp = firstwin; --wnr > 0; )
7770 {
7771 if (wp->w_next == NULL)
7772 break;
7773 else
7774 wp = wp->w_next;
7775 }
7776 win_goto(wp);
7777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 close_others(TRUE, eap->forceit);
7779}
7780
7781/*
7782 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007783 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007785 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007786ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 if (eap->addr_count == 0)
7789 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007790 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
7793 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007794ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007796 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007797 if (!eap->skip)
7798 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007799#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007800 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007801#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007802 if (eap->addr_count == 0)
7803 win_close(curwin, FALSE); /* don't free buffer */
7804 else
7805 {
7806 int winnr = 0;
7807 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007808
Bram Moolenaar2256c992016-11-15 21:17:07 +01007809 FOR_ALL_WINDOWS(win)
7810 {
7811 winnr++;
7812 if (winnr == eap->line2)
7813 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007814 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007815 if (win == NULL)
7816 win = lastwin;
7817 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
7820}
7821
7822/*
7823 * ":stop" and ":suspend": Suspend Vim.
7824 */
7825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007826ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 /*
7829 * Disallow suspending for "rvim".
7830 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007831 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {
7833 if (!eap->forceit)
7834 autowrite_all();
7835 windgoto((int)Rows - 1, 0);
7836 out_char('\n');
7837 out_flush();
7838 stoptermcap();
7839 out_flush(); /* needed for SUN to restore xterm buffer */
7840#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007841 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842#endif
7843 ui_suspend(); /* call machine specific function */
7844#ifdef FEAT_TITLE
7845 maketitle();
7846 resettitle(); /* force updating the title */
7847#endif
7848 starttermcap();
7849 scroll_start(); /* scroll screen before redrawing */
7850 redraw_later_clear();
7851 shell_resized(); /* may have resized window */
7852 }
7853}
7854
7855/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007856 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 */
7858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861#ifdef FEAT_CMDWIN
7862 if (cmdwin_type != 0)
7863 {
7864 cmdwin_result = Ctrl_C;
7865 return;
7866 }
7867#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007868 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007869 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007870 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007871 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007872 return;
7873 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007874
7875 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007876 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877
7878 /*
7879 * if more files or windows we won't exit
7880 */
7881 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7882 exiting = TRUE;
7883 if ( ((eap->cmdidx == CMD_wq
7884 || curbufIsChanged())
7885 && do_write(eap) == FAIL)
7886 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007887 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
7889 not_exiting();
7890 }
7891 else
7892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007895 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896# ifdef FEAT_GUI
7897 need_mouse_correct = TRUE;
7898# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007899 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007900 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902}
7903
7904/*
7905 * ":print", ":list", ":number".
7906 */
7907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007908ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007910 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7911 EMSG(_(e_emptybuf));
7912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 for ( ;!got_int; ui_breakcheck())
7915 {
7916 print_line(eap->line1,
7917 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7918 || (eap->flags & EXFLAG_NR)),
7919 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7920 if (++eap->line1 > eap->line2)
7921 break;
7922 out_flush(); /* show one line at a time */
7923 }
7924 setpcmark();
7925 /* put cursor at last line */
7926 curwin->w_cursor.lnum = eap->line2;
7927 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931}
7932
7933#ifdef FEAT_BYTEOFF
7934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
7937 goto_byte(eap->line2);
7938}
7939#endif
7940
7941/*
7942 * ":shell".
7943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007945ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
7947 do_shell(NULL, 0);
7948}
7949
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007950#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007952static int drop_busy = FALSE;
7953static int drop_filec;
7954static char_u **drop_filev = NULL;
7955static int drop_split;
7956static void (*drop_callback)(void *);
7957static void *drop_cookie;
7958
7959 static void
7960handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 exarg_T ea;
7963 int save_msg_scroll = msg_scroll;
7964
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007965 // Setting the argument list may cause screen updates and being called
7966 // recursively. Avoid that by setting drop_busy.
7967 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
7969 /* Check whether the current buffer is changed. If so, we will need
7970 * to split the current window or data could be lost.
7971 * We don't need to check if the 'hidden' option is set, as in this
7972 * case the buffer won't be lost.
7973 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007974 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
7976 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007977 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 --emsg_off;
7979 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007980 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if (win_split(0, 0) == FAIL)
7983 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007984 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
7986 /* When splitting the window, create a new alist. Otherwise the
7987 * existing one is overwritten. */
7988 alist_unlink(curwin->w_alist);
7989 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 }
7991
7992 /*
7993 * Set up the new argument list.
7994 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007995 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997 /*
7998 * Move to the first file.
7999 */
8000 /* Fake up a minimal "next" command for do_argfile() */
8001 vim_memset(&ea, 0, sizeof(ea));
8002 ea.cmd = (char_u *)"next";
8003 do_argfile(&ea, 0);
8004
8005 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8006 * mode that is not needed here. */
8007 need_start_insertmode = FALSE;
8008
8009 /* Restore msg_scroll, otherwise a following command may cause scrolling
8010 * unexpectedly. The screen will be redrawn by the caller, thus
8011 * msg_scroll being set by displaying a message is irrelevant. */
8012 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008013
8014 if (drop_callback != NULL)
8015 drop_callback(drop_cookie);
8016
8017 drop_filev = NULL;
8018 drop_busy = FALSE;
8019}
8020
8021/*
8022 * Handle a file drop. The code is here because a drop is *nearly* like an
8023 * :args command, but not quite (we have a list of exact filenames, so we
8024 * don't want to (a) parse a command line, or (b) expand wildcards. So the
8025 * code is very similar to :args and hence needs access to a lot of the static
8026 * functions in this file.
8027 *
8028 * The "filev" list must have been allocated using alloc(), as should each item
8029 * in the list. This function takes over responsibility for freeing the "filev"
8030 * list.
8031 */
8032 void
8033handle_drop(
8034 int filec, // the number of files dropped
8035 char_u **filev, // the list of files dropped
8036 int split, // force splitting the window
8037 void (*callback)(void *), // to be called after setting the argument
8038 // list
8039 void *cookie) // argument for "callback" (allocated)
8040{
8041 // Cannot handle recursive drops, finish the pending one.
8042 if (drop_busy)
8043 {
8044 FreeWild(filec, filev);
8045 vim_free(cookie);
8046 return;
8047 }
8048
8049 // When calling handle_drop() more than once in a row we only use the last
8050 // one.
8051 if (drop_filev != NULL)
8052 {
8053 FreeWild(drop_filec, drop_filev);
8054 vim_free(drop_cookie);
8055 }
8056
8057 drop_filec = filec;
8058 drop_filev = filev;
8059 drop_split = split;
8060 drop_callback = callback;
8061 drop_cookie = cookie;
8062
8063 // Postpone this when:
8064 // - editing the command line
8065 // - not possible to change the current buffer
8066 // - updating the screen
8067 // As it may change buffers and window structures that are in use and cause
8068 // freed memory to be used.
8069 if (text_locked() || curbuf_locked() || updating_screen)
8070 return;
8071
8072 handle_drop_internal();
8073}
8074
8075/*
8076 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8077 * reset: Handle a postponed drop.
8078 */
8079 void
8080handle_any_postponed_drop(void)
8081{
8082 if (!drop_busy && drop_filev != NULL
8083 && !text_locked() && !curbuf_locked() && !updating_screen)
8084 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085}
8086#endif
8087
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088/*
8089 * Clear an argument list: free all file names and reset it to zero entries.
8090 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008092alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
8094 while (--al->al_ga.ga_len >= 0)
8095 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8096 ga_clear(&al->al_ga);
8097}
8098
8099/*
8100 * Init an argument list.
8101 */
8102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008103alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8106}
8107
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108/*
8109 * Remove a reference from an argument list.
8110 * Ignored when the argument list is the global one.
8111 * If the argument list is no longer used by any window, free it.
8112 */
8113 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008114alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
8116 if (al != &global_alist && --al->al_refcount <= 0)
8117 {
8118 alist_clear(al);
8119 vim_free(al);
8120 }
8121}
8122
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123/*
8124 * Create a new argument list and use it for the current window.
8125 */
8126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008127alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8130 if (curwin->w_alist == NULL)
8131 {
8132 curwin->w_alist = &global_alist;
8133 ++global_alist.al_refcount;
8134 }
8135 else
8136 {
8137 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008138 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 alist_init(curwin->w_alist);
8140 }
8141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008143#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144/*
8145 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008146 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8147 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 */
8149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008150alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008153 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 char_u **new_arg_files;
8155 int new_arg_file_count;
8156 char_u *save_p_su = p_su;
8157 int i;
8158
8159 /* Don't use 'suffixes' here. This should work like the shell did the
8160 * expansion. Also, the vimrc file isn't read yet, thus the user
8161 * can't set the options. */
8162 p_su = empty_option;
8163 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8164 if (old_arg_files != NULL)
8165 {
8166 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008167 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8168 old_arg_count = GARGCOUNT;
8169 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008171 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 && new_arg_file_count > 0)
8173 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008174 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8175 TRUE, fnum_list, fnum_len);
8176 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179 p_su = save_p_su;
8180}
8181#endif
8182
8183/*
8184 * Set the argument list for the current window.
8185 * Takes over the allocated files[] and the allocated fnames in it.
8186 */
8187 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008188alist_set(
8189 alist_T *al,
8190 int count,
8191 char_u **files,
8192 int use_curbuf,
8193 int *fnum_list,
8194 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008197 static int recursive = 0;
8198
8199 if (recursive)
8200 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008201 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008202 return;
8203 }
8204 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
8206 alist_clear(al);
8207 if (ga_grow(&al->al_ga, count) == OK)
8208 {
8209 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008210 {
8211 if (got_int)
8212 {
8213 /* When adding many buffers this can take a long time. Allow
8214 * interrupting here. */
8215 while (i < count)
8216 vim_free(files[i++]);
8217 break;
8218 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008219
8220 /* May set buffer name of a buffer previously used for the
8221 * argument list, so that it's re-used by alist_add. */
8222 if (fnum_list != NULL && i < fnum_len)
8223 buf_set_name(fnum_list[i], files[i]);
8224
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008226 ui_breakcheck();
8227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 vim_free(files);
8229 }
8230 else
8231 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008234
8235 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236}
8237
8238/*
8239 * Add file "fname" to argument list "al".
8240 * "fname" must have been allocated and "al" must have been checked for room.
8241 */
8242 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008243alist_add(
8244 alist_T *al,
8245 char_u *fname,
8246 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248 if (fname == NULL) /* don't add NULL file names */
8249 return;
8250#ifdef BACKSLASH_IN_FILENAME
8251 slash_adjust(fname);
8252#endif
8253 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8254 if (set_fnum > 0)
8255 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8256 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8257 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258}
8259
8260#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8261/*
8262 * Adjust slashes in file names. Called after 'shellslash' was set.
8263 */
8264 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008265alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266{
8267 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008269 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270
8271 for (i = 0; i < GARGCOUNT; ++i)
8272 if (GARGLIST[i].ae_fname != NULL)
8273 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008274 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 if (wp->w_alist != &global_alist)
8276 for (i = 0; i < WARGCOUNT(wp); ++i)
8277 if (WARGLIST(wp)[i].ae_fname != NULL)
8278 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279}
8280#endif
8281
8282/*
8283 * ":preserve".
8284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008286ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008288 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 ml_preserve(curbuf, TRUE);
8290}
8291
8292/*
8293 * ":recover".
8294 */
8295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008296ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8299 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008300 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8301 | CCGD_MULTWIN
8302 | (eap->forceit ? CCGD_FORCEIT : 0)
8303 | CCGD_EXCMD)
8304
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 && (*eap->arg == NUL
8306 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8307 ml_recover();
8308 recoverymode = FALSE;
8309}
8310
8311/*
8312 * Command modifier used in a wrong way.
8313 */
8314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008315ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
8317 eap->errmsg = e_invcmd;
8318}
8319
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320/*
8321 * :sview [+command] file split window with new file, read-only
8322 * :split [[+command] file] split window with current or new file
8323 * :vsplit [[+command] file] split window vertically with current or new file
8324 * :new [[+command] file] split window with no or new file
8325 * :vnew [[+command] file] split vertically window with no or new file
8326 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008327 *
8328 * :tabedit open new Tab page with empty window
8329 * :tabedit [+command] file open new Tab page and edit "file"
8330 * :tabnew [[+command] file] just like :tabedit
8331 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 */
8333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008334ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008336 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008337#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008339#endif
8340#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008342#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008343 int use_tab = eap->cmdidx == CMD_tabedit
8344 || eap->cmdidx == CMD_tabfind
8345 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
Bram Moolenaar4033c552017-09-16 20:54:51 +02008347#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350
Bram Moolenaar4033c552017-09-16 20:54:51 +02008351#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008353 * quickfix windows. But it's OK when doing ":tab split". */
8354 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 if (eap->cmdidx == CMD_split)
8357 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 if (eap->cmdidx == CMD_vsplit)
8359 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
Bram Moolenaar4033c552017-09-16 20:54:51 +02008363#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008364 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
8366 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8367 FNAME_MESS, TRUE, curbuf->b_ffname);
8368 if (fname == NULL)
8369 goto theend;
8370 eap->arg = fname;
8371 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008372# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008373 else
8374# endif
8375#endif
8376#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 && eap->cmdidx != CMD_new)
8380 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008381 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008382# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008383 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008384# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008385 au_has_group((char_u *)"FileExplorer"))
8386 {
8387 /* No browsing supported but we do have the file explorer:
8388 * Edit the directory. */
8389 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8390 eap->arg = (char_u *)".";
8391 }
8392 else
8393 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008394 fname = do_browse(0, (char_u *)(use_tab
8395 ? _("Edit File in new tab page")
8396 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008398 if (fname == NULL)
8399 goto theend;
8400 eap->arg = fname;
8401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 }
8403 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008406 /*
8407 * Either open new tab page or split the window.
8408 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008409 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008410 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008411 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8412 : eap->addr_count == 0 ? 0
8413 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008414 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008415 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008416
8417 /* set the alternate buffer for the window we came from */
8418 if (curwin != old_curwin
8419 && win_valid(old_curwin)
8420 && old_curwin->w_buffer != curbuf
8421 && !cmdmod.keepalt)
8422 old_curwin->w_alt_fnum = curbuf->b_fnum;
8423 }
8424 }
8425 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 /* Reset 'scrollbind' when editing another file, but keep it when
8429 * doing ":split" without arguments. */
8430 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008431# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008435 {
8436 RESET_BINDING(curwin);
8437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 else
8439 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 do_exedit(eap, old_curwin);
8441 }
8442
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008443# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008447# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448theend:
8449 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008452
8453/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008454 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008455 */
8456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008457tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008458{
8459 exarg_T ea;
8460
8461 vim_memset(&ea, 0, sizeof(ea));
8462 ea.cmdidx = CMD_tabnew;
8463 ea.cmd = (char_u *)"tabn";
8464 ea.arg = (char_u *)"";
8465 ex_splitview(&ea);
8466}
8467
8468/*
8469 * :tabnext command
8470 */
8471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008472ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008473{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008474 int tab_number;
8475
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008476 switch (eap->cmdidx)
8477 {
8478 case CMD_tabfirst:
8479 case CMD_tabrewind:
8480 goto_tabpage(1);
8481 break;
8482 case CMD_tablast:
8483 goto_tabpage(9999);
8484 break;
8485 case CMD_tabprevious:
8486 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008487 if (eap->arg && *eap->arg != NUL)
8488 {
8489 char_u *p = eap->arg;
8490 char_u *p_save = p;
8491
8492 tab_number = getdigits(&p);
8493 if (p == p_save || *p_save == '-' || *p != NUL
8494 || tab_number == 0)
8495 {
8496 /* No numbers as argument. */
8497 eap->errmsg = e_invarg;
8498 return;
8499 }
8500 }
8501 else
8502 {
8503 if (eap->addr_count == 0)
8504 tab_number = 1;
8505 else
8506 {
8507 tab_number = eap->line2;
8508 if (tab_number < 1)
8509 {
8510 eap->errmsg = e_invrange;
8511 return;
8512 }
8513 }
8514 }
8515 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008516 break;
8517 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008518 tab_number = get_tabpage_arg(eap);
8519 if (eap->errmsg == NULL)
8520 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008521 break;
8522 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008523}
8524
8525/*
8526 * :tabmove command
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008530{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008531 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008532
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008533 tab_number = get_tabpage_arg(eap);
8534 if (eap->errmsg == NULL)
8535 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008536}
8537
8538/*
8539 * :tabs command: List tabs and their contents.
8540 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008542ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008543{
8544 tabpage_T *tp;
8545 win_T *wp;
8546 int tabcount = 1;
8547
8548 msg_start();
8549 msg_scroll = TRUE;
8550 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8551 {
8552 msg_putchar('\n');
8553 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008554 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008555 out_flush(); /* output one line at a time */
8556 ui_breakcheck();
8557
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008558 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008559 wp = firstwin;
8560 else
8561 wp = tp->tp_firstwin;
8562 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8563 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008564 msg_putchar('\n');
8565 msg_putchar(wp == curwin ? '>' : ' ');
8566 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008567 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8568 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008569 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008570 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008571 else
8572 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8573 IObuff, IOSIZE, TRUE);
8574 msg_outtrans(IObuff);
8575 out_flush(); /* output one line at a time */
8576 ui_breakcheck();
8577 }
8578 }
8579}
8580
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581/*
8582 * ":mode": Set screen mode.
8583 * If no argument given, just get the screen size and redraw.
8584 */
8585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008586ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
8588 if (*eap->arg == NUL)
8589 shell_resized();
8590 else
8591 mch_screenmode(eap->arg);
8592}
8593
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594/*
8595 * ":resize".
8596 * set, increment or decrement current window height
8597 */
8598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008599ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 int n;
8602 win_T *wp = curwin;
8603
8604 if (eap->addr_count > 0)
8605 {
8606 n = eap->line2;
8607 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8608 ;
8609 }
8610
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008611# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (cmdmod.split & WSP_VERT)
8616 {
8617 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008618 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8620 n = 9999;
8621 win_setwidth_win((int)n, wp);
8622 }
8623 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {
8625 if (*eap->arg == '-' || *eap->arg == '+')
8626 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008627 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 n = 9999;
8629 win_setheight_win((int)n, wp);
8630 }
8631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632
8633/*
8634 * ":find [+command] <file>" command.
8635 */
8636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008637ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
8639#ifdef FEAT_SEARCHPATH
8640 char_u *fname;
8641 int count;
8642
8643 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8644 TRUE, curbuf->b_ffname);
8645 if (eap->addr_count > 0)
8646 {
8647 /* Repeat finding the file "count" times. This matters when it
8648 * appears several times in the path. */
8649 count = eap->line2;
8650 while (fname != NULL && --count > 0)
8651 {
8652 vim_free(fname);
8653 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8654 FALSE, curbuf->b_ffname);
8655 }
8656 }
8657
8658 if (fname != NULL)
8659 {
8660 eap->arg = fname;
8661#endif
8662 do_exedit(eap, NULL);
8663#ifdef FEAT_SEARCHPATH
8664 vim_free(fname);
8665 }
8666#endif
8667}
8668
8669/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008670 * ":open" simulation: for now just work like ":visual".
8671 */
8672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008673ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008674{
8675 regmatch_T regmatch;
8676 char_u *p;
8677
8678 curwin->w_cursor.lnum = eap->line2;
8679 beginline(BL_SOL | BL_FIX);
8680 if (*eap->arg == '/')
8681 {
8682 /* ":open /pattern/": put cursor in column found with pattern */
8683 ++eap->arg;
8684 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8685 *p = NUL;
8686 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8687 if (regmatch.regprog != NULL)
8688 {
8689 regmatch.rm_ic = p_ic;
8690 p = ml_get_curline();
8691 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008692 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008693 else
8694 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008695 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008696 }
8697 /* Move to the NUL, ignore any other arguments. */
8698 eap->arg += STRLEN(eap->arg);
8699 }
8700 check_cursor();
8701
8702 eap->cmdidx = CMD_visual;
8703 do_exedit(eap, NULL);
8704}
8705
8706/*
8707 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 */
8709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 do_exedit(eap, NULL);
8713}
8714
8715/*
8716 * ":edit <file>" command and alikes.
8717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008719do_exedit(
8720 exarg_T *eap,
8721 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
8723 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008725 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726
8727 /*
8728 * ":vi" command ends Ex mode.
8729 */
8730 if (exmode_active && (eap->cmdidx == CMD_visual
8731 || eap->cmdidx == CMD_view))
8732 {
8733 exmode_active = FALSE;
8734 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008735 {
8736 /* Special case: ":global/pat/visual\NLvi-commands" */
8737 if (global_busy)
8738 {
8739 int rd = RedrawingDisabled;
8740 int nwr = no_wait_return;
8741 int ms = msg_scroll;
8742#ifdef FEAT_GUI
8743 int he = hold_gui_events;
8744#endif
8745
8746 if (eap->nextcmd != NULL)
8747 {
8748 stuffReadbuff(eap->nextcmd);
8749 eap->nextcmd = NULL;
8750 }
8751
8752 if (exmode_was != EXMODE_VIM)
8753 settmode(TMODE_RAW);
8754 RedrawingDisabled = 0;
8755 no_wait_return = 0;
8756 need_wait_return = FALSE;
8757 msg_scroll = 0;
8758#ifdef FEAT_GUI
8759 hold_gui_events = 0;
8760#endif
8761 must_redraw = CLEAR;
8762
8763 main_loop(FALSE, TRUE);
8764
8765 RedrawingDisabled = rd;
8766 no_wait_return = nwr;
8767 msg_scroll = ms;
8768#ifdef FEAT_GUI
8769 hold_gui_events = he;
8770#endif
8771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
8775
8776 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008777 || eap->cmdidx == CMD_tabnew
8778 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008779 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008781 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 setpcmark();
8783 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008784 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8785 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008787 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 || *eap->arg != NUL
8789#ifdef FEAT_BROWSE
8790 || cmdmod.browse
8791#endif
8792 )
8793 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008794 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8795 * can bring us here, others are stopped earlier. */
8796 if (*eap->arg != NUL && curbuf_locked())
8797 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 n = readonlymode;
8800 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8801 readonlymode = TRUE;
8802 else if (eap->cmdidx == CMD_enew)
8803 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8804 empty buffer */
8805 setpcmark();
8806 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8807 NULL, eap,
8808 /* ":edit" goes to first line if Vi compatible */
8809 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8810 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8811 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008812 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008814 /* after a split we can use an existing buffer */
8815 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008817 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {
8819 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 if (old_curwin != NULL)
8821 {
8822 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008823 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008825#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008826 cleanup_T cs;
8827
8828 /* Reset the error/interrupt/exception state here so that
8829 * aborting() returns FALSE when closing a window. */
8830 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008831#endif
8832#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008834#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008835 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008836
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008837#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008838 /* Restore the error/interrupt/exception state if not
8839 * discarded by a new aborting error, interrupt, or
8840 * uncaught exception. */
8841 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 }
8844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 }
8846 else if (readonlymode && curbuf->b_nwindows == 1)
8847 {
8848 /* When editing an already visited buffer, 'readonly' won't be set
8849 * but the previous value is kept. With ":view" and ":sview" we
8850 * want the file to be readonly, except when another window is
8851 * editing the same buffer. */
8852 curbuf->b_p_ro = TRUE;
8853 }
8854 readonlymode = n;
8855 }
8856 else
8857 {
8858 if (eap->do_ecmd_cmd != NULL)
8859 do_cmdline_cmd(eap->do_ecmd_cmd);
8860#ifdef FEAT_TITLE
8861 n = curwin->w_arg_idx_invalid;
8862#endif
8863 check_arg_idx(curwin);
8864#ifdef FEAT_TITLE
8865 if (n != curwin->w_arg_idx_invalid)
8866 maketitle();
8867#endif
8868 }
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 /*
8871 * if ":split file" worked, set alternate file name in old window to new
8872 * file
8873 */
8874 if (old_curwin != NULL
8875 && *eap->arg != NUL
8876 && curwin != old_curwin
8877 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008878 && old_curwin->w_buffer != curbuf
8879 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
8882 ex_no_reprint = TRUE;
8883}
8884
8885#ifndef FEAT_GUI
8886/*
8887 * ":gui" and ":gvim" when there is no GUI.
8888 */
8889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008890ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891{
8892 eap->errmsg = e_nogvim;
8893}
8894#endif
8895
8896#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008898ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 gui_make_tearoff(eap->arg);
8901}
8902#endif
8903
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008904#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8905 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008907ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008909# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8910 if (gui.in_use)
8911 gui_make_popup(eap->arg, eap->forceit);
8912# ifdef FEAT_TERM_POPUP_MENU
8913 else
8914# endif
8915# endif
8916# ifdef FEAT_TERM_POPUP_MENU
8917 pum_make_popup(eap->arg, eap->forceit);
8918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919}
8920#endif
8921
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008923ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8926 MSG(_("No swap file"));
8927 else
8928 msg(curbuf->b_ml.ml_mfp->mf_fname);
8929}
8930
8931/*
8932 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8933 * offset.
8934 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008937ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008940 win_T *save_curwin = curwin;
8941 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 long topline;
8943 long y;
8944 linenr_T old_linenr = curwin->w_cursor.lnum;
8945
8946 setpcmark();
8947
8948 /*
8949 * determine max topline
8950 */
8951 if (curwin->w_p_scb)
8952 {
8953 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008954 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
8956 if (wp->w_p_scb && wp->w_buffer)
8957 {
8958 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8959 if (topline > y)
8960 topline = y;
8961 }
8962 }
8963 if (topline < 1)
8964 topline = 1;
8965 }
8966 else
8967 {
8968 topline = 1;
8969 }
8970
8971
8972 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008973 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008975 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 {
8977 if (curwin->w_p_scb)
8978 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008979 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 y = topline - curwin->w_topline;
8981 if (y > 0)
8982 scrollup(y, TRUE);
8983 else
8984 scrolldown(-y, TRUE);
8985 curwin->w_scbind_pos = topline;
8986 redraw_later(VALID);
8987 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008991 curwin = save_curwin;
8992 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 if (curwin->w_p_scb)
8994 {
8995 did_syncbind = TRUE;
8996 checkpcmark();
8997 if (old_linenr != curwin->w_cursor.lnum)
8998 {
8999 char_u ctrl_o[2];
9000
9001 ctrl_o[0] = Ctrl_O;
9002 ctrl_o[1] = 0;
9003 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9004 }
9005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
9008
9009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009010ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009012 int i;
9013 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9014 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015
9016 if (eap->usefilter) /* :r!cmd */
9017 do_bang(1, eap, FALSE, FALSE, TRUE);
9018 else
9019 {
9020 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9021 return;
9022
9023#ifdef FEAT_BROWSE
9024 if (cmdmod.browse)
9025 {
9026 char_u *browseFile;
9027
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009028 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 NULL, NULL, NULL, curbuf);
9030 if (browseFile != NULL)
9031 {
9032 i = readfile(browseFile, NULL,
9033 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9034 vim_free(browseFile);
9035 }
9036 else
9037 i = OK;
9038 }
9039 else
9040#endif
9041 if (*eap->arg == NUL)
9042 {
9043 if (check_fname() == FAIL) /* check for no file name */
9044 return;
9045 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9046 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9047 }
9048 else
9049 {
9050 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9051 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9052 i = readfile(eap->arg, NULL,
9053 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9054
9055 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009056 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009058#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 if (!aborting())
9060#endif
9061 EMSG2(_(e_notopen), eap->arg);
9062 }
9063 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009064 {
9065 if (empty && exmode_active)
9066 {
9067 /* Delete the empty line that remains. Historically ex does
9068 * this but vi doesn't. */
9069 if (eap->line2 == 0)
9070 lnum = curbuf->b_ml.ml_line_count;
9071 else
9072 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009073 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009074 {
9075 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009076 if (curwin->w_cursor.lnum > 1
9077 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009078 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009079 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009080 }
9081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 }
9085}
9086
Bram Moolenaarea408852005-06-25 22:49:46 +00009087static char_u *prev_dir = NULL;
9088
9089#if defined(EXITFREE) || defined(PROTO)
9090 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009091free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009092{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009093 VIM_CLEAR(prev_dir);
9094 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009095}
9096#endif
9097
Bram Moolenaarf4258302013-06-02 18:20:17 +02009098/*
9099 * Deal with the side effects of changing the current directory.
9100 * When "local" is TRUE then this was after an ":lcd" command.
9101 */
9102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009103post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009104{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009105 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009106 if (local)
9107 {
9108 /* If still in global directory, need to remember current
9109 * directory as global directory. */
9110 if (globaldir == NULL && prev_dir != NULL)
9111 globaldir = vim_strsave(prev_dir);
9112 /* Remember this local directory for the window. */
9113 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9114 curwin->w_localdir = vim_strsave(NameBuff);
9115 }
9116 else
9117 {
9118 /* We are now in the global directory, no need to remember its
9119 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009120 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009121 }
9122
9123 shorten_fnames(TRUE);
9124}
9125
Bram Moolenaarea408852005-06-25 22:49:46 +00009126
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127/*
9128 * ":cd", ":lcd", ":chdir" and ":lchdir".
9129 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009131ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009133 char_u *new_dir;
9134 char_u *tofree;
9135 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136
9137 new_dir = eap->arg;
9138#if !defined(UNIX) && !defined(VMS)
9139 /* for non-UNIX ":cd" means: print current directory */
9140 if (*new_dir == NUL)
9141 ex_pwd(NULL);
9142 else
9143#endif
9144 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009145 if (allbuf_locked())
9146 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009147 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9148 && !eap->forceit)
9149 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009150 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009151 return;
9152 }
9153
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 /* ":cd -": Change to previous directory */
9155 if (STRCMP(new_dir, "-") == 0)
9156 {
9157 if (prev_dir == NULL)
9158 {
9159 EMSG(_("E186: No previous directory"));
9160 return;
9161 }
9162 new_dir = prev_dir;
9163 }
9164
9165 /* Save current directory for next ":cd -" */
9166 tofree = prev_dir;
9167 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9168 prev_dir = vim_strsave(NameBuff);
9169 else
9170 prev_dir = NULL;
9171
9172#if defined(UNIX) || defined(VMS)
9173 /* for UNIX ":cd" means: go to home directory */
9174 if (*new_dir == NUL)
9175 {
9176 /* use NameBuff for home directory name */
9177# ifdef VMS
9178 char_u *p;
9179
9180 p = mch_getenv((char_u *)"SYS$LOGIN");
9181 if (p == NULL || *p == NUL) /* empty is the same as not set */
9182 NameBuff[0] = NUL;
9183 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009184 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185# else
9186 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9187# endif
9188 new_dir = NameBuff;
9189 }
9190#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009191 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01009192 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009193 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 EMSG(_(e_failed));
9195 else
9196 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009197 int is_local_chdir = eap->cmdidx == CMD_lcd
9198 || eap->cmdidx == CMD_lchdir;
9199
9200 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009203 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01009205
9206 if (dir_differs)
9207 apply_autocmds(EVENT_DIRCHANGED,
9208 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9209 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 }
9211 vim_free(tofree);
9212 }
9213}
9214
9215/*
9216 * ":pwd".
9217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009219ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220{
9221 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9222 {
9223#ifdef BACKSLASH_IN_FILENAME
9224 slash_adjust(NameBuff);
9225#endif
9226 msg(NameBuff);
9227 }
9228 else
9229 EMSG(_("E187: Unknown"));
9230}
9231
9232/*
9233 * ":=".
9234 */
9235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009236ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009239 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240}
9241
9242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009243ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009245 int n;
9246 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247
9248 if (cursor_valid())
9249 {
9250 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9251 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009252 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009254
9255 len = eap->line2;
9256 switch (*eap->arg)
9257 {
9258 case 'm': break;
9259 case NUL: len *= 1000L; break;
9260 default: EMSG2(_(e_invarg2), eap->arg); return;
9261 }
9262 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263}
9264
9265/*
9266 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9267 */
9268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009269do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270{
9271 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009272 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273
9274 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009275 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009276 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009278 wait_now = msec - done > 1000L ? 1000L : msec - done;
9279#ifdef FEAT_TIMERS
9280 {
9281 long due_time = check_due_timer();
9282
9283 if (due_time > 0 && due_time < wait_now)
9284 wait_now = due_time;
9285 }
9286#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009287#ifdef FEAT_JOB_CHANNEL
9288 if (has_any_channel() && wait_now > 100L)
9289 wait_now = 100L;
9290#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009291 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009292#ifdef FEAT_JOB_CHANNEL
9293 if (has_any_channel())
9294 ui_breakcheck_force(TRUE);
9295 else
9296#endif
9297 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009298#ifdef MESSAGE_QUEUE
9299 /* Process the netbeans and clientserver messages that may have been
9300 * received in the call to ui_breakcheck() when the GUI is in use. This
9301 * may occur when running a test case. */
9302 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009305
9306 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9307 // input buffer, otherwise a following call to input() fails.
9308 if (got_int)
9309 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310}
9311
9312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009313do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315 int mode;
9316 char_u *cmdp;
9317
9318 cmdp = eap->cmd;
9319 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9320
9321 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9322 eap->arg, mode, isabbrev))
9323 {
9324 case 1: EMSG(_(e_invarg));
9325 break;
9326 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9327 break;
9328 }
9329}
9330
9331/*
9332 * ":winsize" command (obsolete).
9333 */
9334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009335ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336{
9337 int w, h;
9338 char_u *arg = eap->arg;
9339 char_u *p;
9340
9341 w = getdigits(&arg);
9342 arg = skipwhite(arg);
9343 p = arg;
9344 h = getdigits(&arg);
9345 if (*p != NUL && *arg == NUL)
9346 set_shellsize(w, h, TRUE);
9347 else
9348 EMSG(_("E465: :winsize requires two number arguments"));
9349}
9350
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009352ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 int xchar = NUL;
9355 char_u *p;
9356
9357 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9358 {
9359 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9360 if (eap->arg[1] == NUL)
9361 {
9362 EMSG(_(e_invarg));
9363 return;
9364 }
9365 xchar = eap->arg[1];
9366 p = eap->arg + 2;
9367 }
9368 else
9369 p = eap->arg + 1;
9370
9371 eap->nextcmd = check_nextcmd(p);
9372 p = skipwhite(p);
9373 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9374 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009375 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
9377 /* Pass flags on for ":vertical wincmd ]". */
9378 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009379 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9381 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009382 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 }
9384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
Bram Moolenaar843ee412004-06-30 16:16:41 +00009386#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387/*
9388 * ":winpos".
9389 */
9390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009391ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392{
9393 int x, y;
9394 char_u *arg = eap->arg;
9395 char_u *p;
9396
9397 if (*arg == NUL)
9398 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009399# if defined(FEAT_GUI) || defined(MSWIN)
9400# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009402# else
9403 if (mch_get_winpos(&x, &y) != FAIL)
9404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 {
9406 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9407 msg(IObuff);
9408 }
9409 else
9410# endif
9411 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9412 }
9413 else
9414 {
9415 x = getdigits(&arg);
9416 arg = skipwhite(arg);
9417 p = arg;
9418 y = getdigits(&arg);
9419 if (*p == NUL || *arg != NUL)
9420 {
9421 EMSG(_("E466: :winpos requires two number arguments"));
9422 return;
9423 }
9424# ifdef FEAT_GUI
9425 if (gui.in_use)
9426 gui_mch_set_winpos(x, y);
9427 else if (gui.starting)
9428 {
9429 /* Remember the coordinates for when the window is opened. */
9430 gui_win_x = x;
9431 gui_win_y = y;
9432 }
9433# ifdef HAVE_TGETENT
9434 else
9435# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009436# else
9437# ifdef MSWIN
9438 mch_set_winpos(x, y);
9439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440# endif
9441# ifdef HAVE_TGETENT
9442 if (*T_CWP)
9443 term_set_winpos(x, y);
9444# endif
9445 }
9446}
9447#endif
9448
9449/*
9450 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9451 */
9452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009453ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454{
9455 oparg_T oa;
9456
9457 clear_oparg(&oa);
9458 oa.regname = eap->regname;
9459 oa.start.lnum = eap->line1;
9460 oa.end.lnum = eap->line2;
9461 oa.line_count = eap->line2 - eap->line1 + 1;
9462 oa.motion_type = MLINE;
9463#ifdef FEAT_VIRTUALEDIT
9464 virtual_op = FALSE;
9465#endif
9466 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9467 {
9468 setpcmark();
9469 curwin->w_cursor.lnum = eap->line1;
9470 beginline(BL_SOL | BL_FIX);
9471 }
9472
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009473 if (VIsual_active)
9474 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009475
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 switch (eap->cmdidx)
9477 {
9478 case CMD_delete:
9479 oa.op_type = OP_DELETE;
9480 op_delete(&oa);
9481 break;
9482
9483 case CMD_yank:
9484 oa.op_type = OP_YANK;
9485 (void)op_yank(&oa, FALSE, TRUE);
9486 break;
9487
9488 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009489 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009491 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9492#else
9493 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009495 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 oa.op_type = OP_RSHIFT;
9497 else
9498 oa.op_type = OP_LSHIFT;
9499 op_shift(&oa, FALSE, eap->amount);
9500 break;
9501 }
9502#ifdef FEAT_VIRTUALEDIT
9503 virtual_op = MAYBE;
9504#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009505 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506}
9507
9508/*
9509 * ":put".
9510 */
9511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009512ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514 /* ":0put" works like ":1put!". */
9515 if (eap->line2 == 0)
9516 {
9517 eap->line2 = 1;
9518 eap->forceit = TRUE;
9519 }
9520 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009521 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9522 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523}
9524
9525/*
9526 * Handle ":copy" and ":move".
9527 */
9528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009529ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 long n;
9532
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009533 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 if (eap->arg == NULL) /* error detected */
9535 {
9536 eap->nextcmd = NULL;
9537 return;
9538 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009539 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 /*
9542 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9543 */
9544 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9545 {
9546 EMSG(_(e_invaddr));
9547 return;
9548 }
9549
9550 if (eap->cmdidx == CMD_move)
9551 {
9552 if (do_move(eap->line1, eap->line2, n) == FAIL)
9553 return;
9554 }
9555 else
9556 ex_copy(eap->line1, eap->line2, n);
9557 u_clearline();
9558 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009559 ex_may_print(eap);
9560}
9561
9562/*
9563 * Print the current line if flags were given to the Ex command.
9564 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009565 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009566ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009567{
9568 if (eap->flags != 0)
9569 {
9570 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9571 (eap->flags & EXFLAG_LIST));
9572 ex_no_reprint = TRUE;
9573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574}
9575
9576/*
9577 * ":smagic" and ":snomagic".
9578 */
9579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009580ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 int magic_save = p_magic;
9583
9584 p_magic = (eap->cmdidx == CMD_smagic);
9585 do_sub(eap);
9586 p_magic = magic_save;
9587}
9588
9589/*
9590 * ":join".
9591 */
9592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009593ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 curwin->w_cursor.lnum = eap->line1;
9596 if (eap->line1 == eap->line2)
9597 {
9598 if (eap->addr_count >= 2) /* :2,2join does nothing */
9599 return;
9600 if (eap->line2 == curbuf->b_ml.ml_line_count)
9601 {
9602 beep_flush();
9603 return;
9604 }
9605 ++eap->line2;
9606 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009607 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009609 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610}
9611
9612/*
9613 * ":[addr]@r" or ":[addr]*r": execute register
9614 */
9615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009616ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
9618 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009619 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620
9621 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009622 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623
9624#ifdef USE_ON_FLY_SCROLL
9625 dont_scroll = TRUE; /* disallow scrolling here */
9626#endif
9627
9628 /* get the register name. No name means to use the previous one */
9629 c = *eap->arg;
9630 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9631 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009632 /* Put the register in the typeahead buffer with the "silent" flag. */
9633 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9634 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 else
9639 {
9640 int save_efr = exec_from_reg;
9641
9642 exec_from_reg = TRUE;
9643
9644 /*
9645 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009646 * Continue until the stuff buffer is empty and all added characters
9647 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009649 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9651
9652 exec_from_reg = save_efr;
9653 }
9654}
9655
9656/*
9657 * ":!".
9658 */
9659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009660ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9663}
9664
9665/*
9666 * ":undo".
9667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009669ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009671 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009672 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009673 else
9674 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675}
9676
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009677#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009679ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009680{
9681 char_u hash[UNDO_HASH_SIZE];
9682
9683 u_compute_hash(hash);
9684 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9685}
9686
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009688ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009689{
9690 char_u hash[UNDO_HASH_SIZE];
9691
9692 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009693 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009694}
9695#endif
9696
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697/*
9698 * ":redo".
9699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009701ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 u_redo(1);
9704}
9705
9706/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009707 * ":earlier" and ":later".
9708 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009710ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009711{
9712 long count = 0;
9713 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009714 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009715 char_u *p = eap->arg;
9716
9717 if (*p == NUL)
9718 count = 1;
9719 else if (isdigit(*p))
9720 {
9721 count = getdigits(&p);
9722 switch (*p)
9723 {
9724 case 's': ++p; sec = TRUE; break;
9725 case 'm': ++p; sec = TRUE; count *= 60; break;
9726 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009727 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9728 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009729 }
9730 }
9731
9732 if (*p != NUL)
9733 EMSG2(_(e_invarg2), eap->arg);
9734 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009735 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9736 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009737}
9738
9739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 * ":redir": start/stop redirection.
9741 */
9742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009743ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744{
9745 char *mode;
9746 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009747 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748
Bram Moolenaarba768492016-07-08 15:32:54 +02009749#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009750 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009751 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009752 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009753 return;
9754 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009755#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009756
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (STRICMP(eap->arg, "END") == 0)
9758 close_redir();
9759 else
9760 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009761 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009763 ++arg;
9764 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009766 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 mode = "a";
9768 }
9769 else
9770 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009771 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772
9773 close_redir();
9774
9775 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009776 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 if (fname == NULL)
9778 return;
9779#ifdef FEAT_BROWSE
9780 if (cmdmod.browse)
9781 {
9782 char_u *browseFile;
9783
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009784 browseFile = do_browse(BROWSE_SAVE,
9785 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009786 fname, NULL, NULL,
9787 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (browseFile == NULL)
9789 return; /* operation cancelled */
9790 vim_free(fname);
9791 fname = browseFile;
9792 eap->forceit = TRUE; /* since dialog already asked */
9793 }
9794#endif
9795
9796 redir_fd = open_exfile(fname, eap->forceit, mode);
9797 vim_free(fname);
9798 }
9799#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009800 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 {
9802 /* redirect to a register a-z (resp. A-Z for appending) */
9803 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009804 ++arg;
9805 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009807 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009808 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009810 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009812 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009813 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009814 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009817 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009818 if (*arg == '>')
9819 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009820 /* Make register empty when not using @A-@Z and the
9821 * command is valid. */
9822 if (*arg == NUL && !isupper(redir_reg))
9823 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009825 }
9826 if (*arg != NUL)
9827 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009828 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009829 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009830 }
9831 }
9832 else if (*arg == '=' && arg[1] == '>')
9833 {
9834 int append;
9835
9836 /* redirect to a variable */
9837 close_redir();
9838 arg += 2;
9839
9840 if (*arg == '>')
9841 {
9842 ++arg;
9843 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 }
9845 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009846 append = FALSE;
9847
9848 if (var_redir_start(skipwhite(arg), append) == OK)
9849 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 }
9851#endif
9852
9853 /* TODO: redirect to a buffer */
9854
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009856 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009858
9859 /* Make sure redirection is not off. Can happen for cmdline completion
9860 * that indirectly invokes a command to catch its output. */
9861 if (redir_fd != NULL
9862#ifdef FEAT_EVAL
9863 || redir_reg || redir_vname
9864#endif
9865 )
9866 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
9869/*
9870 * ":redraw": force redraw
9871 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009873ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875 int r = RedrawingDisabled;
9876 int p = p_lz;
9877
9878 RedrawingDisabled = 0;
9879 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009880 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009882 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883#ifdef FEAT_TITLE
9884 if (need_maketitle)
9885 maketitle();
9886#endif
9887 RedrawingDisabled = r;
9888 p_lz = p;
9889
9890 /* Reset msg_didout, so that a message that's there is overwritten. */
9891 msg_didout = FALSE;
9892 msg_col = 0;
9893
Bram Moolenaar56667a52013-07-17 11:54:28 +02009894 /* No need to wait after an intentional redraw. */
9895 need_wait_return = FALSE;
9896
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 out_flush();
9898}
9899
9900/*
9901 * ":redrawstatus": force redraw of status line(s)
9902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009904ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 int r = RedrawingDisabled;
9907 int p = p_lz;
9908
9909 RedrawingDisabled = 0;
9910 p_lz = FALSE;
9911 if (eap->forceit)
9912 status_redraw_all();
9913 else
9914 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009915 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 RedrawingDisabled = r;
9917 p_lz = p;
9918 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919}
9920
9921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009922close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
9924 if (redir_fd != NULL)
9925 {
9926 fclose(redir_fd);
9927 redir_fd = NULL;
9928 }
9929#ifdef FEAT_EVAL
9930 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009931 if (redir_vname)
9932 {
9933 var_redir_stop();
9934 redir_vname = 0;
9935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936#endif
9937}
9938
9939#if defined(FEAT_SESSION) && defined(USE_CRNL)
9940# define MKSESSION_NL
9941static int mksession_nl = FALSE; /* use NL only in put_eol() */
9942#endif
9943
9944/*
9945 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9946 */
9947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009948ex_mkrc(
9949 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950{
9951 FILE *fd;
9952 int failed = FALSE;
9953 char_u *fname;
9954#ifdef FEAT_BROWSE
9955 char_u *browseFile = NULL;
9956#endif
9957#ifdef FEAT_SESSION
9958 int view_session = FALSE;
9959 int using_vdir = FALSE; /* using 'viewdir'? */
9960 char_u *viewFile = NULL;
9961 unsigned *flagp;
9962#endif
9963
9964 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9965 {
9966#ifdef FEAT_SESSION
9967 view_session = TRUE;
9968#else
9969 ex_ni(eap);
9970 return;
9971#endif
9972 }
9973
9974#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009975 /* Use the short file name until ":lcd" is used. We also don't use the
9976 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009977 did_lcd = FALSE;
9978
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9980 if (eap->cmdidx == CMD_mkview
9981 && (*eap->arg == NUL
9982 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9983 {
9984 eap->forceit = TRUE;
9985 fname = get_view_file(*eap->arg);
9986 if (fname == NULL)
9987 return;
9988 viewFile = fname;
9989 using_vdir = TRUE;
9990 }
9991 else
9992#endif
9993 if (*eap->arg != NUL)
9994 fname = eap->arg;
9995 else if (eap->cmdidx == CMD_mkvimrc)
9996 fname = (char_u *)VIMRC_FILE;
9997#ifdef FEAT_SESSION
9998 else if (eap->cmdidx == CMD_mksession)
9999 fname = (char_u *)SESSION_FILE;
10000#endif
10001 else
10002 fname = (char_u *)EXRC_FILE;
10003
10004#ifdef FEAT_BROWSE
10005 if (cmdmod.browse)
10006 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +000010007 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008# ifdef FEAT_SESSION
10009 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
10010 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
10011# endif
10012 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010013 fname, (char_u *)"vim", NULL,
10014 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 if (browseFile == NULL)
10016 goto theend;
10017 fname = browseFile;
10018 eap->forceit = TRUE; /* since dialog already asked */
10019 }
10020#endif
10021
10022#if defined(FEAT_SESSION) && defined(vim_mkdir)
10023 /* When using 'viewdir' may have to create the directory. */
10024 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010025 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026#endif
10027
10028 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10029 if (fd != NULL)
10030 {
10031#ifdef FEAT_SESSION
10032 if (eap->cmdidx == CMD_mkview)
10033 flagp = &vop_flags;
10034 else
10035 flagp = &ssop_flags;
10036#endif
10037
10038#ifdef MKSESSION_NL
10039 /* "unix" in 'sessionoptions': use NL line separator */
10040 if (view_session && (*flagp & SSOP_UNIX))
10041 mksession_nl = TRUE;
10042#endif
10043
10044 /* Write the version command for :mkvimrc */
10045 if (eap->cmdidx == CMD_mkvimrc)
10046 (void)put_line(fd, "version 6.0");
10047
10048#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010049 if (eap->cmdidx == CMD_mksession)
10050 {
10051 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10052 failed = TRUE;
10053 }
10054
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 if (eap->cmdidx != CMD_mkview)
10056#endif
10057 {
10058 /* Write setting 'compatible' first, because it has side effects.
10059 * For that same reason only do it when needed. */
10060 if (p_cp)
10061 (void)put_line(fd, "if !&cp | set cp | endif");
10062 else
10063 (void)put_line(fd, "if &cp | set nocp | endif");
10064 }
10065
10066#ifdef FEAT_SESSION
10067 if (!view_session
10068 || (eap->cmdidx == CMD_mksession
10069 && (*flagp & SSOP_OPTIONS)))
10070#endif
10071 failed |= (makemap(fd, NULL) == FAIL
10072 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10073
10074#ifdef FEAT_SESSION
10075 if (!failed && view_session)
10076 {
10077 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10078 failed = TRUE;
10079 if (eap->cmdidx == CMD_mksession)
10080 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010081 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
Bram Moolenaard9462e32011-04-11 21:35:11 +020010083 dirnow = alloc(MAXPATHL);
10084 if (dirnow == NULL)
10085 failed = TRUE;
10086 else
10087 {
10088 /*
10089 * Change to session file's dir.
10090 */
10091 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010093 *dirnow = NUL;
10094 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10095 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010096 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010097 shorten_fnames(TRUE);
10098 }
10099 else if (*dirnow != NUL
10100 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10101 {
10102 if (mch_chdir((char *)globaldir) == 0)
10103 shorten_fnames(TRUE);
10104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105
Bram Moolenaard9462e32011-04-11 21:35:11 +020010106 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107
Bram Moolenaard9462e32011-04-11 21:35:11 +020010108 /* restore original dir */
10109 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010111 {
10112 if (mch_chdir((char *)dirnow) != 0)
10113 EMSG(_(e_prev_dir));
10114 shorten_fnames(TRUE);
10115 }
10116 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 }
10118 }
10119 else
10120 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010121 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10122 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 }
10124 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10125 == FAIL)
10126 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010127 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10128 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010129 if (eap->cmdidx == CMD_mksession)
10130 {
10131 if (put_line(fd, "unlet SessionLoad") == FAIL)
10132 failed = TRUE;
10133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010136 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10137 failed = TRUE;
10138
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 failed |= fclose(fd);
10140
10141 if (failed)
10142 EMSG(_(e_write));
10143#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10144 else if (eap->cmdidx == CMD_mksession)
10145 {
10146 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010147 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaard9462e32011-04-11 21:35:11 +020010149 tbuf = alloc(MAXPATHL);
10150 if (tbuf != NULL)
10151 {
10152 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10153 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10154 vim_free(tbuf);
10155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 }
10157#endif
10158#ifdef MKSESSION_NL
10159 mksession_nl = FALSE;
10160#endif
10161 }
10162
10163#ifdef FEAT_BROWSE
10164theend:
10165 vim_free(browseFile);
10166#endif
10167#ifdef FEAT_SESSION
10168 vim_free(viewFile);
10169#endif
10170}
10171
Bram Moolenaardf177f62005-02-22 08:39:57 +000010172#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10173 || defined(PROTO)
10174 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010175vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010176{
10177 if (vim_mkdir(name, prot) != 0)
10178 {
10179 EMSG2(_("E739: Cannot create directory: %s"), name);
10180 return FAIL;
10181 }
10182 return OK;
10183}
10184#endif
10185
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186/*
10187 * Open a file for writing for an Ex command, with some checks.
10188 * Return file descriptor, or NULL on failure.
10189 */
10190 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010191open_exfile(
10192 char_u *fname,
10193 int forceit,
10194 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195{
10196 FILE *fd;
10197
10198#ifdef UNIX
10199 /* with Unix it is possible to open a directory */
10200 if (mch_isdir(fname))
10201 {
10202 EMSG2(_(e_isadir2), fname);
10203 return NULL;
10204 }
10205#endif
10206 if (!forceit && *mode != 'a' && vim_fexists(fname))
10207 {
10208 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10209 return NULL;
10210 }
10211
10212 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10213 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10214
10215 return fd;
10216}
10217
10218/*
10219 * ":mark" and ":k".
10220 */
10221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010222ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223{
10224 pos_T pos;
10225
10226 if (*eap->arg == NUL) /* No argument? */
10227 EMSG(_(e_argreq));
10228 else if (eap->arg[1] != NUL) /* more than one character? */
10229 EMSG(_(e_trailing));
10230 else
10231 {
10232 pos = curwin->w_cursor; /* save curwin->w_cursor */
10233 curwin->w_cursor.lnum = eap->line2;
10234 beginline(BL_WHITE | BL_FIX);
10235 if (setmark(*eap->arg) == FAIL) /* set mark */
10236 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10237 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10238 }
10239}
10240
10241/*
10242 * Update w_topline, w_leftcol and the cursor position.
10243 */
10244 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010245update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246{
10247 check_cursor(); /* put cursor on valid line */
10248 update_topline();
10249 if (!curwin->w_p_wrap)
10250 validate_cursor();
10251 update_curswant();
10252}
10253
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010255 * Save the current State and go to Normal mode.
10256 * Return TRUE if the typeahead could be saved.
10257 */
10258 int
10259save_current_state(save_state_T *sst)
10260{
10261 sst->save_msg_scroll = msg_scroll;
10262 sst->save_restart_edit = restart_edit;
10263 sst->save_msg_didout = msg_didout;
10264 sst->save_State = State;
10265 sst->save_insertmode = p_im;
10266 sst->save_finish_op = finish_op;
10267 sst->save_opcount = opcount;
10268
10269 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10270 restart_edit = 0; /* don't go to Insert mode */
10271 p_im = FALSE; /* don't use 'insertmode' */
10272
10273 /*
10274 * Save the current typeahead. This is required to allow using ":normal"
10275 * from an event handler and makes sure we don't hang when the argument
10276 * ends with half a command.
10277 */
10278 save_typeahead(&sst->tabuf);
10279 return sst->tabuf.typebuf_valid;
10280}
10281
10282 void
10283restore_current_state(save_state_T *sst)
10284{
10285 /* Restore the previous typeahead. */
10286 restore_typeahead(&sst->tabuf);
10287
10288 msg_scroll = sst->save_msg_scroll;
10289 restart_edit = sst->save_restart_edit;
10290 p_im = sst->save_insertmode;
10291 finish_op = sst->save_finish_op;
10292 opcount = sst->save_opcount;
10293 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10294
10295 /* Restore the state (needed when called from a function executed for
10296 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10297 State = sst->save_State;
10298#ifdef CURSOR_SHAPE
10299 ui_cursor_shape(); /* may show different cursor shape */
10300#endif
10301}
10302
10303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 * ":normal[!] {commands}": Execute normal mode commands.
10305 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010306 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010307ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010309 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310#ifdef FEAT_MBYTE
10311 char_u *arg = NULL;
10312 int l;
10313 char_u *p;
10314#endif
10315
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010316 if (ex_normal_lock > 0)
10317 {
10318 EMSG(_(e_secure));
10319 return;
10320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 if (ex_normal_busy >= p_mmd)
10322 {
10323 EMSG(_("E192: Recursive use of :normal too deep"));
10324 return;
10325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
10327#ifdef FEAT_MBYTE
10328 /*
10329 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10330 * this for the K_SPECIAL leading byte, otherwise special keys will not
10331 * work.
10332 */
10333 if (has_mbyte)
10334 {
10335 int len = 0;
10336
10337 /* Count the number of characters to be escaped. */
10338 for (p = eap->arg; *p != NUL; ++p)
10339 {
10340# ifdef FEAT_GUI
10341 if (*p == CSI) /* leadbyte CSI */
10342 len += 2;
10343# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010344 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10346# ifdef FEAT_GUI
10347 || *p == CSI
10348# endif
10349 )
10350 len += 2;
10351 }
10352 if (len > 0)
10353 {
10354 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10355 if (arg != NULL)
10356 {
10357 len = 0;
10358 for (p = eap->arg; *p != NUL; ++p)
10359 {
10360 arg[len++] = *p;
10361# ifdef FEAT_GUI
10362 if (*p == CSI)
10363 {
10364 arg[len++] = KS_EXTRA;
10365 arg[len++] = (int)KE_CSI;
10366 }
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 {
10370 arg[len++] = *++p;
10371 if (*p == K_SPECIAL)
10372 {
10373 arg[len++] = KS_SPECIAL;
10374 arg[len++] = KE_FILLER;
10375 }
10376# ifdef FEAT_GUI
10377 else if (*p == CSI)
10378 {
10379 arg[len++] = KS_EXTRA;
10380 arg[len++] = (int)KE_CSI;
10381 }
10382# endif
10383 }
10384 arg[len] = NUL;
10385 }
10386 }
10387 }
10388 }
10389#endif
10390
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010391 ++ex_normal_busy;
10392 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 {
10394 /*
10395 * Repeat the :normal command for each line in the range. When no
10396 * range given, execute it just once, without positioning the cursor
10397 * first.
10398 */
10399 do
10400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 if (eap->addr_count != 0)
10402 {
10403 curwin->w_cursor.lnum = eap->line1++;
10404 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010405 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 }
10407
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010408 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409#ifdef FEAT_MBYTE
10410 arg != NULL ? arg :
10411#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010412 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 }
10414 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10415 }
10416
10417 /* Might not return to the main loop when in an event handler. */
10418 update_topline_cursor();
10419
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010420 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010422#ifdef FEAT_MOUSE
10423 setmouse();
10424#endif
10425#ifdef CURSOR_SHAPE
10426 ui_cursor_shape(); /* may show different cursor shape */
10427#endif
10428
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#ifdef FEAT_MBYTE
10430 vim_free(arg);
10431#endif
10432}
10433
10434/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010435 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 */
10437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010438ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010440 if (eap->forceit)
10441 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010442 /* cursor line can be zero on startup */
10443 if (!curwin->w_cursor.lnum)
10444 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010445 coladvance((colnr_T)MAXCOL);
10446 curwin->w_curswant = MAXCOL;
10447 curwin->w_set_curswant = FALSE;
10448 }
10449
Bram Moolenaara40c5002005-01-09 21:16:21 +000010450 /* Ignore the command when already in Insert mode. Inserting an
10451 * expression register that invokes a function can do this. */
10452 if (State & INSERT)
10453 return;
10454
Bram Moolenaar64969662005-12-14 21:59:55 +000010455 if (eap->cmdidx == CMD_startinsert)
10456 restart_edit = 'a';
10457 else if (eap->cmdidx == CMD_startreplace)
10458 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010460 restart_edit = 'V';
10461
10462 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010464 if (eap->cmdidx == CMD_startinsert)
10465 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 curwin->w_curswant = 0; /* avoid MAXCOL */
10467 }
10468}
10469
10470/*
10471 * ":stopinsert"
10472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010474ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 restart_edit = 0;
10477 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010478 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010481/*
10482 * Execute normal mode command "cmd".
10483 * "remap" can be REMAP_NONE or REMAP_YES.
10484 */
10485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010486exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010487{
Bram Moolenaar25281632016-01-21 23:32:32 +010010488 /* Stuff the argument into the typeahead buffer. */
10489 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010490 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010491}
Bram Moolenaar25281632016-01-21 23:32:32 +010010492
Bram Moolenaar25281632016-01-21 23:32:32 +010010493/*
10494 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010495 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010496 */
10497 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010498exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010499{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010500 oparg_T oa;
10501
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010502 clear_oparg(&oa);
10503 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010504 while ((!stuff_empty()
10505 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10506 || (use_vpeekc && vpeekc() != NUL))
10507 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010508 {
10509 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010510#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010511 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010512 && oa.op_type == OP_NOP && oa.regname == NUL
10513 && !VIsual_active)
10514 {
10515 /* If terminal_loop() returns OK we got a key that is handled
10516 * in Normal model. With FAIL we first need to position the
10517 * cursor and the screen needs to be redrawn. */
10518 if (terminal_loop(TRUE) == OK)
10519 normal_cmd(&oa, TRUE);
10520 }
10521 else
10522#endif
10523 /* execute a Normal mode cmd */
10524 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010525 }
10526}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010527
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528#ifdef FEAT_FIND_ID
10529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010530ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10533 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10534 (linenr_T)1, (linenr_T)MAXLNUM);
10535}
10536
Bram Moolenaar4033c552017-09-16 20:54:51 +020010537#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538/*
10539 * ":psearch"
10540 */
10541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010542ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543{
10544 g_do_tagpreview = p_pvh;
10545 ex_findpat(eap);
10546 g_do_tagpreview = 0;
10547}
10548#endif
10549
10550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010551ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553 int whole = TRUE;
10554 long n;
10555 char_u *p;
10556 int action;
10557
10558 switch (cmdnames[eap->cmdidx].cmd_name[2])
10559 {
10560 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10561 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10562 action = ACTION_GOTO;
10563 else
10564 action = ACTION_SHOW;
10565 break;
10566 case 'i': /* ":ilist" and ":dlist" */
10567 action = ACTION_SHOW_ALL;
10568 break;
10569 case 'u': /* ":ijump" and ":djump" */
10570 action = ACTION_GOTO;
10571 break;
10572 default: /* ":isplit" and ":dsplit" */
10573 action = ACTION_SPLIT;
10574 break;
10575 }
10576
10577 n = 1;
10578 if (vim_isdigit(*eap->arg)) /* get count */
10579 {
10580 n = getdigits(&eap->arg);
10581 eap->arg = skipwhite(eap->arg);
10582 }
10583 if (*eap->arg == '/') /* Match regexp, not just whole words */
10584 {
10585 whole = FALSE;
10586 ++eap->arg;
10587 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10588 if (*p)
10589 {
10590 *p++ = NUL;
10591 p = skipwhite(p);
10592
10593 /* Check for trailing illegal characters */
10594 if (!ends_excmd(*p))
10595 eap->errmsg = e_trailing;
10596 else
10597 eap->nextcmd = check_nextcmd(p);
10598 }
10599 }
10600 if (!eap->skip)
10601 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10602 whole, !eap->forceit,
10603 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10604 n, action, eap->line1, eap->line2);
10605}
10606#endif
10607
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608
Bram Moolenaar4033c552017-09-16 20:54:51 +020010609#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610/*
10611 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10612 */
10613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010614ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010616 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10618}
10619
10620/*
10621 * ":pedit"
10622 */
10623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010624ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
10626 win_T *curwin_save = curwin;
10627
10628 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010629 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010630 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 do_exedit(eap, NULL);
10632 keep_help_flag = FALSE;
10633 if (curwin != curwin_save && win_valid(curwin_save))
10634 {
10635 /* Return cursor to where we were */
10636 validate_cursor();
10637 redraw_later(VALID);
10638 win_enter(curwin_save, TRUE);
10639 }
10640 g_do_tagpreview = 0;
10641}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643
10644/*
10645 * ":stag", ":stselect" and ":stjump".
10646 */
10647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010648ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
10650 postponed_split = -1;
10651 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010652 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10654 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010655 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657
10658/*
10659 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10660 */
10661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010662ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663{
10664 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10665}
10666
10667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010668ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
10670 int cmd;
10671
10672 switch (name[1])
10673 {
10674 case 'j': cmd = DT_JUMP; /* ":tjump" */
10675 break;
10676 case 's': cmd = DT_SELECT; /* ":tselect" */
10677 break;
10678 case 'p': cmd = DT_PREV; /* ":tprevious" */
10679 break;
10680 case 'N': cmd = DT_PREV; /* ":tNext" */
10681 break;
10682 case 'n': cmd = DT_NEXT; /* ":tnext" */
10683 break;
10684 case 'o': cmd = DT_POP; /* ":pop" */
10685 break;
10686 case 'f': /* ":tfirst" */
10687 case 'r': cmd = DT_FIRST; /* ":trewind" */
10688 break;
10689 case 'l': cmd = DT_LAST; /* ":tlast" */
10690 break;
10691 default: /* ":tag" */
10692#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010693 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010695 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 return;
10697 }
10698#endif
10699 cmd = DT_TAG;
10700 break;
10701 }
10702
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010703 if (name[0] == 'l')
10704 {
10705#ifndef FEAT_QUICKFIX
10706 ex_ni(eap);
10707 return;
10708#else
10709 cmd = DT_LTAG;
10710#endif
10711 }
10712
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10714 eap->forceit, TRUE);
10715}
10716
10717/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010718 * Check "str" for starting with a special cmdline variable.
10719 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10720 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10721 */
10722 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010723find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010724{
10725 int len;
10726 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010727 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010728 "%",
10729#define SPEC_PERC 0
10730 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010731#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010732 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010733#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010734 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010735#define SPEC_CCWORD (SPEC_CWORD + 1)
10736 "<cexpr>", /* expr under cursor */
10737#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010738 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010739#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010740 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010741#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010742 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010743#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010744 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010745#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010746 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010747#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010748 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010749#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010750 "<sflnum>", /* script file line number */
10751#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010752#ifdef FEAT_CLIENTSERVER
10753 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010754# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010755#endif
10756 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010757
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010758 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010759 {
10760 len = (int)STRLEN(spec_str[i]);
10761 if (STRNCMP(src, spec_str[i], len) == 0)
10762 {
10763 *usedlen = len;
10764 return i;
10765 }
10766 }
10767 return -1;
10768}
10769
10770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 * Evaluate cmdline variables.
10772 *
10773 * change '%' to curbuf->b_ffname
10774 * '#' to curwin->w_altfile
10775 * '<cword>' to word under the cursor
10776 * '<cWORD>' to WORD under the cursor
10777 * '<cfile>' to path name under the cursor
10778 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010779 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 * '<afile>' to file name for autocommand
10781 * '<abuf>' to buffer number for autocommand
10782 * '<amatch>' to matching name for autocommand
10783 *
10784 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10785 * "" for error without a message) and NULL is returned.
10786 * Returns an allocated string if a valid match was found.
10787 * Returns NULL if no match was found. "usedlen" then still contains the
10788 * number of characters to skip.
10789 */
10790 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010791eval_vars(
10792 char_u *src, /* pointer into commandline */
10793 char_u *srcstart, /* beginning of valid memory for src */
10794 int *usedlen, /* characters after src that are used */
10795 linenr_T *lnump, /* line number for :e command, or NULL */
10796 char_u **errormsg, /* pointer to error message */
10797 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010798 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799{
10800 int i;
10801 char_u *s;
10802 char_u *result;
10803 char_u *resultbuf = NULL;
10804 int resultlen;
10805 buf_T *buf;
10806 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10807 int spec_idx;
10808#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010809 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 int skip_mod = FALSE;
10811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813
10814 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010815 if (escaped != NULL)
10816 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817
10818 /*
10819 * Check if there is something to do.
10820 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010821 spec_idx = find_cmdline_var(src, usedlen);
10822 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 {
10824 *usedlen = 1;
10825 return NULL;
10826 }
10827
10828 /*
10829 * Skip when preceded with a backslash "\%" and "\#".
10830 * Note: In "\\%" the % is also not recognized!
10831 */
10832 if (src > srcstart && src[-1] == '\\')
10833 {
10834 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010835 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 return NULL;
10837 }
10838
10839 /*
10840 * word or WORD under cursor
10841 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010842 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10843 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010845 resultlen = find_ident_under_cursor(&result,
10846 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10847 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10848 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 if (resultlen == 0)
10850 {
10851 *errormsg = (char_u *)"";
10852 return NULL;
10853 }
10854 }
10855
10856 /*
10857 * '#': Alternate file name
10858 * '%': Current file name
10859 * File name under the cursor
10860 * File name for autocommand
10861 * and following modifiers
10862 */
10863 else
10864 {
10865 switch (spec_idx)
10866 {
10867 case SPEC_PERC: /* '%': current file */
10868 if (curbuf->b_fname == NULL)
10869 {
10870 result = (char_u *)"";
10871 valid = 0; /* Must have ":p:h" to be valid */
10872 }
10873 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010876#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010877 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010878#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 break;
10881
10882 case SPEC_HASH: /* '#' or "#99": alternate file */
10883 if (src[1] == '#') /* "##": the argument list */
10884 {
10885 result = arg_all();
10886 resultbuf = result;
10887 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010888 if (escaped != NULL)
10889 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#ifdef FEAT_MODIFY_FNAME
10891 skip_mod = TRUE;
10892#endif
10893 break;
10894 }
10895 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010896 if (*s == '<') /* "#<99" uses v:oldfiles */
10897 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010899 if (s == src + 2 && src[1] == '-')
10900 /* just a minus sign, don't skip over it */
10901 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 *usedlen = (int)(s - src); /* length of what we expand */
10903
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010904 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010906 if (*usedlen < 2)
10907 {
10908 /* Should we give an error message for #<text? */
10909 *usedlen = 1;
10910 return NULL;
10911 }
10912#ifdef FEAT_EVAL
10913 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10914 (long)i);
10915 if (result == NULL)
10916 {
10917 *errormsg = (char_u *)"";
10918 return NULL;
10919 }
10920#else
10921 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 }
10925 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010926 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010927 if (i == 0 && src[1] == '<' && *usedlen > 1)
10928 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010929 buf = buflist_findnr(i);
10930 if (buf == NULL)
10931 {
10932 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10933 return NULL;
10934 }
10935 if (lnump != NULL)
10936 *lnump = ECMD_LAST;
10937 if (buf->b_fname == NULL)
10938 {
10939 result = (char_u *)"";
10940 valid = 0; /* Must have ":p:h" to be valid */
10941 }
10942 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010943 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010944 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010945#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010946 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010947#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010948 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 break;
10951
10952#ifdef FEAT_SEARCHPATH
10953 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010954 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 if (result == NULL)
10956 {
10957 *errormsg = (char_u *)"";
10958 return NULL;
10959 }
10960 resultbuf = result; /* remember allocated string */
10961 break;
10962#endif
10963
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 case SPEC_AFILE: /* file name for autocommand */
10965 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010966 if (result != NULL && !autocmd_fname_full)
10967 {
10968 /* Still need to turn the fname into a full path. It is
10969 * postponed to avoid a delay when <afile> is not used. */
10970 autocmd_fname_full = TRUE;
10971 result = FullName_save(autocmd_fname, FALSE);
10972 vim_free(autocmd_fname);
10973 autocmd_fname = result;
10974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 if (result == NULL)
10976 {
10977 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10978 return NULL;
10979 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010980 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 break;
10982
10983 case SPEC_ABUF: /* buffer number for autocommand */
10984 if (autocmd_bufnr <= 0)
10985 {
10986 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10987 return NULL;
10988 }
10989 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10990 result = strbuf;
10991 break;
10992
10993 case SPEC_AMATCH: /* match name for autocommand */
10994 result = autocmd_match;
10995 if (result == NULL)
10996 {
10997 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10998 return NULL;
10999 }
11000 break;
11001
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 case SPEC_SFILE: /* file name for ":so" command */
11003 result = sourcing_name;
11004 if (result == NULL)
11005 {
11006 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
11007 return NULL;
11008 }
11009 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011010
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011011 case SPEC_SLNUM: /* line in file for ":so" command */
11012 if (sourcing_name == NULL || sourcing_lnum == 0)
11013 {
11014 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
11015 return NULL;
11016 }
11017 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11018 result = strbuf;
11019 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011020
11021#ifdef FEAT_EVAL
11022 case SPEC_SFLNUM: /* line in script file */
11023 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11024 {
11025 *errormsg = (char_u *)_("E961: no line number to use for \"<sflnum>\"");
11026 return NULL;
11027 }
11028 sprintf((char *)strbuf, "%ld",
11029 (long)(current_sctx.sc_lnum + sourcing_lnum));
11030 result = strbuf;
11031 break;
11032#endif
11033
11034#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011036 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11037 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 result = strbuf;
11039 break;
11040#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011041
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011042 default:
11043 result = (char_u *)""; /* avoid gcc warning */
11044 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 }
11046
11047 resultlen = (int)STRLEN(result); /* length of new string */
11048 if (src[*usedlen] == '<') /* remove the file name extension */
11049 {
11050 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 resultlen = (int)(s - result);
11053 }
11054#ifdef FEAT_MODIFY_FNAME
11055 else if (!skip_mod)
11056 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011057 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 &resultlen);
11059 if (result == NULL)
11060 {
11061 *errormsg = (char_u *)"";
11062 return NULL;
11063 }
11064 }
11065#endif
11066 }
11067
11068 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11069 {
11070 if (valid != VALID_HEAD + VALID_PATH)
11071 /* xgettext:no-c-format */
11072 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
11073 else
11074 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
11075 result = NULL;
11076 }
11077 else
11078 result = vim_strnsave(result, resultlen);
11079 vim_free(resultbuf);
11080 return result;
11081}
11082
11083/*
11084 * Concatenate all files in the argument list, separated by spaces, and return
11085 * it in one allocated string.
11086 * Spaces and backslashes in the file names are escaped with a backslash.
11087 * Returns NULL when out of memory.
11088 */
11089 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011090arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091{
11092 int len;
11093 int idx;
11094 char_u *retval = NULL;
11095 char_u *p;
11096
11097 /*
11098 * Do this loop two times:
11099 * first time: compute the total length
11100 * second time: concatenate the names
11101 */
11102 for (;;)
11103 {
11104 len = 0;
11105 for (idx = 0; idx < ARGCOUNT; ++idx)
11106 {
11107 p = alist_name(&ARGLIST[idx]);
11108 if (p != NULL)
11109 {
11110 if (len > 0)
11111 {
11112 /* insert a space in between names */
11113 if (retval != NULL)
11114 retval[len] = ' ';
11115 ++len;
11116 }
11117 for ( ; *p != NUL; ++p)
11118 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011119 if (*p == ' '
11120#ifndef BACKSLASH_IN_FILENAME
11121 || *p == '\\'
11122#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011123 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 {
11125 /* insert a backslash */
11126 if (retval != NULL)
11127 retval[len] = '\\';
11128 ++len;
11129 }
11130 if (retval != NULL)
11131 retval[len] = *p;
11132 ++len;
11133 }
11134 }
11135 }
11136
11137 /* second time: break here */
11138 if (retval != NULL)
11139 {
11140 retval[len] = NUL;
11141 break;
11142 }
11143
11144 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011145 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 if (retval == NULL)
11147 break;
11148 }
11149
11150 return retval;
11151}
11152
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153/*
11154 * Expand the <sfile> string in "arg".
11155 *
11156 * Returns an allocated string, or NULL for any error.
11157 */
11158 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011159expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160{
11161 char_u *errormsg;
11162 int len;
11163 char_u *result;
11164 char_u *newres;
11165 char_u *repl;
11166 int srclen;
11167 char_u *p;
11168
11169 result = vim_strsave(arg);
11170 if (result == NULL)
11171 return NULL;
11172
11173 for (p = result; *p; )
11174 {
11175 if (STRNCMP(p, "<sfile>", 7) != 0)
11176 ++p;
11177 else
11178 {
11179 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011180 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 if (errormsg != NULL)
11182 {
11183 if (*errormsg)
11184 emsg(errormsg);
11185 vim_free(result);
11186 return NULL;
11187 }
11188 if (repl == NULL) /* no match (cannot happen) */
11189 {
11190 p += srclen;
11191 continue;
11192 }
11193 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11194 newres = alloc(len);
11195 if (newres == NULL)
11196 {
11197 vim_free(repl);
11198 vim_free(result);
11199 return NULL;
11200 }
11201 mch_memmove(newres, result, (size_t)(p - result));
11202 STRCPY(newres + (p - result), repl);
11203 len = (int)STRLEN(newres);
11204 STRCAT(newres, p + srclen);
11205 vim_free(repl);
11206 vim_free(result);
11207 result = newres;
11208 p = newres + len; /* continue after the match */
11209 }
11210 }
11211
11212 return result;
11213}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214
11215#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011216static int ses_winsizes(FILE *fd, int restore_size,
11217 win_T *tab_firstwin);
11218static int ses_win_rec(FILE *fd, frame_T *fr);
11219static frame_T *ses_skipframe(frame_T *fr);
11220static int ses_do_frame(frame_T *fr);
11221static int ses_do_win(win_T *wp);
11222static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11223static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011224static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225
11226/*
11227 * Write openfile commands for the current buffers to an .exrc file.
11228 * Return FAIL on error, OK otherwise.
11229 */
11230 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011231makeopens(
11232 FILE *fd,
11233 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234{
11235 buf_T *buf;
11236 int only_save_windows = TRUE;
11237 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 int restore_size = TRUE;
11239 win_T *wp;
11240 char_u *sname;
11241 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011242 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011243 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011244 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011245 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011246 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011247 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248
11249 if (ssop_flags & SSOP_BUFFERS)
11250 only_save_windows = FALSE; /* Save ALL buffers */
11251
11252 /*
11253 * Begin by setting the this_session variable, and then other
11254 * sessionable variables.
11255 */
11256#ifdef FEAT_EVAL
11257 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11258 return FAIL;
11259 if (ssop_flags & SSOP_GLOBALS)
11260 if (store_session_globals(fd) == FAIL)
11261 return FAIL;
11262#endif
11263
11264 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011265 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 */
11267 if (put_line(fd, "silent only") == FAIL)
11268 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011269 if ((ssop_flags & SSOP_TABPAGES)
11270 && put_line(fd, "silent tabonly") == FAIL)
11271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
11273 /*
11274 * Now a :cd command to the session directory or the current directory
11275 */
11276 if (ssop_flags & SSOP_SESDIR)
11277 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011278 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11279 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 return FAIL;
11281 }
11282 else if (ssop_flags & SSOP_CURDIR)
11283 {
11284 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11285 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011286 || fputs("cd ", fd) < 0
11287 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11288 || put_eol(fd) == FAIL)
11289 {
11290 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 vim_free(sname);
11294 }
11295
11296 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011297 * If there is an empty, unnamed buffer we will wipe it out later.
11298 * Remember the buffer number.
11299 */
11300 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11301 return FAIL;
11302 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11303 return FAIL;
11304 if (put_line(fd, "endif") == FAIL)
11305 return FAIL;
11306
11307 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 * Now save the current files, current buffer first.
11309 */
11310 if (put_line(fd, "set shortmess=aoO") == FAIL)
11311 return FAIL;
11312
11313 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011314 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 {
11316 if (!(only_save_windows && buf->b_nwindows == 0)
11317 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011318#ifdef FEAT_TERMINAL
11319 /* skip terminal buffers: finished ones are not useful, others
11320 * will be resurrected and result in a new buffer */
11321 && !bt_terminal(buf)
11322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 && buf->b_fname != NULL
11324 && buf->b_p_bl)
11325 {
11326 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11327 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011328 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 return FAIL;
11330 }
11331 }
11332
11333 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011334 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11336 return FAIL;
11337
11338 if (ssop_flags & SSOP_RESIZE)
11339 {
11340 /* Note: after the restore we still check it worked!*/
11341 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11342 || put_eol(fd) == FAIL)
11343 return FAIL;
11344 }
11345
11346#ifdef FEAT_GUI
11347 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11348 {
11349 int x, y;
11350
11351 if (gui_mch_get_winpos(&x, &y) == OK)
11352 {
11353 /* Note: after the restore we still check it worked!*/
11354 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11355 return FAIL;
11356 }
11357 }
11358#endif
11359
11360 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011361 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11362 * will be displayed when creating the next tab. That resizes the windows
11363 * in the first tab, which may cause problems. Set 'showtabline' to 2
11364 * temporarily to avoid that.
11365 */
11366 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11367 {
11368 if (put_line(fd, "set stal=2") == FAIL)
11369 return FAIL;
11370 restore_stal = TRUE;
11371 }
11372
11373 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374 * May repeat putting Windows for each tab, when "tabpages" is in
11375 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011376 * Don't use goto_tabpage(), it may change directory and trigger
11377 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011379 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011380 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011381 if ((ssop_flags & SSOP_TABPAGES))
11382 {
11383 int num_tabs;
11384
11385 /*
11386 * Similar to ses_win_rec() below, populate the tab pages first so
11387 * later local options won't be copied to the new tabs.
11388 */
11389 for (tabnr = 1; ; ++tabnr)
11390 {
11391 tabpage_T *tp = find_tabpage(tabnr);
11392
11393 if (tp == NULL) /* done all tab pages */
11394 break;
11395
11396 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11397 return FAIL;
11398 }
11399
11400 num_tabs = tabnr - 1;
11401 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11402 || put_eol(fd) == FAIL))
11403 return FAIL;
11404 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011405 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011407 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011408 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011409
Bram Moolenaar18144c82006-04-12 21:52:12 +000011410 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011412 tabpage_T *tp = find_tabpage(tabnr);
11413
11414 if (tp == NULL)
11415 break; /* done all tab pages */
11416 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011417 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011418 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011419 tab_topframe = topframe;
11420 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011421 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011422 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011423 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011424 tab_topframe = tp->tp_topframe;
11425 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011426 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011427 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429
Bram Moolenaar18144c82006-04-12 21:52:12 +000011430 /*
11431 * Before creating the window layout, try loading one file. If this
11432 * is aborted we don't end up with a number of useless windows.
11433 * This may have side effects! (e.g., compressed or network file).
11434 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011435 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011436 {
11437 if (ses_do_win(wp)
11438 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011439 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011440#ifdef FEAT_QUICKFIX
11441 && !bt_nofile(wp->w_buffer)
11442#endif
11443 )
11444 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011445 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11446 return FAIL;
11447 need_tabnext = FALSE;
11448
11449 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011450 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11451 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011452 return FAIL;
11453 if (!wp->w_arg_idx_invalid)
11454 edited_win = wp;
11455 break;
11456 }
11457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011459 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011460 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011461 return FAIL;
11462
Bram Moolenaar18144c82006-04-12 21:52:12 +000011463 /*
11464 * Save current window layout.
11465 */
11466 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011468 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011470 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11471 return FAIL;
11472 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11473 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
Bram Moolenaar18144c82006-04-12 21:52:12 +000011475 /*
11476 * Check if window sizes can be restored (no windows omitted).
11477 * Remember the window number of the current window after restoring.
11478 */
11479 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011480 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011481 {
11482 if (ses_do_win(wp))
11483 ++nr;
11484 else
11485 restore_size = FALSE;
11486 if (curwin == wp)
11487 cnr = nr;
11488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489
Bram Moolenaar18144c82006-04-12 21:52:12 +000011490 /* Go to the first window. */
11491 if (put_line(fd, "wincmd t") == FAIL)
11492 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011493
Bram Moolenaar18144c82006-04-12 21:52:12 +000011494 /*
11495 * If more than one window, see if sizes can be restored.
11496 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11497 * resized when moving between windows.
11498 * Do this before restoring the view, so that the topline and the
11499 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011500 * winminheight and winminwidth need to be set to avoid an error if the
11501 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011502 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011503 if (put_line(fd, "set winminheight=0") == FAIL
11504 || put_line(fd, "set winheight=1") == FAIL
11505 || put_line(fd, "set winminwidth=0") == FAIL
11506 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011507 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011508 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011509 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510
Bram Moolenaar18144c82006-04-12 21:52:12 +000011511 /*
11512 * Restore the view of the window (options, file, cursor, etc.).
11513 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011514 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011515 {
11516 if (!ses_do_win(wp))
11517 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011518 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11519 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011520 return FAIL;
11521 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11522 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011523 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011524 }
11525
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011526 /* The argument index in the first tab page is zero, need to set it in
11527 * each window. For further tab pages it's the window where we do
11528 * "tabedit". */
11529 cur_arg_idx = next_arg_idx;
11530
Bram Moolenaar18144c82006-04-12 21:52:12 +000011531 /*
11532 * Restore cursor to the current window if it's not the first one.
11533 */
11534 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11535 || put_eol(fd) == FAIL))
11536 return FAIL;
11537
11538 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011539 * Restore window sizes again after jumping around in windows, because
11540 * the current window has a minimum size while others may not.
11541 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011542 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011543 return FAIL;
11544
Bram Moolenaar18144c82006-04-12 21:52:12 +000011545 /* Don't continue in another tab page when doing only the current one
11546 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011547 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011548 break;
11549 }
11550
11551 if (ssop_flags & SSOP_TABPAGES)
11552 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011553 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11554 || put_eol(fd) == FAIL)
11555 return FAIL;
11556 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011557 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11558 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011559
Bram Moolenaar9c102382006-05-03 21:26:49 +000011560 /*
11561 * Wipe out an empty unnamed buffer we started in.
11562 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011563 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011564 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011565 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011566 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011567 return FAIL;
11568 if (put_line(fd, "endif") == FAIL)
11569 return FAIL;
11570 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11571 return FAIL;
11572
11573 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11574 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11575 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11576 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011577 /* Re-apply 'winminheight' and 'winminwidth'. */
11578 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11579 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581
11582 /*
11583 * Lastly, execute the x.vim file if it exists.
11584 */
11585 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11586 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011587 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 || put_line(fd, "endif") == FAIL)
11589 return FAIL;
11590
11591 return OK;
11592}
11593
11594 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011595ses_winsizes(
11596 FILE *fd,
11597 int restore_size,
11598 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599{
11600 int n = 0;
11601 win_T *wp;
11602
11603 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11604 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011605 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 {
11607 if (!ses_do_win(wp))
11608 continue;
11609 ++n;
11610
11611 /* restore height when not full height */
11612 if (wp->w_height + wp->w_status_height < topframe->fr_height
11613 && (fprintf(fd,
11614 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11615 n, (long)wp->w_height, Rows / 2, Rows) < 0
11616 || put_eol(fd) == FAIL))
11617 return FAIL;
11618
11619 /* restore width when not full width */
11620 if (wp->w_width < Columns && (fprintf(fd,
11621 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11622 n, (long)wp->w_width, Columns / 2, Columns) < 0
11623 || put_eol(fd) == FAIL))
11624 return FAIL;
11625 }
11626 }
11627 else
11628 {
11629 /* Just equalise window sizes */
11630 if (put_line(fd, "wincmd =") == FAIL)
11631 return FAIL;
11632 }
11633 return OK;
11634}
11635
11636/*
11637 * Write commands to "fd" to recursively create windows for frame "fr",
11638 * horizontally and vertically split.
11639 * After the commands the last window in the frame is the current window.
11640 * Returns FAIL when writing the commands to "fd" fails.
11641 */
11642 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011643ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644{
11645 frame_T *frc;
11646 int count = 0;
11647
11648 if (fr->fr_layout != FR_LEAF)
11649 {
11650 /* Find first frame that's not skipped and then create a window for
11651 * each following one (first frame is already there). */
11652 frc = ses_skipframe(fr->fr_child);
11653 if (frc != NULL)
11654 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11655 {
11656 /* Make window as big as possible so that we have lots of room
11657 * to split. */
11658 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11659 || put_line(fd, fr->fr_layout == FR_COL
11660 ? "split" : "vsplit") == FAIL)
11661 return FAIL;
11662 ++count;
11663 }
11664
11665 /* Go back to the first window. */
11666 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11667 ? "%dwincmd k" : "%dwincmd h", count) < 0
11668 || put_eol(fd) == FAIL))
11669 return FAIL;
11670
11671 /* Recursively create frames/windows in each window of this column or
11672 * row. */
11673 frc = ses_skipframe(fr->fr_child);
11674 while (frc != NULL)
11675 {
11676 ses_win_rec(fd, frc);
11677 frc = ses_skipframe(frc->fr_next);
11678 /* Go to next window. */
11679 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11680 return FAIL;
11681 }
11682 }
11683 return OK;
11684}
11685
11686/*
11687 * Skip frames that don't contain windows we want to save in the Session.
11688 * Returns NULL when there none.
11689 */
11690 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011691ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692{
11693 frame_T *frc;
11694
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011695 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 if (ses_do_frame(frc))
11697 break;
11698 return frc;
11699}
11700
11701/*
11702 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11703 * the Session.
11704 */
11705 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011706ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707{
11708 frame_T *frc;
11709
11710 if (fr->fr_layout == FR_LEAF)
11711 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010011712 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 if (ses_do_frame(frc))
11714 return TRUE;
11715 return FALSE;
11716}
11717
11718/*
11719 * Return non-zero if window "wp" is to be stored in the Session.
11720 */
11721 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011722ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011724#ifdef FEAT_TERMINAL
11725 if (bt_terminal(wp->w_buffer))
11726 return !term_is_finished(wp->w_buffer)
11727 && (ssop_flags & SSOP_TERMINAL)
11728 && term_should_restore(wp->w_buffer);
11729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 if (wp->w_buffer->b_fname == NULL
11731#ifdef FEAT_QUICKFIX
11732 /* When 'buftype' is "nofile" can't restore the window contents. */
11733 || bt_nofile(wp->w_buffer)
11734#endif
11735 )
11736 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011737 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 return (ssop_flags & SSOP_HELP);
11739 return TRUE;
11740}
11741
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011742 static int
11743put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11744{
11745 int r;
11746
11747 if (wp->w_curswant == MAXCOL)
11748 r = fprintf(fd, "%snormal! $", spaces);
11749 else
11750 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11751 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11752}
11753
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754/*
11755 * Write commands to "fd" to restore the view of a window.
11756 * Caller must make sure 'scrolloff' is zero.
11757 */
11758 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011759put_view(
11760 FILE *fd,
11761 win_T *wp,
11762 int add_edit, /* add ":edit" command to view */
11763 unsigned *flagp, /* vop_flags or ssop_flags */
11764 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011765 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
11767 win_T *save_curwin;
11768 int f;
11769 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011770 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771
11772 /* Always restore cursor position for ":mksession". For ":mkview" only
11773 * when 'viewoptions' contains "cursor". */
11774 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11775
11776 /*
11777 * Local argument list.
11778 */
11779 if (wp->w_alist == &global_alist)
11780 {
11781 if (put_line(fd, "argglobal") == FAIL)
11782 return FAIL;
11783 }
11784 else
11785 {
11786 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11787 flagp == &vop_flags
11788 || !(*flagp & SSOP_CURDIR)
11789 || wp->w_localdir != NULL, flagp) == FAIL)
11790 return FAIL;
11791 }
11792
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011793 /* Only when part of a session: restore the argument index. Some
11794 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011795 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011796 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011798 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 || put_eol(fd) == FAIL)
11800 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011801 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 }
11803
11804 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011805 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011807# ifdef FEAT_TERMINAL
11808 if (bt_terminal(wp->w_buffer))
11809 {
11810 if (term_write_session(fd, wp) == FAIL)
11811 return FAIL;
11812 }
11813 else
11814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 /*
11816 * Load the file.
11817 */
11818 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011819# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 )
11823 {
11824 /*
11825 * Editing a file in this buffer: use ":edit file".
11826 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011827 *
11828 * Note, if a buffer for that file already exists, use :badd to
11829 * edit that buffer, to not lose folding information (:edit resets
11830 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011832 if (fputs("if bufexists('", fd) < 0
11833 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11834 || fputs("') | buffer ", fd) < 0
11835 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11836 || fputs(" | else | edit ", fd) < 0
11837 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11838 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011839 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 return FAIL;
11841 }
11842 else
11843 {
11844 /* No file in this buffer, just make it empty. */
11845 if (put_line(fd, "enew") == FAIL)
11846 return FAIL;
11847#ifdef FEAT_QUICKFIX
11848 if (wp->w_buffer->b_ffname != NULL)
11849 {
11850 /* The buffer does have a name, but it's not a file name. */
11851 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011852 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 return FAIL;
11854 }
11855#endif
11856 do_cursor = FALSE;
11857 }
11858 }
11859
11860 /*
11861 * Local mappings and abbreviations.
11862 */
11863 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11864 && makemap(fd, wp->w_buffer) == FAIL)
11865 return FAIL;
11866
11867 /*
11868 * Local options. Need to go to the window temporarily.
11869 * Store only local values when using ":mkview" and when ":mksession" is
11870 * used and 'sessionoptions' doesn't include "options".
11871 * Some folding options are always stored when "folds" is included,
11872 * otherwise the folds would not be restored correctly.
11873 */
11874 save_curwin = curwin;
11875 curwin = wp;
11876 curbuf = curwin->w_buffer;
11877 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11878 f = makeset(fd, OPT_LOCAL,
11879 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11880#ifdef FEAT_FOLDING
11881 else if (*flagp & SSOP_FOLDS)
11882 f = makefoldset(fd);
11883#endif
11884 else
11885 f = OK;
11886 curwin = save_curwin;
11887 curbuf = curwin->w_buffer;
11888 if (f == FAIL)
11889 return FAIL;
11890
11891#ifdef FEAT_FOLDING
11892 /*
11893 * Save Folds when 'buftype' is empty and for help files.
11894 */
11895 if ((*flagp & SSOP_FOLDS)
11896 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011897 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 {
11899 if (put_folds(fd, wp) == FAIL)
11900 return FAIL;
11901 }
11902#endif
11903
11904 /*
11905 * Set the cursor after creating folds, since that moves the cursor.
11906 */
11907 if (do_cursor)
11908 {
11909
11910 /* Restore the cursor line in the file and relatively in the
11911 * window. Don't use "G", it changes the jumplist. */
11912 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11913 (long)wp->w_cursor.lnum,
11914 (long)(wp->w_cursor.lnum - wp->w_topline),
11915 (long)wp->w_height / 2, (long)wp->w_height) < 0
11916 || put_eol(fd) == FAIL
11917 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11918 || put_line(fd, "exe s:l") == FAIL
11919 || put_line(fd, "normal! zt") == FAIL
11920 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11921 || put_eol(fd) == FAIL)
11922 return FAIL;
11923 /* Restore the cursor column and left offset when not wrapping. */
11924 if (wp->w_cursor.col == 0)
11925 {
11926 if (put_line(fd, "normal! 0") == FAIL)
11927 return FAIL;
11928 }
11929 else
11930 {
11931 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11932 {
11933 if (fprintf(fd,
11934 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011935 (long)wp->w_virtcol + 1,
11936 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 (long)wp->w_width / 2, (long)wp->w_width) < 0
11938 || put_eol(fd) == FAIL
11939 || put_line(fd, "if s:c > 0") == FAIL
11940 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011941 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11942 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 || put_eol(fd) == FAIL
11944 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011945 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 || put_line(fd, "endif") == FAIL)
11947 return FAIL;
11948 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011949 else if (put_view_curpos(fd, wp, "") == FAIL)
11950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 }
11952 }
11953
11954 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011955 * Local directory, if the current flag is not view options or the "curdir"
11956 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011958 if (wp->w_localdir != NULL
11959 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 {
11961 if (fputs("lcd ", fd) < 0
11962 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11963 || put_eol(fd) == FAIL)
11964 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011965 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 }
11967
11968 return OK;
11969}
11970
11971/*
11972 * Write an argument list to the session file.
11973 * Returns FAIL if writing fails.
11974 */
11975 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011976ses_arglist(
11977 FILE *fd,
11978 char *cmd,
11979 garray_T *gap,
11980 int fullname, /* TRUE: use full path name */
11981 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
11983 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011984 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 char_u *s;
11986
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011987 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11988 return FAIL;
11989 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 return FAIL;
11991 for (i = 0; i < gap->ga_len; ++i)
11992 {
11993 /* NULL file names are skipped (only happens when out of memory). */
11994 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11995 if (s != NULL)
11996 {
11997 if (fullname)
11998 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011999 buf = alloc(MAXPATHL);
12000 if (buf != NULL)
12001 {
12002 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
12003 s = buf;
12004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010012006 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012007 || ses_put_fname(fd, s, flagp) == FAIL
12008 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020012009 {
12010 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012012 }
12013 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 }
12015 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012016 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017}
12018
12019/*
12020 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012021 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 * Returns FAIL if writing fails.
12023 */
12024 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012025ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026{
12027 char_u *name;
12028
12029 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012030 * the session file will be sourced.
12031 * Don't do this for ":mkview", we don't know the current directory.
12032 * Don't do this after ":lcd", we don't keep track of what the current
12033 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 if (buf->b_sfname != NULL
12035 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012036 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012037#ifdef FEAT_AUTOCHDIR
12038 && !p_acd
12039#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012040 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 name = buf->b_sfname;
12042 else
12043 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012044 if (ses_put_fname(fd, name, flagp) == FAIL
12045 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 return FAIL;
12047 return OK;
12048}
12049
12050/*
12051 * Write a file name to the session file.
12052 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12053 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012054 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 */
12056 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012057ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058{
12059 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012060 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062
12063 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012064 if (sname == NULL)
12065 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012067 if (*flagp & SSOP_SLASH)
12068 {
12069 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012070 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012071 if (*p == '\\')
12072 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012074
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012075 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012076 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012078 if (p == NULL)
12079 return FAIL;
12080
12081 /* write the result */
12082 if (fputs((char *)p, fd) < 0)
12083 retval = FAIL;
12084
12085 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 return retval;
12087}
12088
12089/*
12090 * ":loadview [nr]"
12091 */
12092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012093ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094{
12095 char_u *fname;
12096
12097 fname = get_view_file(*eap->arg);
12098 if (fname != NULL)
12099 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012100 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 vim_free(fname);
12102 }
12103}
12104
12105/*
12106 * Get the name of the view file for the current buffer.
12107 */
12108 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012109get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110{
12111 int len = 0;
12112 char_u *p, *s;
12113 char_u *retval;
12114 char_u *sname;
12115
12116 if (curbuf->b_ffname == NULL)
12117 {
12118 EMSG(_(e_noname));
12119 return NULL;
12120 }
12121 sname = home_replace_save(NULL, curbuf->b_ffname);
12122 if (sname == NULL)
12123 return NULL;
12124
12125 /*
12126 * We want a file name without separators, because we're not going to make
12127 * a directory.
12128 * "normal" path separator -> "=+"
12129 * "=" -> "=="
12130 * ":" path separator -> "=-"
12131 */
12132 for (p = sname; *p; ++p)
12133 if (*p == '=' || vim_ispathsep(*p))
12134 ++len;
12135 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12136 if (retval != NULL)
12137 {
12138 STRCPY(retval, p_vdir);
12139 add_pathsep(retval);
12140 s = retval + STRLEN(retval);
12141 for (p = sname; *p; ++p)
12142 {
12143 if (*p == '=')
12144 {
12145 *s++ = '=';
12146 *s++ = '=';
12147 }
12148 else if (vim_ispathsep(*p))
12149 {
12150 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012151#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 if (*p == ':')
12153 *s++ = '-';
12154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012156 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 }
12158 else
12159 *s++ = *p;
12160 }
12161 *s++ = '=';
12162 *s++ = c;
12163 STRCPY(s, ".vim");
12164 }
12165
12166 vim_free(sname);
12167 return retval;
12168}
12169
12170#endif /* FEAT_SESSION */
12171
12172/*
12173 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12174 * Return FAIL for a write error.
12175 */
12176 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012177put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178{
12179 if (
12180#ifdef USE_CRNL
12181 (
12182# ifdef MKSESSION_NL
12183 !mksession_nl &&
12184# endif
12185 (putc('\r', fd) < 0)) ||
12186#endif
12187 (putc('\n', fd) < 0))
12188 return FAIL;
12189 return OK;
12190}
12191
12192/*
12193 * Write a line to "fd".
12194 * Return FAIL for a write error.
12195 */
12196 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012197put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12200 return FAIL;
12201 return OK;
12202}
12203
12204#ifdef FEAT_VIMINFO
12205/*
12206 * ":rviminfo" and ":wviminfo".
12207 */
12208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012209ex_viminfo(
12210 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
12212 char_u *save_viminfo;
12213
12214 save_viminfo = p_viminfo;
12215 if (*p_viminfo == NUL)
12216 p_viminfo = (char_u *)"'100";
12217 if (eap->cmdidx == CMD_rviminfo)
12218 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012219 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12220 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 EMSG(_("E195: Cannot open viminfo file for reading"));
12222 }
12223 else
12224 write_viminfo(eap->arg, eap->forceit);
12225 p_viminfo = save_viminfo;
12226}
12227#endif
12228
12229#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012230/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012231 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012232 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012235dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237 if (fname == NULL)
12238 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012239 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240}
12241#endif
12242
12243/*
12244 * ":behave {mswin,xterm}"
12245 */
12246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012247ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248{
12249 if (STRCMP(eap->arg, "mswin") == 0)
12250 {
12251 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12252 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12253 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12254 set_option_value((char_u *)"keymodel", 0L,
12255 (char_u *)"startsel,stopsel", 0);
12256 }
12257 else if (STRCMP(eap->arg, "xterm") == 0)
12258 {
12259 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12260 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12261 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12262 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12263 }
12264 else
12265 EMSG2(_(e_invarg2), eap->arg);
12266}
12267
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012268#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12269/*
12270 * Function given to ExpandGeneric() to obtain the possible arguments of the
12271 * ":behave {mswin,xterm}" command.
12272 */
12273 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012274get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012275{
12276 if (idx == 0)
12277 return (char_u *)"mswin";
12278 if (idx == 1)
12279 return (char_u *)"xterm";
12280 return NULL;
12281}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012282
12283/*
12284 * Function given to ExpandGeneric() to obtain the possible arguments of the
12285 * ":messages {clear}" command.
12286 */
12287 char_u *
12288get_messages_arg(expand_T *xp UNUSED, int idx)
12289{
12290 if (idx == 0)
12291 return (char_u *)"clear";
12292 return NULL;
12293}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012294#endif
12295
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012296 char_u *
12297get_mapclear_arg(expand_T *xp UNUSED, int idx)
12298{
12299 if (idx == 0)
12300 return (char_u *)"<buffer>";
12301 return NULL;
12302}
12303
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304static int filetype_detect = FALSE;
12305static int filetype_plugin = FALSE;
12306static int filetype_indent = FALSE;
12307
12308/*
12309 * ":filetype [plugin] [indent] {on,off,detect}"
12310 * on: Load the filetype.vim file to install autocommands for file types.
12311 * off: Load the ftoff.vim file to remove all autocommands for file types.
12312 * plugin on: load filetype.vim and ftplugin.vim
12313 * plugin off: load ftplugof.vim
12314 * indent on: load filetype.vim and indent.vim
12315 * indent off: load indoff.vim
12316 */
12317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012318ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319{
12320 char_u *arg = eap->arg;
12321 int plugin = FALSE;
12322 int indent = FALSE;
12323
12324 if (*eap->arg == NUL)
12325 {
12326 /* Print current status. */
12327 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12328 filetype_detect ? "ON" : "OFF",
12329 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12330 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12331 return;
12332 }
12333
12334 /* Accept "plugin" and "indent" in any order. */
12335 for (;;)
12336 {
12337 if (STRNCMP(arg, "plugin", 6) == 0)
12338 {
12339 plugin = TRUE;
12340 arg = skipwhite(arg + 6);
12341 continue;
12342 }
12343 if (STRNCMP(arg, "indent", 6) == 0)
12344 {
12345 indent = TRUE;
12346 arg = skipwhite(arg + 6);
12347 continue;
12348 }
12349 break;
12350 }
12351 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12352 {
12353 if (*arg == 'o' || !filetype_detect)
12354 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012355 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 filetype_detect = TRUE;
12357 if (plugin)
12358 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012359 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 filetype_plugin = TRUE;
12361 }
12362 if (indent)
12363 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012364 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 filetype_indent = TRUE;
12366 }
12367 }
12368 if (*arg == 'd')
12369 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012370 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012371 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 }
12373 }
12374 else if (STRCMP(arg, "off") == 0)
12375 {
12376 if (plugin || indent)
12377 {
12378 if (plugin)
12379 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012380 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 filetype_plugin = FALSE;
12382 }
12383 if (indent)
12384 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012385 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 filetype_indent = FALSE;
12387 }
12388 }
12389 else
12390 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012391 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 filetype_detect = FALSE;
12393 }
12394 }
12395 else
12396 EMSG2(_(e_invarg2), arg);
12397}
12398
12399/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012400 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 */
12402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012403ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404{
12405 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012406 {
12407 char_u *arg = eap->arg;
12408
12409 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12410 arg += 9;
12411
12412 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12413 if (arg != eap->arg)
12414 did_filetype = FALSE;
12415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012419ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420{
12421#ifdef FEAT_DIGRAPHS
12422 if (*eap->arg != NUL)
12423 putdigraph(eap->arg);
12424 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010012425 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426#else
12427 EMSG(_("E196: No digraphs in this version"));
12428#endif
12429}
12430
12431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012432ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433{
12434 int flags = 0;
12435
12436 if (eap->cmdidx == CMD_setlocal)
12437 flags = OPT_LOCAL;
12438 else if (eap->cmdidx == CMD_setglobal)
12439 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012440#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 if (cmdmod.browse && flags == 0)
12442 ex_options(eap);
12443 else
12444#endif
12445 (void)do_set(eap->arg, flags);
12446}
12447
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012448#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12449 void
12450set_no_hlsearch(int flag)
12451{
12452 no_hlsearch = flag;
12453# ifdef FEAT_EVAL
12454 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12455# endif
12456}
12457
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458/*
12459 * ":nohlsearch"
12460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012462ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012464 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012465 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466}
12467
12468/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012469 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 * Sets nextcmd to the start of the next command, if any. Also called when
12471 * skipping commands to find the next command.
12472 */
12473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012474ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475{
12476 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012477 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 char_u *end;
12479 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012480 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012481
12482 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012483 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012484 else
12485 {
12486 EMSG(e_invcmd);
12487 return;
12488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489
12490 /* First clear any old pattern. */
12491 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012492 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493
12494 if (ends_excmd(*eap->arg))
12495 end = eap->arg;
12496 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012497 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 end = eap->arg + 4;
12499 else
12500 {
12501 p = skiptowhite(eap->arg);
12502 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012503 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 p = skipwhite(p);
12505 if (*p == NUL)
12506 {
12507 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012508 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 EMSG2(_(e_invarg2), eap->arg);
12510 return;
12511 }
12512 end = skip_regexp(p + 1, *p, TRUE, NULL);
12513 if (!eap->skip)
12514 {
12515 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12516 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012517 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 eap->errmsg = e_trailing;
12519 return;
12520 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012521 if (*end != *p)
12522 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012523 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012524 EMSG2(_(e_invarg2), p);
12525 return;
12526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527
12528 c = *end;
12529 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012530 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012531 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012532 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 }
12534 }
12535 eap->nextcmd = find_nextcmd(end);
12536}
12537#endif
12538
12539#ifdef FEAT_CRYPT
12540/*
12541 * ":X": Get crypt key
12542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012544ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012546 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012547 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548}
12549#endif
12550
12551#ifdef FEAT_FOLDING
12552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012553ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554{
12555 if (foldManualAllowed(TRUE))
12556 foldCreate(eap->line1, eap->line2);
12557}
12558
12559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012560ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561{
12562 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12563 eap->forceit, FALSE);
12564}
12565
12566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012567ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568{
12569 linenr_T lnum;
12570
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012571#ifdef FEAT_CLIPBOARD
12572 start_global_changes();
12573#endif
12574
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575 /* First set the marks for all lines closed/open. */
12576 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12577 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12578 ml_setmarked(lnum);
12579
12580 /* Execute the command on the marked lines. */
12581 global_exe(eap->arg);
12582 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012583#ifdef FEAT_CLIPBOARD
12584 end_global_changes();
12585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586}
12587#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012588
Bram Moolenaar39665952018-08-15 20:59:48 +020012589#ifdef FEAT_QUICKFIX
12590/*
12591 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12592 * instead of a quickfix command.
12593 */
12594 int
12595is_loclist_cmd(int cmdidx)
12596{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012597 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012598 return FALSE;
12599 return cmdnames[cmdidx].cmd_name[0] == 'l';
12600}
12601#endif
12602
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012603# if defined(FEAT_TIMERS) || defined(PROTO)
12604 int
12605get_pressedreturn(void)
12606{
12607 return ex_pressedreturn;
12608}
12609
12610 void
12611set_pressedreturn(int val)
12612{
12613 ex_pressedreturn = val;
12614}
12615#endif