blob: 82904eeaf0e48f2b365f16a1910a90e76cf09a3b [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
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static 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 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020071static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void append_command(char_u *cmd);
73static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010075static void ex_abbreviate(exarg_T *eap);
76static void ex_map(exarg_T *eap);
77static void ex_unmap(exarg_T *eap);
78static void ex_mapclear(exarg_T *eap);
79static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#ifndef FEAT_MENU
81# define ex_emenu ex_ni
82# define ex_menu ex_ni
83# define ex_menutranslate ex_ni
84#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static void ex_bunload(exarg_T *eap);
88static void ex_buffer(exarg_T *eap);
89static void ex_bmodified(exarg_T *eap);
90static void ex_bnext(exarg_T *eap);
91static void ex_bprevious(exarg_T *eap);
92static void ex_brewind(exarg_T *eap);
93static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010094static char_u *getargcmd(char_u **);
95static char_u *skip_cmd_arg(char_u *p, int rembs);
96static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifndef FEAT_QUICKFIX
98# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000099# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100# define ex_cc ex_ni
101# define ex_cnext ex_ni
102# define ex_cfile ex_ni
103# define qf_list ex_ni
104# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200105# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000107# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200109#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110# define ex_cclose ex_ni
111# define ex_copen ex_ni
112# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200113# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000115#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
116# define ex_cexpr ex_ni
117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static int check_more(int, int);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200120static 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 +0100121static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200122#if !defined(FEAT_PERL) \
123 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
124 || !defined(FEAT_TCL) \
125 || !defined(FEAT_RUBY) \
126 || !defined(FEAT_LUA) \
127 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000128# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static char_u *invalid_range(exarg_T *eap);
132static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000133#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100134static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000135#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
137static void ex_highlight(exarg_T *eap);
138static void ex_colorscheme(exarg_T *eap);
139static void ex_quit(exarg_T *eap);
140static void ex_cquit(exarg_T *eap);
141static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100142static void ex_close(exarg_T *eap);
143static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
144static void ex_only(exarg_T *eap);
145static void ex_resize(exarg_T *eap);
146static void ex_stag(exarg_T *eap);
147static void ex_tabclose(exarg_T *eap);
148static void ex_tabonly(exarg_T *eap);
149static void ex_tabnext(exarg_T *eap);
150static void ex_tabmove(exarg_T *eap);
151static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200152#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100153static void ex_pclose(exarg_T *eap);
154static void ex_ptag(exarg_T *eap);
155static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#else
157# define ex_pclose ex_ni
158# define ex_ptag ex_ni
159# define ex_pedit ex_ni
160#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100161static void ex_hide(exarg_T *eap);
162static void ex_stop(exarg_T *eap);
163static void ex_exit(exarg_T *eap);
164static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100166static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#else
168# define ex_goto ex_ni
169#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100170static void ex_shell(exarg_T *eap);
171static void ex_preserve(exarg_T *eap);
172static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100173static void ex_mode(exarg_T *eap);
174static void ex_wrongmodifier(exarg_T *eap);
175static void ex_find(exarg_T *eap);
176static void ex_open(exarg_T *eap);
177static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#ifndef FEAT_GUI
179# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100183static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#else
185# define ex_tearoff ex_ni
186#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100187#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
188 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100189static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_popup ex_ni
192#endif
193#ifndef FEAT_GUI_MSWIN
194# define ex_simalt ex_ni
195#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000196#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define gui_mch_find_dialog ex_ni
198# define gui_mch_replace_dialog ex_ni
199#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000200#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201# define ex_helpfind ex_ni
202#endif
203#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100204# define ex_cscope ex_ni
205# define ex_scscope ex_ni
206# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
208#ifndef FEAT_SYN_HL
209# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200210# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000211#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100212#ifndef FEAT_EVAL
213# define ex_packadd ex_ni
214# define ex_packloadall ex_ni
215#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200216#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200217# define ex_syntime ex_ni
218#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000219#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000220# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000221# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000222# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000223# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000224# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000225#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200226#ifndef FEAT_PERSISTENT_UNDO
227# define ex_rundo ex_ni
228# define ex_wundo ex_ni
229#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200230#ifndef FEAT_LUA
231# define ex_lua ex_script_ni
232# define ex_luado ex_ni
233# define ex_luafile ex_ni
234#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000235#ifndef FEAT_MZSCHEME
236# define ex_mzscheme ex_script_ni
237# define ex_mzfile ex_ni
238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#ifndef FEAT_PERL
240# define ex_perl ex_script_ni
241# define ex_perldo ex_ni
242#endif
243#ifndef FEAT_PYTHON
244# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200245# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246# define ex_pyfile ex_ni
247#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200248#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200249# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200250# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200251# define ex_py3file ex_ni
252#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100253#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
254# define ex_pyx ex_script_ni
255# define ex_pyxdo ex_ni
256# define ex_pyxfile ex_ni
257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifndef FEAT_TCL
259# define ex_tcl ex_script_ni
260# define ex_tcldo ex_ni
261# define ex_tclfile ex_ni
262#endif
263#ifndef FEAT_RUBY
264# define ex_ruby ex_script_ni
265# define ex_rubydo ex_ni
266# define ex_rubyfile ex_ni
267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifndef FEAT_KEYMAP
269# define ex_loadkeymap ex_ni
270#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static void ex_swapname(exarg_T *eap);
272static void ex_syncbind(exarg_T *eap);
273static void ex_read(exarg_T *eap);
274static void ex_pwd(exarg_T *eap);
275static void ex_equal(exarg_T *eap);
276static void ex_sleep(exarg_T *eap);
277static void do_exmap(exarg_T *eap, int isabbrev);
278static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100279static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000280#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100281static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#else
283# define ex_winpos ex_ni
284#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100285static void ex_operators(exarg_T *eap);
286static void ex_put(exarg_T *eap);
287static void ex_copymove(exarg_T *eap);
288static void ex_submagic(exarg_T *eap);
289static void ex_join(exarg_T *eap);
290static void ex_at(exarg_T *eap);
291static void ex_bang(exarg_T *eap);
292static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200293#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100294static void ex_wundo(exarg_T *eap);
295static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200296#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100297static void ex_redo(exarg_T *eap);
298static void ex_later(exarg_T *eap);
299static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100300static void ex_redrawstatus(exarg_T *eap);
301static void close_redir(void);
302static void ex_mkrc(exarg_T *eap);
303static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static char_u *uc_fun_cmd(void);
306static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100308static void ex_startinsert(exarg_T *eap);
309static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100311static void ex_checkpath(exarg_T *eap);
312static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#else
314# define ex_findpat ex_ni
315# define ex_checkpath ex_ni
316#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200317#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_psearch ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_tag(exarg_T *eap);
323static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifndef FEAT_EVAL
325# define ex_scriptnames ex_ni
326# define ex_finish ex_ni
327# define ex_echo ex_ni
328# define ex_echohl ex_ni
329# define ex_execute ex_ni
330# define ex_call ex_ni
331# define ex_if ex_ni
332# define ex_endif ex_ni
333# define ex_else ex_ni
334# define ex_while ex_ni
335# define ex_continue ex_ni
336# define ex_break ex_ni
337# define ex_endwhile ex_ni
338# define ex_throw ex_ni
339# define ex_try ex_ni
340# define ex_catch ex_ni
341# define ex_finally ex_ni
342# define ex_endtry ex_ni
343# define ex_endfunction ex_ni
344# define ex_let ex_ni
345# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000346# define ex_lockvar ex_ni
347# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348# define ex_function ex_ni
349# define ex_delfunction ex_ni
350# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000351# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100355static int makeopens(FILE *fd, char_u *dirnow);
356static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
357static void ex_loadview(exarg_T *eap);
358static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000359static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#else
361# define ex_loadview ex_ni
362#endif
363#ifndef FEAT_EVAL
364# define ex_compiler ex_ni
365#endif
366#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100367static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368#else
369# define ex_viminfo ex_ni
370#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100371static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100372static void ex_filetype(exarg_T *eap);
373static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000375# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376# define ex_diffpatch ex_ni
377# define ex_diffgetput ex_ni
378# define ex_diffsplit ex_ni
379# define ex_diffthis ex_ni
380# define ex_diffupdate ex_ni
381#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100382static void ex_digraphs(exarg_T *eap);
383static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100384#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385# define ex_options ex_ni
386#endif
387#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100388static void ex_nohlsearch(exarg_T *eap);
389static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#else
391# define ex_nohlsearch ex_ni
392# define ex_match ex_ni
393#endif
394#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#else
397# define ex_X ex_ni
398#endif
399#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100400static void ex_fold(exarg_T *eap);
401static void ex_foldopen(exarg_T *eap);
402static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#else
404# define ex_fold ex_ni
405# define ex_foldopen ex_ni
406# define ex_folddo ex_ni
407#endif
408#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
409 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
410# define ex_language ex_ni
411#endif
412#ifndef FEAT_SIGNS
413# define ex_sign ex_ni
414#endif
415#ifndef FEAT_SUN_WORKSHOP
416# define ex_wsverb ex_ni
417#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000418#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200419# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000420# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200421# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423
424#ifndef FEAT_EVAL
425# define ex_debug ex_ni
426# define ex_breakadd ex_ni
427# define ex_debuggreedy ex_ni
428# define ex_breakdel ex_ni
429# define ex_breaklist ex_ni
430#endif
431
432#ifndef FEAT_CMDHIST
433# define ex_history ex_ni
434#endif
435#ifndef FEAT_JUMPLIST
436# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200437# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438# define ex_changes ex_ni
439#endif
440
Bram Moolenaar05159a02005-02-26 23:04:13 +0000441#ifndef FEAT_PROFILE
442# define ex_profile ex_ni
443#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200444#ifndef FEAT_TERMINAL
445# define ex_terminal ex_ni
446#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448/*
449 * Declare cmdnames[].
450 */
451#define DO_DECLARE_EXCMD
452#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200453#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100454
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455static char_u dollar_command[2] = {'$', 0};
456
457
458#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000459/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460typedef struct
461{
462 char_u *line; /* command line */
463 linenr_T lnum; /* sourcing_lnum of the line */
464} wcmd_T;
465
466/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000467 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000469 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000471struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472{
473 garray_T *lines_gap; /* growarray with line info */
474 int current_line; /* last read line from growarray */
475 int repeating; /* TRUE when looping a second time */
476 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100477 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 void *cookie;
479};
480
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100481static char_u *get_loop_line(int c, void *cookie, int indent);
482static int store_loop_line(garray_T *gap, char_u *line);
483static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000484
485/* Struct to save a few things while debugging. Used in do_cmdline() only. */
486struct dbg_stuff
487{
488 int trylevel;
489 int force_abort;
490 except_T *caught_stack;
491 char_u *vv_exception;
492 char_u *vv_throwpoint;
493 int did_emsg;
494 int got_int;
495 int did_throw;
496 int need_rethrow;
497 int check_cstack;
498 except_T *current_exception;
499};
500
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100501static void save_dbg_stuff(struct dbg_stuff *dsp);
502static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000503
504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100505save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000506{
507 dsp->trylevel = trylevel; trylevel = 0;
508 dsp->force_abort = force_abort; force_abort = FALSE;
509 dsp->caught_stack = caught_stack; caught_stack = NULL;
510 dsp->vv_exception = v_exception(NULL);
511 dsp->vv_throwpoint = v_throwpoint(NULL);
512
513 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
514 dsp->did_emsg = did_emsg; did_emsg = FALSE;
515 dsp->got_int = got_int; got_int = FALSE;
516 dsp->did_throw = did_throw; did_throw = FALSE;
517 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
518 dsp->check_cstack = check_cstack; check_cstack = FALSE;
519 dsp->current_exception = current_exception; current_exception = NULL;
520}
521
522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100523restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000524{
525 suppress_errthrow = FALSE;
526 trylevel = dsp->trylevel;
527 force_abort = dsp->force_abort;
528 caught_stack = dsp->caught_stack;
529 (void)v_exception(dsp->vv_exception);
530 (void)v_throwpoint(dsp->vv_throwpoint);
531 did_emsg = dsp->did_emsg;
532 got_int = dsp->got_int;
533 did_throw = dsp->did_throw;
534 need_rethrow = dsp->need_rethrow;
535 check_cstack = dsp->check_cstack;
536 current_exception = dsp->current_exception;
537}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#endif
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540/*
541 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
542 * command is given.
543 */
544 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100545do_exmode(
546 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 int save_msg_scroll;
549 int prev_msg_row;
550 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100551 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552
553 if (improved)
554 exmode_active = EXMODE_VIM;
555 else
556 exmode_active = EXMODE_NORMAL;
557 State = NORMAL;
558
559 /* When using ":global /pat/ visual" and then "Q" we return to continue
560 * the :global command. */
561 if (global_busy)
562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
564 save_msg_scroll = msg_scroll;
565 ++RedrawingDisabled; /* don't redisplay the window */
566 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#ifdef FEAT_GUI
568 /* Ignore scrollbar and mouse events in Ex mode */
569 ++hold_gui_events;
570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
573 while (exmode_active)
574 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000575 /* Check for a ":normal" command and no more characters left. */
576 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
577 {
578 exmode_active = FALSE;
579 break;
580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 msg_scroll = TRUE;
582 need_wait_return = FALSE;
583 ex_pressedreturn = FALSE;
584 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100585 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 prev_msg_row = msg_row;
587 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (improved)
589 {
590 cmdline_row = msg_row;
591 do_cmdline(NULL, getexline, NULL, 0);
592 }
593 else
594 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
595 lines_left = Rows - 1;
596
Bram Moolenaardf177f62005-02-22 08:39:57 +0000597 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100598 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000600 if (curbuf->b_ml.ml_flags & ML_EMPTY)
601 EMSG(_(e_emptybuf));
602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000604 if (ex_pressedreturn)
605 {
606 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000607 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000608 msg_row = prev_msg_row;
609 if (prev_msg_row == Rows - 1)
610 msg_row--;
611 }
612 msg_col = 0;
613 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
614 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000617 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
618 {
619 if (curbuf->b_ml.ml_flags & ML_EMPTY)
620 EMSG(_(e_emptybuf));
621 else
622 EMSG(_("E501: At end-of-file"));
623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 }
625
626#ifdef FEAT_GUI
627 --hold_gui_events;
628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 --RedrawingDisabled;
630 --no_wait_return;
631 update_screen(CLEAR);
632 need_wait_return = FALSE;
633 msg_scroll = save_msg_scroll;
634}
635
636/*
637 * Execute a simple command line. Used for translated commands like "*".
638 */
639 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100640do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 return do_cmdline(cmd, NULL, NULL,
643 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
644}
645
646/*
647 * do_cmdline(): execute one Ex command line
648 *
649 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100650 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 * 2. Split up in parts separated with '|'.
652 *
653 * This function can be called recursively!
654 *
655 * flags:
656 * DOCMD_VERBOSE - The command will be included in the error message.
657 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100658 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 * DOCMD_KEYTYPED - Don't reset KeyTyped.
660 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
661 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
662 *
663 * return FAIL if cmdline could not be executed, OK otherwise
664 */
665 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100666do_cmdline(
667 char_u *cmdline,
668 char_u *(*fgetline)(int, void *, int),
669 void *cookie, /* argument for fgetline() */
670 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 char_u *next_cmdline; /* next cmd to execute */
673 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100674 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 static int recursive = 0; /* recursive depth */
676 int msg_didout_before_start = 0;
677 int count = 0; /* line number count */
678 int did_inc = FALSE; /* incremented RedrawingDisabled */
679 int retval = OK;
680#ifdef FEAT_EVAL
681 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000682 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 int current_line = 0; /* active line in lines_ga */
684 char_u *fname = NULL; /* function or script name */
685 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
686 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000687 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int initial_trylevel;
689 struct msglist **saved_msg_list = NULL;
690 struct msglist *private_msg_list;
691
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100693 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000695 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700# define cmd_cookie cookie
701#endif
702 static int call_depth = 0; /* recursiveness */
703
704#ifdef FEAT_EVAL
705 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
706 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000707 * This ensures that the do_errthrow() call in do_one_cmd() does not
708 * combine the messages stored by an earlier invocation of do_one_cmd()
709 * with the command name of the later one. This would happen when
710 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 saved_msg_list = msg_list;
712 msg_list = &private_msg_list;
713 private_msg_list = NULL;
714#endif
715
716 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100717 * here. The value of 200 allows nested function calls, ":source", etc.
718 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100719 if (call_depth >= 200
720#ifdef FEAT_EVAL
721 && call_depth >= p_mfd
722#endif
723 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
725 EMSG(_("E169: Command too recursive"));
726#ifdef FEAT_EVAL
727 /* When converting to an exception, we do not include the command name
728 * since this is not an error of the specific command. */
729 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
730 msg_list = saved_msg_list;
731#endif
732 return FAIL;
733 }
734 ++call_depth;
735
736#ifdef FEAT_EVAL
737 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000738 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 cstack.cs_trylevel = 0;
740 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000741 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
743
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
746 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000748 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 ++ex_nesting_level;
750
751 /* Get the function or script name and the address where the next breakpoint
752 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000753 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
755 fname = func_name(real_cookie);
756 breakpoint = func_breakpoint(real_cookie);
757 dbg_tick = func_dbg_tick(real_cookie);
758 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
761 fname = sourcing_name;
762 breakpoint = source_breakpoint(real_cookie);
763 dbg_tick = source_dbg_tick(real_cookie);
764 }
765
766 /*
767 * Initialize "force_abort" and "suppress_errthrow" at the top level.
768 */
769 if (!recursive)
770 {
771 force_abort = FALSE;
772 suppress_errthrow = FALSE;
773 }
774
775 /*
776 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000777 * exception handling (used when debugging). Otherwise clear it to avoid
778 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000780 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000781 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000782 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200783 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
785 initial_trylevel = trylevel;
786
787 /*
788 * "did_throw" will be set to TRUE when an exception is being thrown.
789 */
790 did_throw = FALSE;
791#endif
792 /*
793 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000794 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 * If force_abort is set, we cancel everything.
796 */
797 did_emsg = FALSE;
798
799 /*
800 * KeyTyped is only set when calling vgetc(). Reset it here when not
801 * calling vgetc() (sourced command lines).
802 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100803 if (!(flags & DOCMD_KEYTYPED)
804 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 KeyTyped = FALSE;
806
807 /*
808 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 * - for multiple commands on one line, separated with '|'
811 * - when repeating until there are no more lines (for ":source")
812 */
813 next_cmdline = cmdline;
814 do
815 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
819
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000820 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (next_cmdline == NULL
822#ifdef FEAT_EVAL
823 && !force_abort
824 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#endif
827 )
828 did_emsg = FALSE;
829
830 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000831 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * 3. If a line is given: Make a copy, so we can mess with it.
834 */
835
836#ifdef FEAT_EVAL
837 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
840 /* Each '|' separated command is stored separately in lines_ga, to
841 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100842 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844 /* Check if a function has returned or, unless it has an unclosed
845 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000849 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 func_line_end(real_cookie);
851# endif
852 if (func_has_ended(real_cookie))
853 {
854 retval = FAIL;
855 break;
856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000859 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861 script_line_end();
862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
867 retval = FAIL;
868 break;
869 }
870
871 /* If breakpoints have been added/deleted need to check for it. */
872 if (breakpoint != NULL && dbg_tick != NULL
873 && *dbg_tick != debug_tick)
874 {
875 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 fname, sourcing_lnum);
878 *dbg_tick = debug_tick;
879 }
880
881 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
882 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
883
884 /* Did we encounter a breakpoint? */
885 if (breakpoint != NULL && *breakpoint != 0
886 && *breakpoint <= sourcing_lnum)
887 {
888 dbg_breakpoint(fname, sourcing_lnum);
889 /* Find next breakpoint. */
890 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 fname, sourcing_lnum);
893 *dbg_tick = debug_tick;
894 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000896 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897 {
898 if (getline_is_func)
899 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100900 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901 script_line_start();
902 }
903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000906 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000908 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 * below, so that it stores lines in or reads them from
911 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 * while/for loop. */
913 cmd_getline = get_loop_line;
914 cmd_cookie = (void *)&cmd_loop_cookie;
915 cmd_loop_cookie.lines_gap = &lines_ga;
916 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 cmd_loop_cookie.cookie = cookie;
919 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 else
922 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100923 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 cmd_cookie = cookie;
925 }
926#endif
927
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100928 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (next_cmdline == NULL)
930 {
931 /*
932 * Need to set msg_didout for the first line after an ":if",
933 * otherwise the ":if" will be overwritten.
934 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100935 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100937 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#ifdef FEAT_EVAL
939 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
940#else
941 0
942#endif
943 )) == NULL)
944 {
945 /* Don't call wait_return for aborted command line. The NULL
946 * returned for the end of a sourced file or executed function
947 * doesn't do this. */
948 if (KeyTyped && !(flags & DOCMD_REPEAT))
949 need_wait_return = FALSE;
950 retval = FAIL;
951 break;
952 }
953 used_getline = TRUE;
954
955 /*
956 * Keep the first typed line. Clear it when more lines are typed.
957 */
958 if (flags & DOCMD_KEEPLINE)
959 {
960 vim_free(repeat_cmdline);
961 if (count == 0)
962 repeat_cmdline = vim_strsave(next_cmdline);
963 else
964 repeat_cmdline = NULL;
965 }
966 }
967
968 /* 3. Make a copy of the command so we can mess with it. */
969 else if (cmdline_copy == NULL)
970 {
971 next_cmdline = vim_strsave(next_cmdline);
972 if (next_cmdline == NULL)
973 {
974 EMSG(_(e_outofmem));
975 retval = FAIL;
976 break;
977 }
978 }
979 cmdline_copy = next_cmdline;
980
981#ifdef FEAT_EVAL
982 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000983 * Save the current line when inside a ":while" or ":for", and when
984 * the command looks like a ":while" or ":for", because we may need it
985 * later. When there is a '|' and another command, it is stored
986 * separately, because we need to be able to jump back to it from an
987 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 if (current_line == lines_ga.ga_len
990 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
994 retval = FAIL;
995 break;
996 }
997 }
998 did_endif = FALSE;
999#endif
1000
1001 if (count++ == 0)
1002 {
1003 /*
1004 * All output from the commands is put below each other, without
1005 * waiting for a return. Don't do this when executing commands
1006 * from a script or when being called recursive (e.g. for ":e
1007 * +command file").
1008 */
1009 if (!(flags & DOCMD_NOWAIT) && !recursive)
1010 {
1011 msg_didout_before_start = msg_didout;
1012 msg_didany = FALSE; /* no output yet */
1013 msg_start();
1014 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001015 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 ++RedrawingDisabled;
1017 did_inc = TRUE;
1018 }
1019 }
1020
1021 if (p_verbose >= 15 && sourcing_name != NULL)
1022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001024 verbose_enter_scroll();
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 smsg((char_u *)_("line %ld: %s"),
1027 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001028 if (msg_silent == 0)
1029 msg_puts((char_u *)"\n"); /* don't overwrite this */
1030
1031 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 --no_wait_return;
1033 }
1034
1035 /*
1036 * 2. Execute one '|' separated command.
1037 * do_one_cmd() will return NULL if there is no trailing '|'.
1038 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1039 */
1040 ++recursive;
1041 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1042#ifdef FEAT_EVAL
1043 &cstack,
1044#endif
1045 cmd_getline, cmd_cookie);
1046 --recursive;
1047
1048#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cmd_cookie == (void *)&cmd_loop_cookie)
1050 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#endif
1054
1055 if (next_cmdline == NULL)
1056 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001057 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058#ifdef FEAT_CMDHIST
1059 /*
1060 * If the command was typed, remember it for the ':' register.
1061 * Do this AFTER executing the command to make :@: work.
1062 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001063 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 && new_last_cmdline != NULL)
1065 {
1066 vim_free(last_cmdline);
1067 last_cmdline = new_last_cmdline;
1068 new_last_cmdline = NULL;
1069 }
1070#endif
1071 }
1072 else
1073 {
1074 /* need to copy the command after the '|' to cmdline_copy, for the
1075 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001076 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 next_cmdline = cmdline_copy;
1078 }
1079
1080
1081#ifdef FEAT_EVAL
1082 /* reset did_emsg for a function that is not aborted by an error */
1083 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001084 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 && !func_has_abort(real_cookie))
1086 did_emsg = FALSE;
1087
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001088 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
1090 ++current_line;
1091
1092 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 * An ":endwhile", ":endfor" and ":continue" is handled here.
1094 * If we were executing commands, jump back to the ":while" or
1095 * ":for".
1096 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 /* Jump back to the matching ":while" or ":for". Be careful
1103 * not to use a cs_line[] from an entry that isn't a ":while"
1104 * or ":for": It would make "current_line" invalid and can
1105 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (!did_emsg && !got_int && !did_throw
1107 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 && (cstack.cs_flags[cstack.cs_idx]
1109 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 && cstack.cs_line[cstack.cs_idx] >= 0
1111 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1112 {
1113 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 /* remember we jumped there */
1115 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 line_breakcheck(); /* check if CTRL-C typed */
1117
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 /* Check for the next breakpoint at or after the ":while"
1119 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (breakpoint != NULL)
1121 {
1122 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001123 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 fname,
1125 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1126 *dbg_tick = debug_tick;
1127 }
1128 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001133 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1134 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 }
1137
1138 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1145 }
1146 }
1147
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001148 /* Check for the next breakpoint after a watchexpression */
1149 if (breakpoint != NULL && has_watchexpr())
1150 {
1151 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1152 *dbg_tick = debug_tick;
1153 }
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 /*
1156 * When not inside any ":while" loop, clear remembered lines.
1157 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 if (lines_ga.ga_len > 0)
1161 {
1162 sourcing_lnum =
1163 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1164 free_cmdlines(&lines_ga);
1165 }
1166 current_line = 0;
1167 }
1168
1169 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1171 * being restored at the ":endtry". Reset them here and set the
1172 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1173 * This includes the case where a missing ":endif", ":endwhile" or
1174 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 cstack.cs_lflags &= ~CSL_HAD_FINA;
1179 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1180 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 did_throw ? (void *)current_exception : NULL);
1182 did_emsg = got_int = did_throw = FALSE;
1183 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1184 }
1185
1186 /* Update global "trylevel" for recursive calls to do_cmdline() from
1187 * within this loop. */
1188 trylevel = initial_trylevel + cstack.cs_trylevel;
1189
1190 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001191 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 * files) is aborted because of an error, an interrupt, or an uncaught
1193 * exception, cancel everything. If it is left normally, reset
1194 * force_abort to get the non-EH compatible abortion behavior for
1195 * the rest of the script.
1196 */
1197 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1198 force_abort = FALSE;
1199
1200 /* Convert an interrupt to an exception if appropriate. */
1201 (void)do_intthrow(&cstack);
1202#endif /* FEAT_EVAL */
1203
1204 }
1205 /*
1206 * Continue executing command lines when:
1207 * - no CTRL-C typed, no aborting error, no exception thrown or try
1208 * conditionals need to be checked for executing finally clauses or
1209 * catching an interrupt exception
1210 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 * looping for ":source" command or function call.
1213 */
1214 while (!((got_int
1215#ifdef FEAT_EVAL
1216 || (did_emsg && force_abort) || did_throw
1217#endif
1218 )
1219#ifdef FEAT_EVAL
1220 && cstack.cs_trylevel == 0
1221#endif
1222 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001223 && !(did_emsg
1224#ifdef FEAT_EVAL
1225 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001226 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001227 * the :endtry to be missed. */
1228 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1229#endif
1230 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001231 && (getline_equal(fgetline, cookie, getexmodeline)
1232 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 && (next_cmdline != NULL
1234#ifdef FEAT_EVAL
1235 || cstack.cs_idx >= 0
1236#endif
1237 || (flags & DOCMD_REPEAT)));
1238
1239 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001240 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#ifdef FEAT_EVAL
1242 free_cmdlines(&lines_ga);
1243 ga_clear(&lines_ga);
1244
1245 if (cstack.cs_idx >= 0)
1246 {
1247 /*
1248 * If a sourced file or executed function ran to its end, report the
1249 * unclosed conditional.
1250 */
1251 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001252 && ((getline_equal(fgetline, cookie, getsourceline)
1253 && !source_finished(fgetline, cookie))
1254 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 && !func_has_ended(real_cookie))))
1256 {
1257 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1258 EMSG(_(e_endtry));
1259 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1260 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001261 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1262 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 else
1264 EMSG(_(e_endif));
1265 }
1266
1267 /*
1268 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1269 * ":endtry" in a sourced file or executed function. If the try
1270 * conditional is in its finally clause, ignore anything pending.
1271 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001272 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 */
1274 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001275 {
1276 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1277
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001278 if (idx >= 0)
1279 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001280 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1281 &cstack.cs_looplevel);
1282 }
1283 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 trylevel = initial_trylevel;
1285 }
1286
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1288 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001290 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 ? (char_u *)"endfunction" : (char_u *)NULL);
1292
1293 if (trylevel == 0)
1294 {
1295 /*
1296 * When an exception is being thrown out of the outermost try
1297 * conditional, discard the uncaught exception, disable the conversion
1298 * of interrupts or errors to exceptions, and ensure that no more
1299 * commands are executed.
1300 */
1301 if (did_throw)
1302 {
1303 void *p = NULL;
1304 char_u *saved_sourcing_name;
1305 int saved_sourcing_lnum;
1306 struct msglist *messages = NULL, *next;
1307
1308 /*
1309 * If the uncaught exception is a user exception, report it as an
1310 * error. If it is an error exception, display the saved error
1311 * message now. For an interrupt exception, do nothing; the
1312 * interrupt message is given elsewhere.
1313 */
1314 switch (current_exception->type)
1315 {
1316 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001317 vim_snprintf((char *)IObuff, IOSIZE,
1318 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 current_exception->value);
1320 p = vim_strsave(IObuff);
1321 break;
1322 case ET_ERROR:
1323 messages = current_exception->messages;
1324 current_exception->messages = NULL;
1325 break;
1326 case ET_INTERRUPT:
1327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329
1330 saved_sourcing_name = sourcing_name;
1331 saved_sourcing_lnum = sourcing_lnum;
1332 sourcing_name = current_exception->throw_name;
1333 sourcing_lnum = current_exception->throw_lnum;
1334 current_exception->throw_name = NULL;
1335
1336 discard_current_exception(); /* uses IObuff if 'verbose' */
1337 suppress_errthrow = TRUE;
1338 force_abort = TRUE;
1339
1340 if (messages != NULL)
1341 {
1342 do
1343 {
1344 next = messages->next;
1345 emsg(messages->msg);
1346 vim_free(messages->msg);
1347 vim_free(messages);
1348 messages = next;
1349 }
1350 while (messages != NULL);
1351 }
1352 else if (p != NULL)
1353 {
1354 emsg(p);
1355 vim_free(p);
1356 }
1357 vim_free(sourcing_name);
1358 sourcing_name = saved_sourcing_name;
1359 sourcing_lnum = saved_sourcing_lnum;
1360 }
1361
1362 /*
1363 * On an interrupt or an aborting error not converted to an exception,
1364 * disable the conversion of errors to exceptions. (Interrupts are not
1365 * converted any more, here.) This enables also the interrupt message
1366 * when force_abort is set and did_emsg unset in case of an interrupt
1367 * from a finally clause after an error.
1368 */
1369 else if (got_int || (did_emsg && force_abort))
1370 suppress_errthrow = TRUE;
1371 }
1372
1373 /*
1374 * The current cstack will be freed when do_cmdline() returns. An uncaught
1375 * exception will have to be rethrown in the previous cstack. If a function
1376 * has just returned or a script file was just finished and the previous
1377 * cstack belongs to the same function or, respectively, script file, it
1378 * will have to be checked for finally clauses to be executed due to the
1379 * ":return" or ":finish". This is done in do_one_cmd().
1380 */
1381 if (did_throw)
1382 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001383 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001385 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 && ex_nesting_level > func_level(real_cookie) + 1))
1387 {
1388 if (!did_throw)
1389 check_cstack = TRUE;
1390 }
1391 else
1392 {
1393 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001394 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 --ex_nesting_level;
1396 /*
1397 * Go to debug mode when returning from a function in which we are
1398 * single-stepping.
1399 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001400 if ((getline_equal(fgetline, cookie, getsourceline)
1401 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001403 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 ? (char_u *)_("End of sourced file")
1405 : (char_u *)_("End of function"));
1406 }
1407
1408 /*
1409 * Restore the exception environment (done after returning from the
1410 * debugger).
1411 */
1412 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001413 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415 msg_list = saved_msg_list;
1416#endif /* FEAT_EVAL */
1417
1418 /*
1419 * If there was too much output to fit on the command line, ask the user to
1420 * hit return before redrawing the screen. With the ":global" command we do
1421 * this only once after the command is finished.
1422 */
1423 if (did_inc)
1424 {
1425 --RedrawingDisabled;
1426 --no_wait_return;
1427 msg_scroll = FALSE;
1428
1429 /*
1430 * When just finished an ":if"-":else" which was typed, no need to
1431 * wait for hit-return. Also for an error situation.
1432 */
1433 if (retval == FAIL
1434#ifdef FEAT_EVAL
1435 || (did_endif && KeyTyped && !did_emsg)
1436#endif
1437 )
1438 {
1439 need_wait_return = FALSE;
1440 msg_didany = FALSE; /* don't wait when restarting edit */
1441 }
1442 else if (need_wait_return)
1443 {
1444 /*
1445 * The msg_start() above clears msg_didout. The wait_return we do
1446 * here should not overwrite the command that may be shown before
1447 * doing that.
1448 */
1449 msg_didout |= msg_didout_before_start;
1450 wait_return(FALSE);
1451 }
1452 }
1453
Bram Moolenaar2a942252012-11-28 23:03:07 +01001454#ifdef FEAT_EVAL
1455 did_endif = FALSE; /* in case do_cmdline used recursively */
1456#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 /*
1458 * Reset if_level, in case a sourced script file contains more ":if" than
1459 * ":endif" (could be ":if x | foo | endif").
1460 */
1461 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001462#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 --call_depth;
1465 return retval;
1466}
1467
1468#ifdef FEAT_EVAL
1469/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001470 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 */
1472 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001473get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001475 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 wcmd_T *wp;
1477 char_u *line;
1478
1479 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1480 {
1481 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001482 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001484 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (cp->getline == NULL)
1486 line = getcmdline(c, 0L, indent);
1487 else
1488 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001489 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 ++cp->current_line;
1491
1492 return line;
1493 }
1494
1495 KeyTyped = FALSE;
1496 ++cp->current_line;
1497 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1498 sourcing_lnum = wp->lnum;
1499 return vim_strsave(wp->line);
1500}
1501
1502/*
1503 * Store a line in "gap" so that a ":while" loop can execute it again.
1504 */
1505 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001506store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 if (ga_grow(gap, 1) == FAIL)
1509 return FAIL;
1510 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1511 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1512 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 return OK;
1514}
1515
1516/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001520free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 while (gap->ga_len > 0)
1523 {
1524 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1525 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
1527}
1528#endif
1529
1530/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001531 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1532 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001535getline_equal(
1536 char_u *(*fgetline)(int, void *, int),
1537 void *cookie UNUSED, /* argument for fgetline() */
1538 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001541 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001542 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Bram Moolenaar89d40322006-08-29 15:30:07 +00001544 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001545 * function that's originally used to obtain the lines. This may be
1546 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001547 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 cp = (struct loop_cookie *)cookie;
1549 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 gp = cp->getline;
1552 cp = cp->cookie;
1553 }
1554 return gp == func;
1555#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001556 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557#endif
1558}
1559
1560#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1561/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001562 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 * getline function. Otherwise return "cookie".
1564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001566getline_cookie(
1567 char_u *(*fgetline)(int, void *, int) UNUSED,
1568 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001571 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar89d40322006-08-29 15:30:07 +00001574 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001575 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001577 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001578 cp = (struct loop_cookie *)cookie;
1579 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
1581 gp = cp->getline;
1582 cp = cp->cookie;
1583 }
1584 return cp;
1585# else
1586 return cookie;
1587# endif
1588}
1589#endif
1590
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001591
1592/*
1593 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1594 * ":bwipeout", etc.
1595 * Returns the buffer number.
1596 */
1597 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001599{
1600 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001601 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602 int count = offset;
1603
1604 buf = firstbuf;
1605 while (buf->b_next != NULL && buf->b_fnum < lnum)
1606 buf = buf->b_next;
1607 while (count != 0)
1608 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 count += (offset < 0) ? 1 : -1;
1610 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001612 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001613 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 if (addr_type == ADDR_LOADED_BUFFERS)
1615 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001616 while (buf->b_ml.ml_mfp == NULL)
1617 {
1618 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
1620 break;
1621 buf = nextbuf;
1622 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 /* we might have gone too far, last buffer is not loadedd */
1625 if (addr_type == ADDR_LOADED_BUFFERS)
1626 while (buf->b_ml.ml_mfp == NULL)
1627 {
1628 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1629 if (nextbuf == NULL)
1630 break;
1631 buf = nextbuf;
1632 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633 return buf->b_fnum;
1634}
1635
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001636static int current_win_nr(win_T *win);
1637static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001638
1639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001640current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001641{
1642 win_T *wp;
1643 int nr = 0;
1644
Bram Moolenaar29323592016-07-24 22:04:11 +02001645 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001646 {
1647 ++nr;
1648 if (wp == win)
1649 break;
1650 }
1651 return nr;
1652}
1653
1654 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001655current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001656{
1657 tabpage_T *tp;
1658 int nr = 0;
1659
Bram Moolenaar29323592016-07-24 22:04:11 +02001660 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001661 {
1662 ++nr;
1663 if (tp == tab)
1664 break;
1665 }
1666 return nr;
1667}
1668
1669# define CURRENT_WIN_NR current_win_nr(curwin)
1670# define LAST_WIN_NR current_win_nr(NULL)
1671# define CURRENT_TAB_NR current_tab_nr(curtab)
1672# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001673
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674/*
1675 * Execute one Ex command.
1676 *
1677 * If 'sourcing' is TRUE, the command will be included in the error message.
1678 *
1679 * 1. skip comment lines and leading space
1680 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001681 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001682 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001683 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684 * 6. parse arguments
1685 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001687 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 *
1689 * This function may be called recursively!
1690 */
1691#if (_MSC_VER == 1200)
1692/*
Bram Moolenaared203462004-06-16 11:19:22 +00001693 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001695 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
1697 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001698do_one_cmd(
1699 char_u **cmdlinep,
1700 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001702 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001704 char_u *(*fgetline)(int, void *, int),
1705 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 char_u *p;
1708 linenr_T lnum;
1709 long n;
1710 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001711 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001713 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 cmdmod_T save_cmdmod;
1715 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001716 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718 vim_memset(&ea, 0, sizeof(ea));
1719 ea.line1 = 1;
1720 ea.line2 = 1;
1721#ifdef FEAT_EVAL
1722 ++ex_nesting_level;
1723#endif
1724
Bram Moolenaar21691f82012-12-05 19:13:18 +01001725 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (quitmore
1727#ifdef FEAT_EVAL
1728 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001729 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001730#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001731 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001732 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 --quitmore;
1734
1735 /*
1736 * Reset browse, confirm, etc.. They are restored when returning, for
1737 * recursive calls.
1738 */
1739 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001741 /* "#!anything" is handled like a comment. */
1742 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1743 goto doend;
1744
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001745/*
1746 * 1. Skip comment lines and leading white space and colons.
1747 * 2. Handle command modifiers.
1748 */
1749 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001751 ea.cmdlinep = cmdlinep;
1752 ea.getline = fgetline;
1753 ea.cookie = cookie;
1754#ifdef FEAT_EVAL
1755 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001757 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001758 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001760 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762#ifdef FEAT_EVAL
1763 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1764 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1765#else
1766 ea.skip = (if_level > 0);
1767#endif
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001770 * 3. Skip over the range to find the command. Let "p" point to after it.
1771 *
1772 * We need the command to know what kind of range it uses.
1773 */
1774 cmd = ea.cmd;
1775 ea.cmd = skip_range(ea.cmd, NULL);
1776 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1777 ea.cmd = skipwhite(ea.cmd + 1);
1778 p = find_command(&ea, NULL);
1779
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001780#ifdef FEAT_EVAL
1781# ifdef FEAT_PROFILE
1782 // Count this line for profiling if skip is TRUE.
1783 if (do_profiling == PROF_YES
1784 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1785 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1786 {
1787 int skip = did_emsg || got_int || did_throw;
1788
1789 if (ea.cmdidx == CMD_catch)
1790 skip = !skip && !(cstack->cs_idx >= 0
1791 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1792 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1793 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1794 skip = skip || !(cstack->cs_idx >= 0
1795 && !(cstack->cs_flags[cstack->cs_idx]
1796 & (CSF_ACTIVE | CSF_TRUE)));
1797 else if (ea.cmdidx == CMD_finally)
1798 skip = FALSE;
1799 else if (ea.cmdidx != CMD_endif
1800 && ea.cmdidx != CMD_endfor
1801 && ea.cmdidx != CMD_endtry
1802 && ea.cmdidx != CMD_endwhile)
1803 skip = ea.skip;
1804
1805 if (!skip)
1806 {
1807 if (getline_equal(fgetline, cookie, get_func_line))
1808 func_line_exec(getline_cookie(fgetline, cookie));
1809 else if (getline_equal(fgetline, cookie, getsourceline))
1810 script_line_exec();
1811 }
1812 }
1813# endif
1814
1815 /* May go to debug mode. If this happens and the ">quit" debug command is
1816 * used, throw an interrupt exception and skip the next command. */
1817 dbg_check_breakpoint(&ea);
1818 if (!ea.skip && got_int)
1819 {
1820 ea.skip = TRUE;
1821 (void)do_intthrow(cstack);
1822 }
1823#endif
1824
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001825/*
1826 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 *
1828 * where 'addr' is:
1829 *
1830 * % (entire file)
1831 * $ [+-NUM]
1832 * 'x [+-NUM] (where x denotes a currently defined mark)
1833 * . [+-NUM]
1834 * [+-NUM]..
1835 * NUM
1836 *
1837 * The ea.cmd pointer is updated to point to the first character following the
1838 * range spec. If an initial address is found, but no second, the upper bound
1839 * is equal to the lower.
1840 */
1841
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001842 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001843 if (!IS_USER_CMDIDX(ea.cmdidx))
1844 {
1845 if (ea.cmdidx != CMD_SIZE)
1846 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1847 else
1848 ea.addr_type = ADDR_LINES;
1849
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001850 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001851 if (ea.cmdidx == CMD_wincmd && p != NULL)
1852 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001853 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001854
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001855 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001856 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001857 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 */
1862
1863 /*
1864 * Skip ':' and any white space
1865 */
1866 ea.cmd = skipwhite(ea.cmd);
1867 while (*ea.cmd == ':')
1868 ea.cmd = skipwhite(ea.cmd + 1);
1869
1870 /*
1871 * If we got a line, but no command, then go to the line.
1872 * If we find a '|' or '\n' we set ea.nextcmd.
1873 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001874 if (*ea.cmd == NUL || *ea.cmd == '"'
1875 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 /*
1878 * strange vi behaviour:
1879 * ":3" jumps to line 3
1880 * ":3|..." prints line 3
1881 * ":|" prints current line
1882 */
1883 if (ea.skip) /* skip this if inside :if */
1884 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001885 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 ea.cmdidx = CMD_print;
1888 ea.argt = RANGE+COUNT+TRLBAR;
1889 if ((errormsg = invalid_range(&ea)) == NULL)
1890 {
1891 correct_range(&ea);
1892 ex_print(&ea);
1893 }
1894 }
1895 else if (ea.addr_count != 0)
1896 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001897 if (ea.line2 > curbuf->b_ml.ml_line_count)
1898 {
1899 /* With '-' in 'cpoptions' a line number past the file is an
1900 * error, otherwise put it at the end of the file. */
1901 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1902 ea.line2 = -1;
1903 else
1904 ea.line2 = curbuf->b_ml.ml_line_count;
1905 }
1906
1907 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001908 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 else
1910 {
1911 if (ea.line2 == 0)
1912 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else
1914 curwin->w_cursor.lnum = ea.line2;
1915 beginline(BL_SOL | BL_FIX);
1916 }
1917 }
1918 goto doend;
1919 }
1920
Bram Moolenaard5005162014-08-22 23:05:54 +02001921 /* If this looks like an undefined user command and there are CmdUndefined
1922 * autocommands defined, trigger the matching autocommands. */
1923 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1924 && ASCII_ISUPPER(*ea.cmd)
1925 && has_cmdundefined())
1926 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001927 int ret;
1928
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001929 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001930 while (ASCII_ISALNUM(*p))
1931 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001932 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001933 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1934 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001935 /* If the autocommands did something and didn't cause an error, try
1936 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001937 p = (ret
1938#ifdef FEAT_EVAL
1939 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001940#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001941 ) ? find_command(&ea, NULL) : ea.cmd;
1942 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944#ifdef FEAT_USR_CMDS
1945 if (p == NULL)
1946 {
1947 if (!ea.skip)
1948 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
1949 goto doend;
1950 }
1951 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001952 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1953 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 errormsg = uc_fun_cmd();
1956 goto doend;
1957 }
1958#endif
1959 if (ea.cmdidx == CMD_SIZE)
1960 {
1961 if (!ea.skip)
1962 {
1963 STRCPY(IObuff, _("E492: Not an editor command"));
1964 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001965 {
1966 /* If the modifier was parsed OK the error must be in the
1967 * following command */
1968 if (after_modifier != NULL)
1969 append_command(after_modifier);
1970 else
1971 append_command(*cmdlinep);
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001974 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 goto doend;
1977 }
1978
Bram Moolenaar958636c2014-10-21 20:01:58 +02001979 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1980 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001981#ifdef HAVE_EX_SCRIPT_NI
1982 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1983#endif
1984 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
1986#ifndef FEAT_EVAL
1987 /*
1988 * When the expression evaluation is disabled, recognize the ":if" and
1989 * ":endif" commands and ignore everything in between it.
1990 */
1991 if (ea.cmdidx == CMD_if)
1992 ++if_level;
1993 if (if_level)
1994 {
1995 if (ea.cmdidx == CMD_endif)
1996 --if_level;
1997 goto doend;
1998 }
1999
2000#endif
2001
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002002 /* forced commands */
2003 if (*p == '!' && ea.cmdidx != CMD_substitute
2004 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
2006 ++p;
2007 ea.forceit = TRUE;
2008 }
2009 else
2010 ea.forceit = FALSE;
2011
2012/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002013 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002015 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002016 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 if (!ea.skip)
2019 {
2020#ifdef HAVE_SANDBOX
2021 if (sandbox != 0 && !(ea.argt & SBOXOK))
2022 {
2023 /* Command not allowed in sandbox. */
2024 errormsg = (char_u *)_(e_sandbox);
2025 goto doend;
2026 }
2027#endif
2028 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2029 {
2030 /* Command not allowed in non-'modifiable' buffer */
2031 errormsg = (char_u *)_(e_modifiable);
2032 goto doend;
2033 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002034
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002035 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002036 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002038 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002039 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 goto doend;
2041 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002042
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002043 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002044 * Do allow ":checktime" (it is postponed).
2045 * Do allow ":edit" (check for an argument later).
2046 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002047 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002048 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002049 && ea.cmdidx != CMD_edit
2050 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002051 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002052 && curbuf_locked())
2053 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2056 {
2057 /* no range allowed */
2058 errormsg = (char_u *)_(e_norange);
2059 goto doend;
2060 }
2061 }
2062
2063 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2064 {
2065 errormsg = (char_u *)_(e_nobang);
2066 goto doend;
2067 }
2068
2069 /*
2070 * Don't complain about the range if it is not used
2071 * (could happen if line_count is accidentally set to 0).
2072 */
2073 if (!ea.skip && !ni)
2074 {
2075 /*
2076 * If the range is backwards, ask for confirmation and, if given, swap
2077 * ea.line1 & ea.line2 so it's forwards again.
2078 * When global command is busy, don't ask, will fail below.
2079 */
2080 if (!global_busy && ea.line1 > ea.line2)
2081 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002082 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002084 if (sourcing || exmode_active)
2085 {
2086 errormsg = (char_u *)_("E493: Backwards range given");
2087 goto doend;
2088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if (ask_yesno((char_u *)
2090 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002091 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 lnum = ea.line1;
2094 ea.line1 = ea.line2;
2095 ea.line2 = lnum;
2096 }
2097 if ((errormsg = invalid_range(&ea)) != NULL)
2098 goto doend;
2099 }
2100
2101 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2102 ea.line2 = 1;
2103
2104 correct_range(&ea);
2105
2106#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002107 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2108 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 /* Put the first line at the start of a closed fold, put the last line
2111 * at the end of a closed fold. */
2112 (void)hasFolding(ea.line1, &ea.line1, NULL);
2113 (void)hasFolding(ea.line2, NULL, &ea.line2);
2114 }
2115#endif
2116
2117#ifdef FEAT_QUICKFIX
2118 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002119 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 * option here, so things like % get expanded.
2121 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002122 p = replace_makeprg(&ea, p, cmdlinep);
2123 if (p == NULL)
2124 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#endif
2126
2127 /*
2128 * Skip to start of argument.
2129 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2130 */
2131 if (ea.cmdidx == CMD_bang)
2132 ea.arg = p;
2133 else
2134 ea.arg = skipwhite(p);
2135
Bram Moolenaar379fb762018-08-30 15:58:28 +02002136 // ":file" cannot be run with an argument when "curbuf_lock" is set
2137 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2138 goto doend;
2139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 /*
2141 * Check for "++opt=val" argument.
2142 * Must be first, allow ":w ++enc=utf8 !cmd"
2143 */
2144 if (ea.argt & ARGOPT)
2145 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2146 if (getargopt(&ea) == FAIL && !ni)
2147 {
2148 errormsg = (char_u *)_(e_invarg);
2149 goto doend;
2150 }
2151
2152 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2153 {
2154 if (*ea.arg == '>') /* append */
2155 {
2156 if (*++ea.arg != '>') /* typed wrong */
2157 {
2158 errormsg = (char_u *)_("E494: Use w or w>>");
2159 goto doend;
2160 }
2161 ea.arg = skipwhite(ea.arg + 1);
2162 ea.append = TRUE;
2163 }
2164 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2165 {
2166 ++ea.arg;
2167 ea.usefilter = TRUE;
2168 }
2169 }
2170
2171 if (ea.cmdidx == CMD_read)
2172 {
2173 if (ea.forceit)
2174 {
2175 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2176 ea.forceit = FALSE;
2177 }
2178 else if (*ea.arg == '!') /* :r !filter */
2179 {
2180 ++ea.arg;
2181 ea.usefilter = TRUE;
2182 }
2183 }
2184
2185 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2186 {
2187 ea.amount = 1;
2188 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2189 {
2190 ++ea.arg;
2191 ++ea.amount;
2192 }
2193 ea.arg = skipwhite(ea.arg);
2194 }
2195
2196 /*
2197 * Check for "+command" argument, before checking for next command.
2198 * Don't do this for ":read !cmd" and ":write !cmd".
2199 */
2200 if ((ea.argt & EDITCMD) && !ea.usefilter)
2201 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2202
2203 /*
2204 * Check for '|' to separate commands and '"' to start comments.
2205 * Don't do this for ":read !cmd" and ":write !cmd".
2206 */
2207 if ((ea.argt & TRLBAR) && !ea.usefilter)
2208 separate_nextcmd(&ea);
2209
2210 /*
2211 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002212 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2213 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002215 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002216 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002217 || ea.cmdidx == CMD_global
2218 || ea.cmdidx == CMD_vglobal
2219 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
2221 for (p = ea.arg; *p; ++p)
2222 {
2223 /* Remove one backslash before a newline, so that it's possible to
2224 * pass a newline to the shell and also a newline that is preceded
2225 * with a backslash. This makes it impossible to end a shell
2226 * command in a backslash, but that doesn't appear useful.
2227 * Halving the number of backslashes is incompatible with previous
2228 * versions. */
2229 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002230 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 else if (*p == '\n')
2232 {
2233 ea.nextcmd = p + 1;
2234 *p = NUL;
2235 break;
2236 }
2237 }
2238 }
2239
2240 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2241 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002242 buf_T *buf;
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002245 switch (ea.addr_type)
2246 {
2247 case ADDR_LINES:
2248 ea.line2 = curbuf->b_ml.ml_line_count;
2249 break;
2250 case ADDR_LOADED_BUFFERS:
2251 buf = firstbuf;
2252 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2253 buf = buf->b_next;
2254 ea.line1 = buf->b_fnum;
2255 buf = lastbuf;
2256 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2257 buf = buf->b_prev;
2258 ea.line2 = buf->b_fnum;
2259 break;
2260 case ADDR_BUFFERS:
2261 ea.line1 = firstbuf->b_fnum;
2262 ea.line2 = lastbuf->b_fnum;
2263 break;
2264 case ADDR_WINDOWS:
2265 ea.line2 = LAST_WIN_NR;
2266 break;
2267 case ADDR_TABS:
2268 ea.line2 = LAST_TAB_NR;
2269 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002270 case ADDR_TABS_RELATIVE:
2271 ea.line2 = 1;
2272 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002273 case ADDR_ARGUMENTS:
2274 if (ARGCOUNT == 0)
2275 ea.line1 = ea.line2 = 0;
2276 else
2277 ea.line2 = ARGCOUNT;
2278 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002279#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002280 case ADDR_QUICKFIX:
2281 ea.line2 = qf_get_size(&ea);
2282 if (ea.line2 == 0)
2283 ea.line2 = 1;
2284 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002285#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288
2289 /* accept numbered register only when no count allowed (:put) */
2290 if ( (ea.argt & REGSTR)
2291 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002292 /* Do not allow register = for user commands */
2293 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2295 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002296#ifndef FEAT_CLIPBOARD
2297 /* check these explicitly for a more specific error message */
2298 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002300 errormsg = (char_u *)_(e_invalidreg);
2301 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002304 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2305 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002306 {
2307 ea.regname = *ea.arg++;
2308#ifdef FEAT_EVAL
2309 /* for '=' register: accept the rest of the line as an expression */
2310 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2311 {
2312 set_expr_line(vim_strsave(ea.arg));
2313 ea.arg += STRLEN(ea.arg);
2314 }
2315#endif
2316 ea.arg = skipwhite(ea.arg);
2317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
2320 /*
2321 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2322 * count, it's a buffer name.
2323 */
2324 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2325 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002326 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 n = getdigits(&ea.arg);
2329 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002330 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332 errormsg = (char_u *)_(e_zerocount);
2333 goto doend;
2334 }
2335 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2336 {
2337 ea.line2 = n;
2338 if (ea.addr_count == 0)
2339 ea.addr_count = 1;
2340 }
2341 else
2342 {
2343 ea.line1 = ea.line2;
2344 ea.line2 += n - 1;
2345 ++ea.addr_count;
2346 /*
2347 * Be vi compatible: no error message for out of range.
2348 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002349 if (ea.addr_type == ADDR_LINES
2350 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 ea.line2 = curbuf->b_ml.ml_line_count;
2352 }
2353 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002354
2355 /*
2356 * Check for flags: 'l', 'p' and '#'.
2357 */
2358 if (ea.argt & EXFLAGS)
2359 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002361 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002362 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 errormsg = (char_u *)_(e_trailing);
2365 goto doend;
2366 }
2367
2368 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2369 {
2370 errormsg = (char_u *)_(e_argreq);
2371 goto doend;
2372 }
2373
2374#ifdef FEAT_EVAL
2375 /*
2376 * Skip the command when it's not going to be executed.
2377 * The commands like :if, :endif, etc. always need to be executed.
2378 * Also make an exception for commands that handle a trailing command
2379 * themselves.
2380 */
2381 if (ea.skip)
2382 {
2383 switch (ea.cmdidx)
2384 {
2385 /* commands that need evaluation */
2386 case CMD_while:
2387 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002388 case CMD_for:
2389 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_if:
2391 case CMD_elseif:
2392 case CMD_else:
2393 case CMD_endif:
2394 case CMD_try:
2395 case CMD_catch:
2396 case CMD_finally:
2397 case CMD_endtry:
2398 case CMD_function:
2399 break;
2400
2401 /* Commands that handle '|' themselves. Check: A command should
2402 * either have the TRLBAR flag, appear in this list or appear in
2403 * the list at ":help :bar". */
2404 case CMD_aboveleft:
2405 case CMD_and:
2406 case CMD_belowright:
2407 case CMD_botright:
2408 case CMD_browse:
2409 case CMD_call:
2410 case CMD_confirm:
2411 case CMD_delfunction:
2412 case CMD_djump:
2413 case CMD_dlist:
2414 case CMD_dsearch:
2415 case CMD_dsplit:
2416 case CMD_echo:
2417 case CMD_echoerr:
2418 case CMD_echomsg:
2419 case CMD_echon:
2420 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002421 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_help:
2423 case CMD_hide:
2424 case CMD_ijump:
2425 case CMD_ilist:
2426 case CMD_isearch:
2427 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002428 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 case CMD_keepjumps:
2430 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002431 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_leftabove:
2433 case CMD_let:
2434 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002435 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002437 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002438 case CMD_noautocmd:
2439 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_perl:
2441 case CMD_psearch:
2442 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002443 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002444 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_return:
2446 case CMD_rightbelow:
2447 case CMD_ruby:
2448 case CMD_silent:
2449 case CMD_smagic:
2450 case CMD_snomagic:
2451 case CMD_substitute:
2452 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002453 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_tcl:
2455 case CMD_throw:
2456 case CMD_tilde:
2457 case CMD_topleft:
2458 case CMD_unlet:
2459 case CMD_verbose:
2460 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002461 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 break;
2463
2464 default: goto doend;
2465 }
2466 }
2467#endif
2468
2469 if (ea.argt & XFILE)
2470 {
2471 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2472 goto doend;
2473 }
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 /*
2476 * Accept buffer name. Cannot be used at the same time with a buffer
2477 * number. Don't do this for a user command.
2478 */
2479 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002480 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 /*
2483 * :bdelete, :bwipeout and :bunload take several arguments, separated
2484 * by spaces: find next space (skipping over escaped characters).
2485 * The others take one argument: ignore trailing spaces.
2486 */
2487 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2488 || ea.cmdidx == CMD_bunload)
2489 p = skiptowhite_esc(ea.arg);
2490 else
2491 {
2492 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002493 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 --p;
2495 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002496 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2497 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (ea.line2 < 0) /* failed */
2499 goto doend;
2500 ea.addr_count = 1;
2501 ea.arg = skipwhite(p);
2502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
Bram Moolenaar2be57332018-02-13 18:05:18 +01002504 /* The :try command saves the emsg_silent flag, reset it here when
2505 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002506 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002507 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002508 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002509 if (emsg_silent < 0)
2510 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002511 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002512 }
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002515 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002519 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 /*
2522 * Execute a user-defined command.
2523 */
2524 do_ucmd(&ea);
2525 }
2526 else
2527#endif
2528 {
2529 /*
2530 * Call the function to execute the command.
2531 */
2532 ea.errmsg = NULL;
2533 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2534 if (ea.errmsg != NULL)
2535 errormsg = (char_u *)_(ea.errmsg);
2536 }
2537
2538#ifdef FEAT_EVAL
2539 /*
2540 * If the command just executed called do_cmdline(), any throw or ":return"
2541 * or ":finish" encountered there must also check the cstack of the still
2542 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2543 * exception, or reanimate a returned function or finished script file and
2544 * return or finish it again.
2545 */
2546 if (need_rethrow)
2547 do_throw(cstack);
2548 else if (check_cstack)
2549 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002550 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002552 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 && current_func_returned())
2554 do_return(&ea, TRUE, FALSE, NULL);
2555 }
2556 need_rethrow = check_cstack = FALSE;
2557#endif
2558
2559doend:
2560 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002563 curwin->w_cursor.col = 0;
2564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2567 {
2568 if (sourcing)
2569 {
2570 if (errormsg != IObuff)
2571 {
2572 STRCPY(IObuff, errormsg);
2573 errormsg = IObuff;
2574 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002575 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 emsg(errormsg);
2578 }
2579#ifdef FEAT_EVAL
2580 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002581 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2582 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583#endif
2584
Bram Moolenaareffed932018-08-14 13:38:17 +02002585 if (ea.verbose_save >= 0)
2586 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002587
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002588 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 cmdmod = save_cmdmod;
2590
Bram Moolenaareffed932018-08-14 13:38:17 +02002591 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 /* messages could be enabled for a serious error, need to check if the
2594 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002595 if (!did_emsg || msg_silent > ea.save_msg_silent)
2596 msg_silent = ea.save_msg_silent;
2597 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (emsg_silent < 0)
2599 emsg_silent = 0;
2600 /* Restore msg_scroll, it's set by file I/O commands, even when no
2601 * message is actually displayed. */
2602 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002603
2604 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2605 * somewhere in the line. Put it back in the first column. */
2606 if (redirecting())
2607 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002610#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002611 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002612 --sandbox;
2613#endif
2614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2616 ea.nextcmd = NULL;
2617
2618#ifdef FEAT_EVAL
2619 --ex_nesting_level;
2620#endif
2621
2622 return ea.nextcmd;
2623}
2624#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002625 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#endif
2627
2628/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002629 * Parse and skip over command modifiers:
2630 * - update eap->cmd
2631 * - store flags in "cmdmod".
2632 * - Set ex_pressedreturn for an empty command line.
2633 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002634 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002635 * - set p_verbose for ":verbose"
2636 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002637 * When "skip_only" is TRUE the global variables are not changed, except for
2638 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 * Return FAIL when the command is not to be executed.
2640 * May set "errormsg" to an error message.
2641 */
2642 int
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002643parse_command_modifiers(exarg_T *eap, char_u **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002644{
2645 char_u *p;
2646
2647 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2648 eap->verbose_save = -1;
2649 eap->save_msg_silent = -1;
2650
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002651 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002652 for (;;)
2653 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002654 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2655 ++eap->cmd;
2656
2657 /* in ex mode, an empty line works like :+ */
2658 if (*eap->cmd == NUL && exmode_active
2659 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2660 || getline_equal(eap->getline, eap->cookie, getexline))
2661 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2662 {
2663 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002664 if (!skip_only)
2665 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 }
2667
2668 /* ignore comment and empty lines */
2669 if (*eap->cmd == '"')
2670 return FAIL;
2671 if (*eap->cmd == NUL)
2672 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002673 if (!skip_only)
2674 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002675 return FAIL;
2676 }
2677
Bram Moolenaareffed932018-08-14 13:38:17 +02002678 p = skip_range(eap->cmd, NULL);
2679 switch (*p)
2680 {
2681 /* When adding an entry, also modify cmd_exists(). */
2682 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2683 break;
2684 cmdmod.split |= WSP_ABOVE;
2685 continue;
2686
2687 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2688 {
2689 cmdmod.split |= WSP_BELOW;
2690 continue;
2691 }
2692 if (checkforcmd(&eap->cmd, "browse", 3))
2693 {
2694#ifdef FEAT_BROWSE_CMD
2695 cmdmod.browse = TRUE;
2696#endif
2697 continue;
2698 }
2699 if (!checkforcmd(&eap->cmd, "botright", 2))
2700 break;
2701 cmdmod.split |= WSP_BOT;
2702 continue;
2703
2704 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2705 break;
2706#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2707 cmdmod.confirm = TRUE;
2708#endif
2709 continue;
2710
2711 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2712 {
2713 cmdmod.keepmarks = TRUE;
2714 continue;
2715 }
2716 if (checkforcmd(&eap->cmd, "keepalt", 5))
2717 {
2718 cmdmod.keepalt = TRUE;
2719 continue;
2720 }
2721 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2722 {
2723 cmdmod.keeppatterns = TRUE;
2724 continue;
2725 }
2726 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2727 break;
2728 cmdmod.keepjumps = TRUE;
2729 continue;
2730
2731 case 'f': /* only accept ":filter {pat} cmd" */
2732 {
2733 char_u *reg_pat;
2734
2735 if (!checkforcmd(&p, "filter", 4)
2736 || *p == NUL || ends_excmd(*p))
2737 break;
2738 if (*p == '!')
2739 {
2740 cmdmod.filter_force = TRUE;
2741 p = skipwhite(p + 1);
2742 if (*p == NUL || ends_excmd(*p))
2743 break;
2744 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002745 if (skip_only)
2746 p = skip_vimgrep_pat(p, NULL, NULL);
2747 else
2748 // NOTE: This puts a NUL after the pattern.
2749 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002750 if (p == NULL || *p == NUL)
2751 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002752 if (!skip_only)
2753 {
2754 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002755 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002756 if (cmdmod.filter_regmatch.regprog == NULL)
2757 break;
2758 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002759 eap->cmd = p;
2760 continue;
2761 }
2762
2763 /* ":hide" and ":hide | cmd" are not modifiers */
2764 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2765 || *p == NUL || ends_excmd(*p))
2766 break;
2767 eap->cmd = p;
2768 cmdmod.hide = TRUE;
2769 continue;
2770
2771 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2772 {
2773 cmdmod.lockmarks = TRUE;
2774 continue;
2775 }
2776
2777 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2778 break;
2779 cmdmod.split |= WSP_ABOVE;
2780 continue;
2781
2782 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2783 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002784 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002785 {
2786 /* Set 'eventignore' to "all". Restore the
2787 * existing option value later. */
2788 cmdmod.save_ei = vim_strsave(p_ei);
2789 set_string_option_direct((char_u *)"ei", -1,
2790 (char_u *)"all", OPT_FREE, SID_NONE);
2791 }
2792 continue;
2793 }
2794 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2795 break;
2796 cmdmod.noswapfile = TRUE;
2797 continue;
2798
2799 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2800 break;
2801 cmdmod.split |= WSP_BELOW;
2802 continue;
2803
2804 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2805 {
2806#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002807 if (!skip_only)
2808 {
2809 if (!eap->did_sandbox)
2810 ++sandbox;
2811 eap->did_sandbox = TRUE;
2812 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002813#endif
2814 continue;
2815 }
2816 if (!checkforcmd(&eap->cmd, "silent", 3))
2817 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002818 if (!skip_only)
2819 {
2820 if (eap->save_msg_silent == -1)
2821 eap->save_msg_silent = msg_silent;
2822 ++msg_silent;
2823 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2825 {
2826 /* ":silent!", but not "silent !cmd" */
2827 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002828 if (!skip_only)
2829 {
2830 ++emsg_silent;
2831 ++eap->did_esilent;
2832 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 }
2834 continue;
2835
2836 case 't': if (checkforcmd(&p, "tab", 3))
2837 {
2838 long tabnr = get_address(eap, &eap->cmd, ADDR_TABS,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002839 eap->skip, skip_only, FALSE, 1);
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 if (tabnr == MAXLNUM)
2841 cmdmod.tab = tabpage_index(curtab) + 1;
2842 else
2843 {
2844 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2845 {
2846 *errormsg = (char_u *)_(e_invrange);
2847 return FAIL;
2848 }
2849 cmdmod.tab = tabnr + 1;
2850 }
2851 eap->cmd = p;
2852 continue;
2853 }
2854 if (!checkforcmd(&eap->cmd, "topleft", 2))
2855 break;
2856 cmdmod.split |= WSP_TOP;
2857 continue;
2858
2859 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2860 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002861 if (!skip_only)
2862 {
2863 if (eap->save_msg_silent == -1)
2864 eap->save_msg_silent = msg_silent;
2865 msg_silent = 0;
2866 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 continue;
2868
2869 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2870 {
2871 cmdmod.split |= WSP_VERT;
2872 continue;
2873 }
2874 if (!checkforcmd(&p, "verbose", 4))
2875 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002876 if (!skip_only)
2877 {
2878 if (eap->verbose_save < 0)
2879 eap->verbose_save = p_verbose;
2880 if (vim_isdigit(*eap->cmd))
2881 p_verbose = atoi((char *)eap->cmd);
2882 else
2883 p_verbose = 1;
2884 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 eap->cmd = p;
2886 continue;
2887 }
2888 break;
2889 }
2890
2891 return OK;
2892}
2893
2894/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002895 * Free contents of "cmdmod".
2896 */
2897 static void
2898free_cmdmod(void)
2899{
2900 if (cmdmod.save_ei != NULL)
2901 {
2902 /* Restore 'eventignore' to the value before ":noautocmd". */
2903 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2904 OPT_FREE, SID_NONE);
2905 free_string_option(cmdmod.save_ei);
2906 }
2907
2908 if (cmdmod.filter_regmatch.regprog != NULL)
2909 vim_regfree(cmdmod.filter_regmatch.regprog);
2910}
2911
2912/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002913 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002914 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915 * Return FAIL and set "errormsg" or return OK.
2916 */
2917 int
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002918parse_cmd_address(exarg_T *eap, char_u **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002919{
2920 int address_count = 1;
2921 linenr_T lnum;
2922
2923 // Repeat for all ',' or ';' separated addresses.
2924 for (;;)
2925 {
2926 eap->line1 = eap->line2;
2927 switch (eap->addr_type)
2928 {
2929 case ADDR_LINES:
2930 // default is current line number
2931 eap->line2 = curwin->w_cursor.lnum;
2932 break;
2933 case ADDR_WINDOWS:
2934 eap->line2 = CURRENT_WIN_NR;
2935 break;
2936 case ADDR_ARGUMENTS:
2937 eap->line2 = curwin->w_arg_idx + 1;
2938 if (eap->line2 > ARGCOUNT)
2939 eap->line2 = ARGCOUNT;
2940 break;
2941 case ADDR_LOADED_BUFFERS:
2942 case ADDR_BUFFERS:
2943 eap->line2 = curbuf->b_fnum;
2944 break;
2945 case ADDR_TABS:
2946 eap->line2 = CURRENT_TAB_NR;
2947 break;
2948 case ADDR_TABS_RELATIVE:
2949 eap->line2 = 1;
2950 break;
2951#ifdef FEAT_QUICKFIX
2952 case ADDR_QUICKFIX:
2953 eap->line2 = qf_get_cur_valid_idx(eap);
2954 break;
2955#endif
2956 }
2957 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002958 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002959 eap->addr_count == 0, address_count++);
2960 if (eap->cmd == NULL) // error detected
2961 return FAIL;
2962 if (lnum == MAXLNUM)
2963 {
2964 if (*eap->cmd == '%') // '%' - all lines
2965 {
2966 ++eap->cmd;
2967 switch (eap->addr_type)
2968 {
2969 case ADDR_LINES:
2970 eap->line1 = 1;
2971 eap->line2 = curbuf->b_ml.ml_line_count;
2972 break;
2973 case ADDR_LOADED_BUFFERS:
2974 {
2975 buf_T *buf = firstbuf;
2976
2977 while (buf->b_next != NULL
2978 && buf->b_ml.ml_mfp == NULL)
2979 buf = buf->b_next;
2980 eap->line1 = buf->b_fnum;
2981 buf = lastbuf;
2982 while (buf->b_prev != NULL
2983 && buf->b_ml.ml_mfp == NULL)
2984 buf = buf->b_prev;
2985 eap->line2 = buf->b_fnum;
2986 break;
2987 }
2988 case ADDR_BUFFERS:
2989 eap->line1 = firstbuf->b_fnum;
2990 eap->line2 = lastbuf->b_fnum;
2991 break;
2992 case ADDR_WINDOWS:
2993 case ADDR_TABS:
2994 if (IS_USER_CMDIDX(eap->cmdidx))
2995 {
2996 eap->line1 = 1;
2997 eap->line2 = eap->addr_type == ADDR_WINDOWS
2998 ? LAST_WIN_NR : LAST_TAB_NR;
2999 }
3000 else
3001 {
3002 // there is no Vim command which uses '%' and
3003 // ADDR_WINDOWS or ADDR_TABS
3004 *errormsg = (char_u *)_(e_invrange);
3005 return FAIL;
3006 }
3007 break;
3008 case ADDR_TABS_RELATIVE:
3009 *errormsg = (char_u *)_(e_invrange);
3010 return FAIL;
3011 case ADDR_ARGUMENTS:
3012 if (ARGCOUNT == 0)
3013 eap->line1 = eap->line2 = 0;
3014 else
3015 {
3016 eap->line1 = 1;
3017 eap->line2 = ARGCOUNT;
3018 }
3019 break;
3020#ifdef FEAT_QUICKFIX
3021 case ADDR_QUICKFIX:
3022 eap->line1 = 1;
3023 eap->line2 = qf_get_size(eap);
3024 if (eap->line2 == 0)
3025 eap->line2 = 1;
3026 break;
3027#endif
3028 }
3029 ++eap->addr_count;
3030 }
3031 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3032 {
3033 pos_T *fp;
3034
3035 // '*' - visual area
3036 if (eap->addr_type != ADDR_LINES)
3037 {
3038 *errormsg = (char_u *)_(e_invrange);
3039 return FAIL;
3040 }
3041
3042 ++eap->cmd;
3043 if (!eap->skip)
3044 {
3045 fp = getmark('<', FALSE);
3046 if (check_mark(fp) == FAIL)
3047 return FAIL;
3048 eap->line1 = fp->lnum;
3049 fp = getmark('>', FALSE);
3050 if (check_mark(fp) == FAIL)
3051 return FAIL;
3052 eap->line2 = fp->lnum;
3053 ++eap->addr_count;
3054 }
3055 }
3056 }
3057 else
3058 eap->line2 = lnum;
3059 eap->addr_count++;
3060
3061 if (*eap->cmd == ';')
3062 {
3063 if (!eap->skip)
3064 {
3065 curwin->w_cursor.lnum = eap->line2;
3066 // don't leave the cursor on an illegal line or column
3067 check_cursor();
3068 }
3069 }
3070 else if (*eap->cmd != ',')
3071 break;
3072 ++eap->cmd;
3073 }
3074
3075 // One address given: set start and end lines.
3076 if (eap->addr_count == 1)
3077 {
3078 eap->line1 = eap->line2;
3079 // ... but only implicit: really no address given
3080 if (lnum == MAXLNUM)
3081 eap->addr_count = 0;
3082 }
3083 return OK;
3084}
3085
3086/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003087 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * If there is a match advance "pp" to the argument and return TRUE.
3089 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003090 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091checkforcmd(
3092 char_u **pp, /* start of command */
3093 char *cmd, /* name of command */
3094 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 int i;
3097
3098 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003099 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 break;
3101 if (i >= len && !isalpha((*pp)[i]))
3102 {
3103 *pp = skipwhite(*pp + i);
3104 return TRUE;
3105 }
3106 return FALSE;
3107}
3108
3109/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003110 * Append "cmd" to the error message in IObuff.
3111 * Takes care of limiting the length and handling 0xa0, which would be
3112 * invisible otherwise.
3113 */
3114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003115append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003116{
3117 char_u *s = cmd;
3118 char_u *d;
3119
3120 STRCAT(IObuff, ": ");
3121 d = IObuff + STRLEN(IObuff);
3122 while (*s != NUL && d - IObuff < IOSIZE - 7)
3123 {
3124 if (
3125#ifdef FEAT_MBYTE
3126 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3127#endif
3128 *s == 0xa0)
3129 {
3130 s +=
3131#ifdef FEAT_MBYTE
3132 enc_utf8 ? 2 :
3133#endif
3134 1;
3135 STRCPY(d, "<a0>");
3136 d += 4;
3137 }
3138 else
3139 MB_COPY_CHAR(s, d);
3140 }
3141 *d = NUL;
3142}
3143
3144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003148 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * Returns NULL for an ambiguous user command.
3150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
3154 int len;
3155 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003156 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 /*
3159 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003160 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * - the 'k' command can directly be followed by any character.
3162 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003163 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003165 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 */
3167 p = eap->cmd;
3168 if (*p == 'k')
3169 {
3170 eap->cmdidx = CMD_k;
3171 ++p;
3172 }
3173 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003174 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3175 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 || p[1] == 'g'
3177 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3178 || p[1] == 'I'
3179 || (p[1] == 'r' && p[2] != 'e')))
3180 {
3181 eap->cmdidx = CMD_substitute;
3182 ++p;
3183 }
3184 else
3185 {
3186 while (ASCII_ISALPHA(*p))
3187 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003188 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003189 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003190 while (ASCII_ISALNUM(*p))
3191 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 /* check for non-alpha command */
3194 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3195 ++p;
3196 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003197 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3198 {
3199 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3200 * :delete with the 'l' flag. Same for 'p'. */
3201 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003202 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003203 break;
3204 if (i == len - 1)
3205 {
3206 --len;
3207 if (p[-1] == 'l')
3208 eap->flags |= EXFLAG_LIST;
3209 else
3210 eap->flags |= EXFLAG_PRINT;
3211 }
3212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003214 if (ASCII_ISLOWER(eap->cmd[0]))
3215 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003216 int c1 = eap->cmd[0];
3217 int c2 = eap->cmd[1];
3218
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003219 if (command_count != (int)CMD_SIZE)
3220 {
3221 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3222 getout(1);
3223 }
3224
3225 /* Use a precomputed index for fast look-up in cmdnames[]
3226 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003227 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3228 if (ASCII_ISLOWER(c2))
3229 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003232 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3235 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3236 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3237 (size_t)len) == 0)
3238 {
3239#ifdef FEAT_EVAL
3240 if (full != NULL
3241 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3242 *full = TRUE;
3243#endif
3244 break;
3245 }
3246
3247#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003248 /* Look for a user defined command as a last resort. Let ":Print" be
3249 * overruled by a user defined command. */
3250 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3251 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 while (ASCII_ISALNUM(*p))
3255 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003259 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 eap->cmdidx = CMD_SIZE;
3261 }
3262
3263 return p;
3264}
3265
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003266#ifdef FEAT_USR_CMDS
3267/*
3268 * Search for a user command that matches "eap->cmd".
3269 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3270 * Return a pointer to just after the command.
3271 * Return NULL if there is no matching command.
3272 */
3273 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003274find_ucmd(
3275 exarg_T *eap,
3276 char_u *p, /* end of the command (possibly including count) */
3277 int *full, /* set to TRUE for a full match */
3278 expand_T *xp, /* used for completion, NULL otherwise */
3279 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003280{
3281 int len = (int)(p - eap->cmd);
3282 int j, k, matchlen = 0;
3283 ucmd_T *uc;
3284 int found = FALSE;
3285 int possible = FALSE;
3286 char_u *cp, *np; /* Point into typed cmd and test name */
3287 garray_T *gap;
3288 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3289 only full match global is accepted. */
3290
3291 /*
3292 * Look for buffer-local user commands first, then global ones.
3293 */
3294 gap = &curbuf->b_ucmds;
3295 for (;;)
3296 {
3297 for (j = 0; j < gap->ga_len; ++j)
3298 {
3299 uc = USER_CMD_GA(gap, j);
3300 cp = eap->cmd;
3301 np = uc->uc_name;
3302 k = 0;
3303 while (k < len && *np != NUL && *cp++ == *np++)
3304 k++;
3305 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3306 {
3307 /* If finding a second match, the command is ambiguous. But
3308 * not if a buffer-local command wasn't a full match and a
3309 * global command is a full match. */
3310 if (k == len && found && *np != NUL)
3311 {
3312 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003313 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003314 amb_local = TRUE;
3315 }
3316
3317 if (!found || (k == len && *np == NUL))
3318 {
3319 /* If we matched up to a digit, then there could
3320 * be another command including the digit that we
3321 * should use instead.
3322 */
3323 if (k == len)
3324 found = TRUE;
3325 else
3326 possible = TRUE;
3327
3328 if (gap == &ucmds)
3329 eap->cmdidx = CMD_USER;
3330 else
3331 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003332 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003333 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003334 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003335
3336# ifdef FEAT_CMDL_COMPL
3337 if (compl != NULL)
3338 *compl = uc->uc_compl;
3339# ifdef FEAT_EVAL
3340 if (xp != NULL)
3341 {
3342 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003343 xp->xp_script_ctx = uc->uc_script_ctx;
3344 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003345 }
3346# endif
3347# endif
3348 /* Do not search for further abbreviations
3349 * if this is an exact match. */
3350 matchlen = k;
3351 if (k == len && *np == NUL)
3352 {
3353 if (full != NULL)
3354 *full = TRUE;
3355 amb_local = FALSE;
3356 break;
3357 }
3358 }
3359 }
3360 }
3361
3362 /* Stop if we found a full match or searched all. */
3363 if (j < gap->ga_len || gap == &ucmds)
3364 break;
3365 gap = &ucmds;
3366 }
3367
3368 /* Only found ambiguous matches. */
3369 if (amb_local)
3370 {
3371 if (xp != NULL)
3372 xp->xp_context = EXPAND_UNSUCCESSFUL;
3373 return NULL;
3374 }
3375
3376 /* The match we found may be followed immediately by a number. Move "p"
3377 * back to point to it. */
3378 if (found || possible)
3379 return p + (matchlen - len);
3380 return p;
3381}
3382#endif
3383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003385static struct cmdmod
3386{
3387 char *name;
3388 int minlen;
3389 int has_count; /* :123verbose :3tab */
3390} cmdmods[] = {
3391 {"aboveleft", 3, FALSE},
3392 {"belowright", 3, FALSE},
3393 {"botright", 2, FALSE},
3394 {"browse", 3, FALSE},
3395 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003396 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 {"hide", 3, FALSE},
3398 {"keepalt", 5, FALSE},
3399 {"keepjumps", 5, FALSE},
3400 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003401 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003402 {"leftabove", 5, FALSE},
3403 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003404 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003405 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003406 {"rightbelow", 6, FALSE},
3407 {"sandbox", 3, FALSE},
3408 {"silent", 3, FALSE},
3409 {"tab", 3, TRUE},
3410 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003411 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003412 {"verbose", 4, TRUE},
3413 {"vertical", 4, FALSE},
3414};
3415
3416/*
3417 * Return length of a command modifier (including optional count).
3418 * Return zero when it's not a modifier.
3419 */
3420 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003421modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003422{
3423 int i, j;
3424 char_u *p = cmd;
3425
3426 if (VIM_ISDIGIT(*cmd))
3427 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003428 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003429 {
3430 for (j = 0; p[j] != NUL; ++j)
3431 if (p[j] != cmdmods[i].name[j])
3432 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003433 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003434 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003435 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003436 }
3437 return 0;
3438}
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440/*
3441 * Return > 0 if an Ex command "name" exists.
3442 * Return 2 if there is an exact match.
3443 * Return 3 if there is an ambiguous match.
3444 */
3445 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003446cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
3448 exarg_T ea;
3449 int full = FALSE;
3450 int i;
3451 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003452 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003455 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
3457 for (j = 0; name[j] != NUL; ++j)
3458 if (name[j] != cmdmods[i].name[j])
3459 break;
3460 if (name[j] == NUL && j >= cmdmods[i].minlen)
3461 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3462 }
3463
Bram Moolenaara9587612006-05-04 21:47:50 +00003464 /* Check built-in commands and user defined commands.
3465 * For ":2match" and ":3match" we need to skip the number. */
3466 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003468 p = find_command(&ea, &full);
3469 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003471 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3472 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003473 if (*skipwhite(p) != NUL)
3474 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3476}
3477#endif
3478
3479/*
3480 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3481 * we don't need/want deleted. Maybe this could be done better if we didn't
3482 * repeat all this stuff. The only problem is that they may not stay
3483 * perfectly compatible with each other, but then the command line syntax
3484 * probably won't change that much -- webb.
3485 */
3486 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003487set_one_cmd_context(
3488 expand_T *xp,
3489 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 char_u *p;
3492 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003493 int len = 0;
3494 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3496 int compl = EXPAND_NOTHING;
3497#endif
3498#ifdef FEAT_CMDL_COMPL
3499 int delim;
3500#endif
3501 int forceit = FALSE;
3502 int usefilter = FALSE; /* filter instead of file name */
3503
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003504 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 xp->xp_pattern = buff;
3506 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003507 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003510 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 */
3512 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3513 ;
3514 xp->xp_pattern = cmd;
3515
3516 if (*cmd == NUL)
3517 return NULL;
3518 if (*cmd == '"') /* ignore comment lines */
3519 {
3520 xp->xp_context = EXPAND_NOTHING;
3521 return NULL;
3522 }
3523
3524/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003525 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 */
3527 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 xp->xp_pattern = cmd;
3529 if (*cmd == NUL)
3530 return NULL;
3531 if (*cmd == '"')
3532 {
3533 xp->xp_context = EXPAND_NOTHING;
3534 return NULL;
3535 }
3536
3537 if (*cmd == '|' || *cmd == '\n')
3538 return cmd + 1; /* There's another command */
3539
3540 /*
3541 * Isolate the command and search for it in the command table.
3542 * Exceptions:
3543 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003544 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3546 */
3547 if (*cmd == 'k' && cmd[1] != 'e')
3548 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 p = cmd + 1;
3551 }
3552 else
3553 {
3554 p = cmd;
3555 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3556 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003557 /* a user command may contain digits */
3558 if (ASCII_ISUPPER(cmd[0]))
3559 while (ASCII_ISALNUM(*p) || *p == '*')
3560 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003561 /* for python 3.x: ":py3*" commands completion */
3562 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003563 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003564 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003565 while (ASCII_ISALPHA(*p) || *p == '*')
3566 ++p;
3567 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003568 /* check for non-alpha command */
3569 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3570 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 xp->xp_context = EXPAND_UNSUCCESSFUL;
3576 return NULL;
3577 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003579 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3580 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3581 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 break;
3583
3584#ifdef FEAT_USR_CMDS
3585 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3587 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588#endif
3589 }
3590
3591 /*
3592 * If the cursor is touching the command, and it ends in an alpha-numeric
3593 * character, complete the command name.
3594 */
3595 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3596 return NULL;
3597
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003598 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3601 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 p = cmd + 1;
3604 }
3605#ifdef FEAT_USR_CMDS
3606 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3607 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003608 ea.cmd = cmd;
3609 p = find_ucmd(&ea, p, NULL, xp,
3610# if defined(FEAT_CMDL_COMPL)
3611 &compl
3612# else
3613 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003615 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003616 if (p == NULL)
3617 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619#endif
3620 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 /* Not still touching the command and it was an illegal one */
3624 xp->xp_context = EXPAND_UNSUCCESSFUL;
3625 return NULL;
3626 }
3627
3628 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3629
3630 if (*p == '!') /* forced commands */
3631 {
3632 forceit = TRUE;
3633 ++p;
3634 }
3635
3636/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003637 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003639 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003640 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 arg = skipwhite(p);
3643
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
3646 if (*arg == '>') /* append */
3647 {
3648 if (*++arg == '>')
3649 ++arg;
3650 arg = skipwhite(arg);
3651 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003652 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654 ++arg;
3655 usefilter = TRUE;
3656 }
3657 }
3658
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003659 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
3661 usefilter = forceit; /* :r! filter if forced */
3662 if (*arg == '!') /* :r !filter */
3663 {
3664 ++arg;
3665 usefilter = TRUE;
3666 }
3667 }
3668
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003669 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
3671 while (*arg == *cmd) /* allow any number of '>' or '<' */
3672 ++arg;
3673 arg = skipwhite(arg);
3674 }
3675
3676 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003677 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
3679 /* Check if we're in the +command */
3680 p = arg + 1;
3681 arg = skip_cmd_arg(arg, FALSE);
3682
3683 /* Still touching the command after '+'? */
3684 if (*arg == NUL)
3685 return p;
3686
3687 /* Skip space(s) after +command to get to the real argument */
3688 arg = skipwhite(arg);
3689 }
3690
3691 /*
3692 * Check for '|' to separate commands and '"' to start comments.
3693 * Don't do this for ":read !cmd" and ":write !cmd".
3694 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003695 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 p = arg;
3698 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003699 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 p += 2;
3701 while (*p)
3702 {
3703 if (*p == Ctrl_V)
3704 {
3705 if (p[1] != NUL)
3706 ++p;
3707 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003708 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 || *p == '|' || *p == '\n')
3710 {
3711 if (*(p - 1) != '\\')
3712 {
3713 if (*p == '|' || *p == '\n')
3714 return p + 1;
3715 return NULL; /* It's a comment */
3716 }
3717 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003718 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 }
3721
3722 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003723 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 vim_strchr((char_u *)"|\"", *arg) == NULL)
3725 return NULL;
3726
3727 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003728 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003730 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003731 while (*p && p < buff + len)
3732 {
3733 if (*p == ' ' || *p == TAB)
3734 {
3735 /* argument starts after a space */
3736 xp->xp_pattern = ++p;
3737 }
3738 else
3739 {
3740 if (*p == '\\' && *(p + 1) != NUL)
3741 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003742 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003743 }
3744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003746 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003748 int c;
3749 int in_quote = FALSE;
3750 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 /*
3753 * Allow spaces within back-quotes to count as part of the argument
3754 * being expanded.
3755 */
3756 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003757 p = xp->xp_pattern;
3758 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003760#ifdef FEAT_MBYTE
3761 if (has_mbyte)
3762 c = mb_ptr2char(p);
3763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003765 c = *p;
3766 if (c == '\\' && p[1] != NUL)
3767 ++p;
3768 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
3770 if (!in_quote)
3771 {
3772 xp->xp_pattern = p;
3773 bow = p + 1;
3774 }
3775 in_quote = !in_quote;
3776 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003777 /* An argument can contain just about everything, except
3778 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003779 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003780#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003781 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003782#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003783 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003784 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003785 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003786 while (*p != NUL)
3787 {
3788#ifdef FEAT_MBYTE
3789 if (has_mbyte)
3790 c = mb_ptr2char(p);
3791 else
3792#endif
3793 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003794 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003795 break;
3796#ifdef FEAT_MBYTE
3797 if (has_mbyte)
3798 len = (*mb_ptr2len)(p);
3799 else
3800#endif
3801 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003802 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003803 }
3804 if (in_quote)
3805 bow = p;
3806 else
3807 xp->xp_pattern = p;
3808 p -= len;
3809 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003810 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812
3813 /*
3814 * If we are still inside the quotes, and we passed a space, just
3815 * expand from there.
3816 */
3817 if (bow != NULL && in_quote)
3818 xp->xp_pattern = bow;
3819 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003820
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003821 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003822 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003823 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003824#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003825 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003826#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003827 /* When still after the command name expand executables. */
3828 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003829 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 /* Check for environment variable */
3833 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003834#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 || *xp->xp_pattern == '%'
3836#endif
3837 )
3838 {
3839 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3840 if (!vim_isIDc(*p))
3841 break;
3842 if (*p == NUL)
3843 {
3844 xp->xp_context = EXPAND_ENV_VARS;
3845 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003846#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3847 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003848 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003849 compl = EXPAND_ENV_VARS;
3850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003853#if defined(FEAT_CMDL_COMPL)
3854 /* Check for user names */
3855 if (*xp->xp_pattern == '~')
3856 {
3857 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3858 ;
3859 /* Complete ~user only if it partially matches a user name.
3860 * A full match ~user<Tab> will be replaced by user's home
3861 * directory i.e. something like ~user<Tab> -> /home/user/ */
3862 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003863 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003864 {
3865 xp->xp_context = EXPAND_USER;
3866 ++xp->xp_pattern;
3867 }
3868 }
3869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871
3872/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003873 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003875 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003877 case CMD_find:
3878 case CMD_sfind:
3879 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003880 if (xp->xp_context == EXPAND_FILES)
3881 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003882 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_cd:
3884 case CMD_chdir:
3885 case CMD_lcd:
3886 case CMD_lchdir:
3887 if (xp->xp_context == EXPAND_FILES)
3888 xp->xp_context = EXPAND_DIRECTORIES;
3889 break;
3890 case CMD_help:
3891 xp->xp_context = EXPAND_HELP;
3892 xp->xp_pattern = arg;
3893 break;
3894
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003895 /* Command modifiers: return the argument.
3896 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003898 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 case CMD_belowright:
3900 case CMD_botright:
3901 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003902 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003903 case CMD_cdo:
3904 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003906 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 case CMD_folddoclosed:
3908 case CMD_folddoopen:
3909 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003910 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 case CMD_keepjumps:
3912 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003913 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003914 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003916 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003918 case CMD_noautocmd:
3919 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003921 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003923 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003924 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 case CMD_topleft:
3926 case CMD_verbose:
3927 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003928 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 return arg;
3930
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003931 case CMD_filter:
3932 if (*arg != NUL)
3933 arg = skip_vimgrep_pat(arg, NULL, NULL);
3934 if (arg == NULL || *arg == NUL)
3935 {
3936 xp->xp_context = EXPAND_NOTHING;
3937 return NULL;
3938 }
3939 return skipwhite(arg);
3940
Bram Moolenaar4f688582007-07-24 12:34:30 +00003941#ifdef FEAT_CMDL_COMPL
3942# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 case CMD_match:
3944 if (*arg == NUL || !ends_excmd(*arg))
3945 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003946 /* also complete "None" */
3947 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 arg = skipwhite(skiptowhite(arg));
3949 if (*arg != NUL)
3950 {
3951 xp->xp_context = EXPAND_NOTHING;
3952 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3953 }
3954 }
3955 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958/*
3959 * All completion for the +cmdline_compl feature goes here.
3960 */
3961
3962# ifdef FEAT_USR_CMDS
3963 case CMD_command:
3964 /* Check for attributes */
3965 while (*arg == '-')
3966 {
3967 arg++; /* Skip "-" */
3968 p = skiptowhite(arg);
3969 if (*p == NUL)
3970 {
3971 /* Cursor is still in the attribute */
3972 p = vim_strchr(arg, '=');
3973 if (p == NULL)
3974 {
3975 /* No "=", so complete attribute names */
3976 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3977 xp->xp_pattern = arg;
3978 return NULL;
3979 }
3980
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003981 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 * their arguments as well.
3983 */
3984 if (STRNICMP(arg, "complete", p - arg) == 0)
3985 {
3986 xp->xp_context = EXPAND_USER_COMPLETE;
3987 xp->xp_pattern = p + 1;
3988 return NULL;
3989 }
3990 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3991 {
3992 xp->xp_context = EXPAND_USER_NARGS;
3993 xp->xp_pattern = p + 1;
3994 return NULL;
3995 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003996 else if (STRNICMP(arg, "addr", p - arg) == 0)
3997 {
3998 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3999 xp->xp_pattern = p + 1;
4000 return NULL;
4001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 return NULL;
4003 }
4004 arg = skipwhite(p);
4005 }
4006
4007 /* After the attributes comes the new command name */
4008 p = skiptowhite(arg);
4009 if (*p == NUL)
4010 {
4011 xp->xp_context = EXPAND_USER_COMMANDS;
4012 xp->xp_pattern = arg;
4013 break;
4014 }
4015
4016 /* And finally comes a normal command */
4017 return skipwhite(p);
4018
4019 case CMD_delcommand:
4020 xp->xp_context = EXPAND_USER_COMMANDS;
4021 xp->xp_pattern = arg;
4022 break;
4023# endif
4024
4025 case CMD_global:
4026 case CMD_vglobal:
4027 delim = *arg; /* get the delimiter */
4028 if (delim)
4029 ++arg; /* skip delimiter if there is one */
4030
4031 while (arg[0] != NUL && arg[0] != delim)
4032 {
4033 if (arg[0] == '\\' && arg[1] != NUL)
4034 ++arg;
4035 ++arg;
4036 }
4037 if (arg[0] != NUL)
4038 return arg + 1;
4039 break;
4040 case CMD_and:
4041 case CMD_substitute:
4042 delim = *arg;
4043 if (delim)
4044 {
4045 /* skip "from" part */
4046 ++arg;
4047 arg = skip_regexp(arg, delim, p_magic, NULL);
4048 }
4049 /* skip "to" part */
4050 while (arg[0] != NUL && arg[0] != delim)
4051 {
4052 if (arg[0] == '\\' && arg[1] != NUL)
4053 ++arg;
4054 ++arg;
4055 }
4056 if (arg[0] != NUL) /* skip delimiter */
4057 ++arg;
4058 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4059 ++arg;
4060 if (arg[0] != NUL)
4061 return arg;
4062 break;
4063 case CMD_isearch:
4064 case CMD_dsearch:
4065 case CMD_ilist:
4066 case CMD_dlist:
4067 case CMD_ijump:
4068 case CMD_psearch:
4069 case CMD_djump:
4070 case CMD_isplit:
4071 case CMD_dsplit:
4072 arg = skipwhite(skipdigits(arg)); /* skip count */
4073 if (*arg == '/') /* Match regexp, not just whole words */
4074 {
4075 for (++arg; *arg && *arg != '/'; arg++)
4076 if (*arg == '\\' && arg[1] != NUL)
4077 arg++;
4078 if (*arg)
4079 {
4080 arg = skipwhite(arg + 1);
4081
4082 /* Check for trailing illegal characters */
4083 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4084 xp->xp_context = EXPAND_NOTHING;
4085 else
4086 return arg;
4087 }
4088 }
4089 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 case CMD_autocmd:
4092 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004094 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 case CMD_set:
4097 set_context_in_set_cmd(xp, arg, 0);
4098 break;
4099 case CMD_setglobal:
4100 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4101 break;
4102 case CMD_setlocal:
4103 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4104 break;
4105 case CMD_tag:
4106 case CMD_stag:
4107 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004108 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 case CMD_tselect:
4110 case CMD_stselect:
4111 case CMD_ptselect:
4112 case CMD_tjump:
4113 case CMD_stjump:
4114 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004115 if (*p_wop != NUL)
4116 xp->xp_context = EXPAND_TAGS_LISTFILES;
4117 else
4118 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 xp->xp_pattern = arg;
4120 break;
4121 case CMD_augroup:
4122 xp->xp_context = EXPAND_AUGROUP;
4123 xp->xp_pattern = arg;
4124 break;
4125#ifdef FEAT_SYN_HL
4126 case CMD_syntax:
4127 set_context_in_syntax_cmd(xp, arg);
4128 break;
4129#endif
4130#ifdef FEAT_EVAL
4131 case CMD_let:
4132 case CMD_if:
4133 case CMD_elseif:
4134 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004135 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 case CMD_echo:
4137 case CMD_echon:
4138 case CMD_execute:
4139 case CMD_echomsg:
4140 case CMD_echoerr:
4141 case CMD_call:
4142 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004143 case CMD_cexpr:
4144 case CMD_caddexpr:
4145 case CMD_cgetexpr:
4146 case CMD_lexpr:
4147 case CMD_laddexpr:
4148 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004149 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 break;
4151
4152 case CMD_unlet:
4153 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4154 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 xp->xp_context = EXPAND_USER_VARS;
4157 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004158
4159 if (*xp->xp_pattern == '$')
4160 {
4161 xp->xp_context = EXPAND_ENV_VARS;
4162 ++xp->xp_pattern;
4163 }
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 break;
4166
4167 case CMD_function:
4168 case CMD_delfunction:
4169 xp->xp_context = EXPAND_USER_FUNC;
4170 xp->xp_pattern = arg;
4171 break;
4172
4173 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004174 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 break;
4176#endif
4177 case CMD_highlight:
4178 set_context_in_highlight_cmd(xp, arg);
4179 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004180#ifdef FEAT_CSCOPE
4181 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004182 case CMD_lcscope:
4183 case CMD_scscope:
4184 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004185 break;
4186#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004187#ifdef FEAT_SIGNS
4188 case CMD_sign:
4189 set_context_in_sign_cmd(xp, arg);
4190 break;
4191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 case CMD_bdelete:
4193 case CMD_bwipeout:
4194 case CMD_bunload:
4195 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4196 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004197 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 case CMD_buffer:
4199 case CMD_sbuffer:
4200 case CMD_checktime:
4201 xp->xp_context = EXPAND_BUFFERS;
4202 xp->xp_pattern = arg;
4203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204#ifdef FEAT_USR_CMDS
4205 case CMD_USER:
4206 case CMD_USER_BUF:
4207 if (compl != EXPAND_NOTHING)
4208 {
4209 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004210 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212# ifdef FEAT_MENU
4213 if (compl == EXPAND_MENUS)
4214 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4215# endif
4216 if (compl == EXPAND_COMMANDS)
4217 return arg;
4218 if (compl == EXPAND_MAPPINGS)
4219 return set_context_in_map_cmd(xp, (char_u *)"map",
4220 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004221 /* Find start of last argument. */
4222 p = arg;
4223 while (*p)
4224 {
4225 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004226 /* argument starts after a space */
4227 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004228 else if (*p == '\\' && *(p + 1) != NUL)
4229 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004230 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 xp->xp_pattern = arg;
4233 }
4234 xp->xp_context = compl;
4235 }
4236 break;
4237#endif
4238 case CMD_map: case CMD_noremap:
4239 case CMD_nmap: case CMD_nnoremap:
4240 case CMD_vmap: case CMD_vnoremap:
4241 case CMD_omap: case CMD_onoremap:
4242 case CMD_imap: case CMD_inoremap:
4243 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004244 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004245 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004246 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004247 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004249 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 case CMD_unmap:
4251 case CMD_nunmap:
4252 case CMD_vunmap:
4253 case CMD_ounmap:
4254 case CMD_iunmap:
4255 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004256 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004257 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004258 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004259 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004261 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004262 case CMD_mapclear:
4263 case CMD_nmapclear:
4264 case CMD_vmapclear:
4265 case CMD_omapclear:
4266 case CMD_imapclear:
4267 case CMD_cmapclear:
4268 case CMD_lmapclear:
4269 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004270 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004271 case CMD_xmapclear:
4272 xp->xp_context = EXPAND_MAPCLEAR;
4273 xp->xp_pattern = arg;
4274 break;
4275
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 case CMD_abbreviate: case CMD_noreabbrev:
4277 case CMD_cabbrev: case CMD_cnoreabbrev:
4278 case CMD_iabbrev: case CMD_inoreabbrev:
4279 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004280 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 case CMD_unabbreviate:
4282 case CMD_cunabbrev:
4283 case CMD_iunabbrev:
4284 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004285 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#ifdef FEAT_MENU
4287 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4288 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4289 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4290 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4291 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4292 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4293 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4294 case CMD_tmenu: case CMD_tunmenu:
4295 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4296 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4297#endif
4298
4299 case CMD_colorscheme:
4300 xp->xp_context = EXPAND_COLORS;
4301 xp->xp_pattern = arg;
4302 break;
4303
4304 case CMD_compiler:
4305 xp->xp_context = EXPAND_COMPILER;
4306 xp->xp_pattern = arg;
4307 break;
4308
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004309 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004310 xp->xp_context = EXPAND_OWNSYNTAX;
4311 xp->xp_pattern = arg;
4312 break;
4313
4314 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004315 xp->xp_context = EXPAND_FILETYPE;
4316 xp->xp_pattern = arg;
4317 break;
4318
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004319 case CMD_packadd:
4320 xp->xp_context = EXPAND_PACKADD;
4321 xp->xp_pattern = arg;
4322 break;
4323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4325 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4326 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004327 p = skiptowhite(arg);
4328 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 xp->xp_context = EXPAND_LANGUAGE;
4331 xp->xp_pattern = arg;
4332 }
4333 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004334 {
4335 if ( STRNCMP(arg, "messages", p - arg) == 0
4336 || STRNCMP(arg, "ctype", p - arg) == 0
4337 || STRNCMP(arg, "time", p - arg) == 0)
4338 {
4339 xp->xp_context = EXPAND_LOCALES;
4340 xp->xp_pattern = skipwhite(p);
4341 }
4342 else
4343 xp->xp_context = EXPAND_NOTHING;
4344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 break;
4346#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004347#if defined(FEAT_PROFILE)
4348 case CMD_profile:
4349 set_context_in_profile_cmd(xp, arg);
4350 break;
4351#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004352 case CMD_behave:
4353 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004354 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004357 case CMD_messages:
4358 xp->xp_context = EXPAND_MESSAGES;
4359 xp->xp_pattern = arg;
4360 break;
4361
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004362#if defined(FEAT_CMDHIST)
4363 case CMD_history:
4364 xp->xp_context = EXPAND_HISTORY;
4365 xp->xp_pattern = arg;
4366 break;
4367#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004368#if defined(FEAT_PROFILE)
4369 case CMD_syntime:
4370 xp->xp_context = EXPAND_SYNTIME;
4371 xp->xp_pattern = arg;
4372 break;
4373#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004374
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004375 case CMD_argdelete:
4376 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4377 arg = xp->xp_pattern + 1;
4378 xp->xp_context = EXPAND_ARGLIST;
4379 xp->xp_pattern = arg;
4380 break;
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382#endif /* FEAT_CMDL_COMPL */
4383
4384 default:
4385 break;
4386 }
4387 return NULL;
4388}
4389
4390/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004391 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 *
4393 * Backslashed delimiters after / or ? will be skipped, and commands will
4394 * not be expanded between /'s and ?'s or after "'".
4395 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004396 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 * Returns the "cmd" pointer advanced to beyond the range.
4398 */
4399 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004400skip_range(
4401 char_u *cmd,
4402 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004404 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004406 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004408 if (*cmd == '\\')
4409 {
4410 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4411 ++cmd;
4412 else
4413 break;
4414 }
4415 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
4417 if (*++cmd == NUL && ctx != NULL)
4418 *ctx = EXPAND_NOTHING;
4419 }
4420 else if (*cmd == '/' || *cmd == '?')
4421 {
4422 delim = *cmd++;
4423 while (*cmd != NUL && *cmd != delim)
4424 if (*cmd++ == '\\' && *cmd != NUL)
4425 ++cmd;
4426 if (*cmd == NUL && ctx != NULL)
4427 *ctx = EXPAND_NOTHING;
4428 }
4429 if (*cmd != NUL)
4430 ++cmd;
4431 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004432
4433 /* Skip ":" and white space. */
4434 while (*cmd == ':')
4435 cmd = skipwhite(cmd + 1);
4436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 return cmd;
4438}
4439
4440/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004441 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 *
4443 * Set ptr to the next character after the part that was interpreted.
4444 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004445 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 *
4447 * Return MAXLNUM when no Ex address was found.
4448 */
4449 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004450get_address(
4451 exarg_T *eap UNUSED,
4452 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004453 int addr_type, // flag: one of ADDR_LINES, ...
4454 int skip, // only skip the address, don't use it
4455 int silent, // no errors or side effects
4456 int to_other_file, // flag: may jump to other file
4457 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 int c;
4460 int i;
4461 long n;
4462 char_u *cmd;
4463 pos_T pos;
4464 pos_T *fp;
4465 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004466 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 cmd = skipwhite(*ptr);
4469 lnum = MAXLNUM;
4470 do
4471 {
4472 switch (*cmd)
4473 {
4474 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 ++cmd;
4476 switch (addr_type)
4477 {
4478 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 lnum = curwin->w_cursor.lnum;
4480 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004482 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 break;
4484 case ADDR_ARGUMENTS:
4485 lnum = curwin->w_arg_idx + 1;
4486 break;
4487 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004488 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 lnum = curbuf->b_fnum;
4490 break;
4491 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004492 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004494 case ADDR_TABS_RELATIVE:
4495 EMSG(_(e_invrange));
4496 cmd = NULL;
4497 goto error;
4498 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004499#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004500 case ADDR_QUICKFIX:
4501 lnum = qf_get_cur_valid_idx(eap);
4502 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004503#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 }
4505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 ++cmd;
4509 switch (addr_type)
4510 {
4511 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 lnum = curbuf->b_ml.ml_line_count;
4513 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004515 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 break;
4517 case ADDR_ARGUMENTS:
4518 lnum = ARGCOUNT;
4519 break;
4520 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004521 buf = lastbuf;
4522 while (buf->b_ml.ml_mfp == NULL)
4523 {
4524 if (buf->b_prev == NULL)
4525 break;
4526 buf = buf->b_prev;
4527 }
4528 lnum = buf->b_fnum;
4529 break;
4530 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 lnum = lastbuf->b_fnum;
4532 break;
4533 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004534 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004536 case ADDR_TABS_RELATIVE:
4537 EMSG(_(e_invrange));
4538 cmd = NULL;
4539 goto error;
4540 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004541#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004542 case ADDR_QUICKFIX:
4543 lnum = qf_get_size(eap);
4544 if (lnum == 0)
4545 lnum = 1;
4546 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004547#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004548 }
4549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004552 if (*++cmd == NUL)
4553 {
4554 cmd = NULL;
4555 goto error;
4556 }
4557 if (addr_type != ADDR_LINES)
4558 {
4559 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004560 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004561 goto error;
4562 }
4563 if (skip)
4564 ++cmd;
4565 else
4566 {
4567 /* Only accept a mark in another file when it is
4568 * used by itself: ":'M". */
4569 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4570 ++cmd;
4571 if (fp == (pos_T *)-1)
4572 /* Jumped to another file. */
4573 lnum = curwin->w_cursor.lnum;
4574 else
4575 {
4576 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
4578 cmd = NULL;
4579 goto error;
4580 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004581 lnum = fp->lnum;
4582 }
4583 }
4584 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 case '/':
4587 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004588 c = *cmd++;
4589 if (addr_type != ADDR_LINES)
4590 {
4591 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004592 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 goto error;
4594 }
4595 if (skip) /* skip "/pat/" */
4596 {
4597 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4598 if (*cmd == c)
4599 ++cmd;
4600 }
4601 else
4602 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004603 int flags;
4604
4605 pos = curwin->w_cursor; // save curwin->w_cursor
4606
4607 // When '/' or '?' follows another address, start from
4608 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004609 if (lnum != MAXLNUM)
4610 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004611
4612 // Start a forward search at the end of the line (unless
4613 // before the first line).
4614 // Start a backward search at the start of the line.
4615 // This makes sure we never match in the current
4616 // line, and can match anywhere in the
4617 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004618 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004619 curwin->w_cursor.col = MAXCOL;
4620 else
4621 curwin->w_cursor.col = 0;
4622 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004623 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4624 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 {
4626 curwin->w_cursor = pos;
4627 cmd = NULL;
4628 goto error;
4629 }
4630 lnum = curwin->w_cursor.lnum;
4631 curwin->w_cursor = pos;
4632 /* adjust command string pointer */
4633 cmd += searchcmdlen;
4634 }
4635 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
4637 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 ++cmd;
4639 if (addr_type != ADDR_LINES)
4640 {
4641 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004642 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 goto error;
4644 }
4645 if (*cmd == '&')
4646 i = RE_SUBST;
4647 else if (*cmd == '?' || *cmd == '/')
4648 i = RE_SEARCH;
4649 else
4650 {
4651 EMSG(_(e_backslash));
4652 cmd = NULL;
4653 goto error;
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004656 if (!skip)
4657 {
4658 /*
4659 * When search follows another address, start from
4660 * there.
4661 */
4662 if (lnum != MAXLNUM)
4663 pos.lnum = lnum;
4664 else
4665 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004667 /*
4668 * Start the search just like for the above
4669 * do_search().
4670 */
4671 if (*cmd != '?')
4672 pos.col = MAXCOL;
4673 else
4674 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004675#ifdef FEAT_VIRTUALEDIT
4676 pos.coladd = 0;
4677#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004678 if (searchit(curwin, curbuf, &pos,
4679 *cmd == '?' ? BACKWARD : FORWARD,
4680 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004681 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004682 lnum = pos.lnum;
4683 else
4684 {
4685 cmd = NULL;
4686 goto error;
4687 }
4688 }
4689 ++cmd;
4690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
4692 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004693 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4694 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696
4697 for (;;)
4698 {
4699 cmd = skipwhite(cmd);
4700 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4701 break;
4702
4703 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004704 {
4705 switch (addr_type)
4706 {
4707 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004708 /* "+1" is same as ".+1" */
4709 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004710 break;
4711 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004712 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004713 break;
4714 case ADDR_ARGUMENTS:
4715 lnum = curwin->w_arg_idx + 1;
4716 break;
4717 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004718 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004719 lnum = curbuf->b_fnum;
4720 break;
4721 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004722 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004723 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004724 case ADDR_TABS_RELATIVE:
4725 lnum = 1;
4726 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004727#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004728 case ADDR_QUICKFIX:
4729 lnum = qf_get_cur_valid_idx(eap);
4730 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004731#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004732 }
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (VIM_ISDIGIT(*cmd))
4736 i = '+'; /* "number" is same as "+number" */
4737 else
4738 i = *cmd++;
4739 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4740 n = 1;
4741 else
4742 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004743
4744 if (addr_type == ADDR_TABS_RELATIVE)
4745 {
4746 EMSG(_(e_invrange));
4747 cmd = NULL;
4748 goto error;
4749 }
4750 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004751 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004752 lnum = compute_buffer_local_count(
4753 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004755 {
4756#ifdef FEAT_FOLDING
4757 /* Relative line addressing, need to adjust for folded lines
4758 * now, but only do it after the first address. */
4759 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4760 && address_count >= 2)
4761 (void)hasFolding(lnum, NULL, &lnum);
4762#endif
4763 if (i == '-')
4764 lnum -= n;
4765 else
4766 lnum += n;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769 } while (*cmd == '/' || *cmd == '?');
4770
4771error:
4772 *ptr = cmd;
4773 return lnum;
4774}
4775
4776/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004777 * Get flags from an Ex command argument.
4778 */
4779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004780get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004781{
4782 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4783 {
4784 if (*eap->arg == 'l')
4785 eap->flags |= EXFLAG_LIST;
4786 else if (*eap->arg == 'p')
4787 eap->flags |= EXFLAG_PRINT;
4788 else
4789 eap->flags |= EXFLAG_NR;
4790 eap->arg = skipwhite(eap->arg + 1);
4791 }
4792}
4793
4794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * Function called for command which is Not Implemented. NI!
4796 */
4797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004798ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800 if (!eap->skip)
4801 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4802}
4803
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004804#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805/*
4806 * Function called for script command which is Not Implemented. NI!
4807 * Skips over ":perl <<EOF" constructs.
4808 */
4809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 if (!eap->skip)
4813 ex_ni(eap);
4814 else
4815 vim_free(script_get(eap, eap->arg));
4816}
4817#endif
4818
4819/*
4820 * Check range in Ex command for validity.
4821 * Return NULL when valid, error message when invalid.
4822 */
4823 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004824invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004826 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if ( eap->line1 < 0
4828 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004829 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004831
4832 if (eap->argt & RANGE)
4833 {
4834 switch(eap->addr_type)
4835 {
4836 case ADDR_LINES:
4837 if (!(eap->argt & NOTADR)
4838 && eap->line2 > curbuf->b_ml.ml_line_count
4839#ifdef FEAT_DIFF
4840 + (eap->cmdidx == CMD_diffget)
4841#endif
4842 )
4843 return (char_u *)_(e_invrange);
4844 break;
4845 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004846 /* add 1 if ARGCOUNT is 0 */
4847 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004848 return (char_u *)_(e_invrange);
4849 break;
4850 case ADDR_BUFFERS:
4851 if (eap->line1 < firstbuf->b_fnum
4852 || eap->line2 > lastbuf->b_fnum)
4853 return (char_u *)_(e_invrange);
4854 break;
4855 case ADDR_LOADED_BUFFERS:
4856 buf = firstbuf;
4857 while (buf->b_ml.ml_mfp == NULL)
4858 {
4859 if (buf->b_next == NULL)
4860 return (char_u *)_(e_invrange);
4861 buf = buf->b_next;
4862 }
4863 if (eap->line1 < buf->b_fnum)
4864 return (char_u *)_(e_invrange);
4865 buf = lastbuf;
4866 while (buf->b_ml.ml_mfp == NULL)
4867 {
4868 if (buf->b_prev == NULL)
4869 return (char_u *)_(e_invrange);
4870 buf = buf->b_prev;
4871 }
4872 if (eap->line2 > buf->b_fnum)
4873 return (char_u *)_(e_invrange);
4874 break;
4875 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004876 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004877 return (char_u *)_(e_invrange);
4878 break;
4879 case ADDR_TABS:
4880 if (eap->line2 > LAST_TAB_NR)
4881 return (char_u *)_(e_invrange);
4882 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004883 case ADDR_TABS_RELATIVE:
4884 /* Do nothing */
4885 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004886#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004887 case ADDR_QUICKFIX:
4888 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4889 return (char_u *)_(e_invrange);
4890 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004891#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004892 }
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 return NULL;
4895}
4896
4897/*
4898 * Correct the range for zero line number, if required.
4899 */
4900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004901correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4904 {
4905 if (eap->line1 == 0)
4906 eap->line1 = 1;
4907 if (eap->line2 == 0)
4908 eap->line2 = 1;
4909 }
4910}
4911
Bram Moolenaar748bf032005-02-02 23:04:36 +00004912#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004913static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004914
4915/*
4916 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4917 * pattern. Otherwise return eap->arg.
4918 */
4919 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004920skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004921{
4922 char_u *p = eap->arg;
4923
Bram Moolenaara37420f2006-02-04 22:37:47 +00004924 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4925 || eap->cmdidx == CMD_vimgrepadd
4926 || eap->cmdidx == CMD_lvimgrepadd
4927 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004929 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004930 if (p == NULL)
4931 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004932 }
4933 return p;
4934}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004935
4936/*
4937 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4938 * in the command line, so that things like % get expanded.
4939 */
4940 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004941replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004942{
4943 char_u *new_cmdline;
4944 char_u *program;
4945 char_u *pos;
4946 char_u *ptr;
4947 int len;
4948 int i;
4949
4950 /*
4951 * Don't do it when ":vimgrep" is used for ":grep".
4952 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004953 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4954 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4955 || eap->cmdidx == CMD_grepadd
4956 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004957 && !grep_internal(eap->cmdidx))
4958 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004959 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4960 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004961 {
4962 if (*curbuf->b_p_gp == NUL)
4963 program = p_gp;
4964 else
4965 program = curbuf->b_p_gp;
4966 }
4967 else
4968 {
4969 if (*curbuf->b_p_mp == NUL)
4970 program = p_mp;
4971 else
4972 program = curbuf->b_p_mp;
4973 }
4974
4975 p = skipwhite(p);
4976
4977 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4978 {
4979 /* replace $* by given arguments */
4980 i = 1;
4981 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4982 ++i;
4983 len = (int)STRLEN(p);
4984 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4985 if (new_cmdline == NULL)
4986 return NULL; /* out of memory */
4987 ptr = new_cmdline;
4988 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4989 {
4990 i = (int)(pos - program);
4991 STRNCPY(ptr, program, i);
4992 STRCPY(ptr += i, p);
4993 ptr += len;
4994 program = pos + 2;
4995 }
4996 STRCPY(ptr, program);
4997 }
4998 else
4999 {
5000 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
5001 if (new_cmdline == NULL)
5002 return NULL; /* out of memory */
5003 STRCPY(new_cmdline, program);
5004 STRCAT(new_cmdline, " ");
5005 STRCAT(new_cmdline, p);
5006 }
5007 msg_make(p);
5008
5009 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
5010 vim_free(*cmdlinep);
5011 *cmdlinep = new_cmdline;
5012 p = new_cmdline;
5013 }
5014 return p;
5015}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005016#endif
5017
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018/*
5019 * Expand file name in Ex command argument.
5020 * Return FAIL for failure, OK otherwise.
5021 */
5022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005023expand_filename(
5024 exarg_T *eap,
5025 char_u **cmdlinep,
5026 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028 int has_wildcards; /* need to expand wildcards */
5029 char_u *repl;
5030 int srclen;
5031 char_u *p;
5032 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005033 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar748bf032005-02-02 23:04:36 +00005035#ifdef FEAT_QUICKFIX
5036 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5037 p = skip_grep_pat(eap);
5038#else
5039 p = eap->arg;
5040#endif
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /*
5043 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5044 * the file name contains a wildcard it should not cause expanding.
5045 * (it will be expanded anyway if there is a wildcard before replacing).
5046 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005047 has_wildcards = mch_has_wildcard(p);
5048 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005050#ifdef FEAT_EVAL
5051 /* Skip over `=expr`, wildcards in it are not expanded. */
5052 if (p[0] == '`' && p[1] == '=')
5053 {
5054 p += 2;
5055 (void)skip_expr(&p);
5056 if (*p == '`')
5057 ++p;
5058 continue;
5059 }
5060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 /*
5062 * Quick check if this cannot be the start of a special string.
5063 * Also removes backslash before '%', '#' and '<'.
5064 */
5065 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5066 {
5067 ++p;
5068 continue;
5069 }
5070
5071 /*
5072 * Try to find a match at this position.
5073 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005074 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5075 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (*errormsgp != NULL) /* error detected */
5077 return FAIL;
5078 if (repl == NULL) /* no match found */
5079 {
5080 p += srclen;
5081 continue;
5082 }
5083
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005084 /* Wildcards won't be expanded below, the replacement is taken
5085 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5086 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5087 {
5088 char_u *l = repl;
5089
5090 repl = expand_env_save(repl);
5091 vim_free(l);
5092 }
5093
Bram Moolenaar63b92542007-03-27 14:57:09 +00005094 /* Need to escape white space et al. with a backslash.
5095 * Don't do this for:
5096 * - replacement that already has been escaped: "##"
5097 * - shell commands (may have to use quotes instead).
5098 * - non-unix systems when there is a single argument (spaces don't
5099 * separate arguments then).
5100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005102 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 && eap->cmdidx != CMD_grep
5105 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005106 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005107 && eap->cmdidx != CMD_lgrep
5108 && eap->cmdidx != CMD_lgrepadd
5109 && eap->cmdidx != CMD_lmake
5110 && eap->cmdidx != CMD_make
5111 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112#ifndef UNIX
5113 && !(eap->argt & NOSPC)
5114#endif
5115 )
5116 {
5117 char_u *l;
5118#ifdef BACKSLASH_IN_FILENAME
5119 /* Don't escape a backslash here, because rem_backslash() doesn't
5120 * remove it later. */
5121 static char_u *nobslash = (char_u *)" \t\"|";
5122# define ESCAPE_CHARS nobslash
5123#else
5124# define ESCAPE_CHARS escape_chars
5125#endif
5126
5127 for (l = repl; *l; ++l)
5128 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5129 {
5130 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5131 if (l != NULL)
5132 {
5133 vim_free(repl);
5134 repl = l;
5135 }
5136 break;
5137 }
5138 }
5139
5140 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005141 if ((eap->usefilter || eap->cmdidx == CMD_bang
5142 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005143 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 char_u *l;
5146
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005147 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (l != NULL)
5149 {
5150 vim_free(repl);
5151 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 }
5154
5155 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5156 vim_free(repl);
5157 if (p == NULL)
5158 return FAIL;
5159 }
5160
5161 /*
5162 * One file argument: Expand wildcards.
5163 * Don't do this with ":r !command" or ":w !command".
5164 */
5165 if ((eap->argt & NOSPC) && !eap->usefilter)
5166 {
5167 /*
5168 * May do this twice:
5169 * 1. Replace environment variables.
5170 * 2. Replace any other wildcards, remove backslashes.
5171 */
5172 for (n = 1; n <= 2; ++n)
5173 {
5174 if (n == 2)
5175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 /*
5177 * Halve the number of backslashes (this is Vi compatible).
5178 * For Unix and OS/2, when wildcards are expanded, this is
5179 * done by ExpandOne() below.
5180 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005181#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (!has_wildcards)
5183#endif
5184 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186
5187 if (has_wildcards)
5188 {
5189 if (n == 1)
5190 {
5191 /*
5192 * First loop: May expand environment variables. This
5193 * can be done much faster with expand_env() than with
5194 * something else (e.g., calling a shell).
5195 * After expanding environment variables, check again
5196 * if there are still wildcards present.
5197 */
5198 if (vim_strchr(eap->arg, '$') != NULL
5199 || vim_strchr(eap->arg, '~') != NULL)
5200 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005201 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005202 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 has_wildcards = mch_has_wildcard(NameBuff);
5204 p = NameBuff;
5205 }
5206 else
5207 p = NULL;
5208 }
5209 else /* n == 2 */
5210 {
5211 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005212 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
5214 ExpandInit(&xpc);
5215 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005216 if (p_wic)
5217 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005219 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (p == NULL)
5221 return FAIL;
5222 }
5223 if (p != NULL)
5224 {
5225 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5226 p, cmdlinep);
5227 if (n == 2) /* p came from ExpandOne() */
5228 vim_free(p);
5229 }
5230 }
5231 }
5232 }
5233 return OK;
5234}
5235
5236/*
5237 * Replace part of the command line, keeping eap->cmd, eap->arg and
5238 * eap->nextcmd correct.
5239 * "src" points to the part that is to be replaced, of length "srclen".
5240 * "repl" is the replacement string.
5241 * Returns a pointer to the character after the replaced string.
5242 * Returns NULL for failure.
5243 */
5244 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005245repl_cmdline(
5246 exarg_T *eap,
5247 char_u *src,
5248 int srclen,
5249 char_u *repl,
5250 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 int len;
5253 int i;
5254 char_u *new_cmdline;
5255
5256 /*
5257 * The new command line is build in new_cmdline[].
5258 * First allocate it.
5259 * Careful: a "+cmd" argument may have been NUL terminated.
5260 */
5261 len = (int)STRLEN(repl);
5262 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005263 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5265 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5266 return NULL; /* out of memory! */
5267
5268 /*
5269 * Copy the stuff before the expanded part.
5270 * Copy the expanded stuff.
5271 * Copy what came after the expanded part.
5272 * Copy the next commands, if there are any.
5273 */
5274 i = (int)(src - *cmdlinep); /* length of part before match */
5275 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 mch_memmove(new_cmdline + i, repl, (size_t)len);
5278 i += len; /* remember the end of the string */
5279 STRCPY(new_cmdline + i, src + srclen);
5280 src = new_cmdline + i; /* remember where to continue */
5281
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005282 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
5284 i = (int)STRLEN(new_cmdline) + 1;
5285 STRCPY(new_cmdline + i, eap->nextcmd);
5286 eap->nextcmd = new_cmdline + i;
5287 }
5288 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5289 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5290 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5291 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5292 vim_free(*cmdlinep);
5293 *cmdlinep = new_cmdline;
5294
5295 return src;
5296}
5297
5298/*
5299 * Check for '|' to separate commands and '"' to start comments.
5300 */
5301 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
5304 char_u *p;
5305
Bram Moolenaar86b68352004-12-27 21:59:20 +00005306#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005307 p = skip_grep_pat(eap);
5308#else
5309 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005310#endif
5311
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005312 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 {
5314 if (*p == Ctrl_V)
5315 {
5316 if (eap->argt & (USECTRLV | XFILE))
5317 ++p; /* skip CTRL-V and next char */
5318 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005319 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005320 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (*p == NUL) /* stop at NUL after CTRL-V */
5322 break;
5323 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005324
5325#ifdef FEAT_EVAL
5326 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005327 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005328 {
5329 p += 2;
5330 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005331 }
5332#endif
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 /* Check for '"': start of comment or '|': next command */
5335 /* :@" and :*" do not start a comment!
5336 * :redir @" doesn't either. */
5337 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5338 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5339 || p != eap->arg)
5340 && (eap->cmdidx != CMD_redir
5341 || p != eap->arg + 1 || p[-1] != '@'))
5342 || *p == '|' || *p == '\n')
5343 {
5344 /*
5345 * We remove the '\' before the '|', unless USECTRLV is used
5346 * AND 'b' is present in 'cpoptions'.
5347 */
5348 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5349 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5350 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005351 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 --p;
5353 }
5354 else
5355 {
5356 eap->nextcmd = check_nextcmd(p);
5357 *p = NUL;
5358 break;
5359 }
5360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005362
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5364 del_trailing_spaces(eap->arg);
5365}
5366
5367/*
5368 * get + command from ex argument
5369 */
5370 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
5373 char_u *arg = *argp;
5374 char_u *command = NULL;
5375
5376 if (*arg == '+') /* +[command] */
5377 {
5378 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005379 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 command = dollar_command;
5381 else
5382 {
5383 command = arg;
5384 arg = skip_cmd_arg(command, TRUE);
5385 if (*arg != NUL)
5386 *arg++ = NUL; /* terminate command with NUL */
5387 }
5388
5389 arg = skipwhite(arg); /* skip over spaces */
5390 *argp = arg;
5391 }
5392 return command;
5393}
5394
5395/*
5396 * Find end of "+command" argument. Skip over "\ " and "\\".
5397 */
5398 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005399skip_cmd_arg(
5400 char_u *p,
5401 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 while (*p && !vim_isspace(*p))
5404 {
5405 if (*p == '\\' && p[1] != NUL)
5406 {
5407 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005408 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 else
5410 ++p;
5411 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005412 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
5414 return p;
5415}
5416
Bram Moolenaar86676c92018-04-05 18:56:48 +02005417#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005418 int
5419get_bad_opt(char_u *p, exarg_T *eap)
5420{
5421 if (STRICMP(p, "keep") == 0)
5422 eap->bad_char = BAD_KEEP;
5423 else if (STRICMP(p, "drop") == 0)
5424 eap->bad_char = BAD_DROP;
5425 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5426 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005427 else
5428 return FAIL;
5429 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005430}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005431#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005432
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433/*
5434 * Get "++opt=arg" argument.
5435 * Return FAIL or OK.
5436 */
5437 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005438getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 char_u *arg = eap->arg + 2;
5441 int *pp = NULL;
5442#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005443 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 char_u *p;
5445#endif
5446
5447 /* ":edit ++[no]bin[ary] file" */
5448 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5449 {
5450 if (*arg == 'n')
5451 {
5452 arg += 2;
5453 eap->force_bin = FORCE_NOBIN;
5454 }
5455 else
5456 eap->force_bin = FORCE_BIN;
5457 if (!checkforcmd(&arg, "binary", 3))
5458 return FAIL;
5459 eap->arg = skipwhite(arg);
5460 return OK;
5461 }
5462
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005463 /* ":read ++edit file" */
5464 if (STRNCMP(arg, "edit", 4) == 0)
5465 {
5466 eap->read_edit = TRUE;
5467 eap->arg = skipwhite(arg + 4);
5468 return OK;
5469 }
5470
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 if (STRNCMP(arg, "ff", 2) == 0)
5472 {
5473 arg += 2;
5474 pp = &eap->force_ff;
5475 }
5476 else if (STRNCMP(arg, "fileformat", 10) == 0)
5477 {
5478 arg += 10;
5479 pp = &eap->force_ff;
5480 }
5481#ifdef FEAT_MBYTE
5482 else if (STRNCMP(arg, "enc", 3) == 0)
5483 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005484 if (STRNCMP(arg, "encoding", 8) == 0)
5485 arg += 8;
5486 else
5487 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 pp = &eap->force_enc;
5489 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005490 else if (STRNCMP(arg, "bad", 3) == 0)
5491 {
5492 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005493 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495#endif
5496
5497 if (pp == NULL || *arg != '=')
5498 return FAIL;
5499
5500 ++arg;
5501 *pp = (int)(arg - eap->cmd);
5502 arg = skip_cmd_arg(arg, FALSE);
5503 eap->arg = skipwhite(arg);
5504 *arg = NUL;
5505
5506#ifdef FEAT_MBYTE
5507 if (pp == &eap->force_ff)
5508 {
5509#endif
5510 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5511 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005512 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#ifdef FEAT_MBYTE
5514 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005515 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
5517 /* Make 'fileencoding' lower case. */
5518 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5519 *p = TOLOWER_ASC(*p);
5520 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005521 else
5522 {
5523 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5524 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005525 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005526 return FAIL;
5527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#endif
5529
5530 return OK;
5531}
5532
5533/*
5534 * ":abbreviate" and friends.
5535 */
5536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005537ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 do_exmap(eap, TRUE); /* almost the same as mapping */
5540}
5541
5542/*
5543 * ":map" and friends.
5544 */
5545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 /*
5549 * If we are sourcing .exrc or .vimrc in current directory we
5550 * print the mappings for security reasons.
5551 */
5552 if (secure)
5553 {
5554 secure = 2;
5555 msg_outtrans(eap->cmd);
5556 msg_putchar('\n');
5557 }
5558 do_exmap(eap, FALSE);
5559}
5560
5561/*
5562 * ":unmap" and friends.
5563 */
5564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 do_exmap(eap, FALSE);
5568}
5569
5570/*
5571 * ":mapclear" and friends.
5572 */
5573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005574ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5577}
5578
5579/*
5580 * ":abclear" and friends.
5581 */
5582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005583ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
5585 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5586}
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005592 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 * directory for security reasons.
5594 */
5595 if (secure)
5596 {
5597 secure = 2;
5598 eap->errmsg = e_curdir;
5599 }
5600 else if (eap->cmdidx == CMD_autocmd)
5601 do_autocmd(eap->arg, eap->forceit);
5602 else
5603 do_augroup(eap->arg, eap->forceit);
5604}
5605
5606/*
5607 * ":doautocmd": Apply the automatic commands to the current buffer.
5608 */
5609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005612 char_u *arg = eap->arg;
5613 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005614 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005615
Bram Moolenaar1610d052016-06-09 22:53:01 +02005616 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5617 /* Only when there is no <nomodeline>. */
5618 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005619 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622/*
5623 * :[N]bunload[!] [N] [bufname] unload buffer
5624 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5625 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5626 */
5627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 eap->errmsg = do_bufdel(
5631 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5632 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5633 : DOBUF_UNLOAD, eap->arg,
5634 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5635}
5636
5637/*
5638 * :[N]buffer [N] to buffer N
5639 * :[N]sbuffer [N] to buffer N
5640 */
5641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005642ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
5644 if (*eap->arg)
5645 eap->errmsg = e_trailing;
5646 else
5647 {
5648 if (eap->addr_count == 0) /* default is current buffer */
5649 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5650 else
5651 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005652 if (eap->do_ecmd_cmd != NULL)
5653 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655}
5656
5657/*
5658 * :[N]bmodified [N] to next mod. buffer
5659 * :[N]sbmodified [N] to next mod. buffer
5660 */
5661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005665 if (eap->do_ecmd_cmd != NULL)
5666 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667}
5668
5669/*
5670 * :[N]bnext [N] to next buffer
5671 * :[N]sbnext [N] split and to next buffer
5672 */
5673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005674ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005677 if (eap->do_ecmd_cmd != NULL)
5678 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679}
5680
5681/*
5682 * :[N]bNext [N] to previous buffer
5683 * :[N]bprevious [N] to previous buffer
5684 * :[N]sbNext [N] split and to previous buffer
5685 * :[N]sbprevious [N] split and to previous buffer
5686 */
5687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005691 if (eap->do_ecmd_cmd != NULL)
5692 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693}
5694
5695/*
5696 * :brewind to first buffer
5697 * :bfirst to first buffer
5698 * :sbrewind split and to first buffer
5699 * :sbfirst split and to first buffer
5700 */
5701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005702ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005705 if (eap->do_ecmd_cmd != NULL)
5706 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707}
5708
5709/*
5710 * :blast to last buffer
5711 * :sblast split and to last buffer
5712 */
5713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005714ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
5716 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005717 if (eap->do_ecmd_cmd != NULL)
5718 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 return (c == NUL || c == '|' || c == '"' || c == '\n');
5725}
5726
5727#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5728 || defined(PROTO)
5729/*
5730 * Return the next command, after the first '|' or '\n'.
5731 * Return NULL if not found.
5732 */
5733 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005734find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 while (*p != '|' && *p != '\n')
5737 {
5738 if (*p == NUL)
5739 return NULL;
5740 ++p;
5741 }
5742 return (p + 1);
5743}
5744#endif
5745
5746/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005747 * Check if *p is a separator between Ex commands, skipping over white space.
5748 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 */
5750 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005751check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005753 char_u *s = skipwhite(p);
5754
5755 if (*s == '|' || *s == '\n')
5756 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 else
5758 return NULL;
5759}
5760
5761/*
5762 * - if there are more files to edit
5763 * - and this is the last window
5764 * - and forceit not used
5765 * - and not repeated twice on a row
5766 * return FAIL and give error message if 'message' TRUE
5767 * return OK otherwise
5768 */
5769 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005770check_more(
5771 int message, /* when FALSE check only, no messages */
5772 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5775
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005776 if (!forceit && only_one_window()
5777 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
5779 if (message)
5780 {
5781#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5782 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5783 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005784 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005786 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5787 NGETTEXT("%d more file to edit. Quit anyway?",
5788 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5790 return OK;
5791 return FAIL;
5792 }
5793#endif
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005794 EMSGN(NGETTEXT("E173: %ld more file to edit",
5795 "E173: %ld more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 quitmore = 2; /* next try to quit is allowed */
5797 }
5798 return FAIL;
5799 }
5800 return OK;
5801}
5802
5803#ifdef FEAT_CMDL_COMPL
5804/*
5805 * Function given to ExpandGeneric() to obtain the list of command names.
5806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005808get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 if (idx >= (int)CMD_SIZE)
5811# ifdef FEAT_USR_CMDS
5812 return get_user_command_name(idx);
5813# else
5814 return NULL;
5815# endif
5816 return cmdnames[idx].cmd_name;
5817}
5818#endif
5819
5820#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005821static int uc_add_command(char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int addr_type, int force);
5822static void uc_list(char_u *name, size_t name_len);
5823static int uc_scan_attr(char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg, int* attr_type_arg);
5824static char_u *uc_split_args(char_u *arg, size_t *lenp);
5825static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005828uc_add_command(
5829 char_u *name,
5830 size_t name_len,
5831 char_u *rep,
5832 long argt,
5833 long def,
5834 int flags,
5835 int compl,
5836 char_u *compl_arg,
5837 int addr_type,
5838 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 ucmd_T *cmd = NULL;
5841 char_u *p;
5842 int i;
5843 int cmp = 1;
5844 char_u *rep_buf = NULL;
5845 garray_T *gap;
5846
Bram Moolenaar9c102382006-05-03 21:26:49 +00005847 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (rep_buf == NULL)
5849 {
5850 /* Can't replace termcodes - try using the string as is */
5851 rep_buf = vim_strsave(rep);
5852
5853 /* Give up if out of memory */
5854 if (rep_buf == NULL)
5855 return FAIL;
5856 }
5857
5858 /* get address of growarray: global or in curbuf */
5859 if (flags & UC_BUFFER)
5860 {
5861 gap = &curbuf->b_ucmds;
5862 if (gap->ga_itemsize == 0)
5863 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5864 }
5865 else
5866 gap = &ucmds;
5867
5868 /* Search for the command in the already defined commands. */
5869 for (i = 0; i < gap->ga_len; ++i)
5870 {
5871 size_t len;
5872
5873 cmd = USER_CMD_GA(gap, i);
5874 len = STRLEN(cmd->uc_name);
5875 cmp = STRNCMP(name, cmd->uc_name, name_len);
5876 if (cmp == 0)
5877 {
5878 if (name_len < len)
5879 cmp = -1;
5880 else if (name_len > len)
5881 cmp = 1;
5882 }
5883
5884 if (cmp == 0)
5885 {
5886 if (!force)
5887 {
5888 EMSG(_("E174: Command already exists: add ! to replace it"));
5889 goto fail;
5890 }
5891
Bram Moolenaard23a8232018-02-10 18:45:26 +01005892 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005893#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005894 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 break;
5897 }
5898
5899 /* Stop as soon as we pass the name to add */
5900 if (cmp < 0)
5901 break;
5902 }
5903
5904 /* Extend the array unless we're replacing an existing command */
5905 if (cmp != 0)
5906 {
5907 if (ga_grow(gap, 1) != OK)
5908 goto fail;
5909 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5910 goto fail;
5911
5912 cmd = USER_CMD_GA(gap, i);
5913 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5914
5915 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
5917 cmd->uc_name = p;
5918 }
5919
5920 cmd->uc_rep = rep_buf;
5921 cmd->uc_argt = argt;
5922 cmd->uc_def = def;
5923 cmd->uc_compl = compl;
5924#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005925 cmd->uc_script_ctx = current_sctx;
5926 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927# ifdef FEAT_CMDL_COMPL
5928 cmd->uc_compl_arg = compl_arg;
5929# endif
5930#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005931 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 return OK;
5934
5935fail:
5936 vim_free(rep_buf);
5937#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5938 vim_free(compl_arg);
5939#endif
5940 return FAIL;
5941}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005944#if defined(FEAT_USR_CMDS)
5945static struct
5946{
5947 int expand;
5948 char *name;
5949} addr_type_complete[] =
5950{
5951 {ADDR_ARGUMENTS, "arguments"},
5952 {ADDR_LINES, "lines"},
5953 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5954 {ADDR_TABS, "tabs"},
5955 {ADDR_BUFFERS, "buffers"},
5956 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005957 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005958 {-1, NULL}
5959};
5960#endif
5961
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005962#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963/*
5964 * List of names for completion for ":command" with the EXPAND_ flag.
5965 * Must be alphabetical for completion.
5966 */
5967static struct
5968{
5969 int expand;
5970 char *name;
5971} command_complete[] =
5972{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005973 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005975 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005977 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005979 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005980#if defined(FEAT_CSCOPE)
5981 {EXPAND_CSCOPE, "cscope"},
5982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5984 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005985 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986#endif
5987 {EXPAND_DIRECTORIES, "dir"},
5988 {EXPAND_ENV_VARS, "environment"},
5989 {EXPAND_EVENTS, "event"},
5990 {EXPAND_EXPRESSION, "expression"},
5991 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005992 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005993 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 {EXPAND_FUNCTIONS, "function"},
5995 {EXPAND_HELP, "help"},
5996 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005997#if defined(FEAT_CMDHIST)
5998 {EXPAND_HISTORY, "history"},
5999#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006000#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006001 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02006002 {EXPAND_LOCALES, "locale"},
6003#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02006004 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {EXPAND_MAPPINGS, "mapping"},
6006 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02006007 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02006008 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02006009#if defined(FEAT_PROFILE)
6010 {EXPAND_SYNTIME, "syntime"},
6011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006013 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006014 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006015#if defined(FEAT_SIGNS)
6016 {EXPAND_SIGN, "sign"},
6017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 {EXPAND_TAGS, "tag"},
6019 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006020 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 {EXPAND_USER_VARS, "var"},
6022 {0, NULL}
6023};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006026#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006028uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 int i, j;
6031 int found = FALSE;
6032 ucmd_T *cmd;
6033 int len;
6034 long a;
6035 garray_T *gap;
6036
6037 gap = &curbuf->b_ucmds;
6038 for (;;)
6039 {
6040 for (i = 0; i < gap->ga_len; ++i)
6041 {
6042 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006043 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
Bram Moolenaard29459b2016-08-26 22:29:11 +02006045 /* Skip commands which don't match the requested prefix and
6046 * commands filtered out. */
6047 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6048 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 continue;
6050
6051 /* Put out the title first time */
6052 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006053 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 found = TRUE;
6055 msg_putchar('\n');
6056 if (got_int)
6057 break;
6058
6059 /* Special cases */
6060 msg_putchar(a & BANG ? '!' : ' ');
6061 msg_putchar(a & REGSTR ? '"' : ' ');
6062 msg_putchar(gap != &ucmds ? 'b' : ' ');
6063 msg_putchar(' ');
6064
Bram Moolenaar8820b482017-03-16 17:23:31 +01006065 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 len = (int)STRLEN(cmd->uc_name) + 4;
6067
6068 do {
6069 msg_putchar(' ');
6070 ++len;
6071 } while (len < 16);
6072
6073 len = 0;
6074
6075 /* Arguments */
6076 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6077 {
6078 case 0: IObuff[len++] = '0'; break;
6079 case (EXTRA): IObuff[len++] = '*'; break;
6080 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6081 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6082 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6083 }
6084
6085 do {
6086 IObuff[len++] = ' ';
6087 } while (len < 5);
6088
6089 /* Range */
6090 if (a & (RANGE|COUNT))
6091 {
6092 if (a & COUNT)
6093 {
6094 /* -count=N */
6095 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6096 len += (int)STRLEN(IObuff + len);
6097 }
6098 else if (a & DFLALL)
6099 IObuff[len++] = '%';
6100 else if (cmd->uc_def >= 0)
6101 {
6102 /* -range=N */
6103 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6104 len += (int)STRLEN(IObuff + len);
6105 }
6106 else
6107 IObuff[len++] = '.';
6108 }
6109
6110 do {
6111 IObuff[len++] = ' ';
6112 } while (len < 11);
6113
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006114 /* Address Type */
6115 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6116 if (addr_type_complete[j].expand != ADDR_LINES
6117 && addr_type_complete[j].expand == cmd->uc_addr_type)
6118 {
6119 STRCPY(IObuff + len, addr_type_complete[j].name);
6120 len += (int)STRLEN(IObuff + len);
6121 break;
6122 }
6123
6124 do {
6125 IObuff[len++] = ' ';
6126 } while (len < 21);
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 /* Completion */
6129 for (j = 0; command_complete[j].expand != 0; ++j)
6130 if (command_complete[j].expand == cmd->uc_compl)
6131 {
6132 STRCPY(IObuff + len, command_complete[j].name);
6133 len += (int)STRLEN(IObuff + len);
6134 break;
6135 }
6136
6137 do {
6138 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006139 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 IObuff[len] = '\0';
6142 msg_outtrans(IObuff);
6143
6144 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006145#ifdef FEAT_EVAL
6146 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006147 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 out_flush();
6150 ui_breakcheck();
6151 if (got_int)
6152 break;
6153 }
6154 if (gap == &ucmds || i < gap->ga_len)
6155 break;
6156 gap = &ucmds;
6157 }
6158
6159 if (!found)
6160 MSG(_("No user-defined commands found"));
6161}
6162
6163 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006164uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
6166 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6167 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6168 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6169 0xb9, 0x7f, 0};
6170 int i;
6171
6172 for (i = 0; fcmd[i]; ++i)
6173 IObuff[i] = fcmd[i] - 0x40;
6174 IObuff[i] = 0;
6175 return IObuff;
6176}
6177
6178 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006179uc_scan_attr(
6180 char_u *attr,
6181 size_t len,
6182 long *argt,
6183 long *def,
6184 int *flags,
6185 int *compl,
6186 char_u **compl_arg,
6187 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189 char_u *p;
6190
6191 if (len == 0)
6192 {
6193 EMSG(_("E175: No attribute specified"));
6194 return FAIL;
6195 }
6196
6197 /* First, try the simple attributes (no arguments) */
6198 if (STRNICMP(attr, "bang", len) == 0)
6199 *argt |= BANG;
6200 else if (STRNICMP(attr, "buffer", len) == 0)
6201 *flags |= UC_BUFFER;
6202 else if (STRNICMP(attr, "register", len) == 0)
6203 *argt |= REGSTR;
6204 else if (STRNICMP(attr, "bar", len) == 0)
6205 *argt |= TRLBAR;
6206 else
6207 {
6208 int i;
6209 char_u *val = NULL;
6210 size_t vallen = 0;
6211 size_t attrlen = len;
6212
6213 /* Look for the attribute name - which is the part before any '=' */
6214 for (i = 0; i < (int)len; ++i)
6215 {
6216 if (attr[i] == '=')
6217 {
6218 val = &attr[i + 1];
6219 vallen = len - i - 1;
6220 attrlen = i;
6221 break;
6222 }
6223 }
6224
6225 if (STRNICMP(attr, "nargs", attrlen) == 0)
6226 {
6227 if (vallen == 1)
6228 {
6229 if (*val == '0')
6230 /* Do nothing - this is the default */;
6231 else if (*val == '1')
6232 *argt |= (EXTRA | NOSPC | NEEDARG);
6233 else if (*val == '*')
6234 *argt |= EXTRA;
6235 else if (*val == '?')
6236 *argt |= (EXTRA | NOSPC);
6237 else if (*val == '+')
6238 *argt |= (EXTRA | NEEDARG);
6239 else
6240 goto wrong_nargs;
6241 }
6242 else
6243 {
6244wrong_nargs:
6245 EMSG(_("E176: Invalid number of arguments"));
6246 return FAIL;
6247 }
6248 }
6249 else if (STRNICMP(attr, "range", attrlen) == 0)
6250 {
6251 *argt |= RANGE;
6252 if (vallen == 1 && *val == '%')
6253 *argt |= DFLALL;
6254 else if (val != NULL)
6255 {
6256 p = val;
6257 if (*def >= 0)
6258 {
6259two_count:
6260 EMSG(_("E177: Count cannot be specified twice"));
6261 return FAIL;
6262 }
6263
6264 *def = getdigits(&p);
6265 *argt |= (ZEROR | NOTADR);
6266
6267 if (p != val + vallen || vallen == 0)
6268 {
6269invalid_count:
6270 EMSG(_("E178: Invalid default value for count"));
6271 return FAIL;
6272 }
6273 }
6274 }
6275 else if (STRNICMP(attr, "count", attrlen) == 0)
6276 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006277 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
6279 if (val != NULL)
6280 {
6281 p = val;
6282 if (*def >= 0)
6283 goto two_count;
6284
6285 *def = getdigits(&p);
6286
6287 if (p != val + vallen)
6288 goto invalid_count;
6289 }
6290
6291 if (*def < 0)
6292 *def = 0;
6293 }
6294 else if (STRNICMP(attr, "complete", attrlen) == 0)
6295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 if (val == NULL)
6297 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006298 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 return FAIL;
6300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006302 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6303 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006306 else if (STRNICMP(attr, "addr", attrlen) == 0)
6307 {
6308 *argt |= RANGE;
6309 if (val == NULL)
6310 {
6311 EMSG(_("E179: argument required for -addr"));
6312 return FAIL;
6313 }
6314 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6315 == FAIL)
6316 return FAIL;
6317 if (addr_type_arg != ADDR_LINES)
6318 *argt |= (ZEROR | NOTADR) ;
6319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 else
6321 {
6322 char_u ch = attr[len];
6323 attr[len] = '\0';
6324 EMSG2(_("E181: Invalid attribute: %s"), attr);
6325 attr[len] = ch;
6326 return FAIL;
6327 }
6328 }
6329
6330 return OK;
6331}
6332
Bram Moolenaara850a712009-01-28 14:42:59 +00006333/*
6334 * ":command ..."
6335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006337ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
6339 char_u *name;
6340 char_u *end;
6341 char_u *p;
6342 long argt = 0;
6343 long def = -1;
6344 int flags = 0;
6345 int compl = EXPAND_NOTHING;
6346 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006347 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006349 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
6351 p = eap->arg;
6352
6353 /* Check for attributes */
6354 while (*p == '-')
6355 {
6356 ++p;
6357 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006358 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6359 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 == FAIL)
6361 return;
6362 p = skipwhite(end);
6363 }
6364
6365 /* Get the name (if any) and skip to the following argument */
6366 name = p;
6367 if (ASCII_ISALPHA(*p))
6368 while (ASCII_ISALNUM(*p))
6369 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006370 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
6372 EMSG(_("E182: Invalid command name"));
6373 return;
6374 }
6375 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006376 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 /* If there is nothing after the name, and no attributes were specified,
6379 * we are listing commands
6380 */
6381 p = skipwhite(end);
6382 if (!has_attr && ends_excmd(*p))
6383 {
6384 uc_list(name, end - name);
6385 }
6386 else if (!ASCII_ISUPPER(*name))
6387 {
6388 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6389 return;
6390 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006391 else if ((name_len == 1 && *name == 'X')
6392 || (name_len <= 4
6393 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6394 {
6395 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6396 return;
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 else
6399 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006400 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401}
6402
6403/*
6404 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 * Clear all user commands, global and for current buffer.
6406 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006408ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 uc_clear(&ucmds);
6411 uc_clear(&curbuf->b_ucmds);
6412}
6413
6414/*
6415 * Clear all user commands for "gap".
6416 */
6417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006418uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420 int i;
6421 ucmd_T *cmd;
6422
6423 for (i = 0; i < gap->ga_len; ++i)
6424 {
6425 cmd = USER_CMD_GA(gap, i);
6426 vim_free(cmd->uc_name);
6427 vim_free(cmd->uc_rep);
6428# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6429 vim_free(cmd->uc_compl_arg);
6430# endif
6431 }
6432 ga_clear(gap);
6433}
6434
6435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006436ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
6438 int i = 0;
6439 ucmd_T *cmd = NULL;
6440 int cmp = -1;
6441 garray_T *gap;
6442
6443 gap = &curbuf->b_ucmds;
6444 for (;;)
6445 {
6446 for (i = 0; i < gap->ga_len; ++i)
6447 {
6448 cmd = USER_CMD_GA(gap, i);
6449 cmp = STRCMP(eap->arg, cmd->uc_name);
6450 if (cmp <= 0)
6451 break;
6452 }
6453 if (gap == &ucmds || cmp == 0)
6454 break;
6455 gap = &ucmds;
6456 }
6457
6458 if (cmp != 0)
6459 {
6460 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6461 return;
6462 }
6463
6464 vim_free(cmd->uc_name);
6465 vim_free(cmd->uc_rep);
6466# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6467 vim_free(cmd->uc_compl_arg);
6468# endif
6469
6470 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471
6472 if (i < gap->ga_len)
6473 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6474}
6475
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006476/*
6477 * split and quote args for <f-args>
6478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006480uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 char_u *buf;
6483 char_u *p;
6484 char_u *q;
6485 int len;
6486
6487 /* Precalculate length */
6488 p = arg;
6489 len = 2; /* Initial and final quotes */
6490
6491 while (*p)
6492 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006493 if (p[0] == '\\' && p[1] == '\\')
6494 {
6495 len += 2;
6496 p += 2;
6497 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006498 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
6500 len += 1;
6501 p += 2;
6502 }
6503 else if (*p == '\\' || *p == '"')
6504 {
6505 len += 2;
6506 p += 1;
6507 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006508 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
6510 p = skipwhite(p);
6511 if (*p == NUL)
6512 break;
6513 len += 3; /* "," */
6514 }
6515 else
6516 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006517#ifdef FEAT_MBYTE
6518 int charlen = (*mb_ptr2len)(p);
6519 len += charlen;
6520 p += charlen;
6521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 ++len;
6523 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
6526 }
6527
6528 buf = alloc(len + 1);
6529 if (buf == NULL)
6530 {
6531 *lenp = 0;
6532 return buf;
6533 }
6534
6535 p = arg;
6536 q = buf;
6537 *q++ = '"';
6538 while (*p)
6539 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006540 if (p[0] == '\\' && p[1] == '\\')
6541 {
6542 *q++ = '\\';
6543 *q++ = '\\';
6544 p += 2;
6545 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006546 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
6548 *q++ = p[1];
6549 p += 2;
6550 }
6551 else if (*p == '\\' || *p == '"')
6552 {
6553 *q++ = '\\';
6554 *q++ = *p++;
6555 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006556 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
6558 p = skipwhite(p);
6559 if (*p == NUL)
6560 break;
6561 *q++ = '"';
6562 *q++ = ',';
6563 *q++ = '"';
6564 }
6565 else
6566 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006567 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
6569 }
6570 *q++ = '"';
6571 *q = 0;
6572
6573 *lenp = len;
6574 return buf;
6575}
6576
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006577 static size_t
6578add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6579{
6580 size_t result;
6581
6582 result = STRLEN(mod_str);
6583 if (*multi_mods)
6584 result += 1;
6585 if (buf != NULL)
6586 {
6587 if (*multi_mods)
6588 STRCAT(buf, " ");
6589 STRCAT(buf, mod_str);
6590 }
6591
6592 *multi_mods = 1;
6593
6594 return result;
6595}
6596
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597/*
6598 * Check for a <> code in a user command.
6599 * "code" points to the '<'. "len" the length of the <> (inclusive).
6600 * "buf" is where the result is to be added.
6601 * "split_buf" points to a buffer used for splitting, caller should free it.
6602 * "split_len" is the length of what "split_buf" contains.
6603 * Returns the length of the replacement, which has been added to "buf".
6604 * Returns -1 if there was no match, and only the "<" has been copied.
6605 */
6606 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006607uc_check_code(
6608 char_u *code,
6609 size_t len,
6610 char_u *buf,
6611 ucmd_T *cmd, /* the user command we're expanding */
6612 exarg_T *eap, /* ex arguments */
6613 char_u **split_buf,
6614 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 size_t result = 0;
6617 char_u *p = code + 1;
6618 size_t l = len - 2;
6619 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006620 enum {
6621 ct_ARGS,
6622 ct_BANG,
6623 ct_COUNT,
6624 ct_LINE1,
6625 ct_LINE2,
6626 ct_RANGE,
6627 ct_MODS,
6628 ct_REGISTER,
6629 ct_LT,
6630 ct_NONE
6631 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
6633 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6634 {
6635 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6636 p += 2;
6637 l -= 2;
6638 }
6639
Bram Moolenaar371d5402006-03-20 21:47:49 +00006640 ++l;
6641 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006643 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006645 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006647 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006649 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006651 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006653 else if (STRNICMP(p, "range>", l) == 0)
6654 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006655 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006657 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006659 else if (STRNICMP(p, "mods>", l) == 0)
6660 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 switch (type)
6663 {
6664 case ct_ARGS:
6665 /* Simple case first */
6666 if (*eap->arg == NUL)
6667 {
6668 if (quote == 1)
6669 {
6670 result = 2;
6671 if (buf != NULL)
6672 STRCPY(buf, "''");
6673 }
6674 else
6675 result = 0;
6676 break;
6677 }
6678
6679 /* When specified there is a single argument don't split it.
6680 * Works for ":Cmd %" when % is "a b c". */
6681 if ((eap->argt & NOSPC) && quote == 2)
6682 quote = 1;
6683
6684 switch (quote)
6685 {
6686 case 0: /* No quoting, no splitting */
6687 result = STRLEN(eap->arg);
6688 if (buf != NULL)
6689 STRCPY(buf, eap->arg);
6690 break;
6691 case 1: /* Quote, but don't split */
6692 result = STRLEN(eap->arg) + 2;
6693 for (p = eap->arg; *p; ++p)
6694 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006695#ifdef FEAT_MBYTE
6696 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6697 /* DBCS can contain \ in a trail byte, skip the
6698 * double-byte character. */
6699 ++p;
6700 else
6701#endif
6702 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 ++result;
6704 }
6705
6706 if (buf != NULL)
6707 {
6708 *buf++ = '"';
6709 for (p = eap->arg; *p; ++p)
6710 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006711#ifdef FEAT_MBYTE
6712 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6713 /* DBCS can contain \ in a trail byte, copy the
6714 * double-byte character to avoid escaping. */
6715 *buf++ = *p++;
6716 else
6717#endif
6718 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 *buf++ = '\\';
6720 *buf++ = *p;
6721 }
6722 *buf = '"';
6723 }
6724
6725 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006726 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 /* This is hard, so only do it once, and cache the result */
6728 if (*split_buf == NULL)
6729 *split_buf = uc_split_args(eap->arg, split_len);
6730
6731 result = *split_len;
6732 if (buf != NULL && result != 0)
6733 STRCPY(buf, *split_buf);
6734
6735 break;
6736 }
6737 break;
6738
6739 case ct_BANG:
6740 result = eap->forceit ? 1 : 0;
6741 if (quote)
6742 result += 2;
6743 if (buf != NULL)
6744 {
6745 if (quote)
6746 *buf++ = '"';
6747 if (eap->forceit)
6748 *buf++ = '!';
6749 if (quote)
6750 *buf = '"';
6751 }
6752 break;
6753
6754 case ct_LINE1:
6755 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006756 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 case ct_COUNT:
6758 {
6759 char num_buf[20];
6760 long num = (type == ct_LINE1) ? eap->line1 :
6761 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006762 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6764 size_t num_len;
6765
6766 sprintf(num_buf, "%ld", num);
6767 num_len = STRLEN(num_buf);
6768 result = num_len;
6769
6770 if (quote)
6771 result += 2;
6772
6773 if (buf != NULL)
6774 {
6775 if (quote)
6776 *buf++ = '"';
6777 STRCPY(buf, num_buf);
6778 buf += num_len;
6779 if (quote)
6780 *buf = '"';
6781 }
6782
6783 break;
6784 }
6785
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006786 case ct_MODS:
6787 {
6788 int multi_mods = 0;
6789 typedef struct {
6790 int *varp;
6791 char *name;
6792 } mod_entry_T;
6793 static mod_entry_T mod_entries[] = {
6794#ifdef FEAT_BROWSE_CMD
6795 {&cmdmod.browse, "browse"},
6796#endif
6797#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6798 {&cmdmod.confirm, "confirm"},
6799#endif
6800 {&cmdmod.hide, "hide"},
6801 {&cmdmod.keepalt, "keepalt"},
6802 {&cmdmod.keepjumps, "keepjumps"},
6803 {&cmdmod.keepmarks, "keepmarks"},
6804 {&cmdmod.keeppatterns, "keeppatterns"},
6805 {&cmdmod.lockmarks, "lockmarks"},
6806 {&cmdmod.noswapfile, "noswapfile"},
6807 {NULL, NULL}
6808 };
6809 int i;
6810
6811 result = quote ? 2 : 0;
6812 if (buf != NULL)
6813 {
6814 if (quote)
6815 *buf++ = '"';
6816 *buf = '\0';
6817 }
6818
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006819 /* :aboveleft and :leftabove */
6820 if (cmdmod.split & WSP_ABOVE)
6821 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6822 /* :belowright and :rightbelow */
6823 if (cmdmod.split & WSP_BELOW)
6824 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6825 /* :botright */
6826 if (cmdmod.split & WSP_BOT)
6827 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006828
6829 /* the modifiers that are simple flags */
6830 for (i = 0; mod_entries[i].varp != NULL; ++i)
6831 if (*mod_entries[i].varp)
6832 result += add_cmd_modifier(buf, mod_entries[i].name,
6833 &multi_mods);
6834
6835 /* TODO: How to support :noautocmd? */
6836#ifdef HAVE_SANDBOX
6837 /* TODO: How to support :sandbox?*/
6838#endif
6839 /* :silent */
6840 if (msg_silent > 0)
6841 result += add_cmd_modifier(buf,
6842 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006843 /* :tab */
6844 if (cmdmod.tab > 0)
6845 result += add_cmd_modifier(buf, "tab", &multi_mods);
6846 /* :topleft */
6847 if (cmdmod.split & WSP_TOP)
6848 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006849 /* TODO: How to support :unsilent?*/
6850 /* :verbose */
6851 if (p_verbose > 0)
6852 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006853 /* :vertical */
6854 if (cmdmod.split & WSP_VERT)
6855 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006856 if (quote && buf != NULL)
6857 {
6858 buf += result - 2;
6859 *buf = '"';
6860 }
6861 break;
6862 }
6863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 case ct_REGISTER:
6865 result = eap->regname ? 1 : 0;
6866 if (quote)
6867 result += 2;
6868 if (buf != NULL)
6869 {
6870 if (quote)
6871 *buf++ = '\'';
6872 if (eap->regname)
6873 *buf++ = eap->regname;
6874 if (quote)
6875 *buf = '\'';
6876 }
6877 break;
6878
6879 case ct_LT:
6880 result = 1;
6881 if (buf != NULL)
6882 *buf = '<';
6883 break;
6884
6885 default:
6886 /* Not recognized: just copy the '<' and return -1. */
6887 result = (size_t)-1;
6888 if (buf != NULL)
6889 *buf = '<';
6890 break;
6891 }
6892
6893 return result;
6894}
6895
6896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006897do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898{
6899 char_u *buf;
6900 char_u *p;
6901 char_u *q;
6902
6903 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006904 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006905 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 size_t len, totlen;
6907
6908 size_t split_len = 0;
6909 char_u *split_buf = NULL;
6910 ucmd_T *cmd;
6911#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006912 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913#endif
6914
6915 if (eap->cmdidx == CMD_USER)
6916 cmd = USER_CMD(eap->useridx);
6917 else
6918 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6919
6920 /*
6921 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006922 * First round: "buf" is NULL, compute length, allocate "buf".
6923 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 */
6925 buf = NULL;
6926 for (;;)
6927 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006928 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006929 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006931
6932 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006934 start = vim_strchr(p, '<');
6935 if (start != NULL)
6936 end = vim_strchr(start + 1, '>');
6937 if (buf != NULL)
6938 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006939 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6940 ;
6941 if (*ksp == K_SPECIAL
6942 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006943 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6944# ifdef FEAT_GUI
6945 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6946# endif
6947 ))
6948 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006949 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006950 * KS_SPECIAL KE_FILLER, like for mappings, but
6951 * do_cmdline() doesn't handle that, so convert it back.
6952 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6953 len = ksp - p;
6954 if (len > 0)
6955 {
6956 mch_memmove(q, p, len);
6957 q += len;
6958 }
6959 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6960 p = ksp + 3;
6961 continue;
6962 }
6963 }
6964
6965 /* break if there no <item> is found */
6966 if (start == NULL || end == NULL)
6967 break;
6968
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 /* Include the '>' */
6970 ++end;
6971
6972 /* Take everything up to the '<' */
6973 len = start - p;
6974 if (buf == NULL)
6975 totlen += len;
6976 else
6977 {
6978 mch_memmove(q, p, len);
6979 q += len;
6980 }
6981
6982 len = uc_check_code(start, end - start, q, cmd, eap,
6983 &split_buf, &split_len);
6984 if (len == (size_t)-1)
6985 {
6986 /* no match, continue after '<' */
6987 p = start + 1;
6988 len = 1;
6989 }
6990 else
6991 p = end;
6992 if (buf == NULL)
6993 totlen += len;
6994 else
6995 q += len;
6996 }
6997 if (buf != NULL) /* second time here, finished */
6998 {
6999 STRCPY(q, p);
7000 break;
7001 }
7002
7003 totlen += STRLEN(p); /* Add on the trailing characters */
7004 buf = alloc((unsigned)(totlen + 1));
7005 if (buf == NULL)
7006 {
7007 vim_free(split_buf);
7008 return;
7009 }
7010 }
7011
7012#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007013 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014#endif
7015 (void)do_cmdline(buf, eap->getline, eap->cookie,
7016 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7017#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007018 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019#endif
7020 vim_free(buf);
7021 vim_free(split_buf);
7022}
7023
7024# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7025 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007026get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7029}
7030
7031/*
7032 * Function given to ExpandGeneric() to obtain the list of user command names.
7033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007035get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 if (idx < curbuf->b_ucmds.ga_len)
7038 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7039 idx -= curbuf->b_ucmds.ga_len;
7040 if (idx < ucmds.ga_len)
7041 return USER_CMD(idx)->uc_name;
7042 return NULL;
7043}
7044
7045/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007046 * Function given to ExpandGeneric() to obtain the list of user address type names.
7047 */
7048 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007049get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007050{
7051 return (char_u *)addr_type_complete[idx].name;
7052}
7053
7054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 * Function given to ExpandGeneric() to obtain the list of user command
7056 * attributes.
7057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007062 {"addr", "bang", "bar", "buffer", "complete",
7063 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007065 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 return NULL;
7067 return (char_u *)user_cmd_flags[idx];
7068}
7069
7070/*
7071 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007074get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7077
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007078 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 return NULL;
7080 return (char_u *)user_cmd_nargs[idx];
7081}
7082
7083/*
7084 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007087get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
7089 return (char_u *)command_complete[idx].name;
7090}
7091# endif /* FEAT_CMDL_COMPL */
7092
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007093/*
7094 * Parse address type argument
7095 */
7096 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007097parse_addr_type_arg(
7098 char_u *value,
7099 int vallen,
7100 long *argt,
7101 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007102{
7103 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007104
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007105 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7106 {
7107 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7108 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7109 if (a && b)
7110 {
7111 *addr_type_arg = addr_type_complete[i].expand;
7112 break;
7113 }
7114 }
7115
7116 if (addr_type_complete[i].expand == -1)
7117 {
7118 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007119
Bram Moolenaar1c465442017-03-12 20:10:05 +01007120 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007121 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007122 err[i] = NUL;
7123 EMSG2(_("E180: Invalid address type value: %s"), err);
7124 return FAIL;
7125 }
7126
7127 if (*addr_type_arg != ADDR_LINES)
7128 *argt |= NOTADR;
7129
7130 return OK;
7131}
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133#endif /* FEAT_USR_CMDS */
7134
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007135#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7136/*
7137 * Parse a completion argument "value[vallen]".
7138 * The detected completion goes in "*complp", argument type in "*argt".
7139 * When there is an argument, for function and user defined completion, it's
7140 * copied to allocated memory and stored in "*compl_arg".
7141 * Returns FAIL if something is wrong.
7142 */
7143 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007144parse_compl_arg(
7145 char_u *value,
7146 int vallen,
7147 int *complp,
7148 long *argt,
7149 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007150{
7151 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007152# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007153 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007154# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007155 int i;
7156 int valend = vallen;
7157
7158 /* Look for any argument part - which is the part after any ',' */
7159 for (i = 0; i < vallen; ++i)
7160 {
7161 if (value[i] == ',')
7162 {
7163 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007164# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007165 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007166# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007167 valend = i;
7168 break;
7169 }
7170 }
7171
7172 for (i = 0; command_complete[i].expand != 0; ++i)
7173 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007174 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007175 && STRNCMP(value, command_complete[i].name, valend) == 0)
7176 {
7177 *complp = command_complete[i].expand;
7178 if (command_complete[i].expand == EXPAND_BUFFERS)
7179 *argt |= BUFNAME;
7180 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7181 || command_complete[i].expand == EXPAND_FILES)
7182 *argt |= XFILE;
7183 break;
7184 }
7185 }
7186
7187 if (command_complete[i].expand == 0)
7188 {
7189 EMSG2(_("E180: Invalid complete value: %s"), value);
7190 return FAIL;
7191 }
7192
7193# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7194 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7195 && arg != NULL)
7196# else
7197 if (arg != NULL)
7198# endif
7199 {
7200 EMSG(_("E468: Completion argument only allowed for custom completion"));
7201 return FAIL;
7202 }
7203
7204# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7205 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7206 && arg == NULL)
7207 {
7208 EMSG(_("E467: Custom completion requires a function argument"));
7209 return FAIL;
7210 }
7211
7212 if (arg != NULL)
7213 *compl_arg = vim_strnsave(arg, (int)arglen);
7214# endif
7215 return OK;
7216}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007217
7218 int
7219cmdcomplete_str_to_type(char_u *complete_str)
7220{
7221 int i;
7222
7223 for (i = 0; command_complete[i].expand != 0; ++i)
7224 if (STRCMP(complete_str, command_complete[i].name) == 0)
7225 return command_complete[i].expand;
7226
7227 return EXPAND_NOTHING;
7228}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007229#endif
7230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007232ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
Bram Moolenaare6850792010-05-14 15:28:44 +02007234 if (*eap->arg == NUL)
7235 {
7236#ifdef FEAT_EVAL
7237 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7238 char_u *p = NULL;
7239
7240 if (expr != NULL)
7241 {
7242 ++emsg_off;
7243 p = eval_to_string(expr, NULL, FALSE);
7244 --emsg_off;
7245 vim_free(expr);
7246 }
7247 if (p != NULL)
7248 {
7249 MSG(p);
7250 vim_free(p);
7251 }
7252 else
7253 MSG("default");
7254#else
7255 MSG(_("unknown"));
7256#endif
7257 }
7258 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007259 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260}
7261
7262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007263ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
7265 if (*eap->arg == NUL && eap->cmd[2] == '!')
7266 MSG(_("Greetings, Vim user!"));
7267 do_highlight(eap->arg, eap->forceit, FALSE);
7268}
7269
7270
7271/*
7272 * Call this function if we thought we were going to exit, but we won't
7273 * (because of an error). May need to restore the terminal mode.
7274 */
7275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007276not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
7278 exiting = FALSE;
7279 settmode(TMODE_RAW);
7280}
7281
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007282 static int
7283before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7284{
7285 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7286
7287 /* Bail out when autocommands closed the window.
7288 * Refuse to quit when the buffer in the last window is being closed (can
7289 * only happen in autocommands). */
7290 if (!win_valid(wp)
7291 || curbuf_locked()
7292 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7293 return TRUE;
7294
7295 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7296 {
7297 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7298 /* Refuse to quit when locked or when the buffer in the last window is
7299 * being closed (can only happen in autocommands). */
7300 if (curbuf_locked()
7301 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7302 return TRUE;
7303 }
7304
7305 return FALSE;
7306}
7307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007309 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007310 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 */
7312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007313ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007315 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317#ifdef FEAT_CMDWIN
7318 if (cmdwin_type != 0)
7319 {
7320 cmdwin_result = Ctrl_C;
7321 return;
7322 }
7323#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007324 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007325 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007326 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007327 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007328 return;
7329 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007330 if (eap->addr_count > 0)
7331 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007332 int wnr = eap->line2;
7333
7334 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7335 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007336 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007337 }
7338 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007339 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007340
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007341 /* Refuse to quit when locked. */
7342 if (curbuf_locked())
7343 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007344
7345 /* Trigger QuitPre and maybe ExitPre */
7346 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007347 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
7349#ifdef FEAT_NETBEANS_INTG
7350 netbeansForcedQuit = eap->forceit;
7351#endif
7352
7353 /*
7354 * If there are more files or windows we won't exit.
7355 */
7356 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7357 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007358 if ((!buf_hide(wp->w_buffer)
7359 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007360 | (eap->forceit ? CCGD_FORCEIT : 0)
7361 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007363 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
7365 not_exiting();
7366 }
7367 else
7368 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007369 /* quit last window
7370 * Note: only_one_window() returns true, even so a help window is
7371 * still open. In that case only quit, if no address has been
7372 * specified. Example:
7373 * :h|wincmd w|1q - don't quit
7374 * :h|wincmd w|q - quit
7375 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007376 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007378 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007379#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007383 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 }
7385}
7386
7387/*
7388 * ":cquit".
7389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007391ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
7393 getout(1); /* this does not always pass on the exit code to the Manx
7394 compiler. why? */
7395}
7396
7397/*
7398 * ":qall": try to quit all windows
7399 */
7400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007401ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403# ifdef FEAT_CMDWIN
7404 if (cmdwin_type != 0)
7405 {
7406 if (eap->forceit)
7407 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7408 else
7409 cmdwin_result = K_XF2;
7410 return;
7411 }
7412# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007413
7414 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007415 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007416 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007417 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007418 return;
7419 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007420
7421 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007422 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007425 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 getout(0);
7427 not_exiting();
7428}
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430/*
7431 * ":close": close current window, unless it is the last one
7432 */
7433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007434ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007436 win_T *win;
7437 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007438#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007440 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007442#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007443 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007444 {
7445 if (eap->addr_count == 0)
7446 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007447 else
7448 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007449 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007450 {
7451 winnr++;
7452 if (winnr == eap->line2)
7453 break;
7454 }
7455 if (win == NULL)
7456 win = lastwin;
7457 ex_win_close(eap->forceit, win, NULL);
7458 }
7459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460}
7461
Bram Moolenaar4033c552017-09-16 20:54:51 +02007462#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463/*
7464 * ":pclose": Close any preview window.
7465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007467ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007468{
7469 win_T *win;
7470
Bram Moolenaar29323592016-07-24 22:04:11 +02007471 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007472 if (win->w_p_pvw)
7473 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007474 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007475 break;
7476 }
7477}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007478#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007479
Bram Moolenaarf740b292006-02-16 22:11:02 +00007480/*
7481 * Close window "win" and take care of handling closing the last window for a
7482 * modified buffer.
7483 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007485ex_win_close(
7486 int forceit,
7487 win_T *win,
7488 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 int need_hide;
7491 buf_T *buf = win->w_buffer;
7492
7493 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007494 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007496#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 if ((p_confirm || cmdmod.confirm) && p_write)
7498 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007499 bufref_T bufref;
7500
7501 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007503 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 return;
7505 need_hide = FALSE;
7506 }
7507 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007510 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 return;
7512 }
7513 }
7514
Bram Moolenaar4033c552017-09-16 20:54:51 +02007515#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007517#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007520 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007521 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007522 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007523 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524}
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007527 * Handle the argument for a tabpage related ex command.
7528 * Returns a tabpage number.
7529 * When an error is encountered then eap->errmsg is set.
7530 */
7531 static int
7532get_tabpage_arg(exarg_T *eap)
7533{
7534 int tab_number;
7535 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7536
7537 if (eap->arg && *eap->arg != NUL)
7538 {
7539 char_u *p = eap->arg;
7540 char_u *p_save;
7541 int relative = 0; /* argument +N/-N means: go to N places to the
7542 * right/left relative to the current position. */
7543
7544 if (*p == '-')
7545 {
7546 relative = -1;
7547 p++;
7548 }
7549 else if (*p == '+')
7550 {
7551 relative = 1;
7552 p++;
7553 }
7554
7555 p_save = p;
7556 tab_number = getdigits(&p);
7557
7558 if (relative == 0)
7559 {
7560 if (STRCMP(p, "$") == 0)
7561 tab_number = LAST_TAB_NR;
7562 else if (p == p_save || *p_save == '-' || *p != NUL
7563 || tab_number > LAST_TAB_NR)
7564 {
7565 /* No numbers as argument. */
7566 eap->errmsg = e_invarg;
7567 goto theend;
7568 }
7569 }
7570 else
7571 {
7572 if (*p_save == NUL)
7573 tab_number = 1;
7574 else if (p == p_save || *p_save == '-' || *p != NUL
7575 || tab_number == 0)
7576 {
7577 /* No numbers as argument. */
7578 eap->errmsg = e_invarg;
7579 goto theend;
7580 }
7581 tab_number = tab_number * relative + tabpage_index(curtab);
7582 if (!unaccept_arg0 && relative == -1)
7583 --tab_number;
7584 }
7585 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7586 eap->errmsg = e_invarg;
7587 }
7588 else if (eap->addr_count > 0)
7589 {
7590 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007591 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007592 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007593 tab_number = 0;
7594 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007595 else
7596 {
7597 tab_number = eap->line2;
7598 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7599 {
7600 --tab_number;
7601 if (tab_number < unaccept_arg0)
7602 eap->errmsg = e_invarg;
7603 }
7604 }
7605 }
7606 else
7607 {
7608 switch (eap->cmdidx)
7609 {
7610 case CMD_tabnext:
7611 tab_number = tabpage_index(curtab) + 1;
7612 if (tab_number > LAST_TAB_NR)
7613 tab_number = 1;
7614 break;
7615 case CMD_tabmove:
7616 tab_number = LAST_TAB_NR;
7617 break;
7618 default:
7619 tab_number = tabpage_index(curtab);
7620 }
7621 }
7622
7623theend:
7624 return tab_number;
7625}
7626
7627/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007628 * ":tabclose": close current tab page, unless it is the last one.
7629 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 */
7631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007632ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007634 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007635 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007636
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007637# ifdef FEAT_CMDWIN
7638 if (cmdwin_type != 0)
7639 cmdwin_result = K_IGNORE;
7640 else
7641# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007642 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007643 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007646 tab_number = get_tabpage_arg(eap);
7647 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007648 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007649 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007650 if (tp == NULL)
7651 {
7652 beep_flush();
7653 return;
7654 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007655 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007656 {
7657 tabpage_close_other(tp, eap->forceit);
7658 return;
7659 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007660 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007661 tabpage_close(eap->forceit);
7662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007664}
7665
7666/*
7667 * ":tabonly": close all tab pages except the current one
7668 */
7669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007671{
7672 tabpage_T *tp;
7673 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007674 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007675
7676# ifdef FEAT_CMDWIN
7677 if (cmdwin_type != 0)
7678 cmdwin_result = K_IGNORE;
7679 else
7680# endif
7681 if (first_tabpage->tp_next == NULL)
7682 MSG(_("Already only one tab page"));
7683 else
7684 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007685 tab_number = get_tabpage_arg(eap);
7686 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007687 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007688 goto_tabpage(tab_number);
7689 /* Repeat this up to a 1000 times, because autocommands may
7690 * mess up the lists. */
7691 for (done = 0; done < 1000; ++done)
7692 {
7693 FOR_ALL_TABPAGES(tp)
7694 if (tp->tp_topframe != topframe)
7695 {
7696 tabpage_close_other(tp, eap->forceit);
7697 /* if we failed to close it quit */
7698 if (valid_tabpage(tp))
7699 done = 1000;
7700 /* start over, "tp" is now invalid */
7701 break;
7702 }
7703 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007704 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007705 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007706 }
7707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709
7710/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007711 * Close the current tab page.
7712 */
7713 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007714tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007715{
7716 /* First close all the windows but the current one. If that worked then
7717 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007718 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007719 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007720 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007721 ex_win_close(forceit, curwin, NULL);
7722# ifdef FEAT_GUI
7723 need_mouse_correct = TRUE;
7724# endif
7725}
7726
7727/*
7728 * Close tab page "tp", which is not the current tab page.
7729 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007730 * Also takes care of the tab pages line disappearing when closing the
7731 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007732 */
7733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007735{
7736 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007737 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007738 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007739
7740 /* Limit to 1000 windows, autocommands may add a window while we close
7741 * one. OK, so I'm paranoid... */
7742 while (++done < 1000)
7743 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007744 wp = tp->tp_firstwin;
7745 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007746
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007747 /* Autocommands may delete the tab page under our fingers and we may
7748 * fail to close a window with a modified buffer. */
7749 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007750 break;
7751 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007752
Bram Moolenaar29323592016-07-24 22:04:11 +02007753 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007754
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007755 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007756 if (h != tabline_height())
7757 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007758}
7759
7760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 * ":only".
7762 */
7763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007764ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007766 win_T *wp;
7767 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768# ifdef FEAT_GUI
7769 need_mouse_correct = TRUE;
7770# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007771 if (eap->addr_count > 0)
7772 {
7773 wnr = eap->line2;
7774 for (wp = firstwin; --wnr > 0; )
7775 {
7776 if (wp->w_next == NULL)
7777 break;
7778 else
7779 wp = wp->w_next;
7780 }
7781 win_goto(wp);
7782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 close_others(TRUE, eap->forceit);
7784}
7785
7786/*
7787 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007788 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007790 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 if (eap->addr_count == 0)
7794 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007795 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
7798 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007799ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007801 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007802 if (!eap->skip)
7803 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007804#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007805 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007806#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007807 if (eap->addr_count == 0)
7808 win_close(curwin, FALSE); /* don't free buffer */
7809 else
7810 {
7811 int winnr = 0;
7812 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007813
Bram Moolenaar2256c992016-11-15 21:17:07 +01007814 FOR_ALL_WINDOWS(win)
7815 {
7816 winnr++;
7817 if (winnr == eap->line2)
7818 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007819 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007820 if (win == NULL)
7821 win = lastwin;
7822 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825}
7826
7827/*
7828 * ":stop" and ":suspend": Suspend Vim.
7829 */
7830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007831ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
7833 /*
7834 * Disallow suspending for "rvim".
7835 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007836 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 {
7838 if (!eap->forceit)
7839 autowrite_all();
7840 windgoto((int)Rows - 1, 0);
7841 out_char('\n');
7842 out_flush();
7843 stoptermcap();
7844 out_flush(); /* needed for SUN to restore xterm buffer */
7845#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007846 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847#endif
7848 ui_suspend(); /* call machine specific function */
7849#ifdef FEAT_TITLE
7850 maketitle();
7851 resettitle(); /* force updating the title */
7852#endif
7853 starttermcap();
7854 scroll_start(); /* scroll screen before redrawing */
7855 redraw_later_clear();
7856 shell_resized(); /* may have resized window */
7857 }
7858}
7859
7860/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007861 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 */
7863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007864ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866#ifdef FEAT_CMDWIN
7867 if (cmdwin_type != 0)
7868 {
7869 cmdwin_result = Ctrl_C;
7870 return;
7871 }
7872#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007873 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007874 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007875 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007876 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007877 return;
7878 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007879
7880 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007881 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882
7883 /*
7884 * if more files or windows we won't exit
7885 */
7886 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7887 exiting = TRUE;
7888 if ( ((eap->cmdidx == CMD_wq
7889 || curbufIsChanged())
7890 && do_write(eap) == FAIL)
7891 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007892 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {
7894 not_exiting();
7895 }
7896 else
7897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007900 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901# ifdef FEAT_GUI
7902 need_mouse_correct = TRUE;
7903# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007904 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007905 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 }
7907}
7908
7909/*
7910 * ":print", ":list", ":number".
7911 */
7912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007913ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007915 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7916 EMSG(_(e_emptybuf));
7917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007919 for ( ;!got_int; ui_breakcheck())
7920 {
7921 print_line(eap->line1,
7922 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7923 || (eap->flags & EXFLAG_NR)),
7924 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7925 if (++eap->line1 > eap->line2)
7926 break;
7927 out_flush(); /* show one line at a time */
7928 }
7929 setpcmark();
7930 /* put cursor at last line */
7931 curwin->w_cursor.lnum = eap->line2;
7932 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
7934
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936}
7937
7938#ifdef FEAT_BYTEOFF
7939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007940ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
7942 goto_byte(eap->line2);
7943}
7944#endif
7945
7946/*
7947 * ":shell".
7948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 do_shell(NULL, 0);
7953}
7954
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007955#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007957static int drop_busy = FALSE;
7958static int drop_filec;
7959static char_u **drop_filev = NULL;
7960static int drop_split;
7961static void (*drop_callback)(void *);
7962static void *drop_cookie;
7963
7964 static void
7965handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 exarg_T ea;
7968 int save_msg_scroll = msg_scroll;
7969
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007970 // Setting the argument list may cause screen updates and being called
7971 // recursively. Avoid that by setting drop_busy.
7972 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
7974 /* Check whether the current buffer is changed. If so, we will need
7975 * to split the current window or data could be lost.
7976 * We don't need to check if the 'hidden' option is set, as in this
7977 * case the buffer won't be lost.
7978 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007979 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {
7981 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007982 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 --emsg_off;
7984 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007985 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (win_split(0, 0) == FAIL)
7988 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007989 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990
7991 /* When splitting the window, create a new alist. Otherwise the
7992 * existing one is overwritten. */
7993 alist_unlink(curwin->w_alist);
7994 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 }
7996
7997 /*
7998 * Set up the new argument list.
7999 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008000 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001
8002 /*
8003 * Move to the first file.
8004 */
8005 /* Fake up a minimal "next" command for do_argfile() */
8006 vim_memset(&ea, 0, sizeof(ea));
8007 ea.cmd = (char_u *)"next";
8008 do_argfile(&ea, 0);
8009
8010 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8011 * mode that is not needed here. */
8012 need_start_insertmode = FALSE;
8013
8014 /* Restore msg_scroll, otherwise a following command may cause scrolling
8015 * unexpectedly. The screen will be redrawn by the caller, thus
8016 * msg_scroll being set by displaying a message is irrelevant. */
8017 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008018
8019 if (drop_callback != NULL)
8020 drop_callback(drop_cookie);
8021
8022 drop_filev = NULL;
8023 drop_busy = FALSE;
8024}
8025
8026/*
8027 * Handle a file drop. The code is here because a drop is *nearly* like an
8028 * :args command, but not quite (we have a list of exact filenames, so we
8029 * don't want to (a) parse a command line, or (b) expand wildcards. So the
8030 * code is very similar to :args and hence needs access to a lot of the static
8031 * functions in this file.
8032 *
8033 * The "filev" list must have been allocated using alloc(), as should each item
8034 * in the list. This function takes over responsibility for freeing the "filev"
8035 * list.
8036 */
8037 void
8038handle_drop(
8039 int filec, // the number of files dropped
8040 char_u **filev, // the list of files dropped
8041 int split, // force splitting the window
8042 void (*callback)(void *), // to be called after setting the argument
8043 // list
8044 void *cookie) // argument for "callback" (allocated)
8045{
8046 // Cannot handle recursive drops, finish the pending one.
8047 if (drop_busy)
8048 {
8049 FreeWild(filec, filev);
8050 vim_free(cookie);
8051 return;
8052 }
8053
8054 // When calling handle_drop() more than once in a row we only use the last
8055 // one.
8056 if (drop_filev != NULL)
8057 {
8058 FreeWild(drop_filec, drop_filev);
8059 vim_free(drop_cookie);
8060 }
8061
8062 drop_filec = filec;
8063 drop_filev = filev;
8064 drop_split = split;
8065 drop_callback = callback;
8066 drop_cookie = cookie;
8067
8068 // Postpone this when:
8069 // - editing the command line
8070 // - not possible to change the current buffer
8071 // - updating the screen
8072 // As it may change buffers and window structures that are in use and cause
8073 // freed memory to be used.
8074 if (text_locked() || curbuf_locked() || updating_screen)
8075 return;
8076
8077 handle_drop_internal();
8078}
8079
8080/*
8081 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8082 * reset: Handle a postponed drop.
8083 */
8084 void
8085handle_any_postponed_drop(void)
8086{
8087 if (!drop_busy && drop_filev != NULL
8088 && !text_locked() && !curbuf_locked() && !updating_screen)
8089 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090}
8091#endif
8092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093/*
8094 * Clear an argument list: free all file names and reset it to zero entries.
8095 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008096 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 while (--al->al_ga.ga_len >= 0)
8100 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8101 ga_clear(&al->al_ga);
8102}
8103
8104/*
8105 * Init an argument list.
8106 */
8107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008108alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8111}
8112
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113/*
8114 * Remove a reference from an argument list.
8115 * Ignored when the argument list is the global one.
8116 * If the argument list is no longer used by any window, free it.
8117 */
8118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008119alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 if (al != &global_alist && --al->al_refcount <= 0)
8122 {
8123 alist_clear(al);
8124 vim_free(al);
8125 }
8126}
8127
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128/*
8129 * Create a new argument list and use it for the current window.
8130 */
8131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008132alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
8134 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8135 if (curwin->w_alist == NULL)
8136 {
8137 curwin->w_alist = &global_alist;
8138 ++global_alist.al_refcount;
8139 }
8140 else
8141 {
8142 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008143 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 alist_init(curwin->w_alist);
8145 }
8146}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008148#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149/*
8150 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008151 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8152 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 */
8154 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008155alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156{
8157 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008158 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 char_u **new_arg_files;
8160 int new_arg_file_count;
8161 char_u *save_p_su = p_su;
8162 int i;
8163
8164 /* Don't use 'suffixes' here. This should work like the shell did the
8165 * expansion. Also, the vimrc file isn't read yet, thus the user
8166 * can't set the options. */
8167 p_su = empty_option;
8168 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8169 if (old_arg_files != NULL)
8170 {
8171 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008172 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8173 old_arg_count = GARGCOUNT;
8174 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008176 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 && new_arg_file_count > 0)
8178 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008179 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8180 TRUE, fnum_list, fnum_len);
8181 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 }
8184 p_su = save_p_su;
8185}
8186#endif
8187
8188/*
8189 * Set the argument list for the current window.
8190 * Takes over the allocated files[] and the allocated fnames in it.
8191 */
8192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008193alist_set(
8194 alist_T *al,
8195 int count,
8196 char_u **files,
8197 int use_curbuf,
8198 int *fnum_list,
8199 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200{
8201 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008202 static int recursive = 0;
8203
8204 if (recursive)
8205 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008206 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008207 return;
8208 }
8209 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210
8211 alist_clear(al);
8212 if (ga_grow(&al->al_ga, count) == OK)
8213 {
8214 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008215 {
8216 if (got_int)
8217 {
8218 /* When adding many buffers this can take a long time. Allow
8219 * interrupting here. */
8220 while (i < count)
8221 vim_free(files[i++]);
8222 break;
8223 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008224
8225 /* May set buffer name of a buffer previously used for the
8226 * argument list, so that it's re-used by alist_add. */
8227 if (fnum_list != NULL && i < fnum_len)
8228 buf_set_name(fnum_list[i], files[i]);
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008231 ui_breakcheck();
8232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 vim_free(files);
8234 }
8235 else
8236 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008239
8240 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241}
8242
8243/*
8244 * Add file "fname" to argument list "al".
8245 * "fname" must have been allocated and "al" must have been checked for room.
8246 */
8247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008248alist_add(
8249 alist_T *al,
8250 char_u *fname,
8251 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
8253 if (fname == NULL) /* don't add NULL file names */
8254 return;
8255#ifdef BACKSLASH_IN_FILENAME
8256 slash_adjust(fname);
8257#endif
8258 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8259 if (set_fnum > 0)
8260 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8261 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8262 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263}
8264
8265#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8266/*
8267 * Adjust slashes in file names. Called after 'shellslash' was set.
8268 */
8269 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008270alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
8272 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008274 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275
8276 for (i = 0; i < GARGCOUNT; ++i)
8277 if (GARGLIST[i].ae_fname != NULL)
8278 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008279 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (wp->w_alist != &global_alist)
8281 for (i = 0; i < WARGCOUNT(wp); ++i)
8282 if (WARGLIST(wp)[i].ae_fname != NULL)
8283 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284}
8285#endif
8286
8287/*
8288 * ":preserve".
8289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008291ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008293 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 ml_preserve(curbuf, TRUE);
8295}
8296
8297/*
8298 * ":recover".
8299 */
8300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008301ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8304 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008305 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8306 | CCGD_MULTWIN
8307 | (eap->forceit ? CCGD_FORCEIT : 0)
8308 | CCGD_EXCMD)
8309
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 && (*eap->arg == NUL
8311 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8312 ml_recover();
8313 recoverymode = FALSE;
8314}
8315
8316/*
8317 * Command modifier used in a wrong way.
8318 */
8319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008320ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
8322 eap->errmsg = e_invcmd;
8323}
8324
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325/*
8326 * :sview [+command] file split window with new file, read-only
8327 * :split [[+command] file] split window with current or new file
8328 * :vsplit [[+command] file] split window vertically with current or new file
8329 * :new [[+command] file] split window with no or new file
8330 * :vnew [[+command] file] split vertically window with no or new file
8331 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008332 *
8333 * :tabedit open new Tab page with empty window
8334 * :tabedit [+command] file open new Tab page and edit "file"
8335 * :tabnew [[+command] file] just like :tabedit
8336 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 */
8338 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008339ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008341 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008342#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008344#endif
8345#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008347#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008348 int use_tab = eap->cmdidx == CMD_tabedit
8349 || eap->cmdidx == CMD_tabfind
8350 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
Bram Moolenaar4033c552017-09-16 20:54:51 +02008352#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
Bram Moolenaar4033c552017-09-16 20:54:51 +02008356#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008358 * quickfix windows. But it's OK when doing ":tab split". */
8359 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {
8361 if (eap->cmdidx == CMD_split)
8362 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 if (eap->cmdidx == CMD_vsplit)
8364 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367
Bram Moolenaar4033c552017-09-16 20:54:51 +02008368#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008369 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {
8371 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8372 FNAME_MESS, TRUE, curbuf->b_ffname);
8373 if (fname == NULL)
8374 goto theend;
8375 eap->arg = fname;
8376 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008377# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008378 else
8379# endif
8380#endif
8381#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 && eap->cmdidx != CMD_new)
8385 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008386 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008387# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008388 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008389# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008390 au_has_group((char_u *)"FileExplorer"))
8391 {
8392 /* No browsing supported but we do have the file explorer:
8393 * Edit the directory. */
8394 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8395 eap->arg = (char_u *)".";
8396 }
8397 else
8398 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008399 fname = do_browse(0, (char_u *)(use_tab
8400 ? _("Edit File in new tab page")
8401 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008403 if (fname == NULL)
8404 goto theend;
8405 eap->arg = fname;
8406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 }
8408 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008411 /*
8412 * Either open new tab page or split the window.
8413 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008414 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008415 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008416 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8417 : eap->addr_count == 0 ? 0
8418 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008419 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008420 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008421
8422 /* set the alternate buffer for the window we came from */
8423 if (curwin != old_curwin
8424 && win_valid(old_curwin)
8425 && old_curwin->w_buffer != curbuf
8426 && !cmdmod.keepalt)
8427 old_curwin->w_alt_fnum = curbuf->b_fnum;
8428 }
8429 }
8430 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 /* Reset 'scrollbind' when editing another file, but keep it when
8434 * doing ":split" without arguments. */
8435 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008436# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008440 {
8441 RESET_BINDING(curwin);
8442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 else
8444 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 do_exedit(eap, old_curwin);
8446 }
8447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008448# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 cmdmod.browse = browse_flag;
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# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453theend:
8454 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008457
8458/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008459 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008460 */
8461 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008462tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008463{
8464 exarg_T ea;
8465
8466 vim_memset(&ea, 0, sizeof(ea));
8467 ea.cmdidx = CMD_tabnew;
8468 ea.cmd = (char_u *)"tabn";
8469 ea.arg = (char_u *)"";
8470 ex_splitview(&ea);
8471}
8472
8473/*
8474 * :tabnext command
8475 */
8476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008477ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008478{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008479 int tab_number;
8480
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008481 switch (eap->cmdidx)
8482 {
8483 case CMD_tabfirst:
8484 case CMD_tabrewind:
8485 goto_tabpage(1);
8486 break;
8487 case CMD_tablast:
8488 goto_tabpage(9999);
8489 break;
8490 case CMD_tabprevious:
8491 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008492 if (eap->arg && *eap->arg != NUL)
8493 {
8494 char_u *p = eap->arg;
8495 char_u *p_save = p;
8496
8497 tab_number = getdigits(&p);
8498 if (p == p_save || *p_save == '-' || *p != NUL
8499 || tab_number == 0)
8500 {
8501 /* No numbers as argument. */
8502 eap->errmsg = e_invarg;
8503 return;
8504 }
8505 }
8506 else
8507 {
8508 if (eap->addr_count == 0)
8509 tab_number = 1;
8510 else
8511 {
8512 tab_number = eap->line2;
8513 if (tab_number < 1)
8514 {
8515 eap->errmsg = e_invrange;
8516 return;
8517 }
8518 }
8519 }
8520 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008521 break;
8522 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008523 tab_number = get_tabpage_arg(eap);
8524 if (eap->errmsg == NULL)
8525 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008526 break;
8527 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008528}
8529
8530/*
8531 * :tabmove command
8532 */
8533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008534ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008535{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008536 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008537
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008538 tab_number = get_tabpage_arg(eap);
8539 if (eap->errmsg == NULL)
8540 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008541}
8542
8543/*
8544 * :tabs command: List tabs and their contents.
8545 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008547ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008548{
8549 tabpage_T *tp;
8550 win_T *wp;
8551 int tabcount = 1;
8552
8553 msg_start();
8554 msg_scroll = TRUE;
8555 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8556 {
8557 msg_putchar('\n');
8558 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008559 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008560 out_flush(); /* output one line at a time */
8561 ui_breakcheck();
8562
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008563 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008564 wp = firstwin;
8565 else
8566 wp = tp->tp_firstwin;
8567 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8568 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008569 msg_putchar('\n');
8570 msg_putchar(wp == curwin ? '>' : ' ');
8571 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008572 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8573 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008574 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008575 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008576 else
8577 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8578 IObuff, IOSIZE, TRUE);
8579 msg_outtrans(IObuff);
8580 out_flush(); /* output one line at a time */
8581 ui_breakcheck();
8582 }
8583 }
8584}
8585
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586/*
8587 * ":mode": Set screen mode.
8588 * If no argument given, just get the screen size and redraw.
8589 */
8590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008591ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 if (*eap->arg == NUL)
8594 shell_resized();
8595 else
8596 mch_screenmode(eap->arg);
8597}
8598
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599/*
8600 * ":resize".
8601 * set, increment or decrement current window height
8602 */
8603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008604ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
8606 int n;
8607 win_T *wp = curwin;
8608
8609 if (eap->addr_count > 0)
8610 {
8611 n = eap->line2;
8612 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8613 ;
8614 }
8615
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008616# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 if (cmdmod.split & WSP_VERT)
8621 {
8622 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008623 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8625 n = 9999;
8626 win_setwidth_win((int)n, wp);
8627 }
8628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {
8630 if (*eap->arg == '-' || *eap->arg == '+')
8631 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008632 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 n = 9999;
8634 win_setheight_win((int)n, wp);
8635 }
8636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638/*
8639 * ":find [+command] <file>" command.
8640 */
8641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008642ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644#ifdef FEAT_SEARCHPATH
8645 char_u *fname;
8646 int count;
8647
8648 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8649 TRUE, curbuf->b_ffname);
8650 if (eap->addr_count > 0)
8651 {
8652 /* Repeat finding the file "count" times. This matters when it
8653 * appears several times in the path. */
8654 count = eap->line2;
8655 while (fname != NULL && --count > 0)
8656 {
8657 vim_free(fname);
8658 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8659 FALSE, curbuf->b_ffname);
8660 }
8661 }
8662
8663 if (fname != NULL)
8664 {
8665 eap->arg = fname;
8666#endif
8667 do_exedit(eap, NULL);
8668#ifdef FEAT_SEARCHPATH
8669 vim_free(fname);
8670 }
8671#endif
8672}
8673
8674/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008675 * ":open" simulation: for now just work like ":visual".
8676 */
8677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008678ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008679{
8680 regmatch_T regmatch;
8681 char_u *p;
8682
8683 curwin->w_cursor.lnum = eap->line2;
8684 beginline(BL_SOL | BL_FIX);
8685 if (*eap->arg == '/')
8686 {
8687 /* ":open /pattern/": put cursor in column found with pattern */
8688 ++eap->arg;
8689 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8690 *p = NUL;
8691 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8692 if (regmatch.regprog != NULL)
8693 {
8694 regmatch.rm_ic = p_ic;
8695 p = ml_get_curline();
8696 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008697 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008698 else
8699 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008700 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008701 }
8702 /* Move to the NUL, ignore any other arguments. */
8703 eap->arg += STRLEN(eap->arg);
8704 }
8705 check_cursor();
8706
8707 eap->cmdidx = CMD_visual;
8708 do_exedit(eap, NULL);
8709}
8710
8711/*
8712 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 */
8714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008715ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716{
8717 do_exedit(eap, NULL);
8718}
8719
8720/*
8721 * ":edit <file>" command and alikes.
8722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008724do_exedit(
8725 exarg_T *eap,
8726 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008730 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731
8732 /*
8733 * ":vi" command ends Ex mode.
8734 */
8735 if (exmode_active && (eap->cmdidx == CMD_visual
8736 || eap->cmdidx == CMD_view))
8737 {
8738 exmode_active = FALSE;
8739 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008740 {
8741 /* Special case: ":global/pat/visual\NLvi-commands" */
8742 if (global_busy)
8743 {
8744 int rd = RedrawingDisabled;
8745 int nwr = no_wait_return;
8746 int ms = msg_scroll;
8747#ifdef FEAT_GUI
8748 int he = hold_gui_events;
8749#endif
8750
8751 if (eap->nextcmd != NULL)
8752 {
8753 stuffReadbuff(eap->nextcmd);
8754 eap->nextcmd = NULL;
8755 }
8756
8757 if (exmode_was != EXMODE_VIM)
8758 settmode(TMODE_RAW);
8759 RedrawingDisabled = 0;
8760 no_wait_return = 0;
8761 need_wait_return = FALSE;
8762 msg_scroll = 0;
8763#ifdef FEAT_GUI
8764 hold_gui_events = 0;
8765#endif
8766 must_redraw = CLEAR;
8767
8768 main_loop(FALSE, TRUE);
8769
8770 RedrawingDisabled = rd;
8771 no_wait_return = nwr;
8772 msg_scroll = ms;
8773#ifdef FEAT_GUI
8774 hold_gui_events = he;
8775#endif
8776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
8780
8781 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008782 || eap->cmdidx == CMD_tabnew
8783 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008784 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008786 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 setpcmark();
8788 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008789 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8790 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008792 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 || *eap->arg != NUL
8794#ifdef FEAT_BROWSE
8795 || cmdmod.browse
8796#endif
8797 )
8798 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008799 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8800 * can bring us here, others are stopped earlier. */
8801 if (*eap->arg != NUL && curbuf_locked())
8802 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008803
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 n = readonlymode;
8805 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8806 readonlymode = TRUE;
8807 else if (eap->cmdidx == CMD_enew)
8808 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8809 empty buffer */
8810 setpcmark();
8811 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8812 NULL, eap,
8813 /* ":edit" goes to first line if Vi compatible */
8814 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8815 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8816 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008817 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008819 /* after a split we can use an existing buffer */
8820 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008822 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 {
8824 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 if (old_curwin != NULL)
8826 {
8827 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008828 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008830#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008831 cleanup_T cs;
8832
8833 /* Reset the error/interrupt/exception state here so that
8834 * aborting() returns FALSE when closing a window. */
8835 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008836#endif
8837#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008839#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008840 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008841
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008842#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008843 /* Restore the error/interrupt/exception state if not
8844 * discarded by a new aborting error, interrupt, or
8845 * uncaught exception. */
8846 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 }
8849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851 else if (readonlymode && curbuf->b_nwindows == 1)
8852 {
8853 /* When editing an already visited buffer, 'readonly' won't be set
8854 * but the previous value is kept. With ":view" and ":sview" we
8855 * want the file to be readonly, except when another window is
8856 * editing the same buffer. */
8857 curbuf->b_p_ro = TRUE;
8858 }
8859 readonlymode = n;
8860 }
8861 else
8862 {
8863 if (eap->do_ecmd_cmd != NULL)
8864 do_cmdline_cmd(eap->do_ecmd_cmd);
8865#ifdef FEAT_TITLE
8866 n = curwin->w_arg_idx_invalid;
8867#endif
8868 check_arg_idx(curwin);
8869#ifdef FEAT_TITLE
8870 if (n != curwin->w_arg_idx_invalid)
8871 maketitle();
8872#endif
8873 }
8874
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 /*
8876 * if ":split file" worked, set alternate file name in old window to new
8877 * file
8878 */
8879 if (old_curwin != NULL
8880 && *eap->arg != NUL
8881 && curwin != old_curwin
8882 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008883 && old_curwin->w_buffer != curbuf
8884 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
8887 ex_no_reprint = TRUE;
8888}
8889
8890#ifndef FEAT_GUI
8891/*
8892 * ":gui" and ":gvim" when there is no GUI.
8893 */
8894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008895ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 eap->errmsg = e_nogvim;
8898}
8899#endif
8900
8901#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008903ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905 gui_make_tearoff(eap->arg);
8906}
8907#endif
8908
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008909#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8910 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008912ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008914# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8915 if (gui.in_use)
8916 gui_make_popup(eap->arg, eap->forceit);
8917# ifdef FEAT_TERM_POPUP_MENU
8918 else
8919# endif
8920# endif
8921# ifdef FEAT_TERM_POPUP_MENU
8922 pum_make_popup(eap->arg, eap->forceit);
8923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924}
8925#endif
8926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008928ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8931 MSG(_("No swap file"));
8932 else
8933 msg(curbuf->b_ml.ml_mfp->mf_fname);
8934}
8935
8936/*
8937 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8938 * offset.
8939 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008942ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008945 win_T *save_curwin = curwin;
8946 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 long topline;
8948 long y;
8949 linenr_T old_linenr = curwin->w_cursor.lnum;
8950
8951 setpcmark();
8952
8953 /*
8954 * determine max topline
8955 */
8956 if (curwin->w_p_scb)
8957 {
8958 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008959 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 {
8961 if (wp->w_p_scb && wp->w_buffer)
8962 {
8963 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8964 if (topline > y)
8965 topline = y;
8966 }
8967 }
8968 if (topline < 1)
8969 topline = 1;
8970 }
8971 else
8972 {
8973 topline = 1;
8974 }
8975
8976
8977 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008978 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008980 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
8982 if (curwin->w_p_scb)
8983 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008984 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 y = topline - curwin->w_topline;
8986 if (y > 0)
8987 scrollup(y, TRUE);
8988 else
8989 scrolldown(-y, TRUE);
8990 curwin->w_scbind_pos = topline;
8991 redraw_later(VALID);
8992 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 }
8995 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008996 curwin = save_curwin;
8997 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 if (curwin->w_p_scb)
8999 {
9000 did_syncbind = TRUE;
9001 checkpcmark();
9002 if (old_linenr != curwin->w_cursor.lnum)
9003 {
9004 char_u ctrl_o[2];
9005
9006 ctrl_o[0] = Ctrl_O;
9007 ctrl_o[1] = 0;
9008 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
9009 }
9010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011}
9012
9013
9014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009015ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009017 int i;
9018 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9019 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
9021 if (eap->usefilter) /* :r!cmd */
9022 do_bang(1, eap, FALSE, FALSE, TRUE);
9023 else
9024 {
9025 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9026 return;
9027
9028#ifdef FEAT_BROWSE
9029 if (cmdmod.browse)
9030 {
9031 char_u *browseFile;
9032
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009033 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 NULL, NULL, NULL, curbuf);
9035 if (browseFile != NULL)
9036 {
9037 i = readfile(browseFile, NULL,
9038 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9039 vim_free(browseFile);
9040 }
9041 else
9042 i = OK;
9043 }
9044 else
9045#endif
9046 if (*eap->arg == NUL)
9047 {
9048 if (check_fname() == FAIL) /* check for no file name */
9049 return;
9050 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9051 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9052 }
9053 else
9054 {
9055 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9056 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9057 i = readfile(eap->arg, NULL,
9058 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9059
9060 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009061 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009063#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 if (!aborting())
9065#endif
9066 EMSG2(_(e_notopen), eap->arg);
9067 }
9068 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009069 {
9070 if (empty && exmode_active)
9071 {
9072 /* Delete the empty line that remains. Historically ex does
9073 * this but vi doesn't. */
9074 if (eap->line2 == 0)
9075 lnum = curbuf->b_ml.ml_line_count;
9076 else
9077 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009078 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009079 {
9080 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009081 if (curwin->w_cursor.lnum > 1
9082 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009083 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009084 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009085 }
9086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 }
9090}
9091
Bram Moolenaarea408852005-06-25 22:49:46 +00009092static char_u *prev_dir = NULL;
9093
9094#if defined(EXITFREE) || defined(PROTO)
9095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009096free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009097{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009098 VIM_CLEAR(prev_dir);
9099 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009100}
9101#endif
9102
Bram Moolenaarf4258302013-06-02 18:20:17 +02009103/*
9104 * Deal with the side effects of changing the current directory.
9105 * When "local" is TRUE then this was after an ":lcd" command.
9106 */
9107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009108post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009109{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009110 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009111 if (local)
9112 {
9113 /* If still in global directory, need to remember current
9114 * directory as global directory. */
9115 if (globaldir == NULL && prev_dir != NULL)
9116 globaldir = vim_strsave(prev_dir);
9117 /* Remember this local directory for the window. */
9118 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9119 curwin->w_localdir = vim_strsave(NameBuff);
9120 }
9121 else
9122 {
9123 /* We are now in the global directory, no need to remember its
9124 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009125 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009126 }
9127
9128 shorten_fnames(TRUE);
9129}
9130
Bram Moolenaarea408852005-06-25 22:49:46 +00009131
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132/*
9133 * ":cd", ":lcd", ":chdir" and ":lchdir".
9134 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009136ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 char_u *new_dir;
9139 char_u *tofree;
9140
9141 new_dir = eap->arg;
9142#if !defined(UNIX) && !defined(VMS)
9143 /* for non-UNIX ":cd" means: print current directory */
9144 if (*new_dir == NUL)
9145 ex_pwd(NULL);
9146 else
9147#endif
9148 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009149 if (allbuf_locked())
9150 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009151 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9152 && !eap->forceit)
9153 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009154 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009155 return;
9156 }
9157
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 /* ":cd -": Change to previous directory */
9159 if (STRCMP(new_dir, "-") == 0)
9160 {
9161 if (prev_dir == NULL)
9162 {
9163 EMSG(_("E186: No previous directory"));
9164 return;
9165 }
9166 new_dir = prev_dir;
9167 }
9168
9169 /* Save current directory for next ":cd -" */
9170 tofree = prev_dir;
9171 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9172 prev_dir = vim_strsave(NameBuff);
9173 else
9174 prev_dir = NULL;
9175
9176#if defined(UNIX) || defined(VMS)
9177 /* for UNIX ":cd" means: go to home directory */
9178 if (*new_dir == NUL)
9179 {
9180 /* use NameBuff for home directory name */
9181# ifdef VMS
9182 char_u *p;
9183
9184 p = mch_getenv((char_u *)"SYS$LOGIN");
9185 if (p == NULL || *p == NUL) /* empty is the same as not set */
9186 NameBuff[0] = NUL;
9187 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009188 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189# else
9190 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9191# endif
9192 new_dir = NameBuff;
9193 }
9194#endif
9195 if (new_dir == NULL || vim_chdir(new_dir))
9196 EMSG(_(e_failed));
9197 else
9198 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009199 int is_local_chdir = eap->cmdidx == CMD_lcd
9200 || eap->cmdidx == CMD_lchdir;
9201
9202 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203
9204 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009205 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009207 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();
9275 out_flush();
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 Moolenaar655a82a2018-05-08 22:01:07 +020010490 exec_normal(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.
10495 */
10496 void
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010497exec_normal(int was_typed, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010498{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010499 oparg_T oa;
10500
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010501 clear_oparg(&oa);
10502 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010503 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10504 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010505 {
10506 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010507#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010508 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010509 && oa.op_type == OP_NOP && oa.regname == NUL
10510 && !VIsual_active)
10511 {
10512 /* If terminal_loop() returns OK we got a key that is handled
10513 * in Normal model. With FAIL we first need to position the
10514 * cursor and the screen needs to be redrawn. */
10515 if (terminal_loop(TRUE) == OK)
10516 normal_cmd(&oa, TRUE);
10517 }
10518 else
10519#endif
10520 /* execute a Normal mode cmd */
10521 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010522 }
10523}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010524
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525#ifdef FEAT_FIND_ID
10526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010527ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528{
10529 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10530 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10531 (linenr_T)1, (linenr_T)MAXLNUM);
10532}
10533
Bram Moolenaar4033c552017-09-16 20:54:51 +020010534#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535/*
10536 * ":psearch"
10537 */
10538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010539ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540{
10541 g_do_tagpreview = p_pvh;
10542 ex_findpat(eap);
10543 g_do_tagpreview = 0;
10544}
10545#endif
10546
10547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010548ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
10550 int whole = TRUE;
10551 long n;
10552 char_u *p;
10553 int action;
10554
10555 switch (cmdnames[eap->cmdidx].cmd_name[2])
10556 {
10557 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10558 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10559 action = ACTION_GOTO;
10560 else
10561 action = ACTION_SHOW;
10562 break;
10563 case 'i': /* ":ilist" and ":dlist" */
10564 action = ACTION_SHOW_ALL;
10565 break;
10566 case 'u': /* ":ijump" and ":djump" */
10567 action = ACTION_GOTO;
10568 break;
10569 default: /* ":isplit" and ":dsplit" */
10570 action = ACTION_SPLIT;
10571 break;
10572 }
10573
10574 n = 1;
10575 if (vim_isdigit(*eap->arg)) /* get count */
10576 {
10577 n = getdigits(&eap->arg);
10578 eap->arg = skipwhite(eap->arg);
10579 }
10580 if (*eap->arg == '/') /* Match regexp, not just whole words */
10581 {
10582 whole = FALSE;
10583 ++eap->arg;
10584 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10585 if (*p)
10586 {
10587 *p++ = NUL;
10588 p = skipwhite(p);
10589
10590 /* Check for trailing illegal characters */
10591 if (!ends_excmd(*p))
10592 eap->errmsg = e_trailing;
10593 else
10594 eap->nextcmd = check_nextcmd(p);
10595 }
10596 }
10597 if (!eap->skip)
10598 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10599 whole, !eap->forceit,
10600 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10601 n, action, eap->line1, eap->line2);
10602}
10603#endif
10604
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
Bram Moolenaar4033c552017-09-16 20:54:51 +020010606#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607/*
10608 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10609 */
10610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010611ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010613 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10615}
10616
10617/*
10618 * ":pedit"
10619 */
10620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010621ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 win_T *curwin_save = curwin;
10624
10625 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010626 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010627 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 do_exedit(eap, NULL);
10629 keep_help_flag = FALSE;
10630 if (curwin != curwin_save && win_valid(curwin_save))
10631 {
10632 /* Return cursor to where we were */
10633 validate_cursor();
10634 redraw_later(VALID);
10635 win_enter(curwin_save, TRUE);
10636 }
10637 g_do_tagpreview = 0;
10638}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
10641/*
10642 * ":stag", ":stselect" and ":stjump".
10643 */
10644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010645ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647 postponed_split = -1;
10648 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010649 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10651 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010652 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654
10655/*
10656 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10657 */
10658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010659ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660{
10661 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10662}
10663
10664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010665ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
10667 int cmd;
10668
10669 switch (name[1])
10670 {
10671 case 'j': cmd = DT_JUMP; /* ":tjump" */
10672 break;
10673 case 's': cmd = DT_SELECT; /* ":tselect" */
10674 break;
10675 case 'p': cmd = DT_PREV; /* ":tprevious" */
10676 break;
10677 case 'N': cmd = DT_PREV; /* ":tNext" */
10678 break;
10679 case 'n': cmd = DT_NEXT; /* ":tnext" */
10680 break;
10681 case 'o': cmd = DT_POP; /* ":pop" */
10682 break;
10683 case 'f': /* ":tfirst" */
10684 case 'r': cmd = DT_FIRST; /* ":trewind" */
10685 break;
10686 case 'l': cmd = DT_LAST; /* ":tlast" */
10687 break;
10688 default: /* ":tag" */
10689#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010690 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010692 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 return;
10694 }
10695#endif
10696 cmd = DT_TAG;
10697 break;
10698 }
10699
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010700 if (name[0] == 'l')
10701 {
10702#ifndef FEAT_QUICKFIX
10703 ex_ni(eap);
10704 return;
10705#else
10706 cmd = DT_LTAG;
10707#endif
10708 }
10709
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10711 eap->forceit, TRUE);
10712}
10713
10714/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010715 * Check "str" for starting with a special cmdline variable.
10716 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10717 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10718 */
10719 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010720find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010721{
10722 int len;
10723 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010724 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010725 "%",
10726#define SPEC_PERC 0
10727 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010728#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010729 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010730#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010731 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010732#define SPEC_CCWORD (SPEC_CWORD + 1)
10733 "<cexpr>", /* expr under cursor */
10734#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010735 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010736#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010737 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010738#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010739 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010740#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010741 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010742#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010743 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010744#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010745 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010746#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010747 "<sflnum>", /* script file line number */
10748#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010749#ifdef FEAT_CLIENTSERVER
10750 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010751# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010752#endif
10753 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010754
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010755 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010756 {
10757 len = (int)STRLEN(spec_str[i]);
10758 if (STRNCMP(src, spec_str[i], len) == 0)
10759 {
10760 *usedlen = len;
10761 return i;
10762 }
10763 }
10764 return -1;
10765}
10766
10767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 * Evaluate cmdline variables.
10769 *
10770 * change '%' to curbuf->b_ffname
10771 * '#' to curwin->w_altfile
10772 * '<cword>' to word under the cursor
10773 * '<cWORD>' to WORD under the cursor
10774 * '<cfile>' to path name under the cursor
10775 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010776 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 * '<afile>' to file name for autocommand
10778 * '<abuf>' to buffer number for autocommand
10779 * '<amatch>' to matching name for autocommand
10780 *
10781 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10782 * "" for error without a message) and NULL is returned.
10783 * Returns an allocated string if a valid match was found.
10784 * Returns NULL if no match was found. "usedlen" then still contains the
10785 * number of characters to skip.
10786 */
10787 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010788eval_vars(
10789 char_u *src, /* pointer into commandline */
10790 char_u *srcstart, /* beginning of valid memory for src */
10791 int *usedlen, /* characters after src that are used */
10792 linenr_T *lnump, /* line number for :e command, or NULL */
10793 char_u **errormsg, /* pointer to error message */
10794 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010795 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796{
10797 int i;
10798 char_u *s;
10799 char_u *result;
10800 char_u *resultbuf = NULL;
10801 int resultlen;
10802 buf_T *buf;
10803 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10804 int spec_idx;
10805#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010806 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 int skip_mod = FALSE;
10808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810
10811 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010812 if (escaped != NULL)
10813 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
10815 /*
10816 * Check if there is something to do.
10817 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010818 spec_idx = find_cmdline_var(src, usedlen);
10819 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
10821 *usedlen = 1;
10822 return NULL;
10823 }
10824
10825 /*
10826 * Skip when preceded with a backslash "\%" and "\#".
10827 * Note: In "\\%" the % is also not recognized!
10828 */
10829 if (src > srcstart && src[-1] == '\\')
10830 {
10831 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010832 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 return NULL;
10834 }
10835
10836 /*
10837 * word or WORD under cursor
10838 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010839 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10840 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010842 resultlen = find_ident_under_cursor(&result,
10843 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10844 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10845 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 if (resultlen == 0)
10847 {
10848 *errormsg = (char_u *)"";
10849 return NULL;
10850 }
10851 }
10852
10853 /*
10854 * '#': Alternate file name
10855 * '%': Current file name
10856 * File name under the cursor
10857 * File name for autocommand
10858 * and following modifiers
10859 */
10860 else
10861 {
10862 switch (spec_idx)
10863 {
10864 case SPEC_PERC: /* '%': current file */
10865 if (curbuf->b_fname == NULL)
10866 {
10867 result = (char_u *)"";
10868 valid = 0; /* Must have ":p:h" to be valid */
10869 }
10870 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010873#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010874 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010875#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 break;
10878
10879 case SPEC_HASH: /* '#' or "#99": alternate file */
10880 if (src[1] == '#') /* "##": the argument list */
10881 {
10882 result = arg_all();
10883 resultbuf = result;
10884 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010885 if (escaped != NULL)
10886 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887#ifdef FEAT_MODIFY_FNAME
10888 skip_mod = TRUE;
10889#endif
10890 break;
10891 }
10892 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010893 if (*s == '<') /* "#<99" uses v:oldfiles */
10894 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010896 if (s == src + 2 && src[1] == '-')
10897 /* just a minus sign, don't skip over it */
10898 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 *usedlen = (int)(s - src); /* length of what we expand */
10900
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010901 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010903 if (*usedlen < 2)
10904 {
10905 /* Should we give an error message for #<text? */
10906 *usedlen = 1;
10907 return NULL;
10908 }
10909#ifdef FEAT_EVAL
10910 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10911 (long)i);
10912 if (result == NULL)
10913 {
10914 *errormsg = (char_u *)"";
10915 return NULL;
10916 }
10917#else
10918 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 }
10922 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010923 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010924 if (i == 0 && src[1] == '<' && *usedlen > 1)
10925 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010926 buf = buflist_findnr(i);
10927 if (buf == NULL)
10928 {
10929 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10930 return NULL;
10931 }
10932 if (lnump != NULL)
10933 *lnump = ECMD_LAST;
10934 if (buf->b_fname == NULL)
10935 {
10936 result = (char_u *)"";
10937 valid = 0; /* Must have ":p:h" to be valid */
10938 }
10939 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010940 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010941 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010942#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010943 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010944#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010945 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 break;
10948
10949#ifdef FEAT_SEARCHPATH
10950 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010951 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 if (result == NULL)
10953 {
10954 *errormsg = (char_u *)"";
10955 return NULL;
10956 }
10957 resultbuf = result; /* remember allocated string */
10958 break;
10959#endif
10960
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 case SPEC_AFILE: /* file name for autocommand */
10962 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010963 if (result != NULL && !autocmd_fname_full)
10964 {
10965 /* Still need to turn the fname into a full path. It is
10966 * postponed to avoid a delay when <afile> is not used. */
10967 autocmd_fname_full = TRUE;
10968 result = FullName_save(autocmd_fname, FALSE);
10969 vim_free(autocmd_fname);
10970 autocmd_fname = result;
10971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 if (result == NULL)
10973 {
10974 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10975 return NULL;
10976 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010977 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 break;
10979
10980 case SPEC_ABUF: /* buffer number for autocommand */
10981 if (autocmd_bufnr <= 0)
10982 {
10983 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10984 return NULL;
10985 }
10986 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10987 result = strbuf;
10988 break;
10989
10990 case SPEC_AMATCH: /* match name for autocommand */
10991 result = autocmd_match;
10992 if (result == NULL)
10993 {
10994 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10995 return NULL;
10996 }
10997 break;
10998
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 case SPEC_SFILE: /* file name for ":so" command */
11000 result = sourcing_name;
11001 if (result == NULL)
11002 {
11003 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
11004 return NULL;
11005 }
11006 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011007
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010011008 case SPEC_SLNUM: /* line in file for ":so" command */
11009 if (sourcing_name == NULL || sourcing_lnum == 0)
11010 {
11011 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
11012 return NULL;
11013 }
11014 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11015 result = strbuf;
11016 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011017
11018#ifdef FEAT_EVAL
11019 case SPEC_SFLNUM: /* line in script file */
11020 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11021 {
11022 *errormsg = (char_u *)_("E961: no line number to use for \"<sflnum>\"");
11023 return NULL;
11024 }
11025 sprintf((char *)strbuf, "%ld",
11026 (long)(current_sctx.sc_lnum + sourcing_lnum));
11027 result = strbuf;
11028 break;
11029#endif
11030
11031#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011033 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11034 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 result = strbuf;
11036 break;
11037#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011038
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011039 default:
11040 result = (char_u *)""; /* avoid gcc warning */
11041 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 }
11043
11044 resultlen = (int)STRLEN(result); /* length of new string */
11045 if (src[*usedlen] == '<') /* remove the file name extension */
11046 {
11047 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 resultlen = (int)(s - result);
11050 }
11051#ifdef FEAT_MODIFY_FNAME
11052 else if (!skip_mod)
11053 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011054 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 &resultlen);
11056 if (result == NULL)
11057 {
11058 *errormsg = (char_u *)"";
11059 return NULL;
11060 }
11061 }
11062#endif
11063 }
11064
11065 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11066 {
11067 if (valid != VALID_HEAD + VALID_PATH)
11068 /* xgettext:no-c-format */
11069 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
11070 else
11071 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
11072 result = NULL;
11073 }
11074 else
11075 result = vim_strnsave(result, resultlen);
11076 vim_free(resultbuf);
11077 return result;
11078}
11079
11080/*
11081 * Concatenate all files in the argument list, separated by spaces, and return
11082 * it in one allocated string.
11083 * Spaces and backslashes in the file names are escaped with a backslash.
11084 * Returns NULL when out of memory.
11085 */
11086 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011087arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 int len;
11090 int idx;
11091 char_u *retval = NULL;
11092 char_u *p;
11093
11094 /*
11095 * Do this loop two times:
11096 * first time: compute the total length
11097 * second time: concatenate the names
11098 */
11099 for (;;)
11100 {
11101 len = 0;
11102 for (idx = 0; idx < ARGCOUNT; ++idx)
11103 {
11104 p = alist_name(&ARGLIST[idx]);
11105 if (p != NULL)
11106 {
11107 if (len > 0)
11108 {
11109 /* insert a space in between names */
11110 if (retval != NULL)
11111 retval[len] = ' ';
11112 ++len;
11113 }
11114 for ( ; *p != NUL; ++p)
11115 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011116 if (*p == ' '
11117#ifndef BACKSLASH_IN_FILENAME
11118 || *p == '\\'
11119#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011120 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 {
11122 /* insert a backslash */
11123 if (retval != NULL)
11124 retval[len] = '\\';
11125 ++len;
11126 }
11127 if (retval != NULL)
11128 retval[len] = *p;
11129 ++len;
11130 }
11131 }
11132 }
11133
11134 /* second time: break here */
11135 if (retval != NULL)
11136 {
11137 retval[len] = NUL;
11138 break;
11139 }
11140
11141 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011142 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (retval == NULL)
11144 break;
11145 }
11146
11147 return retval;
11148}
11149
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150/*
11151 * Expand the <sfile> string in "arg".
11152 *
11153 * Returns an allocated string, or NULL for any error.
11154 */
11155 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011156expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157{
11158 char_u *errormsg;
11159 int len;
11160 char_u *result;
11161 char_u *newres;
11162 char_u *repl;
11163 int srclen;
11164 char_u *p;
11165
11166 result = vim_strsave(arg);
11167 if (result == NULL)
11168 return NULL;
11169
11170 for (p = result; *p; )
11171 {
11172 if (STRNCMP(p, "<sfile>", 7) != 0)
11173 ++p;
11174 else
11175 {
11176 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011177 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 if (errormsg != NULL)
11179 {
11180 if (*errormsg)
11181 emsg(errormsg);
11182 vim_free(result);
11183 return NULL;
11184 }
11185 if (repl == NULL) /* no match (cannot happen) */
11186 {
11187 p += srclen;
11188 continue;
11189 }
11190 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11191 newres = alloc(len);
11192 if (newres == NULL)
11193 {
11194 vim_free(repl);
11195 vim_free(result);
11196 return NULL;
11197 }
11198 mch_memmove(newres, result, (size_t)(p - result));
11199 STRCPY(newres + (p - result), repl);
11200 len = (int)STRLEN(newres);
11201 STRCAT(newres, p + srclen);
11202 vim_free(repl);
11203 vim_free(result);
11204 result = newres;
11205 p = newres + len; /* continue after the match */
11206 }
11207 }
11208
11209 return result;
11210}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211
11212#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011213static int ses_winsizes(FILE *fd, int restore_size,
11214 win_T *tab_firstwin);
11215static int ses_win_rec(FILE *fd, frame_T *fr);
11216static frame_T *ses_skipframe(frame_T *fr);
11217static int ses_do_frame(frame_T *fr);
11218static int ses_do_win(win_T *wp);
11219static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11220static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011221static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222
11223/*
11224 * Write openfile commands for the current buffers to an .exrc file.
11225 * Return FAIL on error, OK otherwise.
11226 */
11227 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011228makeopens(
11229 FILE *fd,
11230 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231{
11232 buf_T *buf;
11233 int only_save_windows = TRUE;
11234 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 int restore_size = TRUE;
11236 win_T *wp;
11237 char_u *sname;
11238 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011239 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011240 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011241 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011242 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011243 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011244 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245
11246 if (ssop_flags & SSOP_BUFFERS)
11247 only_save_windows = FALSE; /* Save ALL buffers */
11248
11249 /*
11250 * Begin by setting the this_session variable, and then other
11251 * sessionable variables.
11252 */
11253#ifdef FEAT_EVAL
11254 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11255 return FAIL;
11256 if (ssop_flags & SSOP_GLOBALS)
11257 if (store_session_globals(fd) == FAIL)
11258 return FAIL;
11259#endif
11260
11261 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011262 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 */
11264 if (put_line(fd, "silent only") == FAIL)
11265 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011266 if ((ssop_flags & SSOP_TABPAGES)
11267 && put_line(fd, "silent tabonly") == FAIL)
11268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269
11270 /*
11271 * Now a :cd command to the session directory or the current directory
11272 */
11273 if (ssop_flags & SSOP_SESDIR)
11274 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011275 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11276 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 return FAIL;
11278 }
11279 else if (ssop_flags & SSOP_CURDIR)
11280 {
11281 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11282 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011283 || fputs("cd ", fd) < 0
11284 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11285 || put_eol(fd) == FAIL)
11286 {
11287 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 vim_free(sname);
11291 }
11292
11293 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011294 * If there is an empty, unnamed buffer we will wipe it out later.
11295 * Remember the buffer number.
11296 */
11297 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11298 return FAIL;
11299 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11300 return FAIL;
11301 if (put_line(fd, "endif") == FAIL)
11302 return FAIL;
11303
11304 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 * Now save the current files, current buffer first.
11306 */
11307 if (put_line(fd, "set shortmess=aoO") == FAIL)
11308 return FAIL;
11309
11310 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011311 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 {
11313 if (!(only_save_windows && buf->b_nwindows == 0)
11314 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011315#ifdef FEAT_TERMINAL
11316 /* skip terminal buffers: finished ones are not useful, others
11317 * will be resurrected and result in a new buffer */
11318 && !bt_terminal(buf)
11319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 && buf->b_fname != NULL
11321 && buf->b_p_bl)
11322 {
11323 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11324 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011325 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 return FAIL;
11327 }
11328 }
11329
11330 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011331 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11333 return FAIL;
11334
11335 if (ssop_flags & SSOP_RESIZE)
11336 {
11337 /* Note: after the restore we still check it worked!*/
11338 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11339 || put_eol(fd) == FAIL)
11340 return FAIL;
11341 }
11342
11343#ifdef FEAT_GUI
11344 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11345 {
11346 int x, y;
11347
11348 if (gui_mch_get_winpos(&x, &y) == OK)
11349 {
11350 /* Note: after the restore we still check it worked!*/
11351 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11352 return FAIL;
11353 }
11354 }
11355#endif
11356
11357 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011358 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11359 * will be displayed when creating the next tab. That resizes the windows
11360 * in the first tab, which may cause problems. Set 'showtabline' to 2
11361 * temporarily to avoid that.
11362 */
11363 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11364 {
11365 if (put_line(fd, "set stal=2") == FAIL)
11366 return FAIL;
11367 restore_stal = TRUE;
11368 }
11369
11370 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011371 * May repeat putting Windows for each tab, when "tabpages" is in
11372 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011373 * Don't use goto_tabpage(), it may change directory and trigger
11374 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011376 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011377 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011378 if ((ssop_flags & SSOP_TABPAGES))
11379 {
11380 int num_tabs;
11381
11382 /*
11383 * Similar to ses_win_rec() below, populate the tab pages first so
11384 * later local options won't be copied to the new tabs.
11385 */
11386 for (tabnr = 1; ; ++tabnr)
11387 {
11388 tabpage_T *tp = find_tabpage(tabnr);
11389
11390 if (tp == NULL) /* done all tab pages */
11391 break;
11392
11393 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11394 return FAIL;
11395 }
11396
11397 num_tabs = tabnr - 1;
11398 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11399 || put_eol(fd) == FAIL))
11400 return FAIL;
11401 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011402 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011404 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011405 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011406
Bram Moolenaar18144c82006-04-12 21:52:12 +000011407 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011409 tabpage_T *tp = find_tabpage(tabnr);
11410
11411 if (tp == NULL)
11412 break; /* done all tab pages */
11413 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011414 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011415 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011416 tab_topframe = topframe;
11417 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011418 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011419 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011420 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011421 tab_topframe = tp->tp_topframe;
11422 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011423 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011424 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426
Bram Moolenaar18144c82006-04-12 21:52:12 +000011427 /*
11428 * Before creating the window layout, try loading one file. If this
11429 * is aborted we don't end up with a number of useless windows.
11430 * This may have side effects! (e.g., compressed or network file).
11431 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011432 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011433 {
11434 if (ses_do_win(wp)
11435 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011436 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011437#ifdef FEAT_QUICKFIX
11438 && !bt_nofile(wp->w_buffer)
11439#endif
11440 )
11441 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011442 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11443 return FAIL;
11444 need_tabnext = FALSE;
11445
11446 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011447 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11448 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011449 return FAIL;
11450 if (!wp->w_arg_idx_invalid)
11451 edited_win = wp;
11452 break;
11453 }
11454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011456 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011457 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011458 return FAIL;
11459
Bram Moolenaar18144c82006-04-12 21:52:12 +000011460 /*
11461 * Save current window layout.
11462 */
11463 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011465 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011467 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11468 return FAIL;
11469 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11470 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471
Bram Moolenaar18144c82006-04-12 21:52:12 +000011472 /*
11473 * Check if window sizes can be restored (no windows omitted).
11474 * Remember the window number of the current window after restoring.
11475 */
11476 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011477 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011478 {
11479 if (ses_do_win(wp))
11480 ++nr;
11481 else
11482 restore_size = FALSE;
11483 if (curwin == wp)
11484 cnr = nr;
11485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486
Bram Moolenaar18144c82006-04-12 21:52:12 +000011487 /* Go to the first window. */
11488 if (put_line(fd, "wincmd t") == FAIL)
11489 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011490
Bram Moolenaar18144c82006-04-12 21:52:12 +000011491 /*
11492 * If more than one window, see if sizes can be restored.
11493 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11494 * resized when moving between windows.
11495 * Do this before restoring the view, so that the topline and the
11496 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011497 * winminheight and winminwidth need to be set to avoid an error if the
11498 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011499 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011500 if (put_line(fd, "set winminheight=0") == FAIL
11501 || put_line(fd, "set winheight=1") == FAIL
11502 || put_line(fd, "set winminwidth=0") == FAIL
11503 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011504 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011505 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011506 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507
Bram Moolenaar18144c82006-04-12 21:52:12 +000011508 /*
11509 * Restore the view of the window (options, file, cursor, etc.).
11510 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011511 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011512 {
11513 if (!ses_do_win(wp))
11514 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011515 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11516 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011517 return FAIL;
11518 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11519 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011520 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011521 }
11522
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011523 /* The argument index in the first tab page is zero, need to set it in
11524 * each window. For further tab pages it's the window where we do
11525 * "tabedit". */
11526 cur_arg_idx = next_arg_idx;
11527
Bram Moolenaar18144c82006-04-12 21:52:12 +000011528 /*
11529 * Restore cursor to the current window if it's not the first one.
11530 */
11531 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11532 || put_eol(fd) == FAIL))
11533 return FAIL;
11534
11535 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011536 * Restore window sizes again after jumping around in windows, because
11537 * the current window has a minimum size while others may not.
11538 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011539 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011540 return FAIL;
11541
Bram Moolenaar18144c82006-04-12 21:52:12 +000011542 /* Don't continue in another tab page when doing only the current one
11543 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011544 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011545 break;
11546 }
11547
11548 if (ssop_flags & SSOP_TABPAGES)
11549 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011550 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11551 || put_eol(fd) == FAIL)
11552 return FAIL;
11553 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011554 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11555 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011556
Bram Moolenaar9c102382006-05-03 21:26:49 +000011557 /*
11558 * Wipe out an empty unnamed buffer we started in.
11559 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011560 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011561 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011562 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011563 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011564 return FAIL;
11565 if (put_line(fd, "endif") == FAIL)
11566 return FAIL;
11567 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11568 return FAIL;
11569
11570 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11571 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11572 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11573 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011574 /* Re-apply 'winminheight' and 'winminwidth'. */
11575 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11576 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578
11579 /*
11580 * Lastly, execute the x.vim file if it exists.
11581 */
11582 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11583 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011584 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 || put_line(fd, "endif") == FAIL)
11586 return FAIL;
11587
11588 return OK;
11589}
11590
11591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011592ses_winsizes(
11593 FILE *fd,
11594 int restore_size,
11595 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596{
11597 int n = 0;
11598 win_T *wp;
11599
11600 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11601 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011602 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 {
11604 if (!ses_do_win(wp))
11605 continue;
11606 ++n;
11607
11608 /* restore height when not full height */
11609 if (wp->w_height + wp->w_status_height < topframe->fr_height
11610 && (fprintf(fd,
11611 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11612 n, (long)wp->w_height, Rows / 2, Rows) < 0
11613 || put_eol(fd) == FAIL))
11614 return FAIL;
11615
11616 /* restore width when not full width */
11617 if (wp->w_width < Columns && (fprintf(fd,
11618 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11619 n, (long)wp->w_width, Columns / 2, Columns) < 0
11620 || put_eol(fd) == FAIL))
11621 return FAIL;
11622 }
11623 }
11624 else
11625 {
11626 /* Just equalise window sizes */
11627 if (put_line(fd, "wincmd =") == FAIL)
11628 return FAIL;
11629 }
11630 return OK;
11631}
11632
11633/*
11634 * Write commands to "fd" to recursively create windows for frame "fr",
11635 * horizontally and vertically split.
11636 * After the commands the last window in the frame is the current window.
11637 * Returns FAIL when writing the commands to "fd" fails.
11638 */
11639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011640ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641{
11642 frame_T *frc;
11643 int count = 0;
11644
11645 if (fr->fr_layout != FR_LEAF)
11646 {
11647 /* Find first frame that's not skipped and then create a window for
11648 * each following one (first frame is already there). */
11649 frc = ses_skipframe(fr->fr_child);
11650 if (frc != NULL)
11651 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11652 {
11653 /* Make window as big as possible so that we have lots of room
11654 * to split. */
11655 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11656 || put_line(fd, fr->fr_layout == FR_COL
11657 ? "split" : "vsplit") == FAIL)
11658 return FAIL;
11659 ++count;
11660 }
11661
11662 /* Go back to the first window. */
11663 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11664 ? "%dwincmd k" : "%dwincmd h", count) < 0
11665 || put_eol(fd) == FAIL))
11666 return FAIL;
11667
11668 /* Recursively create frames/windows in each window of this column or
11669 * row. */
11670 frc = ses_skipframe(fr->fr_child);
11671 while (frc != NULL)
11672 {
11673 ses_win_rec(fd, frc);
11674 frc = ses_skipframe(frc->fr_next);
11675 /* Go to next window. */
11676 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11677 return FAIL;
11678 }
11679 }
11680 return OK;
11681}
11682
11683/*
11684 * Skip frames that don't contain windows we want to save in the Session.
11685 * Returns NULL when there none.
11686 */
11687 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011688ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689{
11690 frame_T *frc;
11691
11692 for (frc = fr; frc != NULL; frc = frc->fr_next)
11693 if (ses_do_frame(frc))
11694 break;
11695 return frc;
11696}
11697
11698/*
11699 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11700 * the Session.
11701 */
11702 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011703ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704{
11705 frame_T *frc;
11706
11707 if (fr->fr_layout == FR_LEAF)
11708 return ses_do_win(fr->fr_win);
11709 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11710 if (ses_do_frame(frc))
11711 return TRUE;
11712 return FALSE;
11713}
11714
11715/*
11716 * Return non-zero if window "wp" is to be stored in the Session.
11717 */
11718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011719ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011721#ifdef FEAT_TERMINAL
11722 if (bt_terminal(wp->w_buffer))
11723 return !term_is_finished(wp->w_buffer)
11724 && (ssop_flags & SSOP_TERMINAL)
11725 && term_should_restore(wp->w_buffer);
11726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 if (wp->w_buffer->b_fname == NULL
11728#ifdef FEAT_QUICKFIX
11729 /* When 'buftype' is "nofile" can't restore the window contents. */
11730 || bt_nofile(wp->w_buffer)
11731#endif
11732 )
11733 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011734 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 return (ssop_flags & SSOP_HELP);
11736 return TRUE;
11737}
11738
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011739 static int
11740put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11741{
11742 int r;
11743
11744 if (wp->w_curswant == MAXCOL)
11745 r = fprintf(fd, "%snormal! $", spaces);
11746 else
11747 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11748 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11749}
11750
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751/*
11752 * Write commands to "fd" to restore the view of a window.
11753 * Caller must make sure 'scrolloff' is zero.
11754 */
11755 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011756put_view(
11757 FILE *fd,
11758 win_T *wp,
11759 int add_edit, /* add ":edit" command to view */
11760 unsigned *flagp, /* vop_flags or ssop_flags */
11761 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011762 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763{
11764 win_T *save_curwin;
11765 int f;
11766 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011767 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768
11769 /* Always restore cursor position for ":mksession". For ":mkview" only
11770 * when 'viewoptions' contains "cursor". */
11771 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11772
11773 /*
11774 * Local argument list.
11775 */
11776 if (wp->w_alist == &global_alist)
11777 {
11778 if (put_line(fd, "argglobal") == FAIL)
11779 return FAIL;
11780 }
11781 else
11782 {
11783 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11784 flagp == &vop_flags
11785 || !(*flagp & SSOP_CURDIR)
11786 || wp->w_localdir != NULL, flagp) == FAIL)
11787 return FAIL;
11788 }
11789
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011790 /* Only when part of a session: restore the argument index. Some
11791 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011792 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011793 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011795 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 || put_eol(fd) == FAIL)
11797 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011798 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 }
11800
11801 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011802 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011804# ifdef FEAT_TERMINAL
11805 if (bt_terminal(wp->w_buffer))
11806 {
11807 if (term_write_session(fd, wp) == FAIL)
11808 return FAIL;
11809 }
11810 else
11811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 /*
11813 * Load the file.
11814 */
11815 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011816# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 )
11820 {
11821 /*
11822 * Editing a file in this buffer: use ":edit file".
11823 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011824 *
11825 * Note, if a buffer for that file already exists, use :badd to
11826 * edit that buffer, to not lose folding information (:edit resets
11827 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011829 if (fputs("if bufexists('", fd) < 0
11830 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11831 || fputs("') | buffer ", fd) < 0
11832 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11833 || fputs(" | else | edit ", fd) < 0
11834 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11835 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011836 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 return FAIL;
11838 }
11839 else
11840 {
11841 /* No file in this buffer, just make it empty. */
11842 if (put_line(fd, "enew") == FAIL)
11843 return FAIL;
11844#ifdef FEAT_QUICKFIX
11845 if (wp->w_buffer->b_ffname != NULL)
11846 {
11847 /* The buffer does have a name, but it's not a file name. */
11848 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011849 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 return FAIL;
11851 }
11852#endif
11853 do_cursor = FALSE;
11854 }
11855 }
11856
11857 /*
11858 * Local mappings and abbreviations.
11859 */
11860 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11861 && makemap(fd, wp->w_buffer) == FAIL)
11862 return FAIL;
11863
11864 /*
11865 * Local options. Need to go to the window temporarily.
11866 * Store only local values when using ":mkview" and when ":mksession" is
11867 * used and 'sessionoptions' doesn't include "options".
11868 * Some folding options are always stored when "folds" is included,
11869 * otherwise the folds would not be restored correctly.
11870 */
11871 save_curwin = curwin;
11872 curwin = wp;
11873 curbuf = curwin->w_buffer;
11874 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11875 f = makeset(fd, OPT_LOCAL,
11876 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11877#ifdef FEAT_FOLDING
11878 else if (*flagp & SSOP_FOLDS)
11879 f = makefoldset(fd);
11880#endif
11881 else
11882 f = OK;
11883 curwin = save_curwin;
11884 curbuf = curwin->w_buffer;
11885 if (f == FAIL)
11886 return FAIL;
11887
11888#ifdef FEAT_FOLDING
11889 /*
11890 * Save Folds when 'buftype' is empty and for help files.
11891 */
11892 if ((*flagp & SSOP_FOLDS)
11893 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011894 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 {
11896 if (put_folds(fd, wp) == FAIL)
11897 return FAIL;
11898 }
11899#endif
11900
11901 /*
11902 * Set the cursor after creating folds, since that moves the cursor.
11903 */
11904 if (do_cursor)
11905 {
11906
11907 /* Restore the cursor line in the file and relatively in the
11908 * window. Don't use "G", it changes the jumplist. */
11909 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11910 (long)wp->w_cursor.lnum,
11911 (long)(wp->w_cursor.lnum - wp->w_topline),
11912 (long)wp->w_height / 2, (long)wp->w_height) < 0
11913 || put_eol(fd) == FAIL
11914 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11915 || put_line(fd, "exe s:l") == FAIL
11916 || put_line(fd, "normal! zt") == FAIL
11917 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11918 || put_eol(fd) == FAIL)
11919 return FAIL;
11920 /* Restore the cursor column and left offset when not wrapping. */
11921 if (wp->w_cursor.col == 0)
11922 {
11923 if (put_line(fd, "normal! 0") == FAIL)
11924 return FAIL;
11925 }
11926 else
11927 {
11928 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11929 {
11930 if (fprintf(fd,
11931 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011932 (long)wp->w_virtcol + 1,
11933 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 (long)wp->w_width / 2, (long)wp->w_width) < 0
11935 || put_eol(fd) == FAIL
11936 || put_line(fd, "if s:c > 0") == FAIL
11937 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011938 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11939 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 || put_eol(fd) == FAIL
11941 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011942 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 || put_line(fd, "endif") == FAIL)
11944 return FAIL;
11945 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011946 else if (put_view_curpos(fd, wp, "") == FAIL)
11947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 }
11949 }
11950
11951 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011952 * Local directory, if the current flag is not view options or the "curdir"
11953 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011955 if (wp->w_localdir != NULL
11956 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 {
11958 if (fputs("lcd ", fd) < 0
11959 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11960 || put_eol(fd) == FAIL)
11961 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011962 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 }
11964
11965 return OK;
11966}
11967
11968/*
11969 * Write an argument list to the session file.
11970 * Returns FAIL if writing fails.
11971 */
11972 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011973ses_arglist(
11974 FILE *fd,
11975 char *cmd,
11976 garray_T *gap,
11977 int fullname, /* TRUE: use full path name */
11978 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979{
11980 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011981 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 char_u *s;
11983
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011984 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11985 return FAIL;
11986 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 return FAIL;
11988 for (i = 0; i < gap->ga_len; ++i)
11989 {
11990 /* NULL file names are skipped (only happens when out of memory). */
11991 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11992 if (s != NULL)
11993 {
11994 if (fullname)
11995 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011996 buf = alloc(MAXPATHL);
11997 if (buf != NULL)
11998 {
11999 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
12000 s = buf;
12001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010012003 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012004 || ses_put_fname(fd, s, flagp) == FAIL
12005 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020012006 {
12007 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012009 }
12010 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 }
12012 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012013 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014}
12015
12016/*
12017 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012018 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 * Returns FAIL if writing fails.
12020 */
12021 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012022ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023{
12024 char_u *name;
12025
12026 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012027 * the session file will be sourced.
12028 * Don't do this for ":mkview", we don't know the current directory.
12029 * Don't do this after ":lcd", we don't keep track of what the current
12030 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 if (buf->b_sfname != NULL
12032 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012033 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012034#ifdef FEAT_AUTOCHDIR
12035 && !p_acd
12036#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012037 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 name = buf->b_sfname;
12039 else
12040 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012041 if (ses_put_fname(fd, name, flagp) == FAIL
12042 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 return FAIL;
12044 return OK;
12045}
12046
12047/*
12048 * Write a file name to the session file.
12049 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12050 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012051 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 */
12053 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012054ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055{
12056 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012057 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
12060 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012061 if (sname == NULL)
12062 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012064 if (*flagp & SSOP_SLASH)
12065 {
12066 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012067 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012068 if (*p == '\\')
12069 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012071
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012072 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012073 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012075 if (p == NULL)
12076 return FAIL;
12077
12078 /* write the result */
12079 if (fputs((char *)p, fd) < 0)
12080 retval = FAIL;
12081
12082 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 return retval;
12084}
12085
12086/*
12087 * ":loadview [nr]"
12088 */
12089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012090ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091{
12092 char_u *fname;
12093
12094 fname = get_view_file(*eap->arg);
12095 if (fname != NULL)
12096 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012097 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 vim_free(fname);
12099 }
12100}
12101
12102/*
12103 * Get the name of the view file for the current buffer.
12104 */
12105 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012106get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107{
12108 int len = 0;
12109 char_u *p, *s;
12110 char_u *retval;
12111 char_u *sname;
12112
12113 if (curbuf->b_ffname == NULL)
12114 {
12115 EMSG(_(e_noname));
12116 return NULL;
12117 }
12118 sname = home_replace_save(NULL, curbuf->b_ffname);
12119 if (sname == NULL)
12120 return NULL;
12121
12122 /*
12123 * We want a file name without separators, because we're not going to make
12124 * a directory.
12125 * "normal" path separator -> "=+"
12126 * "=" -> "=="
12127 * ":" path separator -> "=-"
12128 */
12129 for (p = sname; *p; ++p)
12130 if (*p == '=' || vim_ispathsep(*p))
12131 ++len;
12132 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12133 if (retval != NULL)
12134 {
12135 STRCPY(retval, p_vdir);
12136 add_pathsep(retval);
12137 s = retval + STRLEN(retval);
12138 for (p = sname; *p; ++p)
12139 {
12140 if (*p == '=')
12141 {
12142 *s++ = '=';
12143 *s++ = '=';
12144 }
12145 else if (vim_ispathsep(*p))
12146 {
12147 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012148#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 if (*p == ':')
12150 *s++ = '-';
12151 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012153 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 }
12155 else
12156 *s++ = *p;
12157 }
12158 *s++ = '=';
12159 *s++ = c;
12160 STRCPY(s, ".vim");
12161 }
12162
12163 vim_free(sname);
12164 return retval;
12165}
12166
12167#endif /* FEAT_SESSION */
12168
12169/*
12170 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12171 * Return FAIL for a write error.
12172 */
12173 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012174put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175{
12176 if (
12177#ifdef USE_CRNL
12178 (
12179# ifdef MKSESSION_NL
12180 !mksession_nl &&
12181# endif
12182 (putc('\r', fd) < 0)) ||
12183#endif
12184 (putc('\n', fd) < 0))
12185 return FAIL;
12186 return OK;
12187}
12188
12189/*
12190 * Write a line to "fd".
12191 * Return FAIL for a write error.
12192 */
12193 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012194put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195{
12196 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12197 return FAIL;
12198 return OK;
12199}
12200
12201#ifdef FEAT_VIMINFO
12202/*
12203 * ":rviminfo" and ":wviminfo".
12204 */
12205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012206ex_viminfo(
12207 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208{
12209 char_u *save_viminfo;
12210
12211 save_viminfo = p_viminfo;
12212 if (*p_viminfo == NUL)
12213 p_viminfo = (char_u *)"'100";
12214 if (eap->cmdidx == CMD_rviminfo)
12215 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012216 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12217 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 EMSG(_("E195: Cannot open viminfo file for reading"));
12219 }
12220 else
12221 write_viminfo(eap->arg, eap->forceit);
12222 p_viminfo = save_viminfo;
12223}
12224#endif
12225
12226#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012227/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012228 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012229 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012232dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 if (fname == NULL)
12235 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012236 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237}
12238#endif
12239
12240/*
12241 * ":behave {mswin,xterm}"
12242 */
12243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012244ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245{
12246 if (STRCMP(eap->arg, "mswin") == 0)
12247 {
12248 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12249 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12250 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12251 set_option_value((char_u *)"keymodel", 0L,
12252 (char_u *)"startsel,stopsel", 0);
12253 }
12254 else if (STRCMP(eap->arg, "xterm") == 0)
12255 {
12256 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12257 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12258 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12259 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12260 }
12261 else
12262 EMSG2(_(e_invarg2), eap->arg);
12263}
12264
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012265#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12266/*
12267 * Function given to ExpandGeneric() to obtain the possible arguments of the
12268 * ":behave {mswin,xterm}" command.
12269 */
12270 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012271get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012272{
12273 if (idx == 0)
12274 return (char_u *)"mswin";
12275 if (idx == 1)
12276 return (char_u *)"xterm";
12277 return NULL;
12278}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012279
12280/*
12281 * Function given to ExpandGeneric() to obtain the possible arguments of the
12282 * ":messages {clear}" command.
12283 */
12284 char_u *
12285get_messages_arg(expand_T *xp UNUSED, int idx)
12286{
12287 if (idx == 0)
12288 return (char_u *)"clear";
12289 return NULL;
12290}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012291#endif
12292
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012293 char_u *
12294get_mapclear_arg(expand_T *xp UNUSED, int idx)
12295{
12296 if (idx == 0)
12297 return (char_u *)"<buffer>";
12298 return NULL;
12299}
12300
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301static int filetype_detect = FALSE;
12302static int filetype_plugin = FALSE;
12303static int filetype_indent = FALSE;
12304
12305/*
12306 * ":filetype [plugin] [indent] {on,off,detect}"
12307 * on: Load the filetype.vim file to install autocommands for file types.
12308 * off: Load the ftoff.vim file to remove all autocommands for file types.
12309 * plugin on: load filetype.vim and ftplugin.vim
12310 * plugin off: load ftplugof.vim
12311 * indent on: load filetype.vim and indent.vim
12312 * indent off: load indoff.vim
12313 */
12314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012315ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316{
12317 char_u *arg = eap->arg;
12318 int plugin = FALSE;
12319 int indent = FALSE;
12320
12321 if (*eap->arg == NUL)
12322 {
12323 /* Print current status. */
12324 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12325 filetype_detect ? "ON" : "OFF",
12326 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12327 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12328 return;
12329 }
12330
12331 /* Accept "plugin" and "indent" in any order. */
12332 for (;;)
12333 {
12334 if (STRNCMP(arg, "plugin", 6) == 0)
12335 {
12336 plugin = TRUE;
12337 arg = skipwhite(arg + 6);
12338 continue;
12339 }
12340 if (STRNCMP(arg, "indent", 6) == 0)
12341 {
12342 indent = TRUE;
12343 arg = skipwhite(arg + 6);
12344 continue;
12345 }
12346 break;
12347 }
12348 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12349 {
12350 if (*arg == 'o' || !filetype_detect)
12351 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012352 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 filetype_detect = TRUE;
12354 if (plugin)
12355 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012356 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 filetype_plugin = TRUE;
12358 }
12359 if (indent)
12360 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012361 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 filetype_indent = TRUE;
12363 }
12364 }
12365 if (*arg == 'd')
12366 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012367 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012368 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 }
12370 }
12371 else if (STRCMP(arg, "off") == 0)
12372 {
12373 if (plugin || indent)
12374 {
12375 if (plugin)
12376 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012377 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 filetype_plugin = FALSE;
12379 }
12380 if (indent)
12381 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012382 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 filetype_indent = FALSE;
12384 }
12385 }
12386 else
12387 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012388 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 filetype_detect = FALSE;
12390 }
12391 }
12392 else
12393 EMSG2(_(e_invarg2), arg);
12394}
12395
12396/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012397 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 */
12399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012400ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
12402 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012403 {
12404 char_u *arg = eap->arg;
12405
12406 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12407 arg += 9;
12408
12409 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12410 if (arg != eap->arg)
12411 did_filetype = FALSE;
12412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012416ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417{
12418#ifdef FEAT_DIGRAPHS
12419 if (*eap->arg != NUL)
12420 putdigraph(eap->arg);
12421 else
12422 listdigraphs();
12423#else
12424 EMSG(_("E196: No digraphs in this version"));
12425#endif
12426}
12427
12428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012429ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430{
12431 int flags = 0;
12432
12433 if (eap->cmdidx == CMD_setlocal)
12434 flags = OPT_LOCAL;
12435 else if (eap->cmdidx == CMD_setglobal)
12436 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012437#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 if (cmdmod.browse && flags == 0)
12439 ex_options(eap);
12440 else
12441#endif
12442 (void)do_set(eap->arg, flags);
12443}
12444
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012445#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12446 void
12447set_no_hlsearch(int flag)
12448{
12449 no_hlsearch = flag;
12450# ifdef FEAT_EVAL
12451 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12452# endif
12453}
12454
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455/*
12456 * ":nohlsearch"
12457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012459ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012461 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012462 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463}
12464
12465/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012466 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 * Sets nextcmd to the start of the next command, if any. Also called when
12468 * skipping commands to find the next command.
12469 */
12470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012471ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472{
12473 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012474 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 char_u *end;
12476 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012477 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012478
12479 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012480 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012481 else
12482 {
12483 EMSG(e_invcmd);
12484 return;
12485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486
12487 /* First clear any old pattern. */
12488 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012489 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490
12491 if (ends_excmd(*eap->arg))
12492 end = eap->arg;
12493 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012494 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 end = eap->arg + 4;
12496 else
12497 {
12498 p = skiptowhite(eap->arg);
12499 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012500 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 p = skipwhite(p);
12502 if (*p == NUL)
12503 {
12504 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012505 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 EMSG2(_(e_invarg2), eap->arg);
12507 return;
12508 }
12509 end = skip_regexp(p + 1, *p, TRUE, NULL);
12510 if (!eap->skip)
12511 {
12512 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12513 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012514 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515 eap->errmsg = e_trailing;
12516 return;
12517 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012518 if (*end != *p)
12519 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012520 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012521 EMSG2(_(e_invarg2), p);
12522 return;
12523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524
12525 c = *end;
12526 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012527 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012528 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012529 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 }
12531 }
12532 eap->nextcmd = find_nextcmd(end);
12533}
12534#endif
12535
12536#ifdef FEAT_CRYPT
12537/*
12538 * ":X": Get crypt key
12539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012541ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012543 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012544 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545}
12546#endif
12547
12548#ifdef FEAT_FOLDING
12549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012550ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551{
12552 if (foldManualAllowed(TRUE))
12553 foldCreate(eap->line1, eap->line2);
12554}
12555
12556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012557ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558{
12559 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12560 eap->forceit, FALSE);
12561}
12562
12563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012564ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565{
12566 linenr_T lnum;
12567
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012568#ifdef FEAT_CLIPBOARD
12569 start_global_changes();
12570#endif
12571
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 /* First set the marks for all lines closed/open. */
12573 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12574 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12575 ml_setmarked(lnum);
12576
12577 /* Execute the command on the marked lines. */
12578 global_exe(eap->arg);
12579 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012580#ifdef FEAT_CLIPBOARD
12581 end_global_changes();
12582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583}
12584#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012585
Bram Moolenaar39665952018-08-15 20:59:48 +020012586#ifdef FEAT_QUICKFIX
12587/*
12588 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12589 * instead of a quickfix command.
12590 */
12591 int
12592is_loclist_cmd(int cmdidx)
12593{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012594 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012595 return FALSE;
12596 return cmdnames[cmdidx].cmd_name[0] == 'l';
12597}
12598#endif
12599
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012600# if defined(FEAT_TIMERS) || defined(PROTO)
12601 int
12602get_pressedreturn(void)
12603{
12604 return ex_pressedreturn;
12605}
12606
12607 void
12608set_pressedreturn(int val)
12609{
12610 ex_pressedreturn = val;
12611}
12612#endif