blob: ccd96b1d326e1b574b8f28ce03671ac4bad3a54b [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
32 scid_T uc_scriptID; /* SID where the command was defined */
33# 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 Moolenaarded27822017-01-02 14:27:34 +0100120static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, 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
1769#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001770# ifdef FEAT_PROFILE
1771 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001772 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001773 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001774 if (getline_equal(fgetline, cookie, get_func_line))
1775 func_line_exec(getline_cookie(fgetline, cookie));
1776 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001777 script_line_exec();
1778 }
1779#endif
1780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 /* May go to debug mode. If this happens and the ">quit" debug command is
1782 * used, throw an interrupt exception and skip the next command. */
1783 dbg_check_breakpoint(&ea);
1784 if (!ea.skip && got_int)
1785 {
1786 ea.skip = TRUE;
1787 (void)do_intthrow(cstack);
1788 }
1789#endif
1790
1791/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001792 * 3. Skip over the range to find the command. Let "p" point to after it.
1793 *
1794 * We need the command to know what kind of range it uses.
1795 */
1796 cmd = ea.cmd;
1797 ea.cmd = skip_range(ea.cmd, NULL);
1798 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1799 ea.cmd = skipwhite(ea.cmd + 1);
1800 p = find_command(&ea, NULL);
1801
1802/*
1803 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 *
1805 * where 'addr' is:
1806 *
1807 * % (entire file)
1808 * $ [+-NUM]
1809 * 'x [+-NUM] (where x denotes a currently defined mark)
1810 * . [+-NUM]
1811 * [+-NUM]..
1812 * NUM
1813 *
1814 * The ea.cmd pointer is updated to point to the first character following the
1815 * range spec. If an initial address is found, but no second, the upper bound
1816 * is equal to the lower.
1817 */
1818
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001819 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001820 if (!IS_USER_CMDIDX(ea.cmdidx))
1821 {
1822 if (ea.cmdidx != CMD_SIZE)
1823 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1824 else
1825 ea.addr_type = ADDR_LINES;
1826
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001827 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001828 if (ea.cmdidx == CMD_wincmd && p != NULL)
1829 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001830 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001831
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001832 ea.cmd = cmd;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001833 if (parse_cmd_address(&ea, &errormsg) == FAIL)
1834 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001837 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839
1840 /*
1841 * Skip ':' and any white space
1842 */
1843 ea.cmd = skipwhite(ea.cmd);
1844 while (*ea.cmd == ':')
1845 ea.cmd = skipwhite(ea.cmd + 1);
1846
1847 /*
1848 * If we got a line, but no command, then go to the line.
1849 * If we find a '|' or '\n' we set ea.nextcmd.
1850 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001851 if (*ea.cmd == NUL || *ea.cmd == '"'
1852 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
1854 /*
1855 * strange vi behaviour:
1856 * ":3" jumps to line 3
1857 * ":3|..." prints line 3
1858 * ":|" prints current line
1859 */
1860 if (ea.skip) /* skip this if inside :if */
1861 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001862 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 ea.cmdidx = CMD_print;
1865 ea.argt = RANGE+COUNT+TRLBAR;
1866 if ((errormsg = invalid_range(&ea)) == NULL)
1867 {
1868 correct_range(&ea);
1869 ex_print(&ea);
1870 }
1871 }
1872 else if (ea.addr_count != 0)
1873 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001874 if (ea.line2 > curbuf->b_ml.ml_line_count)
1875 {
1876 /* With '-' in 'cpoptions' a line number past the file is an
1877 * error, otherwise put it at the end of the file. */
1878 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1879 ea.line2 = -1;
1880 else
1881 ea.line2 = curbuf->b_ml.ml_line_count;
1882 }
1883
1884 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001885 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 else
1887 {
1888 if (ea.line2 == 0)
1889 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 else
1891 curwin->w_cursor.lnum = ea.line2;
1892 beginline(BL_SOL | BL_FIX);
1893 }
1894 }
1895 goto doend;
1896 }
1897
Bram Moolenaard5005162014-08-22 23:05:54 +02001898 /* If this looks like an undefined user command and there are CmdUndefined
1899 * autocommands defined, trigger the matching autocommands. */
1900 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1901 && ASCII_ISUPPER(*ea.cmd)
1902 && has_cmdundefined())
1903 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001904 int ret;
1905
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001906 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001907 while (ASCII_ISALNUM(*p))
1908 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001909 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001910 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1911 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001912 /* If the autocommands did something and didn't cause an error, try
1913 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001914 p = (ret
1915#ifdef FEAT_EVAL
1916 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001917#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001918 ) ? find_command(&ea, NULL) : ea.cmd;
1919 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921#ifdef FEAT_USR_CMDS
1922 if (p == NULL)
1923 {
1924 if (!ea.skip)
1925 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
1926 goto doend;
1927 }
1928 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001929 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1930 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 errormsg = uc_fun_cmd();
1933 goto doend;
1934 }
1935#endif
1936 if (ea.cmdidx == CMD_SIZE)
1937 {
1938 if (!ea.skip)
1939 {
1940 STRCPY(IObuff, _("E492: Not an editor command"));
1941 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001942 {
1943 /* If the modifier was parsed OK the error must be in the
1944 * following command */
1945 if (after_modifier != NULL)
1946 append_command(after_modifier);
1947 else
1948 append_command(*cmdlinep);
1949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001951 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953 goto doend;
1954 }
1955
Bram Moolenaar958636c2014-10-21 20:01:58 +02001956 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1957 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001958#ifdef HAVE_EX_SCRIPT_NI
1959 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1960#endif
1961 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963#ifndef FEAT_EVAL
1964 /*
1965 * When the expression evaluation is disabled, recognize the ":if" and
1966 * ":endif" commands and ignore everything in between it.
1967 */
1968 if (ea.cmdidx == CMD_if)
1969 ++if_level;
1970 if (if_level)
1971 {
1972 if (ea.cmdidx == CMD_endif)
1973 --if_level;
1974 goto doend;
1975 }
1976
1977#endif
1978
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001979 /* forced commands */
1980 if (*p == '!' && ea.cmdidx != CMD_substitute
1981 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 ++p;
1984 ea.forceit = TRUE;
1985 }
1986 else
1987 ea.forceit = FALSE;
1988
1989/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001990 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001992 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001993 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995 if (!ea.skip)
1996 {
1997#ifdef HAVE_SANDBOX
1998 if (sandbox != 0 && !(ea.argt & SBOXOK))
1999 {
2000 /* Command not allowed in sandbox. */
2001 errormsg = (char_u *)_(e_sandbox);
2002 goto doend;
2003 }
2004#endif
2005 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2006 {
2007 /* Command not allowed in non-'modifiable' buffer */
2008 errormsg = (char_u *)_(e_modifiable);
2009 goto doend;
2010 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002011
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002012 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002013 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002015 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002016 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002019 /* Disallow editing another buffer when "curbuf_lock" is set.
2020 * Do allow ":edit" (check for argument later).
2021 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002022 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002023 && ea.cmdidx != CMD_edit
2024 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002025 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002026 && curbuf_locked())
2027 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2030 {
2031 /* no range allowed */
2032 errormsg = (char_u *)_(e_norange);
2033 goto doend;
2034 }
2035 }
2036
2037 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2038 {
2039 errormsg = (char_u *)_(e_nobang);
2040 goto doend;
2041 }
2042
2043 /*
2044 * Don't complain about the range if it is not used
2045 * (could happen if line_count is accidentally set to 0).
2046 */
2047 if (!ea.skip && !ni)
2048 {
2049 /*
2050 * If the range is backwards, ask for confirmation and, if given, swap
2051 * ea.line1 & ea.line2 so it's forwards again.
2052 * When global command is busy, don't ask, will fail below.
2053 */
2054 if (!global_busy && ea.line1 > ea.line2)
2055 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002056 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002058 if (sourcing || exmode_active)
2059 {
2060 errormsg = (char_u *)_("E493: Backwards range given");
2061 goto doend;
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (ask_yesno((char_u *)
2064 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002065 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067 lnum = ea.line1;
2068 ea.line1 = ea.line2;
2069 ea.line2 = lnum;
2070 }
2071 if ((errormsg = invalid_range(&ea)) != NULL)
2072 goto doend;
2073 }
2074
2075 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2076 ea.line2 = 1;
2077
2078 correct_range(&ea);
2079
2080#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002081 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2082 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 /* Put the first line at the start of a closed fold, put the last line
2085 * at the end of a closed fold. */
2086 (void)hasFolding(ea.line1, &ea.line1, NULL);
2087 (void)hasFolding(ea.line2, NULL, &ea.line2);
2088 }
2089#endif
2090
2091#ifdef FEAT_QUICKFIX
2092 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002093 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 * option here, so things like % get expanded.
2095 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002096 p = replace_makeprg(&ea, p, cmdlinep);
2097 if (p == NULL)
2098 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099#endif
2100
2101 /*
2102 * Skip to start of argument.
2103 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2104 */
2105 if (ea.cmdidx == CMD_bang)
2106 ea.arg = p;
2107 else
2108 ea.arg = skipwhite(p);
2109
2110 /*
2111 * Check for "++opt=val" argument.
2112 * Must be first, allow ":w ++enc=utf8 !cmd"
2113 */
2114 if (ea.argt & ARGOPT)
2115 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2116 if (getargopt(&ea) == FAIL && !ni)
2117 {
2118 errormsg = (char_u *)_(e_invarg);
2119 goto doend;
2120 }
2121
2122 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2123 {
2124 if (*ea.arg == '>') /* append */
2125 {
2126 if (*++ea.arg != '>') /* typed wrong */
2127 {
2128 errormsg = (char_u *)_("E494: Use w or w>>");
2129 goto doend;
2130 }
2131 ea.arg = skipwhite(ea.arg + 1);
2132 ea.append = TRUE;
2133 }
2134 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2135 {
2136 ++ea.arg;
2137 ea.usefilter = TRUE;
2138 }
2139 }
2140
2141 if (ea.cmdidx == CMD_read)
2142 {
2143 if (ea.forceit)
2144 {
2145 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2146 ea.forceit = FALSE;
2147 }
2148 else if (*ea.arg == '!') /* :r !filter */
2149 {
2150 ++ea.arg;
2151 ea.usefilter = TRUE;
2152 }
2153 }
2154
2155 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2156 {
2157 ea.amount = 1;
2158 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2159 {
2160 ++ea.arg;
2161 ++ea.amount;
2162 }
2163 ea.arg = skipwhite(ea.arg);
2164 }
2165
2166 /*
2167 * Check for "+command" argument, before checking for next command.
2168 * Don't do this for ":read !cmd" and ":write !cmd".
2169 */
2170 if ((ea.argt & EDITCMD) && !ea.usefilter)
2171 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2172
2173 /*
2174 * Check for '|' to separate commands and '"' to start comments.
2175 * Don't do this for ":read !cmd" and ":write !cmd".
2176 */
2177 if ((ea.argt & TRLBAR) && !ea.usefilter)
2178 separate_nextcmd(&ea);
2179
2180 /*
2181 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002182 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2183 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002185 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002186 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002187 || ea.cmdidx == CMD_global
2188 || ea.cmdidx == CMD_vglobal
2189 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
2191 for (p = ea.arg; *p; ++p)
2192 {
2193 /* Remove one backslash before a newline, so that it's possible to
2194 * pass a newline to the shell and also a newline that is preceded
2195 * with a backslash. This makes it impossible to end a shell
2196 * command in a backslash, but that doesn't appear useful.
2197 * Halving the number of backslashes is incompatible with previous
2198 * versions. */
2199 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002200 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 else if (*p == '\n')
2202 {
2203 ea.nextcmd = p + 1;
2204 *p = NUL;
2205 break;
2206 }
2207 }
2208 }
2209
2210 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2211 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002212 buf_T *buf;
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002215 switch (ea.addr_type)
2216 {
2217 case ADDR_LINES:
2218 ea.line2 = curbuf->b_ml.ml_line_count;
2219 break;
2220 case ADDR_LOADED_BUFFERS:
2221 buf = firstbuf;
2222 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2223 buf = buf->b_next;
2224 ea.line1 = buf->b_fnum;
2225 buf = lastbuf;
2226 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2227 buf = buf->b_prev;
2228 ea.line2 = buf->b_fnum;
2229 break;
2230 case ADDR_BUFFERS:
2231 ea.line1 = firstbuf->b_fnum;
2232 ea.line2 = lastbuf->b_fnum;
2233 break;
2234 case ADDR_WINDOWS:
2235 ea.line2 = LAST_WIN_NR;
2236 break;
2237 case ADDR_TABS:
2238 ea.line2 = LAST_TAB_NR;
2239 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002240 case ADDR_TABS_RELATIVE:
2241 ea.line2 = 1;
2242 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002243 case ADDR_ARGUMENTS:
2244 if (ARGCOUNT == 0)
2245 ea.line1 = ea.line2 = 0;
2246 else
2247 ea.line2 = ARGCOUNT;
2248 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002249#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002250 case ADDR_QUICKFIX:
2251 ea.line2 = qf_get_size(&ea);
2252 if (ea.line2 == 0)
2253 ea.line2 = 1;
2254 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002255#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
2259 /* accept numbered register only when no count allowed (:put) */
2260 if ( (ea.argt & REGSTR)
2261 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002262 /* Do not allow register = for user commands */
2263 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2265 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002266#ifndef FEAT_CLIPBOARD
2267 /* check these explicitly for a more specific error message */
2268 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002270 errormsg = (char_u *)_(e_invalidreg);
2271 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002274 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2275 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002276 {
2277 ea.regname = *ea.arg++;
2278#ifdef FEAT_EVAL
2279 /* for '=' register: accept the rest of the line as an expression */
2280 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2281 {
2282 set_expr_line(vim_strsave(ea.arg));
2283 ea.arg += STRLEN(ea.arg);
2284 }
2285#endif
2286 ea.arg = skipwhite(ea.arg);
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289
2290 /*
2291 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2292 * count, it's a buffer name.
2293 */
2294 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2295 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002296 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 n = getdigits(&ea.arg);
2299 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002300 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 errormsg = (char_u *)_(e_zerocount);
2303 goto doend;
2304 }
2305 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2306 {
2307 ea.line2 = n;
2308 if (ea.addr_count == 0)
2309 ea.addr_count = 1;
2310 }
2311 else
2312 {
2313 ea.line1 = ea.line2;
2314 ea.line2 += n - 1;
2315 ++ea.addr_count;
2316 /*
2317 * Be vi compatible: no error message for out of range.
2318 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002319 if (ea.addr_type == ADDR_LINES
2320 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 ea.line2 = curbuf->b_ml.ml_line_count;
2322 }
2323 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002324
2325 /*
2326 * Check for flags: 'l', 'p' and '#'.
2327 */
2328 if (ea.argt & EXFLAGS)
2329 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002331 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002332 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
2334 errormsg = (char_u *)_(e_trailing);
2335 goto doend;
2336 }
2337
2338 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2339 {
2340 errormsg = (char_u *)_(e_argreq);
2341 goto doend;
2342 }
2343
2344#ifdef FEAT_EVAL
2345 /*
2346 * Skip the command when it's not going to be executed.
2347 * The commands like :if, :endif, etc. always need to be executed.
2348 * Also make an exception for commands that handle a trailing command
2349 * themselves.
2350 */
2351 if (ea.skip)
2352 {
2353 switch (ea.cmdidx)
2354 {
2355 /* commands that need evaluation */
2356 case CMD_while:
2357 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002358 case CMD_for:
2359 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 case CMD_if:
2361 case CMD_elseif:
2362 case CMD_else:
2363 case CMD_endif:
2364 case CMD_try:
2365 case CMD_catch:
2366 case CMD_finally:
2367 case CMD_endtry:
2368 case CMD_function:
2369 break;
2370
2371 /* Commands that handle '|' themselves. Check: A command should
2372 * either have the TRLBAR flag, appear in this list or appear in
2373 * the list at ":help :bar". */
2374 case CMD_aboveleft:
2375 case CMD_and:
2376 case CMD_belowright:
2377 case CMD_botright:
2378 case CMD_browse:
2379 case CMD_call:
2380 case CMD_confirm:
2381 case CMD_delfunction:
2382 case CMD_djump:
2383 case CMD_dlist:
2384 case CMD_dsearch:
2385 case CMD_dsplit:
2386 case CMD_echo:
2387 case CMD_echoerr:
2388 case CMD_echomsg:
2389 case CMD_echon:
2390 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002391 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 case CMD_help:
2393 case CMD_hide:
2394 case CMD_ijump:
2395 case CMD_ilist:
2396 case CMD_isearch:
2397 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002398 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 case CMD_keepjumps:
2400 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002401 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 case CMD_leftabove:
2403 case CMD_let:
2404 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002405 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002407 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002408 case CMD_noautocmd:
2409 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_perl:
2411 case CMD_psearch:
2412 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002413 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002414 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_return:
2416 case CMD_rightbelow:
2417 case CMD_ruby:
2418 case CMD_silent:
2419 case CMD_smagic:
2420 case CMD_snomagic:
2421 case CMD_substitute:
2422 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002423 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_tcl:
2425 case CMD_throw:
2426 case CMD_tilde:
2427 case CMD_topleft:
2428 case CMD_unlet:
2429 case CMD_verbose:
2430 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002431 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 break;
2433
2434 default: goto doend;
2435 }
2436 }
2437#endif
2438
2439 if (ea.argt & XFILE)
2440 {
2441 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2442 goto doend;
2443 }
2444
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 /*
2446 * Accept buffer name. Cannot be used at the same time with a buffer
2447 * number. Don't do this for a user command.
2448 */
2449 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002450 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
2452 /*
2453 * :bdelete, :bwipeout and :bunload take several arguments, separated
2454 * by spaces: find next space (skipping over escaped characters).
2455 * The others take one argument: ignore trailing spaces.
2456 */
2457 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2458 || ea.cmdidx == CMD_bunload)
2459 p = skiptowhite_esc(ea.arg);
2460 else
2461 {
2462 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002463 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 --p;
2465 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002466 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2467 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (ea.line2 < 0) /* failed */
2469 goto doend;
2470 ea.addr_count = 1;
2471 ea.arg = skipwhite(p);
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
Bram Moolenaar2be57332018-02-13 18:05:18 +01002474 /* The :try command saves the emsg_silent flag, reset it here when
2475 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002476 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002477 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002478 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002479 if (emsg_silent < 0)
2480 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002481 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002482 }
2483
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002485 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002489 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 /*
2492 * Execute a user-defined command.
2493 */
2494 do_ucmd(&ea);
2495 }
2496 else
2497#endif
2498 {
2499 /*
2500 * Call the function to execute the command.
2501 */
2502 ea.errmsg = NULL;
2503 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2504 if (ea.errmsg != NULL)
2505 errormsg = (char_u *)_(ea.errmsg);
2506 }
2507
2508#ifdef FEAT_EVAL
2509 /*
2510 * If the command just executed called do_cmdline(), any throw or ":return"
2511 * or ":finish" encountered there must also check the cstack of the still
2512 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2513 * exception, or reanimate a returned function or finished script file and
2514 * return or finish it again.
2515 */
2516 if (need_rethrow)
2517 do_throw(cstack);
2518 else if (check_cstack)
2519 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002520 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002522 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 && current_func_returned())
2524 do_return(&ea, TRUE, FALSE, NULL);
2525 }
2526 need_rethrow = check_cstack = FALSE;
2527#endif
2528
2529doend:
2530 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002533 curwin->w_cursor.col = 0;
2534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2537 {
2538 if (sourcing)
2539 {
2540 if (errormsg != IObuff)
2541 {
2542 STRCPY(IObuff, errormsg);
2543 errormsg = IObuff;
2544 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002545 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547 emsg(errormsg);
2548 }
2549#ifdef FEAT_EVAL
2550 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002551 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2552 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#endif
2554
Bram Moolenaareffed932018-08-14 13:38:17 +02002555 if (ea.verbose_save >= 0)
2556 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002557
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002558 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 cmdmod = save_cmdmod;
2560
Bram Moolenaareffed932018-08-14 13:38:17 +02002561 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 /* messages could be enabled for a serious error, need to check if the
2564 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002565 if (!did_emsg || msg_silent > ea.save_msg_silent)
2566 msg_silent = ea.save_msg_silent;
2567 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (emsg_silent < 0)
2569 emsg_silent = 0;
2570 /* Restore msg_scroll, it's set by file I/O commands, even when no
2571 * message is actually displayed. */
2572 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002573
2574 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2575 * somewhere in the line. Put it back in the first column. */
2576 if (redirecting())
2577 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002580#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002581 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002582 --sandbox;
2583#endif
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2586 ea.nextcmd = NULL;
2587
2588#ifdef FEAT_EVAL
2589 --ex_nesting_level;
2590#endif
2591
2592 return ea.nextcmd;
2593}
2594#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002595 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596#endif
2597
2598/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 * Parse and skip over command modifiers:
2600 * - update eap->cmd
2601 * - store flags in "cmdmod".
2602 * - Set ex_pressedreturn for an empty command line.
2603 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002604 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 * - set p_verbose for ":verbose"
2606 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002607 * When "skip_only" is TRUE the global variables are not changed, except for
2608 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002609 * Return FAIL when the command is not to be executed.
2610 * May set "errormsg" to an error message.
2611 */
2612 int
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002613parse_command_modifiers(exarg_T *eap, char_u **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002614{
2615 char_u *p;
2616
2617 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2618 eap->verbose_save = -1;
2619 eap->save_msg_silent = -1;
2620
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002621 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 for (;;)
2623 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002624 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2625 ++eap->cmd;
2626
2627 /* in ex mode, an empty line works like :+ */
2628 if (*eap->cmd == NUL && exmode_active
2629 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2630 || getline_equal(eap->getline, eap->cookie, getexline))
2631 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2632 {
2633 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002634 if (!skip_only)
2635 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 }
2637
2638 /* ignore comment and empty lines */
2639 if (*eap->cmd == '"')
2640 return FAIL;
2641 if (*eap->cmd == NUL)
2642 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002643 if (!skip_only)
2644 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 return FAIL;
2646 }
2647
Bram Moolenaareffed932018-08-14 13:38:17 +02002648 p = skip_range(eap->cmd, NULL);
2649 switch (*p)
2650 {
2651 /* When adding an entry, also modify cmd_exists(). */
2652 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2653 break;
2654 cmdmod.split |= WSP_ABOVE;
2655 continue;
2656
2657 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2658 {
2659 cmdmod.split |= WSP_BELOW;
2660 continue;
2661 }
2662 if (checkforcmd(&eap->cmd, "browse", 3))
2663 {
2664#ifdef FEAT_BROWSE_CMD
2665 cmdmod.browse = TRUE;
2666#endif
2667 continue;
2668 }
2669 if (!checkforcmd(&eap->cmd, "botright", 2))
2670 break;
2671 cmdmod.split |= WSP_BOT;
2672 continue;
2673
2674 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2675 break;
2676#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2677 cmdmod.confirm = TRUE;
2678#endif
2679 continue;
2680
2681 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2682 {
2683 cmdmod.keepmarks = TRUE;
2684 continue;
2685 }
2686 if (checkforcmd(&eap->cmd, "keepalt", 5))
2687 {
2688 cmdmod.keepalt = TRUE;
2689 continue;
2690 }
2691 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2692 {
2693 cmdmod.keeppatterns = TRUE;
2694 continue;
2695 }
2696 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2697 break;
2698 cmdmod.keepjumps = TRUE;
2699 continue;
2700
2701 case 'f': /* only accept ":filter {pat} cmd" */
2702 {
2703 char_u *reg_pat;
2704
2705 if (!checkforcmd(&p, "filter", 4)
2706 || *p == NUL || ends_excmd(*p))
2707 break;
2708 if (*p == '!')
2709 {
2710 cmdmod.filter_force = TRUE;
2711 p = skipwhite(p + 1);
2712 if (*p == NUL || ends_excmd(*p))
2713 break;
2714 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002715 if (skip_only)
2716 p = skip_vimgrep_pat(p, NULL, NULL);
2717 else
2718 // NOTE: This puts a NUL after the pattern.
2719 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 if (p == NULL || *p == NUL)
2721 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002722 if (!skip_only)
2723 {
2724 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002725 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002726 if (cmdmod.filter_regmatch.regprog == NULL)
2727 break;
2728 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002729 eap->cmd = p;
2730 continue;
2731 }
2732
2733 /* ":hide" and ":hide | cmd" are not modifiers */
2734 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2735 || *p == NUL || ends_excmd(*p))
2736 break;
2737 eap->cmd = p;
2738 cmdmod.hide = TRUE;
2739 continue;
2740
2741 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2742 {
2743 cmdmod.lockmarks = TRUE;
2744 continue;
2745 }
2746
2747 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2748 break;
2749 cmdmod.split |= WSP_ABOVE;
2750 continue;
2751
2752 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2753 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002754 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002755 {
2756 /* Set 'eventignore' to "all". Restore the
2757 * existing option value later. */
2758 cmdmod.save_ei = vim_strsave(p_ei);
2759 set_string_option_direct((char_u *)"ei", -1,
2760 (char_u *)"all", OPT_FREE, SID_NONE);
2761 }
2762 continue;
2763 }
2764 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2765 break;
2766 cmdmod.noswapfile = TRUE;
2767 continue;
2768
2769 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2770 break;
2771 cmdmod.split |= WSP_BELOW;
2772 continue;
2773
2774 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2775 {
2776#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002777 if (!skip_only)
2778 {
2779 if (!eap->did_sandbox)
2780 ++sandbox;
2781 eap->did_sandbox = TRUE;
2782 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002783#endif
2784 continue;
2785 }
2786 if (!checkforcmd(&eap->cmd, "silent", 3))
2787 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002788 if (!skip_only)
2789 {
2790 if (eap->save_msg_silent == -1)
2791 eap->save_msg_silent = msg_silent;
2792 ++msg_silent;
2793 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2795 {
2796 /* ":silent!", but not "silent !cmd" */
2797 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002798 if (!skip_only)
2799 {
2800 ++emsg_silent;
2801 ++eap->did_esilent;
2802 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 }
2804 continue;
2805
2806 case 't': if (checkforcmd(&p, "tab", 3))
2807 {
2808 long tabnr = get_address(eap, &eap->cmd, ADDR_TABS,
2809 eap->skip, FALSE, 1);
2810 if (tabnr == MAXLNUM)
2811 cmdmod.tab = tabpage_index(curtab) + 1;
2812 else
2813 {
2814 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2815 {
2816 *errormsg = (char_u *)_(e_invrange);
2817 return FAIL;
2818 }
2819 cmdmod.tab = tabnr + 1;
2820 }
2821 eap->cmd = p;
2822 continue;
2823 }
2824 if (!checkforcmd(&eap->cmd, "topleft", 2))
2825 break;
2826 cmdmod.split |= WSP_TOP;
2827 continue;
2828
2829 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2830 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 {
2833 if (eap->save_msg_silent == -1)
2834 eap->save_msg_silent = msg_silent;
2835 msg_silent = 0;
2836 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 continue;
2838
2839 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2840 {
2841 cmdmod.split |= WSP_VERT;
2842 continue;
2843 }
2844 if (!checkforcmd(&p, "verbose", 4))
2845 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002846 if (!skip_only)
2847 {
2848 if (eap->verbose_save < 0)
2849 eap->verbose_save = p_verbose;
2850 if (vim_isdigit(*eap->cmd))
2851 p_verbose = atoi((char *)eap->cmd);
2852 else
2853 p_verbose = 1;
2854 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 eap->cmd = p;
2856 continue;
2857 }
2858 break;
2859 }
2860
2861 return OK;
2862}
2863
2864/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002865 * Free contents of "cmdmod".
2866 */
2867 static void
2868free_cmdmod(void)
2869{
2870 if (cmdmod.save_ei != NULL)
2871 {
2872 /* Restore 'eventignore' to the value before ":noautocmd". */
2873 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2874 OPT_FREE, SID_NONE);
2875 free_string_option(cmdmod.save_ei);
2876 }
2877
2878 if (cmdmod.filter_regmatch.regprog != NULL)
2879 vim_regfree(cmdmod.filter_regmatch.regprog);
2880}
2881
2882/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002883 * Parse the address range, if any, in "eap".
2884 * Return FAIL and set "errormsg" or return OK.
2885 */
2886 int
2887parse_cmd_address(exarg_T *eap, char_u **errormsg)
2888{
2889 int address_count = 1;
2890 linenr_T lnum;
2891
2892 // Repeat for all ',' or ';' separated addresses.
2893 for (;;)
2894 {
2895 eap->line1 = eap->line2;
2896 switch (eap->addr_type)
2897 {
2898 case ADDR_LINES:
2899 // default is current line number
2900 eap->line2 = curwin->w_cursor.lnum;
2901 break;
2902 case ADDR_WINDOWS:
2903 eap->line2 = CURRENT_WIN_NR;
2904 break;
2905 case ADDR_ARGUMENTS:
2906 eap->line2 = curwin->w_arg_idx + 1;
2907 if (eap->line2 > ARGCOUNT)
2908 eap->line2 = ARGCOUNT;
2909 break;
2910 case ADDR_LOADED_BUFFERS:
2911 case ADDR_BUFFERS:
2912 eap->line2 = curbuf->b_fnum;
2913 break;
2914 case ADDR_TABS:
2915 eap->line2 = CURRENT_TAB_NR;
2916 break;
2917 case ADDR_TABS_RELATIVE:
2918 eap->line2 = 1;
2919 break;
2920#ifdef FEAT_QUICKFIX
2921 case ADDR_QUICKFIX:
2922 eap->line2 = qf_get_cur_valid_idx(eap);
2923 break;
2924#endif
2925 }
2926 eap->cmd = skipwhite(eap->cmd);
2927 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip,
2928 eap->addr_count == 0, address_count++);
2929 if (eap->cmd == NULL) // error detected
2930 return FAIL;
2931 if (lnum == MAXLNUM)
2932 {
2933 if (*eap->cmd == '%') // '%' - all lines
2934 {
2935 ++eap->cmd;
2936 switch (eap->addr_type)
2937 {
2938 case ADDR_LINES:
2939 eap->line1 = 1;
2940 eap->line2 = curbuf->b_ml.ml_line_count;
2941 break;
2942 case ADDR_LOADED_BUFFERS:
2943 {
2944 buf_T *buf = firstbuf;
2945
2946 while (buf->b_next != NULL
2947 && buf->b_ml.ml_mfp == NULL)
2948 buf = buf->b_next;
2949 eap->line1 = buf->b_fnum;
2950 buf = lastbuf;
2951 while (buf->b_prev != NULL
2952 && buf->b_ml.ml_mfp == NULL)
2953 buf = buf->b_prev;
2954 eap->line2 = buf->b_fnum;
2955 break;
2956 }
2957 case ADDR_BUFFERS:
2958 eap->line1 = firstbuf->b_fnum;
2959 eap->line2 = lastbuf->b_fnum;
2960 break;
2961 case ADDR_WINDOWS:
2962 case ADDR_TABS:
2963 if (IS_USER_CMDIDX(eap->cmdidx))
2964 {
2965 eap->line1 = 1;
2966 eap->line2 = eap->addr_type == ADDR_WINDOWS
2967 ? LAST_WIN_NR : LAST_TAB_NR;
2968 }
2969 else
2970 {
2971 // there is no Vim command which uses '%' and
2972 // ADDR_WINDOWS or ADDR_TABS
2973 *errormsg = (char_u *)_(e_invrange);
2974 return FAIL;
2975 }
2976 break;
2977 case ADDR_TABS_RELATIVE:
2978 *errormsg = (char_u *)_(e_invrange);
2979 return FAIL;
2980 case ADDR_ARGUMENTS:
2981 if (ARGCOUNT == 0)
2982 eap->line1 = eap->line2 = 0;
2983 else
2984 {
2985 eap->line1 = 1;
2986 eap->line2 = ARGCOUNT;
2987 }
2988 break;
2989#ifdef FEAT_QUICKFIX
2990 case ADDR_QUICKFIX:
2991 eap->line1 = 1;
2992 eap->line2 = qf_get_size(eap);
2993 if (eap->line2 == 0)
2994 eap->line2 = 1;
2995 break;
2996#endif
2997 }
2998 ++eap->addr_count;
2999 }
3000 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3001 {
3002 pos_T *fp;
3003
3004 // '*' - visual area
3005 if (eap->addr_type != ADDR_LINES)
3006 {
3007 *errormsg = (char_u *)_(e_invrange);
3008 return FAIL;
3009 }
3010
3011 ++eap->cmd;
3012 if (!eap->skip)
3013 {
3014 fp = getmark('<', FALSE);
3015 if (check_mark(fp) == FAIL)
3016 return FAIL;
3017 eap->line1 = fp->lnum;
3018 fp = getmark('>', FALSE);
3019 if (check_mark(fp) == FAIL)
3020 return FAIL;
3021 eap->line2 = fp->lnum;
3022 ++eap->addr_count;
3023 }
3024 }
3025 }
3026 else
3027 eap->line2 = lnum;
3028 eap->addr_count++;
3029
3030 if (*eap->cmd == ';')
3031 {
3032 if (!eap->skip)
3033 {
3034 curwin->w_cursor.lnum = eap->line2;
3035 // don't leave the cursor on an illegal line or column
3036 check_cursor();
3037 }
3038 }
3039 else if (*eap->cmd != ',')
3040 break;
3041 ++eap->cmd;
3042 }
3043
3044 // One address given: set start and end lines.
3045 if (eap->addr_count == 1)
3046 {
3047 eap->line1 = eap->line2;
3048 // ... but only implicit: really no address given
3049 if (lnum == MAXLNUM)
3050 eap->addr_count = 0;
3051 }
3052 return OK;
3053}
3054
3055/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003056 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 * If there is a match advance "pp" to the argument and return TRUE.
3058 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003059 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003060checkforcmd(
3061 char_u **pp, /* start of command */
3062 char *cmd, /* name of command */
3063 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 int i;
3066
3067 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003068 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 break;
3070 if (i >= len && !isalpha((*pp)[i]))
3071 {
3072 *pp = skipwhite(*pp + i);
3073 return TRUE;
3074 }
3075 return FALSE;
3076}
3077
3078/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003079 * Append "cmd" to the error message in IObuff.
3080 * Takes care of limiting the length and handling 0xa0, which would be
3081 * invisible otherwise.
3082 */
3083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003084append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003085{
3086 char_u *s = cmd;
3087 char_u *d;
3088
3089 STRCAT(IObuff, ": ");
3090 d = IObuff + STRLEN(IObuff);
3091 while (*s != NUL && d - IObuff < IOSIZE - 7)
3092 {
3093 if (
3094#ifdef FEAT_MBYTE
3095 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3096#endif
3097 *s == 0xa0)
3098 {
3099 s +=
3100#ifdef FEAT_MBYTE
3101 enc_utf8 ? 2 :
3102#endif
3103 1;
3104 STRCPY(d, "<a0>");
3105 d += 4;
3106 }
3107 else
3108 MB_COPY_CHAR(s, d);
3109 }
3110 *d = NUL;
3111}
3112
3113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003115 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003117 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * Returns NULL for an ambiguous user command.
3119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003121find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 int len;
3124 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003125 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 /*
3128 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003129 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 * - the 'k' command can directly be followed by any character.
3131 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003132 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003134 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 */
3136 p = eap->cmd;
3137 if (*p == 'k')
3138 {
3139 eap->cmdidx = CMD_k;
3140 ++p;
3141 }
3142 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003143 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3144 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 || p[1] == 'g'
3146 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3147 || p[1] == 'I'
3148 || (p[1] == 'r' && p[2] != 'e')))
3149 {
3150 eap->cmdidx = CMD_substitute;
3151 ++p;
3152 }
3153 else
3154 {
3155 while (ASCII_ISALPHA(*p))
3156 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003157 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003158 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003159 while (ASCII_ISALNUM(*p))
3160 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 /* check for non-alpha command */
3163 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3164 ++p;
3165 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003166 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3167 {
3168 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3169 * :delete with the 'l' flag. Same for 'p'. */
3170 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003171 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003172 break;
3173 if (i == len - 1)
3174 {
3175 --len;
3176 if (p[-1] == 'l')
3177 eap->flags |= EXFLAG_LIST;
3178 else
3179 eap->flags |= EXFLAG_PRINT;
3180 }
3181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003183 if (ASCII_ISLOWER(eap->cmd[0]))
3184 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003185 int c1 = eap->cmd[0];
3186 int c2 = eap->cmd[1];
3187
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003188 if (command_count != (int)CMD_SIZE)
3189 {
3190 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3191 getout(1);
3192 }
3193
3194 /* Use a precomputed index for fast look-up in cmdnames[]
3195 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003196 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3197 if (ASCII_ISLOWER(c2))
3198 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003201 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3204 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3205 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3206 (size_t)len) == 0)
3207 {
3208#ifdef FEAT_EVAL
3209 if (full != NULL
3210 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3211 *full = TRUE;
3212#endif
3213 break;
3214 }
3215
3216#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003217 /* Look for a user defined command as a last resort. Let ":Print" be
3218 * overruled by a user defined command. */
3219 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3220 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003222 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 while (ASCII_ISALNUM(*p))
3224 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003225 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003228 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 eap->cmdidx = CMD_SIZE;
3230 }
3231
3232 return p;
3233}
3234
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235#ifdef FEAT_USR_CMDS
3236/*
3237 * Search for a user command that matches "eap->cmd".
3238 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3239 * Return a pointer to just after the command.
3240 * Return NULL if there is no matching command.
3241 */
3242 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003243find_ucmd(
3244 exarg_T *eap,
3245 char_u *p, /* end of the command (possibly including count) */
3246 int *full, /* set to TRUE for a full match */
3247 expand_T *xp, /* used for completion, NULL otherwise */
3248 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003249{
3250 int len = (int)(p - eap->cmd);
3251 int j, k, matchlen = 0;
3252 ucmd_T *uc;
3253 int found = FALSE;
3254 int possible = FALSE;
3255 char_u *cp, *np; /* Point into typed cmd and test name */
3256 garray_T *gap;
3257 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3258 only full match global is accepted. */
3259
3260 /*
3261 * Look for buffer-local user commands first, then global ones.
3262 */
3263 gap = &curbuf->b_ucmds;
3264 for (;;)
3265 {
3266 for (j = 0; j < gap->ga_len; ++j)
3267 {
3268 uc = USER_CMD_GA(gap, j);
3269 cp = eap->cmd;
3270 np = uc->uc_name;
3271 k = 0;
3272 while (k < len && *np != NUL && *cp++ == *np++)
3273 k++;
3274 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3275 {
3276 /* If finding a second match, the command is ambiguous. But
3277 * not if a buffer-local command wasn't a full match and a
3278 * global command is a full match. */
3279 if (k == len && found && *np != NUL)
3280 {
3281 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003282 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003283 amb_local = TRUE;
3284 }
3285
3286 if (!found || (k == len && *np == NUL))
3287 {
3288 /* If we matched up to a digit, then there could
3289 * be another command including the digit that we
3290 * should use instead.
3291 */
3292 if (k == len)
3293 found = TRUE;
3294 else
3295 possible = TRUE;
3296
3297 if (gap == &ucmds)
3298 eap->cmdidx = CMD_USER;
3299 else
3300 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003301 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003302 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003303 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003304
3305# ifdef FEAT_CMDL_COMPL
3306 if (compl != NULL)
3307 *compl = uc->uc_compl;
3308# ifdef FEAT_EVAL
3309 if (xp != NULL)
3310 {
3311 xp->xp_arg = uc->uc_compl_arg;
3312 xp->xp_scriptID = uc->uc_scriptID;
3313 }
3314# endif
3315# endif
3316 /* Do not search for further abbreviations
3317 * if this is an exact match. */
3318 matchlen = k;
3319 if (k == len && *np == NUL)
3320 {
3321 if (full != NULL)
3322 *full = TRUE;
3323 amb_local = FALSE;
3324 break;
3325 }
3326 }
3327 }
3328 }
3329
3330 /* Stop if we found a full match or searched all. */
3331 if (j < gap->ga_len || gap == &ucmds)
3332 break;
3333 gap = &ucmds;
3334 }
3335
3336 /* Only found ambiguous matches. */
3337 if (amb_local)
3338 {
3339 if (xp != NULL)
3340 xp->xp_context = EXPAND_UNSUCCESSFUL;
3341 return NULL;
3342 }
3343
3344 /* The match we found may be followed immediately by a number. Move "p"
3345 * back to point to it. */
3346 if (found || possible)
3347 return p + (matchlen - len);
3348 return p;
3349}
3350#endif
3351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003353static struct cmdmod
3354{
3355 char *name;
3356 int minlen;
3357 int has_count; /* :123verbose :3tab */
3358} cmdmods[] = {
3359 {"aboveleft", 3, FALSE},
3360 {"belowright", 3, FALSE},
3361 {"botright", 2, FALSE},
3362 {"browse", 3, FALSE},
3363 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003364 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003365 {"hide", 3, FALSE},
3366 {"keepalt", 5, FALSE},
3367 {"keepjumps", 5, FALSE},
3368 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003369 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003370 {"leftabove", 5, FALSE},
3371 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003372 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003373 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374 {"rightbelow", 6, FALSE},
3375 {"sandbox", 3, FALSE},
3376 {"silent", 3, FALSE},
3377 {"tab", 3, TRUE},
3378 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003379 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003380 {"verbose", 4, TRUE},
3381 {"vertical", 4, FALSE},
3382};
3383
3384/*
3385 * Return length of a command modifier (including optional count).
3386 * Return zero when it's not a modifier.
3387 */
3388 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003389modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003390{
3391 int i, j;
3392 char_u *p = cmd;
3393
3394 if (VIM_ISDIGIT(*cmd))
3395 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003396 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 {
3398 for (j = 0; p[j] != NUL; ++j)
3399 if (p[j] != cmdmods[i].name[j])
3400 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003401 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003402 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003403 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003404 }
3405 return 0;
3406}
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408/*
3409 * Return > 0 if an Ex command "name" exists.
3410 * Return 2 if there is an exact match.
3411 * Return 3 if there is an ambiguous match.
3412 */
3413 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003414cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 exarg_T ea;
3417 int full = FALSE;
3418 int i;
3419 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003420 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003423 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 for (j = 0; name[j] != NUL; ++j)
3426 if (name[j] != cmdmods[i].name[j])
3427 break;
3428 if (name[j] == NUL && j >= cmdmods[i].minlen)
3429 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3430 }
3431
Bram Moolenaara9587612006-05-04 21:47:50 +00003432 /* Check built-in commands and user defined commands.
3433 * For ":2match" and ":3match" we need to skip the number. */
3434 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003436 p = find_command(&ea, &full);
3437 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003439 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3440 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003441 if (*skipwhite(p) != NUL)
3442 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3444}
3445#endif
3446
3447/*
3448 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3449 * we don't need/want deleted. Maybe this could be done better if we didn't
3450 * repeat all this stuff. The only problem is that they may not stay
3451 * perfectly compatible with each other, but then the command line syntax
3452 * probably won't change that much -- webb.
3453 */
3454 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003455set_one_cmd_context(
3456 expand_T *xp,
3457 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 char_u *p;
3460 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003461 int len = 0;
3462 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3464 int compl = EXPAND_NOTHING;
3465#endif
3466#ifdef FEAT_CMDL_COMPL
3467 int delim;
3468#endif
3469 int forceit = FALSE;
3470 int usefilter = FALSE; /* filter instead of file name */
3471
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003472 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 xp->xp_pattern = buff;
3474 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003478 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 */
3480 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3481 ;
3482 xp->xp_pattern = cmd;
3483
3484 if (*cmd == NUL)
3485 return NULL;
3486 if (*cmd == '"') /* ignore comment lines */
3487 {
3488 xp->xp_context = EXPAND_NOTHING;
3489 return NULL;
3490 }
3491
3492/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003493 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 */
3495 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 xp->xp_pattern = cmd;
3497 if (*cmd == NUL)
3498 return NULL;
3499 if (*cmd == '"')
3500 {
3501 xp->xp_context = EXPAND_NOTHING;
3502 return NULL;
3503 }
3504
3505 if (*cmd == '|' || *cmd == '\n')
3506 return cmd + 1; /* There's another command */
3507
3508 /*
3509 * Isolate the command and search for it in the command table.
3510 * Exceptions:
3511 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003512 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3514 */
3515 if (*cmd == 'k' && cmd[1] != 'e')
3516 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003517 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 p = cmd + 1;
3519 }
3520 else
3521 {
3522 p = cmd;
3523 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3524 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003525 /* a user command may contain digits */
3526 if (ASCII_ISUPPER(cmd[0]))
3527 while (ASCII_ISALNUM(*p) || *p == '*')
3528 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003529 /* for python 3.x: ":py3*" commands completion */
3530 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003531 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003532 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003533 while (ASCII_ISALPHA(*p) || *p == '*')
3534 ++p;
3535 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003536 /* check for non-alpha command */
3537 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3538 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
3543 xp->xp_context = EXPAND_UNSUCCESSFUL;
3544 return NULL;
3545 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003547 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3548 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3549 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 break;
3551
3552#ifdef FEAT_USR_CMDS
3553 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3555 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
3557 }
3558
3559 /*
3560 * If the cursor is touching the command, and it ends in an alpha-numeric
3561 * character, complete the command name.
3562 */
3563 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3564 return NULL;
3565
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003566 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3569 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 p = cmd + 1;
3572 }
3573#ifdef FEAT_USR_CMDS
3574 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3575 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576 ea.cmd = cmd;
3577 p = find_ucmd(&ea, p, NULL, xp,
3578# if defined(FEAT_CMDL_COMPL)
3579 &compl
3580# else
3581 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003584 if (p == NULL)
3585 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587#endif
3588 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003589 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 /* Not still touching the command and it was an illegal one */
3592 xp->xp_context = EXPAND_UNSUCCESSFUL;
3593 return NULL;
3594 }
3595
3596 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3597
3598 if (*p == '!') /* forced commands */
3599 {
3600 forceit = TRUE;
3601 ++p;
3602 }
3603
3604/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003605 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003607 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003608 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 arg = skipwhite(p);
3611
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 if (*arg == '>') /* append */
3615 {
3616 if (*++arg == '>')
3617 ++arg;
3618 arg = skipwhite(arg);
3619 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003620 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 ++arg;
3623 usefilter = TRUE;
3624 }
3625 }
3626
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003627 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629 usefilter = forceit; /* :r! filter if forced */
3630 if (*arg == '!') /* :r !filter */
3631 {
3632 ++arg;
3633 usefilter = TRUE;
3634 }
3635 }
3636
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
3639 while (*arg == *cmd) /* allow any number of '>' or '<' */
3640 ++arg;
3641 arg = skipwhite(arg);
3642 }
3643
3644 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003645 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 /* Check if we're in the +command */
3648 p = arg + 1;
3649 arg = skip_cmd_arg(arg, FALSE);
3650
3651 /* Still touching the command after '+'? */
3652 if (*arg == NUL)
3653 return p;
3654
3655 /* Skip space(s) after +command to get to the real argument */
3656 arg = skipwhite(arg);
3657 }
3658
3659 /*
3660 * Check for '|' to separate commands and '"' to start comments.
3661 * Don't do this for ":read !cmd" and ":write !cmd".
3662 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003663 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
3665 p = arg;
3666 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003667 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 p += 2;
3669 while (*p)
3670 {
3671 if (*p == Ctrl_V)
3672 {
3673 if (p[1] != NUL)
3674 ++p;
3675 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003676 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 || *p == '|' || *p == '\n')
3678 {
3679 if (*(p - 1) != '\\')
3680 {
3681 if (*p == '|' || *p == '\n')
3682 return p + 1;
3683 return NULL; /* It's a comment */
3684 }
3685 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003686 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688 }
3689
3690 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003691 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 vim_strchr((char_u *)"|\"", *arg) == NULL)
3693 return NULL;
3694
3695 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003696 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003698 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003699 while (*p && p < buff + len)
3700 {
3701 if (*p == ' ' || *p == TAB)
3702 {
3703 /* argument starts after a space */
3704 xp->xp_pattern = ++p;
3705 }
3706 else
3707 {
3708 if (*p == '\\' && *(p + 1) != NUL)
3709 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003710 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003711 }
3712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003714 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003716 int c;
3717 int in_quote = FALSE;
3718 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /*
3721 * Allow spaces within back-quotes to count as part of the argument
3722 * being expanded.
3723 */
3724 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003725 p = xp->xp_pattern;
3726 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003728#ifdef FEAT_MBYTE
3729 if (has_mbyte)
3730 c = mb_ptr2char(p);
3731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003733 c = *p;
3734 if (c == '\\' && p[1] != NUL)
3735 ++p;
3736 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
3738 if (!in_quote)
3739 {
3740 xp->xp_pattern = p;
3741 bow = p + 1;
3742 }
3743 in_quote = !in_quote;
3744 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003745 /* An argument can contain just about everything, except
3746 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003747 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003748#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003749 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003750#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003751 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003752 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003753 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003754 while (*p != NUL)
3755 {
3756#ifdef FEAT_MBYTE
3757 if (has_mbyte)
3758 c = mb_ptr2char(p);
3759 else
3760#endif
3761 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003762 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003763 break;
3764#ifdef FEAT_MBYTE
3765 if (has_mbyte)
3766 len = (*mb_ptr2len)(p);
3767 else
3768#endif
3769 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003770 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003771 }
3772 if (in_quote)
3773 bow = p;
3774 else
3775 xp->xp_pattern = p;
3776 p -= len;
3777 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003778 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780
3781 /*
3782 * If we are still inside the quotes, and we passed a space, just
3783 * expand from there.
3784 */
3785 if (bow != NULL && in_quote)
3786 xp->xp_pattern = bow;
3787 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003788
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003789 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003790 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003791 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003792#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003793 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003794#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003795 /* When still after the command name expand executables. */
3796 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003797 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 /* Check for environment variable */
3801 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003802#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 || *xp->xp_pattern == '%'
3804#endif
3805 )
3806 {
3807 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3808 if (!vim_isIDc(*p))
3809 break;
3810 if (*p == NUL)
3811 {
3812 xp->xp_context = EXPAND_ENV_VARS;
3813 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003814#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3815 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003816 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003817 compl = EXPAND_ENV_VARS;
3818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003821#if defined(FEAT_CMDL_COMPL)
3822 /* Check for user names */
3823 if (*xp->xp_pattern == '~')
3824 {
3825 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3826 ;
3827 /* Complete ~user only if it partially matches a user name.
3828 * A full match ~user<Tab> will be replaced by user's home
3829 * directory i.e. something like ~user<Tab> -> /home/user/ */
3830 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003831 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003832 {
3833 xp->xp_context = EXPAND_USER;
3834 ++xp->xp_pattern;
3835 }
3836 }
3837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839
3840/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003841 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003843 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003845 case CMD_find:
3846 case CMD_sfind:
3847 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003848 if (xp->xp_context == EXPAND_FILES)
3849 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_cd:
3852 case CMD_chdir:
3853 case CMD_lcd:
3854 case CMD_lchdir:
3855 if (xp->xp_context == EXPAND_FILES)
3856 xp->xp_context = EXPAND_DIRECTORIES;
3857 break;
3858 case CMD_help:
3859 xp->xp_context = EXPAND_HELP;
3860 xp->xp_pattern = arg;
3861 break;
3862
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003863 /* Command modifiers: return the argument.
3864 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003866 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 case CMD_belowright:
3868 case CMD_botright:
3869 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003870 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003871 case CMD_cdo:
3872 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003874 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 case CMD_folddoclosed:
3876 case CMD_folddoopen:
3877 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003878 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case CMD_keepjumps:
3880 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003881 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003882 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003884 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003886 case CMD_noautocmd:
3887 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003889 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003891 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003892 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 case CMD_topleft:
3894 case CMD_verbose:
3895 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003896 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 return arg;
3898
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003899 case CMD_filter:
3900 if (*arg != NUL)
3901 arg = skip_vimgrep_pat(arg, NULL, NULL);
3902 if (arg == NULL || *arg == NUL)
3903 {
3904 xp->xp_context = EXPAND_NOTHING;
3905 return NULL;
3906 }
3907 return skipwhite(arg);
3908
Bram Moolenaar4f688582007-07-24 12:34:30 +00003909#ifdef FEAT_CMDL_COMPL
3910# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 case CMD_match:
3912 if (*arg == NUL || !ends_excmd(*arg))
3913 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003914 /* also complete "None" */
3915 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 arg = skipwhite(skiptowhite(arg));
3917 if (*arg != NUL)
3918 {
3919 xp->xp_context = EXPAND_NOTHING;
3920 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3921 }
3922 }
3923 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926/*
3927 * All completion for the +cmdline_compl feature goes here.
3928 */
3929
3930# ifdef FEAT_USR_CMDS
3931 case CMD_command:
3932 /* Check for attributes */
3933 while (*arg == '-')
3934 {
3935 arg++; /* Skip "-" */
3936 p = skiptowhite(arg);
3937 if (*p == NUL)
3938 {
3939 /* Cursor is still in the attribute */
3940 p = vim_strchr(arg, '=');
3941 if (p == NULL)
3942 {
3943 /* No "=", so complete attribute names */
3944 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3945 xp->xp_pattern = arg;
3946 return NULL;
3947 }
3948
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003949 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 * their arguments as well.
3951 */
3952 if (STRNICMP(arg, "complete", p - arg) == 0)
3953 {
3954 xp->xp_context = EXPAND_USER_COMPLETE;
3955 xp->xp_pattern = p + 1;
3956 return NULL;
3957 }
3958 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3959 {
3960 xp->xp_context = EXPAND_USER_NARGS;
3961 xp->xp_pattern = p + 1;
3962 return NULL;
3963 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003964 else if (STRNICMP(arg, "addr", p - arg) == 0)
3965 {
3966 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3967 xp->xp_pattern = p + 1;
3968 return NULL;
3969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 return NULL;
3971 }
3972 arg = skipwhite(p);
3973 }
3974
3975 /* After the attributes comes the new command name */
3976 p = skiptowhite(arg);
3977 if (*p == NUL)
3978 {
3979 xp->xp_context = EXPAND_USER_COMMANDS;
3980 xp->xp_pattern = arg;
3981 break;
3982 }
3983
3984 /* And finally comes a normal command */
3985 return skipwhite(p);
3986
3987 case CMD_delcommand:
3988 xp->xp_context = EXPAND_USER_COMMANDS;
3989 xp->xp_pattern = arg;
3990 break;
3991# endif
3992
3993 case CMD_global:
3994 case CMD_vglobal:
3995 delim = *arg; /* get the delimiter */
3996 if (delim)
3997 ++arg; /* skip delimiter if there is one */
3998
3999 while (arg[0] != NUL && arg[0] != delim)
4000 {
4001 if (arg[0] == '\\' && arg[1] != NUL)
4002 ++arg;
4003 ++arg;
4004 }
4005 if (arg[0] != NUL)
4006 return arg + 1;
4007 break;
4008 case CMD_and:
4009 case CMD_substitute:
4010 delim = *arg;
4011 if (delim)
4012 {
4013 /* skip "from" part */
4014 ++arg;
4015 arg = skip_regexp(arg, delim, p_magic, NULL);
4016 }
4017 /* skip "to" part */
4018 while (arg[0] != NUL && arg[0] != delim)
4019 {
4020 if (arg[0] == '\\' && arg[1] != NUL)
4021 ++arg;
4022 ++arg;
4023 }
4024 if (arg[0] != NUL) /* skip delimiter */
4025 ++arg;
4026 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4027 ++arg;
4028 if (arg[0] != NUL)
4029 return arg;
4030 break;
4031 case CMD_isearch:
4032 case CMD_dsearch:
4033 case CMD_ilist:
4034 case CMD_dlist:
4035 case CMD_ijump:
4036 case CMD_psearch:
4037 case CMD_djump:
4038 case CMD_isplit:
4039 case CMD_dsplit:
4040 arg = skipwhite(skipdigits(arg)); /* skip count */
4041 if (*arg == '/') /* Match regexp, not just whole words */
4042 {
4043 for (++arg; *arg && *arg != '/'; arg++)
4044 if (*arg == '\\' && arg[1] != NUL)
4045 arg++;
4046 if (*arg)
4047 {
4048 arg = skipwhite(arg + 1);
4049
4050 /* Check for trailing illegal characters */
4051 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4052 xp->xp_context = EXPAND_NOTHING;
4053 else
4054 return arg;
4055 }
4056 }
4057 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 case CMD_autocmd:
4060 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004062 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 case CMD_set:
4065 set_context_in_set_cmd(xp, arg, 0);
4066 break;
4067 case CMD_setglobal:
4068 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4069 break;
4070 case CMD_setlocal:
4071 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4072 break;
4073 case CMD_tag:
4074 case CMD_stag:
4075 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004076 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 case CMD_tselect:
4078 case CMD_stselect:
4079 case CMD_ptselect:
4080 case CMD_tjump:
4081 case CMD_stjump:
4082 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004083 if (*p_wop != NUL)
4084 xp->xp_context = EXPAND_TAGS_LISTFILES;
4085 else
4086 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 xp->xp_pattern = arg;
4088 break;
4089 case CMD_augroup:
4090 xp->xp_context = EXPAND_AUGROUP;
4091 xp->xp_pattern = arg;
4092 break;
4093#ifdef FEAT_SYN_HL
4094 case CMD_syntax:
4095 set_context_in_syntax_cmd(xp, arg);
4096 break;
4097#endif
4098#ifdef FEAT_EVAL
4099 case CMD_let:
4100 case CMD_if:
4101 case CMD_elseif:
4102 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004103 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 case CMD_echo:
4105 case CMD_echon:
4106 case CMD_execute:
4107 case CMD_echomsg:
4108 case CMD_echoerr:
4109 case CMD_call:
4110 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004111 case CMD_cexpr:
4112 case CMD_caddexpr:
4113 case CMD_cgetexpr:
4114 case CMD_lexpr:
4115 case CMD_laddexpr:
4116 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004117 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119
4120 case CMD_unlet:
4121 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4122 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004123
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 xp->xp_context = EXPAND_USER_VARS;
4125 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004126
4127 if (*xp->xp_pattern == '$')
4128 {
4129 xp->xp_context = EXPAND_ENV_VARS;
4130 ++xp->xp_pattern;
4131 }
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
4135 case CMD_function:
4136 case CMD_delfunction:
4137 xp->xp_context = EXPAND_USER_FUNC;
4138 xp->xp_pattern = arg;
4139 break;
4140
4141 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004142 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 break;
4144#endif
4145 case CMD_highlight:
4146 set_context_in_highlight_cmd(xp, arg);
4147 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004148#ifdef FEAT_CSCOPE
4149 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004150 case CMD_lcscope:
4151 case CMD_scscope:
4152 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004153 break;
4154#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004155#ifdef FEAT_SIGNS
4156 case CMD_sign:
4157 set_context_in_sign_cmd(xp, arg);
4158 break;
4159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 case CMD_bdelete:
4161 case CMD_bwipeout:
4162 case CMD_bunload:
4163 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4164 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004165 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 case CMD_buffer:
4167 case CMD_sbuffer:
4168 case CMD_checktime:
4169 xp->xp_context = EXPAND_BUFFERS;
4170 xp->xp_pattern = arg;
4171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#ifdef FEAT_USR_CMDS
4173 case CMD_USER:
4174 case CMD_USER_BUF:
4175 if (compl != EXPAND_NOTHING)
4176 {
4177 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004178 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180# ifdef FEAT_MENU
4181 if (compl == EXPAND_MENUS)
4182 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4183# endif
4184 if (compl == EXPAND_COMMANDS)
4185 return arg;
4186 if (compl == EXPAND_MAPPINGS)
4187 return set_context_in_map_cmd(xp, (char_u *)"map",
4188 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004189 /* Find start of last argument. */
4190 p = arg;
4191 while (*p)
4192 {
4193 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004194 /* argument starts after a space */
4195 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004196 else if (*p == '\\' && *(p + 1) != NUL)
4197 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004198 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 xp->xp_pattern = arg;
4201 }
4202 xp->xp_context = compl;
4203 }
4204 break;
4205#endif
4206 case CMD_map: case CMD_noremap:
4207 case CMD_nmap: case CMD_nnoremap:
4208 case CMD_vmap: case CMD_vnoremap:
4209 case CMD_omap: case CMD_onoremap:
4210 case CMD_imap: case CMD_inoremap:
4211 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004212 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004213 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004214 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004215 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004217 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 case CMD_unmap:
4219 case CMD_nunmap:
4220 case CMD_vunmap:
4221 case CMD_ounmap:
4222 case CMD_iunmap:
4223 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004224 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004225 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004226 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004227 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004229 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004230 case CMD_mapclear:
4231 case CMD_nmapclear:
4232 case CMD_vmapclear:
4233 case CMD_omapclear:
4234 case CMD_imapclear:
4235 case CMD_cmapclear:
4236 case CMD_lmapclear:
4237 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004238 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004239 case CMD_xmapclear:
4240 xp->xp_context = EXPAND_MAPCLEAR;
4241 xp->xp_pattern = arg;
4242 break;
4243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 case CMD_abbreviate: case CMD_noreabbrev:
4245 case CMD_cabbrev: case CMD_cnoreabbrev:
4246 case CMD_iabbrev: case CMD_inoreabbrev:
4247 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004248 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 case CMD_unabbreviate:
4250 case CMD_cunabbrev:
4251 case CMD_iunabbrev:
4252 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004253 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#ifdef FEAT_MENU
4255 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4256 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4257 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4258 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4259 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4260 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4261 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4262 case CMD_tmenu: case CMD_tunmenu:
4263 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4264 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4265#endif
4266
4267 case CMD_colorscheme:
4268 xp->xp_context = EXPAND_COLORS;
4269 xp->xp_pattern = arg;
4270 break;
4271
4272 case CMD_compiler:
4273 xp->xp_context = EXPAND_COMPILER;
4274 xp->xp_pattern = arg;
4275 break;
4276
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004277 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004278 xp->xp_context = EXPAND_OWNSYNTAX;
4279 xp->xp_pattern = arg;
4280 break;
4281
4282 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004283 xp->xp_context = EXPAND_FILETYPE;
4284 xp->xp_pattern = arg;
4285 break;
4286
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004287 case CMD_packadd:
4288 xp->xp_context = EXPAND_PACKADD;
4289 xp->xp_pattern = arg;
4290 break;
4291
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4293 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4294 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004295 p = skiptowhite(arg);
4296 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 xp->xp_context = EXPAND_LANGUAGE;
4299 xp->xp_pattern = arg;
4300 }
4301 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004302 {
4303 if ( STRNCMP(arg, "messages", p - arg) == 0
4304 || STRNCMP(arg, "ctype", p - arg) == 0
4305 || STRNCMP(arg, "time", p - arg) == 0)
4306 {
4307 xp->xp_context = EXPAND_LOCALES;
4308 xp->xp_pattern = skipwhite(p);
4309 }
4310 else
4311 xp->xp_context = EXPAND_NOTHING;
4312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 break;
4314#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004315#if defined(FEAT_PROFILE)
4316 case CMD_profile:
4317 set_context_in_profile_cmd(xp, arg);
4318 break;
4319#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004320 case CMD_behave:
4321 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004322 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004325 case CMD_messages:
4326 xp->xp_context = EXPAND_MESSAGES;
4327 xp->xp_pattern = arg;
4328 break;
4329
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004330#if defined(FEAT_CMDHIST)
4331 case CMD_history:
4332 xp->xp_context = EXPAND_HISTORY;
4333 xp->xp_pattern = arg;
4334 break;
4335#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004336#if defined(FEAT_PROFILE)
4337 case CMD_syntime:
4338 xp->xp_context = EXPAND_SYNTIME;
4339 xp->xp_pattern = arg;
4340 break;
4341#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004342
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004343 case CMD_argdelete:
4344 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4345 arg = xp->xp_pattern + 1;
4346 xp->xp_context = EXPAND_ARGLIST;
4347 xp->xp_pattern = arg;
4348 break;
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#endif /* FEAT_CMDL_COMPL */
4351
4352 default:
4353 break;
4354 }
4355 return NULL;
4356}
4357
4358/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004359 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 *
4361 * Backslashed delimiters after / or ? will be skipped, and commands will
4362 * not be expanded between /'s and ?'s or after "'".
4363 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004364 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 * Returns the "cmd" pointer advanced to beyond the range.
4366 */
4367 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004368skip_range(
4369 char_u *cmd,
4370 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004372 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004374 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004376 if (*cmd == '\\')
4377 {
4378 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4379 ++cmd;
4380 else
4381 break;
4382 }
4383 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
4385 if (*++cmd == NUL && ctx != NULL)
4386 *ctx = EXPAND_NOTHING;
4387 }
4388 else if (*cmd == '/' || *cmd == '?')
4389 {
4390 delim = *cmd++;
4391 while (*cmd != NUL && *cmd != delim)
4392 if (*cmd++ == '\\' && *cmd != NUL)
4393 ++cmd;
4394 if (*cmd == NUL && ctx != NULL)
4395 *ctx = EXPAND_NOTHING;
4396 }
4397 if (*cmd != NUL)
4398 ++cmd;
4399 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004400
4401 /* Skip ":" and white space. */
4402 while (*cmd == ':')
4403 cmd = skipwhite(cmd + 1);
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return cmd;
4406}
4407
4408/*
4409 * get a single EX address
4410 *
4411 * Set ptr to the next character after the part that was interpreted.
4412 * Set ptr to NULL when an error is encountered.
4413 *
4414 * Return MAXLNUM when no Ex address was found.
4415 */
4416 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004417get_address(
4418 exarg_T *eap UNUSED,
4419 char_u **ptr,
4420 int addr_type, /* flag: one of ADDR_LINES, ... */
4421 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004422 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004423 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 int c;
4426 int i;
4427 long n;
4428 char_u *cmd;
4429 pos_T pos;
4430 pos_T *fp;
4431 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004432 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 cmd = skipwhite(*ptr);
4435 lnum = MAXLNUM;
4436 do
4437 {
4438 switch (*cmd)
4439 {
4440 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 ++cmd;
4442 switch (addr_type)
4443 {
4444 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 lnum = curwin->w_cursor.lnum;
4446 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004448 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 break;
4450 case ADDR_ARGUMENTS:
4451 lnum = curwin->w_arg_idx + 1;
4452 break;
4453 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004454 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 lnum = curbuf->b_fnum;
4456 break;
4457 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004458 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004460 case ADDR_TABS_RELATIVE:
4461 EMSG(_(e_invrange));
4462 cmd = NULL;
4463 goto error;
4464 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004465#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004466 case ADDR_QUICKFIX:
4467 lnum = qf_get_cur_valid_idx(eap);
4468 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004469#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 }
4471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 ++cmd;
4475 switch (addr_type)
4476 {
4477 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 lnum = curbuf->b_ml.ml_line_count;
4479 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004481 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 break;
4483 case ADDR_ARGUMENTS:
4484 lnum = ARGCOUNT;
4485 break;
4486 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004487 buf = lastbuf;
4488 while (buf->b_ml.ml_mfp == NULL)
4489 {
4490 if (buf->b_prev == NULL)
4491 break;
4492 buf = buf->b_prev;
4493 }
4494 lnum = buf->b_fnum;
4495 break;
4496 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 lnum = lastbuf->b_fnum;
4498 break;
4499 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004500 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004502 case ADDR_TABS_RELATIVE:
4503 EMSG(_(e_invrange));
4504 cmd = NULL;
4505 goto error;
4506 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004507#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004508 case ADDR_QUICKFIX:
4509 lnum = qf_get_size(eap);
4510 if (lnum == 0)
4511 lnum = 1;
4512 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004513#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 }
4515 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 if (*++cmd == NUL)
4519 {
4520 cmd = NULL;
4521 goto error;
4522 }
4523 if (addr_type != ADDR_LINES)
4524 {
4525 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004526 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004527 goto error;
4528 }
4529 if (skip)
4530 ++cmd;
4531 else
4532 {
4533 /* Only accept a mark in another file when it is
4534 * used by itself: ":'M". */
4535 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4536 ++cmd;
4537 if (fp == (pos_T *)-1)
4538 /* Jumped to another file. */
4539 lnum = curwin->w_cursor.lnum;
4540 else
4541 {
4542 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
4544 cmd = NULL;
4545 goto error;
4546 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 lnum = fp->lnum;
4548 }
4549 }
4550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 case '/':
4553 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 c = *cmd++;
4555 if (addr_type != ADDR_LINES)
4556 {
4557 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004558 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 goto error;
4560 }
4561 if (skip) /* skip "/pat/" */
4562 {
4563 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4564 if (*cmd == c)
4565 ++cmd;
4566 }
4567 else
4568 {
4569 pos = curwin->w_cursor; /* save curwin->w_cursor */
4570 /*
4571 * When '/' or '?' follows another address, start
4572 * from there.
4573 */
4574 if (lnum != MAXLNUM)
4575 curwin->w_cursor.lnum = lnum;
4576 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004577 * Start a forward search at the end of the line (unless
4578 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 * Start a backward search at the start of the line.
4580 * This makes sure we never match in the current
4581 * line, and can match anywhere in the
4582 * next/previous line.
4583 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004584 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004585 curwin->w_cursor.col = MAXCOL;
4586 else
4587 curwin->w_cursor.col = 0;
4588 searchcmdlen = 0;
4589 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004590 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 {
4592 curwin->w_cursor = pos;
4593 cmd = NULL;
4594 goto error;
4595 }
4596 lnum = curwin->w_cursor.lnum;
4597 curwin->w_cursor = pos;
4598 /* adjust command string pointer */
4599 cmd += searchcmdlen;
4600 }
4601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 ++cmd;
4605 if (addr_type != ADDR_LINES)
4606 {
4607 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004608 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004609 goto error;
4610 }
4611 if (*cmd == '&')
4612 i = RE_SUBST;
4613 else if (*cmd == '?' || *cmd == '/')
4614 i = RE_SEARCH;
4615 else
4616 {
4617 EMSG(_(e_backslash));
4618 cmd = NULL;
4619 goto error;
4620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 if (!skip)
4623 {
4624 /*
4625 * When search follows another address, start from
4626 * there.
4627 */
4628 if (lnum != MAXLNUM)
4629 pos.lnum = lnum;
4630 else
4631 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004633 /*
4634 * Start the search just like for the above
4635 * do_search().
4636 */
4637 if (*cmd != '?')
4638 pos.col = MAXCOL;
4639 else
4640 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004641#ifdef FEAT_VIRTUALEDIT
4642 pos.coladd = 0;
4643#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 if (searchit(curwin, curbuf, &pos,
4645 *cmd == '?' ? BACKWARD : FORWARD,
4646 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004647 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 lnum = pos.lnum;
4649 else
4650 {
4651 cmd = NULL;
4652 goto error;
4653 }
4654 }
4655 ++cmd;
4656 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004659 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4660 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662
4663 for (;;)
4664 {
4665 cmd = skipwhite(cmd);
4666 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4667 break;
4668
4669 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004670 {
4671 switch (addr_type)
4672 {
4673 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004674 /* "+1" is same as ".+1" */
4675 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004676 break;
4677 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004678 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004679 break;
4680 case ADDR_ARGUMENTS:
4681 lnum = curwin->w_arg_idx + 1;
4682 break;
4683 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004684 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004685 lnum = curbuf->b_fnum;
4686 break;
4687 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004688 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004689 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004690 case ADDR_TABS_RELATIVE:
4691 lnum = 1;
4692 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004693#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004694 case ADDR_QUICKFIX:
4695 lnum = qf_get_cur_valid_idx(eap);
4696 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004697#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004698 }
4699 }
4700
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 if (VIM_ISDIGIT(*cmd))
4702 i = '+'; /* "number" is same as "+number" */
4703 else
4704 i = *cmd++;
4705 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4706 n = 1;
4707 else
4708 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004709
4710 if (addr_type == ADDR_TABS_RELATIVE)
4711 {
4712 EMSG(_(e_invrange));
4713 cmd = NULL;
4714 goto error;
4715 }
4716 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004717 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004718 lnum = compute_buffer_local_count(
4719 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004721 {
4722#ifdef FEAT_FOLDING
4723 /* Relative line addressing, need to adjust for folded lines
4724 * now, but only do it after the first address. */
4725 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4726 && address_count >= 2)
4727 (void)hasFolding(lnum, NULL, &lnum);
4728#endif
4729 if (i == '-')
4730 lnum -= n;
4731 else
4732 lnum += n;
4733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 } while (*cmd == '/' || *cmd == '?');
4736
4737error:
4738 *ptr = cmd;
4739 return lnum;
4740}
4741
4742/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004743 * Get flags from an Ex command argument.
4744 */
4745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004746get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004747{
4748 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4749 {
4750 if (*eap->arg == 'l')
4751 eap->flags |= EXFLAG_LIST;
4752 else if (*eap->arg == 'p')
4753 eap->flags |= EXFLAG_PRINT;
4754 else
4755 eap->flags |= EXFLAG_NR;
4756 eap->arg = skipwhite(eap->arg + 1);
4757 }
4758}
4759
4760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 * Function called for command which is Not Implemented. NI!
4762 */
4763 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004764ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 if (!eap->skip)
4767 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4768}
4769
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004770#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771/*
4772 * Function called for script command which is Not Implemented. NI!
4773 * Skips over ":perl <<EOF" constructs.
4774 */
4775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
4778 if (!eap->skip)
4779 ex_ni(eap);
4780 else
4781 vim_free(script_get(eap, eap->arg));
4782}
4783#endif
4784
4785/*
4786 * Check range in Ex command for validity.
4787 * Return NULL when valid, error message when invalid.
4788 */
4789 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004790invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004792 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if ( eap->line1 < 0
4794 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004795 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004797
4798 if (eap->argt & RANGE)
4799 {
4800 switch(eap->addr_type)
4801 {
4802 case ADDR_LINES:
4803 if (!(eap->argt & NOTADR)
4804 && eap->line2 > curbuf->b_ml.ml_line_count
4805#ifdef FEAT_DIFF
4806 + (eap->cmdidx == CMD_diffget)
4807#endif
4808 )
4809 return (char_u *)_(e_invrange);
4810 break;
4811 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004812 /* add 1 if ARGCOUNT is 0 */
4813 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004814 return (char_u *)_(e_invrange);
4815 break;
4816 case ADDR_BUFFERS:
4817 if (eap->line1 < firstbuf->b_fnum
4818 || eap->line2 > lastbuf->b_fnum)
4819 return (char_u *)_(e_invrange);
4820 break;
4821 case ADDR_LOADED_BUFFERS:
4822 buf = firstbuf;
4823 while (buf->b_ml.ml_mfp == NULL)
4824 {
4825 if (buf->b_next == NULL)
4826 return (char_u *)_(e_invrange);
4827 buf = buf->b_next;
4828 }
4829 if (eap->line1 < buf->b_fnum)
4830 return (char_u *)_(e_invrange);
4831 buf = lastbuf;
4832 while (buf->b_ml.ml_mfp == NULL)
4833 {
4834 if (buf->b_prev == NULL)
4835 return (char_u *)_(e_invrange);
4836 buf = buf->b_prev;
4837 }
4838 if (eap->line2 > buf->b_fnum)
4839 return (char_u *)_(e_invrange);
4840 break;
4841 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004842 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 return (char_u *)_(e_invrange);
4844 break;
4845 case ADDR_TABS:
4846 if (eap->line2 > LAST_TAB_NR)
4847 return (char_u *)_(e_invrange);
4848 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004849 case ADDR_TABS_RELATIVE:
4850 /* Do nothing */
4851 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004852#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004853 case ADDR_QUICKFIX:
4854 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4855 return (char_u *)_(e_invrange);
4856 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004857#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858 }
4859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 return NULL;
4861}
4862
4863/*
4864 * Correct the range for zero line number, if required.
4865 */
4866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004867correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
4869 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4870 {
4871 if (eap->line1 == 0)
4872 eap->line1 = 1;
4873 if (eap->line2 == 0)
4874 eap->line2 = 1;
4875 }
4876}
4877
Bram Moolenaar748bf032005-02-02 23:04:36 +00004878#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004879static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004880
4881/*
4882 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4883 * pattern. Otherwise return eap->arg.
4884 */
4885 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004886skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004887{
4888 char_u *p = eap->arg;
4889
Bram Moolenaara37420f2006-02-04 22:37:47 +00004890 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4891 || eap->cmdidx == CMD_vimgrepadd
4892 || eap->cmdidx == CMD_lvimgrepadd
4893 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004894 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004895 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004896 if (p == NULL)
4897 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004898 }
4899 return p;
4900}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004901
4902/*
4903 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4904 * in the command line, so that things like % get expanded.
4905 */
4906 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004907replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004908{
4909 char_u *new_cmdline;
4910 char_u *program;
4911 char_u *pos;
4912 char_u *ptr;
4913 int len;
4914 int i;
4915
4916 /*
4917 * Don't do it when ":vimgrep" is used for ":grep".
4918 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004919 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4920 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4921 || eap->cmdidx == CMD_grepadd
4922 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004923 && !grep_internal(eap->cmdidx))
4924 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004925 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4926 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004927 {
4928 if (*curbuf->b_p_gp == NUL)
4929 program = p_gp;
4930 else
4931 program = curbuf->b_p_gp;
4932 }
4933 else
4934 {
4935 if (*curbuf->b_p_mp == NUL)
4936 program = p_mp;
4937 else
4938 program = curbuf->b_p_mp;
4939 }
4940
4941 p = skipwhite(p);
4942
4943 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4944 {
4945 /* replace $* by given arguments */
4946 i = 1;
4947 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4948 ++i;
4949 len = (int)STRLEN(p);
4950 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4951 if (new_cmdline == NULL)
4952 return NULL; /* out of memory */
4953 ptr = new_cmdline;
4954 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4955 {
4956 i = (int)(pos - program);
4957 STRNCPY(ptr, program, i);
4958 STRCPY(ptr += i, p);
4959 ptr += len;
4960 program = pos + 2;
4961 }
4962 STRCPY(ptr, program);
4963 }
4964 else
4965 {
4966 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4967 if (new_cmdline == NULL)
4968 return NULL; /* out of memory */
4969 STRCPY(new_cmdline, program);
4970 STRCAT(new_cmdline, " ");
4971 STRCAT(new_cmdline, p);
4972 }
4973 msg_make(p);
4974
4975 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4976 vim_free(*cmdlinep);
4977 *cmdlinep = new_cmdline;
4978 p = new_cmdline;
4979 }
4980 return p;
4981}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004982#endif
4983
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984/*
4985 * Expand file name in Ex command argument.
4986 * Return FAIL for failure, OK otherwise.
4987 */
4988 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004989expand_filename(
4990 exarg_T *eap,
4991 char_u **cmdlinep,
4992 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
4994 int has_wildcards; /* need to expand wildcards */
4995 char_u *repl;
4996 int srclen;
4997 char_u *p;
4998 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004999 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
Bram Moolenaar748bf032005-02-02 23:04:36 +00005001#ifdef FEAT_QUICKFIX
5002 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5003 p = skip_grep_pat(eap);
5004#else
5005 p = eap->arg;
5006#endif
5007
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 /*
5009 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5010 * the file name contains a wildcard it should not cause expanding.
5011 * (it will be expanded anyway if there is a wildcard before replacing).
5012 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005013 has_wildcards = mch_has_wildcard(p);
5014 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005016#ifdef FEAT_EVAL
5017 /* Skip over `=expr`, wildcards in it are not expanded. */
5018 if (p[0] == '`' && p[1] == '=')
5019 {
5020 p += 2;
5021 (void)skip_expr(&p);
5022 if (*p == '`')
5023 ++p;
5024 continue;
5025 }
5026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 /*
5028 * Quick check if this cannot be the start of a special string.
5029 * Also removes backslash before '%', '#' and '<'.
5030 */
5031 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5032 {
5033 ++p;
5034 continue;
5035 }
5036
5037 /*
5038 * Try to find a match at this position.
5039 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005040 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5041 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (*errormsgp != NULL) /* error detected */
5043 return FAIL;
5044 if (repl == NULL) /* no match found */
5045 {
5046 p += srclen;
5047 continue;
5048 }
5049
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005050 /* Wildcards won't be expanded below, the replacement is taken
5051 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5052 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5053 {
5054 char_u *l = repl;
5055
5056 repl = expand_env_save(repl);
5057 vim_free(l);
5058 }
5059
Bram Moolenaar63b92542007-03-27 14:57:09 +00005060 /* Need to escape white space et al. with a backslash.
5061 * Don't do this for:
5062 * - replacement that already has been escaped: "##"
5063 * - shell commands (may have to use quotes instead).
5064 * - non-unix systems when there is a single argument (spaces don't
5065 * separate arguments then).
5066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005068 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 && eap->cmdidx != CMD_grep
5071 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005072 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005073 && eap->cmdidx != CMD_lgrep
5074 && eap->cmdidx != CMD_lgrepadd
5075 && eap->cmdidx != CMD_lmake
5076 && eap->cmdidx != CMD_make
5077 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078#ifndef UNIX
5079 && !(eap->argt & NOSPC)
5080#endif
5081 )
5082 {
5083 char_u *l;
5084#ifdef BACKSLASH_IN_FILENAME
5085 /* Don't escape a backslash here, because rem_backslash() doesn't
5086 * remove it later. */
5087 static char_u *nobslash = (char_u *)" \t\"|";
5088# define ESCAPE_CHARS nobslash
5089#else
5090# define ESCAPE_CHARS escape_chars
5091#endif
5092
5093 for (l = repl; *l; ++l)
5094 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5095 {
5096 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5097 if (l != NULL)
5098 {
5099 vim_free(repl);
5100 repl = l;
5101 }
5102 break;
5103 }
5104 }
5105
5106 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005107 if ((eap->usefilter || eap->cmdidx == CMD_bang
5108 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005109 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 char_u *l;
5112
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005113 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (l != NULL)
5115 {
5116 vim_free(repl);
5117 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 }
5120
5121 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5122 vim_free(repl);
5123 if (p == NULL)
5124 return FAIL;
5125 }
5126
5127 /*
5128 * One file argument: Expand wildcards.
5129 * Don't do this with ":r !command" or ":w !command".
5130 */
5131 if ((eap->argt & NOSPC) && !eap->usefilter)
5132 {
5133 /*
5134 * May do this twice:
5135 * 1. Replace environment variables.
5136 * 2. Replace any other wildcards, remove backslashes.
5137 */
5138 for (n = 1; n <= 2; ++n)
5139 {
5140 if (n == 2)
5141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 /*
5143 * Halve the number of backslashes (this is Vi compatible).
5144 * For Unix and OS/2, when wildcards are expanded, this is
5145 * done by ExpandOne() below.
5146 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005147#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (!has_wildcards)
5149#endif
5150 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152
5153 if (has_wildcards)
5154 {
5155 if (n == 1)
5156 {
5157 /*
5158 * First loop: May expand environment variables. This
5159 * can be done much faster with expand_env() than with
5160 * something else (e.g., calling a shell).
5161 * After expanding environment variables, check again
5162 * if there are still wildcards present.
5163 */
5164 if (vim_strchr(eap->arg, '$') != NULL
5165 || vim_strchr(eap->arg, '~') != NULL)
5166 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005167 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005168 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 has_wildcards = mch_has_wildcard(NameBuff);
5170 p = NameBuff;
5171 }
5172 else
5173 p = NULL;
5174 }
5175 else /* n == 2 */
5176 {
5177 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005178 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 ExpandInit(&xpc);
5181 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005182 if (p_wic)
5183 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005185 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (p == NULL)
5187 return FAIL;
5188 }
5189 if (p != NULL)
5190 {
5191 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5192 p, cmdlinep);
5193 if (n == 2) /* p came from ExpandOne() */
5194 vim_free(p);
5195 }
5196 }
5197 }
5198 }
5199 return OK;
5200}
5201
5202/*
5203 * Replace part of the command line, keeping eap->cmd, eap->arg and
5204 * eap->nextcmd correct.
5205 * "src" points to the part that is to be replaced, of length "srclen".
5206 * "repl" is the replacement string.
5207 * Returns a pointer to the character after the replaced string.
5208 * Returns NULL for failure.
5209 */
5210 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005211repl_cmdline(
5212 exarg_T *eap,
5213 char_u *src,
5214 int srclen,
5215 char_u *repl,
5216 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
5218 int len;
5219 int i;
5220 char_u *new_cmdline;
5221
5222 /*
5223 * The new command line is build in new_cmdline[].
5224 * First allocate it.
5225 * Careful: a "+cmd" argument may have been NUL terminated.
5226 */
5227 len = (int)STRLEN(repl);
5228 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005229 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5231 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5232 return NULL; /* out of memory! */
5233
5234 /*
5235 * Copy the stuff before the expanded part.
5236 * Copy the expanded stuff.
5237 * Copy what came after the expanded part.
5238 * Copy the next commands, if there are any.
5239 */
5240 i = (int)(src - *cmdlinep); /* length of part before match */
5241 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005242
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 mch_memmove(new_cmdline + i, repl, (size_t)len);
5244 i += len; /* remember the end of the string */
5245 STRCPY(new_cmdline + i, src + srclen);
5246 src = new_cmdline + i; /* remember where to continue */
5247
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005248 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 i = (int)STRLEN(new_cmdline) + 1;
5251 STRCPY(new_cmdline + i, eap->nextcmd);
5252 eap->nextcmd = new_cmdline + i;
5253 }
5254 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5255 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5256 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5257 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5258 vim_free(*cmdlinep);
5259 *cmdlinep = new_cmdline;
5260
5261 return src;
5262}
5263
5264/*
5265 * Check for '|' to separate commands and '"' to start comments.
5266 */
5267 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005268separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
5270 char_u *p;
5271
Bram Moolenaar86b68352004-12-27 21:59:20 +00005272#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005273 p = skip_grep_pat(eap);
5274#else
5275 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005276#endif
5277
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005278 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 if (*p == Ctrl_V)
5281 {
5282 if (eap->argt & (USECTRLV | XFILE))
5283 ++p; /* skip CTRL-V and next char */
5284 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005285 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005286 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (*p == NUL) /* stop at NUL after CTRL-V */
5288 break;
5289 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005290
5291#ifdef FEAT_EVAL
5292 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005293 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005294 {
5295 p += 2;
5296 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005297 }
5298#endif
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 /* Check for '"': start of comment or '|': next command */
5301 /* :@" and :*" do not start a comment!
5302 * :redir @" doesn't either. */
5303 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5304 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5305 || p != eap->arg)
5306 && (eap->cmdidx != CMD_redir
5307 || p != eap->arg + 1 || p[-1] != '@'))
5308 || *p == '|' || *p == '\n')
5309 {
5310 /*
5311 * We remove the '\' before the '|', unless USECTRLV is used
5312 * AND 'b' is present in 'cpoptions'.
5313 */
5314 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5315 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5316 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005317 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 --p;
5319 }
5320 else
5321 {
5322 eap->nextcmd = check_nextcmd(p);
5323 *p = NUL;
5324 break;
5325 }
5326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005328
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5330 del_trailing_spaces(eap->arg);
5331}
5332
5333/*
5334 * get + command from ex argument
5335 */
5336 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005337getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 char_u *arg = *argp;
5340 char_u *command = NULL;
5341
5342 if (*arg == '+') /* +[command] */
5343 {
5344 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005345 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 command = dollar_command;
5347 else
5348 {
5349 command = arg;
5350 arg = skip_cmd_arg(command, TRUE);
5351 if (*arg != NUL)
5352 *arg++ = NUL; /* terminate command with NUL */
5353 }
5354
5355 arg = skipwhite(arg); /* skip over spaces */
5356 *argp = arg;
5357 }
5358 return command;
5359}
5360
5361/*
5362 * Find end of "+command" argument. Skip over "\ " and "\\".
5363 */
5364 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005365skip_cmd_arg(
5366 char_u *p,
5367 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 while (*p && !vim_isspace(*p))
5370 {
5371 if (*p == '\\' && p[1] != NUL)
5372 {
5373 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005374 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 else
5376 ++p;
5377 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005378 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
5380 return p;
5381}
5382
Bram Moolenaar86676c92018-04-05 18:56:48 +02005383#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005384 int
5385get_bad_opt(char_u *p, exarg_T *eap)
5386{
5387 if (STRICMP(p, "keep") == 0)
5388 eap->bad_char = BAD_KEEP;
5389 else if (STRICMP(p, "drop") == 0)
5390 eap->bad_char = BAD_DROP;
5391 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5392 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005393 else
5394 return FAIL;
5395 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005396}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005397#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399/*
5400 * Get "++opt=arg" argument.
5401 * Return FAIL or OK.
5402 */
5403 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005404getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 char_u *arg = eap->arg + 2;
5407 int *pp = NULL;
5408#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005409 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 char_u *p;
5411#endif
5412
5413 /* ":edit ++[no]bin[ary] file" */
5414 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5415 {
5416 if (*arg == 'n')
5417 {
5418 arg += 2;
5419 eap->force_bin = FORCE_NOBIN;
5420 }
5421 else
5422 eap->force_bin = FORCE_BIN;
5423 if (!checkforcmd(&arg, "binary", 3))
5424 return FAIL;
5425 eap->arg = skipwhite(arg);
5426 return OK;
5427 }
5428
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005429 /* ":read ++edit file" */
5430 if (STRNCMP(arg, "edit", 4) == 0)
5431 {
5432 eap->read_edit = TRUE;
5433 eap->arg = skipwhite(arg + 4);
5434 return OK;
5435 }
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 if (STRNCMP(arg, "ff", 2) == 0)
5438 {
5439 arg += 2;
5440 pp = &eap->force_ff;
5441 }
5442 else if (STRNCMP(arg, "fileformat", 10) == 0)
5443 {
5444 arg += 10;
5445 pp = &eap->force_ff;
5446 }
5447#ifdef FEAT_MBYTE
5448 else if (STRNCMP(arg, "enc", 3) == 0)
5449 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005450 if (STRNCMP(arg, "encoding", 8) == 0)
5451 arg += 8;
5452 else
5453 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 pp = &eap->force_enc;
5455 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005456 else if (STRNCMP(arg, "bad", 3) == 0)
5457 {
5458 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005459 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461#endif
5462
5463 if (pp == NULL || *arg != '=')
5464 return FAIL;
5465
5466 ++arg;
5467 *pp = (int)(arg - eap->cmd);
5468 arg = skip_cmd_arg(arg, FALSE);
5469 eap->arg = skipwhite(arg);
5470 *arg = NUL;
5471
5472#ifdef FEAT_MBYTE
5473 if (pp == &eap->force_ff)
5474 {
5475#endif
5476 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5477 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005478 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479#ifdef FEAT_MBYTE
5480 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005481 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
5483 /* Make 'fileencoding' lower case. */
5484 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5485 *p = TOLOWER_ASC(*p);
5486 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005487 else
5488 {
5489 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5490 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005491 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005492 return FAIL;
5493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494#endif
5495
5496 return OK;
5497}
5498
5499/*
5500 * ":abbreviate" and friends.
5501 */
5502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 do_exmap(eap, TRUE); /* almost the same as mapping */
5506}
5507
5508/*
5509 * ":map" and friends.
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 /*
5515 * If we are sourcing .exrc or .vimrc in current directory we
5516 * print the mappings for security reasons.
5517 */
5518 if (secure)
5519 {
5520 secure = 2;
5521 msg_outtrans(eap->cmd);
5522 msg_putchar('\n');
5523 }
5524 do_exmap(eap, FALSE);
5525}
5526
5527/*
5528 * ":unmap" and friends.
5529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 do_exmap(eap, FALSE);
5534}
5535
5536/*
5537 * ":mapclear" and friends.
5538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5543}
5544
5545/*
5546 * ":abclear" and friends.
5547 */
5548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5552}
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005558 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 * directory for security reasons.
5560 */
5561 if (secure)
5562 {
5563 secure = 2;
5564 eap->errmsg = e_curdir;
5565 }
5566 else if (eap->cmdidx == CMD_autocmd)
5567 do_autocmd(eap->arg, eap->forceit);
5568 else
5569 do_augroup(eap->arg, eap->forceit);
5570}
5571
5572/*
5573 * ":doautocmd": Apply the automatic commands to the current buffer.
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005578 char_u *arg = eap->arg;
5579 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005580 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005581
Bram Moolenaar1610d052016-06-09 22:53:01 +02005582 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5583 /* Only when there is no <nomodeline>. */
5584 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005585 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588/*
5589 * :[N]bunload[!] [N] [bufname] unload buffer
5590 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5591 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5592 */
5593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 eap->errmsg = do_bufdel(
5597 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5598 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5599 : DOBUF_UNLOAD, eap->arg,
5600 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5601}
5602
5603/*
5604 * :[N]buffer [N] to buffer N
5605 * :[N]sbuffer [N] to buffer N
5606 */
5607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 if (*eap->arg)
5611 eap->errmsg = e_trailing;
5612 else
5613 {
5614 if (eap->addr_count == 0) /* default is current buffer */
5615 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5616 else
5617 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005618 if (eap->do_ecmd_cmd != NULL)
5619 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
5621}
5622
5623/*
5624 * :[N]bmodified [N] to next mod. buffer
5625 * :[N]sbmodified [N] to next mod. buffer
5626 */
5627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005631 if (eap->do_ecmd_cmd != NULL)
5632 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635/*
5636 * :[N]bnext [N] to next buffer
5637 * :[N]sbnext [N] split and to next buffer
5638 */
5639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005643 if (eap->do_ecmd_cmd != NULL)
5644 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645}
5646
5647/*
5648 * :[N]bNext [N] to previous buffer
5649 * :[N]bprevious [N] to previous buffer
5650 * :[N]sbNext [N] split and to previous buffer
5651 * :[N]sbprevious [N] split and to previous buffer
5652 */
5653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005657 if (eap->do_ecmd_cmd != NULL)
5658 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659}
5660
5661/*
5662 * :brewind to first buffer
5663 * :bfirst to first buffer
5664 * :sbrewind split and to first buffer
5665 * :sbfirst split and to first buffer
5666 */
5667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005671 if (eap->do_ecmd_cmd != NULL)
5672 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673}
5674
5675/*
5676 * :blast to last buffer
5677 * :sblast split and to last buffer
5678 */
5679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005683 if (eap->do_ecmd_cmd != NULL)
5684 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 return (c == NUL || c == '|' || c == '"' || c == '\n');
5691}
5692
5693#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5694 || defined(PROTO)
5695/*
5696 * Return the next command, after the first '|' or '\n'.
5697 * Return NULL if not found.
5698 */
5699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005700find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 while (*p != '|' && *p != '\n')
5703 {
5704 if (*p == NUL)
5705 return NULL;
5706 ++p;
5707 }
5708 return (p + 1);
5709}
5710#endif
5711
5712/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005713 * Check if *p is a separator between Ex commands, skipping over white space.
5714 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 */
5716 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005717check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005719 char_u *s = skipwhite(p);
5720
5721 if (*s == '|' || *s == '\n')
5722 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 else
5724 return NULL;
5725}
5726
5727/*
5728 * - if there are more files to edit
5729 * - and this is the last window
5730 * - and forceit not used
5731 * - and not repeated twice on a row
5732 * return FAIL and give error message if 'message' TRUE
5733 * return OK otherwise
5734 */
5735 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736check_more(
5737 int message, /* when FALSE check only, no messages */
5738 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5741
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005742 if (!forceit && only_one_window()
5743 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
5745 if (message)
5746 {
5747#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5748 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5749 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005750 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005752 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5753 NGETTEXT("%d more file to edit. Quit anyway?",
5754 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5756 return OK;
5757 return FAIL;
5758 }
5759#endif
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005760 EMSGN(NGETTEXT("E173: %ld more file to edit",
5761 "E173: %ld more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 quitmore = 2; /* next try to quit is allowed */
5763 }
5764 return FAIL;
5765 }
5766 return OK;
5767}
5768
5769#ifdef FEAT_CMDL_COMPL
5770/*
5771 * Function given to ExpandGeneric() to obtain the list of command names.
5772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005774get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 if (idx >= (int)CMD_SIZE)
5777# ifdef FEAT_USR_CMDS
5778 return get_user_command_name(idx);
5779# else
5780 return NULL;
5781# endif
5782 return cmdnames[idx].cmd_name;
5783}
5784#endif
5785
5786#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005787static 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);
5788static void uc_list(char_u *name, size_t name_len);
5789static 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);
5790static char_u *uc_split_args(char_u *arg, size_t *lenp);
5791static 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 +00005792
5793 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794uc_add_command(
5795 char_u *name,
5796 size_t name_len,
5797 char_u *rep,
5798 long argt,
5799 long def,
5800 int flags,
5801 int compl,
5802 char_u *compl_arg,
5803 int addr_type,
5804 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 ucmd_T *cmd = NULL;
5807 char_u *p;
5808 int i;
5809 int cmp = 1;
5810 char_u *rep_buf = NULL;
5811 garray_T *gap;
5812
Bram Moolenaar9c102382006-05-03 21:26:49 +00005813 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if (rep_buf == NULL)
5815 {
5816 /* Can't replace termcodes - try using the string as is */
5817 rep_buf = vim_strsave(rep);
5818
5819 /* Give up if out of memory */
5820 if (rep_buf == NULL)
5821 return FAIL;
5822 }
5823
5824 /* get address of growarray: global or in curbuf */
5825 if (flags & UC_BUFFER)
5826 {
5827 gap = &curbuf->b_ucmds;
5828 if (gap->ga_itemsize == 0)
5829 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5830 }
5831 else
5832 gap = &ucmds;
5833
5834 /* Search for the command in the already defined commands. */
5835 for (i = 0; i < gap->ga_len; ++i)
5836 {
5837 size_t len;
5838
5839 cmd = USER_CMD_GA(gap, i);
5840 len = STRLEN(cmd->uc_name);
5841 cmp = STRNCMP(name, cmd->uc_name, name_len);
5842 if (cmp == 0)
5843 {
5844 if (name_len < len)
5845 cmp = -1;
5846 else if (name_len > len)
5847 cmp = 1;
5848 }
5849
5850 if (cmp == 0)
5851 {
5852 if (!force)
5853 {
5854 EMSG(_("E174: Command already exists: add ! to replace it"));
5855 goto fail;
5856 }
5857
Bram Moolenaard23a8232018-02-10 18:45:26 +01005858 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005859#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005860 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 break;
5863 }
5864
5865 /* Stop as soon as we pass the name to add */
5866 if (cmp < 0)
5867 break;
5868 }
5869
5870 /* Extend the array unless we're replacing an existing command */
5871 if (cmp != 0)
5872 {
5873 if (ga_grow(gap, 1) != OK)
5874 goto fail;
5875 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5876 goto fail;
5877
5878 cmd = USER_CMD_GA(gap, i);
5879 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5880
5881 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 cmd->uc_name = p;
5884 }
5885
5886 cmd->uc_rep = rep_buf;
5887 cmd->uc_argt = argt;
5888 cmd->uc_def = def;
5889 cmd->uc_compl = compl;
5890#ifdef FEAT_EVAL
5891 cmd->uc_scriptID = current_SID;
5892# ifdef FEAT_CMDL_COMPL
5893 cmd->uc_compl_arg = compl_arg;
5894# endif
5895#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005896 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 return OK;
5899
5900fail:
5901 vim_free(rep_buf);
5902#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5903 vim_free(compl_arg);
5904#endif
5905 return FAIL;
5906}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005909#if defined(FEAT_USR_CMDS)
5910static struct
5911{
5912 int expand;
5913 char *name;
5914} addr_type_complete[] =
5915{
5916 {ADDR_ARGUMENTS, "arguments"},
5917 {ADDR_LINES, "lines"},
5918 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5919 {ADDR_TABS, "tabs"},
5920 {ADDR_BUFFERS, "buffers"},
5921 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005922 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005923 {-1, NULL}
5924};
5925#endif
5926
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005927#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * List of names for completion for ":command" with the EXPAND_ flag.
5930 * Must be alphabetical for completion.
5931 */
5932static struct
5933{
5934 int expand;
5935 char *name;
5936} command_complete[] =
5937{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005938 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005940 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005942 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005944 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005945#if defined(FEAT_CSCOPE)
5946 {EXPAND_CSCOPE, "cscope"},
5947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5949 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005950 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951#endif
5952 {EXPAND_DIRECTORIES, "dir"},
5953 {EXPAND_ENV_VARS, "environment"},
5954 {EXPAND_EVENTS, "event"},
5955 {EXPAND_EXPRESSION, "expression"},
5956 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005957 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005958 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {EXPAND_FUNCTIONS, "function"},
5960 {EXPAND_HELP, "help"},
5961 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005962#if defined(FEAT_CMDHIST)
5963 {EXPAND_HISTORY, "history"},
5964#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005965#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005966 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005967 {EXPAND_LOCALES, "locale"},
5968#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005969 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {EXPAND_MAPPINGS, "mapping"},
5971 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005972 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005973 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005974#if defined(FEAT_PROFILE)
5975 {EXPAND_SYNTIME, "syntime"},
5976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005978 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005979 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005980#if defined(FEAT_SIGNS)
5981 {EXPAND_SIGN, "sign"},
5982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {EXPAND_TAGS, "tag"},
5984 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005985 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {EXPAND_USER_VARS, "var"},
5987 {0, NULL}
5988};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005991#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005993uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 int i, j;
5996 int found = FALSE;
5997 ucmd_T *cmd;
5998 int len;
5999 long a;
6000 garray_T *gap;
6001
6002 gap = &curbuf->b_ucmds;
6003 for (;;)
6004 {
6005 for (i = 0; i < gap->ga_len; ++i)
6006 {
6007 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006008 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
Bram Moolenaard29459b2016-08-26 22:29:11 +02006010 /* Skip commands which don't match the requested prefix and
6011 * commands filtered out. */
6012 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6013 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 continue;
6015
6016 /* Put out the title first time */
6017 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006018 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 found = TRUE;
6020 msg_putchar('\n');
6021 if (got_int)
6022 break;
6023
6024 /* Special cases */
6025 msg_putchar(a & BANG ? '!' : ' ');
6026 msg_putchar(a & REGSTR ? '"' : ' ');
6027 msg_putchar(gap != &ucmds ? 'b' : ' ');
6028 msg_putchar(' ');
6029
Bram Moolenaar8820b482017-03-16 17:23:31 +01006030 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 len = (int)STRLEN(cmd->uc_name) + 4;
6032
6033 do {
6034 msg_putchar(' ');
6035 ++len;
6036 } while (len < 16);
6037
6038 len = 0;
6039
6040 /* Arguments */
6041 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6042 {
6043 case 0: IObuff[len++] = '0'; break;
6044 case (EXTRA): IObuff[len++] = '*'; break;
6045 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6046 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6047 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6048 }
6049
6050 do {
6051 IObuff[len++] = ' ';
6052 } while (len < 5);
6053
6054 /* Range */
6055 if (a & (RANGE|COUNT))
6056 {
6057 if (a & COUNT)
6058 {
6059 /* -count=N */
6060 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6061 len += (int)STRLEN(IObuff + len);
6062 }
6063 else if (a & DFLALL)
6064 IObuff[len++] = '%';
6065 else if (cmd->uc_def >= 0)
6066 {
6067 /* -range=N */
6068 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6069 len += (int)STRLEN(IObuff + len);
6070 }
6071 else
6072 IObuff[len++] = '.';
6073 }
6074
6075 do {
6076 IObuff[len++] = ' ';
6077 } while (len < 11);
6078
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006079 /* Address Type */
6080 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6081 if (addr_type_complete[j].expand != ADDR_LINES
6082 && addr_type_complete[j].expand == cmd->uc_addr_type)
6083 {
6084 STRCPY(IObuff + len, addr_type_complete[j].name);
6085 len += (int)STRLEN(IObuff + len);
6086 break;
6087 }
6088
6089 do {
6090 IObuff[len++] = ' ';
6091 } while (len < 21);
6092
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 /* Completion */
6094 for (j = 0; command_complete[j].expand != 0; ++j)
6095 if (command_complete[j].expand == cmd->uc_compl)
6096 {
6097 STRCPY(IObuff + len, command_complete[j].name);
6098 len += (int)STRLEN(IObuff + len);
6099 break;
6100 }
6101
6102 do {
6103 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006104 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 IObuff[len] = '\0';
6107 msg_outtrans(IObuff);
6108
6109 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006110#ifdef FEAT_EVAL
6111 if (p_verbose > 0)
6112 last_set_msg(cmd->uc_scriptID);
6113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 out_flush();
6115 ui_breakcheck();
6116 if (got_int)
6117 break;
6118 }
6119 if (gap == &ucmds || i < gap->ga_len)
6120 break;
6121 gap = &ucmds;
6122 }
6123
6124 if (!found)
6125 MSG(_("No user-defined commands found"));
6126}
6127
6128 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006129uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6132 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6133 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6134 0xb9, 0x7f, 0};
6135 int i;
6136
6137 for (i = 0; fcmd[i]; ++i)
6138 IObuff[i] = fcmd[i] - 0x40;
6139 IObuff[i] = 0;
6140 return IObuff;
6141}
6142
6143 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006144uc_scan_attr(
6145 char_u *attr,
6146 size_t len,
6147 long *argt,
6148 long *def,
6149 int *flags,
6150 int *compl,
6151 char_u **compl_arg,
6152 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 char_u *p;
6155
6156 if (len == 0)
6157 {
6158 EMSG(_("E175: No attribute specified"));
6159 return FAIL;
6160 }
6161
6162 /* First, try the simple attributes (no arguments) */
6163 if (STRNICMP(attr, "bang", len) == 0)
6164 *argt |= BANG;
6165 else if (STRNICMP(attr, "buffer", len) == 0)
6166 *flags |= UC_BUFFER;
6167 else if (STRNICMP(attr, "register", len) == 0)
6168 *argt |= REGSTR;
6169 else if (STRNICMP(attr, "bar", len) == 0)
6170 *argt |= TRLBAR;
6171 else
6172 {
6173 int i;
6174 char_u *val = NULL;
6175 size_t vallen = 0;
6176 size_t attrlen = len;
6177
6178 /* Look for the attribute name - which is the part before any '=' */
6179 for (i = 0; i < (int)len; ++i)
6180 {
6181 if (attr[i] == '=')
6182 {
6183 val = &attr[i + 1];
6184 vallen = len - i - 1;
6185 attrlen = i;
6186 break;
6187 }
6188 }
6189
6190 if (STRNICMP(attr, "nargs", attrlen) == 0)
6191 {
6192 if (vallen == 1)
6193 {
6194 if (*val == '0')
6195 /* Do nothing - this is the default */;
6196 else if (*val == '1')
6197 *argt |= (EXTRA | NOSPC | NEEDARG);
6198 else if (*val == '*')
6199 *argt |= EXTRA;
6200 else if (*val == '?')
6201 *argt |= (EXTRA | NOSPC);
6202 else if (*val == '+')
6203 *argt |= (EXTRA | NEEDARG);
6204 else
6205 goto wrong_nargs;
6206 }
6207 else
6208 {
6209wrong_nargs:
6210 EMSG(_("E176: Invalid number of arguments"));
6211 return FAIL;
6212 }
6213 }
6214 else if (STRNICMP(attr, "range", attrlen) == 0)
6215 {
6216 *argt |= RANGE;
6217 if (vallen == 1 && *val == '%')
6218 *argt |= DFLALL;
6219 else if (val != NULL)
6220 {
6221 p = val;
6222 if (*def >= 0)
6223 {
6224two_count:
6225 EMSG(_("E177: Count cannot be specified twice"));
6226 return FAIL;
6227 }
6228
6229 *def = getdigits(&p);
6230 *argt |= (ZEROR | NOTADR);
6231
6232 if (p != val + vallen || vallen == 0)
6233 {
6234invalid_count:
6235 EMSG(_("E178: Invalid default value for count"));
6236 return FAIL;
6237 }
6238 }
6239 }
6240 else if (STRNICMP(attr, "count", attrlen) == 0)
6241 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006242 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
6244 if (val != NULL)
6245 {
6246 p = val;
6247 if (*def >= 0)
6248 goto two_count;
6249
6250 *def = getdigits(&p);
6251
6252 if (p != val + vallen)
6253 goto invalid_count;
6254 }
6255
6256 if (*def < 0)
6257 *def = 0;
6258 }
6259 else if (STRNICMP(attr, "complete", attrlen) == 0)
6260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if (val == NULL)
6262 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006263 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 return FAIL;
6265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006267 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6268 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006271 else if (STRNICMP(attr, "addr", attrlen) == 0)
6272 {
6273 *argt |= RANGE;
6274 if (val == NULL)
6275 {
6276 EMSG(_("E179: argument required for -addr"));
6277 return FAIL;
6278 }
6279 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6280 == FAIL)
6281 return FAIL;
6282 if (addr_type_arg != ADDR_LINES)
6283 *argt |= (ZEROR | NOTADR) ;
6284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 else
6286 {
6287 char_u ch = attr[len];
6288 attr[len] = '\0';
6289 EMSG2(_("E181: Invalid attribute: %s"), attr);
6290 attr[len] = ch;
6291 return FAIL;
6292 }
6293 }
6294
6295 return OK;
6296}
6297
Bram Moolenaara850a712009-01-28 14:42:59 +00006298/*
6299 * ":command ..."
6300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006302ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 char_u *name;
6305 char_u *end;
6306 char_u *p;
6307 long argt = 0;
6308 long def = -1;
6309 int flags = 0;
6310 int compl = EXPAND_NOTHING;
6311 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006312 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006314 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 p = eap->arg;
6317
6318 /* Check for attributes */
6319 while (*p == '-')
6320 {
6321 ++p;
6322 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006323 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6324 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 == FAIL)
6326 return;
6327 p = skipwhite(end);
6328 }
6329
6330 /* Get the name (if any) and skip to the following argument */
6331 name = p;
6332 if (ASCII_ISALPHA(*p))
6333 while (ASCII_ISALNUM(*p))
6334 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006335 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 {
6337 EMSG(_("E182: Invalid command name"));
6338 return;
6339 }
6340 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006341 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
6343 /* If there is nothing after the name, and no attributes were specified,
6344 * we are listing commands
6345 */
6346 p = skipwhite(end);
6347 if (!has_attr && ends_excmd(*p))
6348 {
6349 uc_list(name, end - name);
6350 }
6351 else if (!ASCII_ISUPPER(*name))
6352 {
6353 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6354 return;
6355 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006356 else if ((name_len == 1 && *name == 'X')
6357 || (name_len <= 4
6358 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6359 {
6360 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6361 return;
6362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 else
6364 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006365 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366}
6367
6368/*
6369 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 * Clear all user commands, global and for current buffer.
6371 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006372 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006373ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 uc_clear(&ucmds);
6376 uc_clear(&curbuf->b_ucmds);
6377}
6378
6379/*
6380 * Clear all user commands for "gap".
6381 */
6382 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006383uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 int i;
6386 ucmd_T *cmd;
6387
6388 for (i = 0; i < gap->ga_len; ++i)
6389 {
6390 cmd = USER_CMD_GA(gap, i);
6391 vim_free(cmd->uc_name);
6392 vim_free(cmd->uc_rep);
6393# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6394 vim_free(cmd->uc_compl_arg);
6395# endif
6396 }
6397 ga_clear(gap);
6398}
6399
6400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006401ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 int i = 0;
6404 ucmd_T *cmd = NULL;
6405 int cmp = -1;
6406 garray_T *gap;
6407
6408 gap = &curbuf->b_ucmds;
6409 for (;;)
6410 {
6411 for (i = 0; i < gap->ga_len; ++i)
6412 {
6413 cmd = USER_CMD_GA(gap, i);
6414 cmp = STRCMP(eap->arg, cmd->uc_name);
6415 if (cmp <= 0)
6416 break;
6417 }
6418 if (gap == &ucmds || cmp == 0)
6419 break;
6420 gap = &ucmds;
6421 }
6422
6423 if (cmp != 0)
6424 {
6425 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6426 return;
6427 }
6428
6429 vim_free(cmd->uc_name);
6430 vim_free(cmd->uc_rep);
6431# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6432 vim_free(cmd->uc_compl_arg);
6433# endif
6434
6435 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
6437 if (i < gap->ga_len)
6438 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6439}
6440
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006441/*
6442 * split and quote args for <f-args>
6443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006445uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 char_u *buf;
6448 char_u *p;
6449 char_u *q;
6450 int len;
6451
6452 /* Precalculate length */
6453 p = arg;
6454 len = 2; /* Initial and final quotes */
6455
6456 while (*p)
6457 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006458 if (p[0] == '\\' && p[1] == '\\')
6459 {
6460 len += 2;
6461 p += 2;
6462 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006463 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 len += 1;
6466 p += 2;
6467 }
6468 else if (*p == '\\' || *p == '"')
6469 {
6470 len += 2;
6471 p += 1;
6472 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006473 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 {
6475 p = skipwhite(p);
6476 if (*p == NUL)
6477 break;
6478 len += 3; /* "," */
6479 }
6480 else
6481 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006482#ifdef FEAT_MBYTE
6483 int charlen = (*mb_ptr2len)(p);
6484 len += charlen;
6485 p += charlen;
6486#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 ++len;
6488 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 }
6491 }
6492
6493 buf = alloc(len + 1);
6494 if (buf == NULL)
6495 {
6496 *lenp = 0;
6497 return buf;
6498 }
6499
6500 p = arg;
6501 q = buf;
6502 *q++ = '"';
6503 while (*p)
6504 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006505 if (p[0] == '\\' && p[1] == '\\')
6506 {
6507 *q++ = '\\';
6508 *q++ = '\\';
6509 p += 2;
6510 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006511 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
6513 *q++ = p[1];
6514 p += 2;
6515 }
6516 else if (*p == '\\' || *p == '"')
6517 {
6518 *q++ = '\\';
6519 *q++ = *p++;
6520 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006521 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
6523 p = skipwhite(p);
6524 if (*p == NUL)
6525 break;
6526 *q++ = '"';
6527 *q++ = ',';
6528 *q++ = '"';
6529 }
6530 else
6531 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006532 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
6534 }
6535 *q++ = '"';
6536 *q = 0;
6537
6538 *lenp = len;
6539 return buf;
6540}
6541
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006542 static size_t
6543add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6544{
6545 size_t result;
6546
6547 result = STRLEN(mod_str);
6548 if (*multi_mods)
6549 result += 1;
6550 if (buf != NULL)
6551 {
6552 if (*multi_mods)
6553 STRCAT(buf, " ");
6554 STRCAT(buf, mod_str);
6555 }
6556
6557 *multi_mods = 1;
6558
6559 return result;
6560}
6561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562/*
6563 * Check for a <> code in a user command.
6564 * "code" points to the '<'. "len" the length of the <> (inclusive).
6565 * "buf" is where the result is to be added.
6566 * "split_buf" points to a buffer used for splitting, caller should free it.
6567 * "split_len" is the length of what "split_buf" contains.
6568 * Returns the length of the replacement, which has been added to "buf".
6569 * Returns -1 if there was no match, and only the "<" has been copied.
6570 */
6571 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006572uc_check_code(
6573 char_u *code,
6574 size_t len,
6575 char_u *buf,
6576 ucmd_T *cmd, /* the user command we're expanding */
6577 exarg_T *eap, /* ex arguments */
6578 char_u **split_buf,
6579 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 size_t result = 0;
6582 char_u *p = code + 1;
6583 size_t l = len - 2;
6584 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006585 enum {
6586 ct_ARGS,
6587 ct_BANG,
6588 ct_COUNT,
6589 ct_LINE1,
6590 ct_LINE2,
6591 ct_RANGE,
6592 ct_MODS,
6593 ct_REGISTER,
6594 ct_LT,
6595 ct_NONE
6596 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
6598 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6599 {
6600 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6601 p += 2;
6602 l -= 2;
6603 }
6604
Bram Moolenaar371d5402006-03-20 21:47:49 +00006605 ++l;
6606 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006608 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006610 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006612 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006614 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006616 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006618 else if (STRNICMP(p, "range>", l) == 0)
6619 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006620 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006622 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006624 else if (STRNICMP(p, "mods>", l) == 0)
6625 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 switch (type)
6628 {
6629 case ct_ARGS:
6630 /* Simple case first */
6631 if (*eap->arg == NUL)
6632 {
6633 if (quote == 1)
6634 {
6635 result = 2;
6636 if (buf != NULL)
6637 STRCPY(buf, "''");
6638 }
6639 else
6640 result = 0;
6641 break;
6642 }
6643
6644 /* When specified there is a single argument don't split it.
6645 * Works for ":Cmd %" when % is "a b c". */
6646 if ((eap->argt & NOSPC) && quote == 2)
6647 quote = 1;
6648
6649 switch (quote)
6650 {
6651 case 0: /* No quoting, no splitting */
6652 result = STRLEN(eap->arg);
6653 if (buf != NULL)
6654 STRCPY(buf, eap->arg);
6655 break;
6656 case 1: /* Quote, but don't split */
6657 result = STRLEN(eap->arg) + 2;
6658 for (p = eap->arg; *p; ++p)
6659 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006660#ifdef FEAT_MBYTE
6661 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6662 /* DBCS can contain \ in a trail byte, skip the
6663 * double-byte character. */
6664 ++p;
6665 else
6666#endif
6667 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 ++result;
6669 }
6670
6671 if (buf != NULL)
6672 {
6673 *buf++ = '"';
6674 for (p = eap->arg; *p; ++p)
6675 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006676#ifdef FEAT_MBYTE
6677 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6678 /* DBCS can contain \ in a trail byte, copy the
6679 * double-byte character to avoid escaping. */
6680 *buf++ = *p++;
6681 else
6682#endif
6683 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 *buf++ = '\\';
6685 *buf++ = *p;
6686 }
6687 *buf = '"';
6688 }
6689
6690 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006691 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 /* This is hard, so only do it once, and cache the result */
6693 if (*split_buf == NULL)
6694 *split_buf = uc_split_args(eap->arg, split_len);
6695
6696 result = *split_len;
6697 if (buf != NULL && result != 0)
6698 STRCPY(buf, *split_buf);
6699
6700 break;
6701 }
6702 break;
6703
6704 case ct_BANG:
6705 result = eap->forceit ? 1 : 0;
6706 if (quote)
6707 result += 2;
6708 if (buf != NULL)
6709 {
6710 if (quote)
6711 *buf++ = '"';
6712 if (eap->forceit)
6713 *buf++ = '!';
6714 if (quote)
6715 *buf = '"';
6716 }
6717 break;
6718
6719 case ct_LINE1:
6720 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006721 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 case ct_COUNT:
6723 {
6724 char num_buf[20];
6725 long num = (type == ct_LINE1) ? eap->line1 :
6726 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006727 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6729 size_t num_len;
6730
6731 sprintf(num_buf, "%ld", num);
6732 num_len = STRLEN(num_buf);
6733 result = num_len;
6734
6735 if (quote)
6736 result += 2;
6737
6738 if (buf != NULL)
6739 {
6740 if (quote)
6741 *buf++ = '"';
6742 STRCPY(buf, num_buf);
6743 buf += num_len;
6744 if (quote)
6745 *buf = '"';
6746 }
6747
6748 break;
6749 }
6750
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006751 case ct_MODS:
6752 {
6753 int multi_mods = 0;
6754 typedef struct {
6755 int *varp;
6756 char *name;
6757 } mod_entry_T;
6758 static mod_entry_T mod_entries[] = {
6759#ifdef FEAT_BROWSE_CMD
6760 {&cmdmod.browse, "browse"},
6761#endif
6762#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6763 {&cmdmod.confirm, "confirm"},
6764#endif
6765 {&cmdmod.hide, "hide"},
6766 {&cmdmod.keepalt, "keepalt"},
6767 {&cmdmod.keepjumps, "keepjumps"},
6768 {&cmdmod.keepmarks, "keepmarks"},
6769 {&cmdmod.keeppatterns, "keeppatterns"},
6770 {&cmdmod.lockmarks, "lockmarks"},
6771 {&cmdmod.noswapfile, "noswapfile"},
6772 {NULL, NULL}
6773 };
6774 int i;
6775
6776 result = quote ? 2 : 0;
6777 if (buf != NULL)
6778 {
6779 if (quote)
6780 *buf++ = '"';
6781 *buf = '\0';
6782 }
6783
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006784 /* :aboveleft and :leftabove */
6785 if (cmdmod.split & WSP_ABOVE)
6786 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6787 /* :belowright and :rightbelow */
6788 if (cmdmod.split & WSP_BELOW)
6789 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6790 /* :botright */
6791 if (cmdmod.split & WSP_BOT)
6792 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006793
6794 /* the modifiers that are simple flags */
6795 for (i = 0; mod_entries[i].varp != NULL; ++i)
6796 if (*mod_entries[i].varp)
6797 result += add_cmd_modifier(buf, mod_entries[i].name,
6798 &multi_mods);
6799
6800 /* TODO: How to support :noautocmd? */
6801#ifdef HAVE_SANDBOX
6802 /* TODO: How to support :sandbox?*/
6803#endif
6804 /* :silent */
6805 if (msg_silent > 0)
6806 result += add_cmd_modifier(buf,
6807 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006808 /* :tab */
6809 if (cmdmod.tab > 0)
6810 result += add_cmd_modifier(buf, "tab", &multi_mods);
6811 /* :topleft */
6812 if (cmdmod.split & WSP_TOP)
6813 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006814 /* TODO: How to support :unsilent?*/
6815 /* :verbose */
6816 if (p_verbose > 0)
6817 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006818 /* :vertical */
6819 if (cmdmod.split & WSP_VERT)
6820 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006821 if (quote && buf != NULL)
6822 {
6823 buf += result - 2;
6824 *buf = '"';
6825 }
6826 break;
6827 }
6828
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 case ct_REGISTER:
6830 result = eap->regname ? 1 : 0;
6831 if (quote)
6832 result += 2;
6833 if (buf != NULL)
6834 {
6835 if (quote)
6836 *buf++ = '\'';
6837 if (eap->regname)
6838 *buf++ = eap->regname;
6839 if (quote)
6840 *buf = '\'';
6841 }
6842 break;
6843
6844 case ct_LT:
6845 result = 1;
6846 if (buf != NULL)
6847 *buf = '<';
6848 break;
6849
6850 default:
6851 /* Not recognized: just copy the '<' and return -1. */
6852 result = (size_t)-1;
6853 if (buf != NULL)
6854 *buf = '<';
6855 break;
6856 }
6857
6858 return result;
6859}
6860
6861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006862do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 char_u *buf;
6865 char_u *p;
6866 char_u *q;
6867
6868 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006869 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006870 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 size_t len, totlen;
6872
6873 size_t split_len = 0;
6874 char_u *split_buf = NULL;
6875 ucmd_T *cmd;
6876#ifdef FEAT_EVAL
6877 scid_T save_current_SID = current_SID;
6878#endif
6879
6880 if (eap->cmdidx == CMD_USER)
6881 cmd = USER_CMD(eap->useridx);
6882 else
6883 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6884
6885 /*
6886 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006887 * First round: "buf" is NULL, compute length, allocate "buf".
6888 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 */
6890 buf = NULL;
6891 for (;;)
6892 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006893 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006894 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006896
6897 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006899 start = vim_strchr(p, '<');
6900 if (start != NULL)
6901 end = vim_strchr(start + 1, '>');
6902 if (buf != NULL)
6903 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006904 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6905 ;
6906 if (*ksp == K_SPECIAL
6907 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006908 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6909# ifdef FEAT_GUI
6910 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6911# endif
6912 ))
6913 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006914 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006915 * KS_SPECIAL KE_FILLER, like for mappings, but
6916 * do_cmdline() doesn't handle that, so convert it back.
6917 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6918 len = ksp - p;
6919 if (len > 0)
6920 {
6921 mch_memmove(q, p, len);
6922 q += len;
6923 }
6924 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6925 p = ksp + 3;
6926 continue;
6927 }
6928 }
6929
6930 /* break if there no <item> is found */
6931 if (start == NULL || end == NULL)
6932 break;
6933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 /* Include the '>' */
6935 ++end;
6936
6937 /* Take everything up to the '<' */
6938 len = start - p;
6939 if (buf == NULL)
6940 totlen += len;
6941 else
6942 {
6943 mch_memmove(q, p, len);
6944 q += len;
6945 }
6946
6947 len = uc_check_code(start, end - start, q, cmd, eap,
6948 &split_buf, &split_len);
6949 if (len == (size_t)-1)
6950 {
6951 /* no match, continue after '<' */
6952 p = start + 1;
6953 len = 1;
6954 }
6955 else
6956 p = end;
6957 if (buf == NULL)
6958 totlen += len;
6959 else
6960 q += len;
6961 }
6962 if (buf != NULL) /* second time here, finished */
6963 {
6964 STRCPY(q, p);
6965 break;
6966 }
6967
6968 totlen += STRLEN(p); /* Add on the trailing characters */
6969 buf = alloc((unsigned)(totlen + 1));
6970 if (buf == NULL)
6971 {
6972 vim_free(split_buf);
6973 return;
6974 }
6975 }
6976
6977#ifdef FEAT_EVAL
6978 current_SID = cmd->uc_scriptID;
6979#endif
6980 (void)do_cmdline(buf, eap->getline, eap->cookie,
6981 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6982#ifdef FEAT_EVAL
6983 current_SID = save_current_SID;
6984#endif
6985 vim_free(buf);
6986 vim_free(split_buf);
6987}
6988
6989# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6990 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006991get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6994}
6995
6996/*
6997 * Function given to ExpandGeneric() to obtain the list of user command names.
6998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007000get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 if (idx < curbuf->b_ucmds.ga_len)
7003 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7004 idx -= curbuf->b_ucmds.ga_len;
7005 if (idx < ucmds.ga_len)
7006 return USER_CMD(idx)->uc_name;
7007 return NULL;
7008}
7009
7010/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007011 * Function given to ExpandGeneric() to obtain the list of user address type names.
7012 */
7013 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007014get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007015{
7016 return (char_u *)addr_type_complete[idx].name;
7017}
7018
7019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 * Function given to ExpandGeneric() to obtain the list of user command
7021 * attributes.
7022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007024get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007027 {"addr", "bang", "bar", "buffer", "complete",
7028 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007030 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 return NULL;
7032 return (char_u *)user_cmd_flags[idx];
7033}
7034
7035/*
7036 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007039get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7042
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007043 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 return NULL;
7045 return (char_u *)user_cmd_nargs[idx];
7046}
7047
7048/*
7049 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007052get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 return (char_u *)command_complete[idx].name;
7055}
7056# endif /* FEAT_CMDL_COMPL */
7057
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007058/*
7059 * Parse address type argument
7060 */
7061 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007062parse_addr_type_arg(
7063 char_u *value,
7064 int vallen,
7065 long *argt,
7066 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007067{
7068 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007069
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007070 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7071 {
7072 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7073 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7074 if (a && b)
7075 {
7076 *addr_type_arg = addr_type_complete[i].expand;
7077 break;
7078 }
7079 }
7080
7081 if (addr_type_complete[i].expand == -1)
7082 {
7083 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007084
Bram Moolenaar1c465442017-03-12 20:10:05 +01007085 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007086 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007087 err[i] = NUL;
7088 EMSG2(_("E180: Invalid address type value: %s"), err);
7089 return FAIL;
7090 }
7091
7092 if (*addr_type_arg != ADDR_LINES)
7093 *argt |= NOTADR;
7094
7095 return OK;
7096}
7097
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098#endif /* FEAT_USR_CMDS */
7099
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007100#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7101/*
7102 * Parse a completion argument "value[vallen]".
7103 * The detected completion goes in "*complp", argument type in "*argt".
7104 * When there is an argument, for function and user defined completion, it's
7105 * copied to allocated memory and stored in "*compl_arg".
7106 * Returns FAIL if something is wrong.
7107 */
7108 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007109parse_compl_arg(
7110 char_u *value,
7111 int vallen,
7112 int *complp,
7113 long *argt,
7114 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007115{
7116 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007117# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007118 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007119# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007120 int i;
7121 int valend = vallen;
7122
7123 /* Look for any argument part - which is the part after any ',' */
7124 for (i = 0; i < vallen; ++i)
7125 {
7126 if (value[i] == ',')
7127 {
7128 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007129# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007130 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007131# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007132 valend = i;
7133 break;
7134 }
7135 }
7136
7137 for (i = 0; command_complete[i].expand != 0; ++i)
7138 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007139 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007140 && STRNCMP(value, command_complete[i].name, valend) == 0)
7141 {
7142 *complp = command_complete[i].expand;
7143 if (command_complete[i].expand == EXPAND_BUFFERS)
7144 *argt |= BUFNAME;
7145 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7146 || command_complete[i].expand == EXPAND_FILES)
7147 *argt |= XFILE;
7148 break;
7149 }
7150 }
7151
7152 if (command_complete[i].expand == 0)
7153 {
7154 EMSG2(_("E180: Invalid complete value: %s"), value);
7155 return FAIL;
7156 }
7157
7158# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7159 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7160 && arg != NULL)
7161# else
7162 if (arg != NULL)
7163# endif
7164 {
7165 EMSG(_("E468: Completion argument only allowed for custom completion"));
7166 return FAIL;
7167 }
7168
7169# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7170 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7171 && arg == NULL)
7172 {
7173 EMSG(_("E467: Custom completion requires a function argument"));
7174 return FAIL;
7175 }
7176
7177 if (arg != NULL)
7178 *compl_arg = vim_strnsave(arg, (int)arglen);
7179# endif
7180 return OK;
7181}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007182
7183 int
7184cmdcomplete_str_to_type(char_u *complete_str)
7185{
7186 int i;
7187
7188 for (i = 0; command_complete[i].expand != 0; ++i)
7189 if (STRCMP(complete_str, command_complete[i].name) == 0)
7190 return command_complete[i].expand;
7191
7192 return EXPAND_NOTHING;
7193}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007194#endif
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007197ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
Bram Moolenaare6850792010-05-14 15:28:44 +02007199 if (*eap->arg == NUL)
7200 {
7201#ifdef FEAT_EVAL
7202 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7203 char_u *p = NULL;
7204
7205 if (expr != NULL)
7206 {
7207 ++emsg_off;
7208 p = eval_to_string(expr, NULL, FALSE);
7209 --emsg_off;
7210 vim_free(expr);
7211 }
7212 if (p != NULL)
7213 {
7214 MSG(p);
7215 vim_free(p);
7216 }
7217 else
7218 MSG("default");
7219#else
7220 MSG(_("unknown"));
7221#endif
7222 }
7223 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007224 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225}
7226
7227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 if (*eap->arg == NUL && eap->cmd[2] == '!')
7231 MSG(_("Greetings, Vim user!"));
7232 do_highlight(eap->arg, eap->forceit, FALSE);
7233}
7234
7235
7236/*
7237 * Call this function if we thought we were going to exit, but we won't
7238 * (because of an error). May need to restore the terminal mode.
7239 */
7240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007241not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 exiting = FALSE;
7244 settmode(TMODE_RAW);
7245}
7246
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007247 static int
7248before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7249{
7250 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7251
7252 /* Bail out when autocommands closed the window.
7253 * Refuse to quit when the buffer in the last window is being closed (can
7254 * only happen in autocommands). */
7255 if (!win_valid(wp)
7256 || curbuf_locked()
7257 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7258 return TRUE;
7259
7260 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7261 {
7262 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7263 /* Refuse to quit when locked or when the buffer in the last window is
7264 * being closed (can only happen in autocommands). */
7265 if (curbuf_locked()
7266 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7267 return TRUE;
7268 }
7269
7270 return FALSE;
7271}
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007274 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007275 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 */
7277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007278ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007280 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282#ifdef FEAT_CMDWIN
7283 if (cmdwin_type != 0)
7284 {
7285 cmdwin_result = Ctrl_C;
7286 return;
7287 }
7288#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007289 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007290 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007291 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007292 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007293 return;
7294 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007295 if (eap->addr_count > 0)
7296 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007297 int wnr = eap->line2;
7298
7299 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7300 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007301 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007302 }
7303 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007304 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007305
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007306 /* Refuse to quit when locked. */
7307 if (curbuf_locked())
7308 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007309
7310 /* Trigger QuitPre and maybe ExitPre */
7311 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007312 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
7314#ifdef FEAT_NETBEANS_INTG
7315 netbeansForcedQuit = eap->forceit;
7316#endif
7317
7318 /*
7319 * If there are more files or windows we won't exit.
7320 */
7321 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7322 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007323 if ((!buf_hide(wp->w_buffer)
7324 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007325 | (eap->forceit ? CCGD_FORCEIT : 0)
7326 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007328 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 {
7330 not_exiting();
7331 }
7332 else
7333 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007334 /* quit last window
7335 * Note: only_one_window() returns true, even so a help window is
7336 * still open. In that case only quit, if no address has been
7337 * specified. Example:
7338 * :h|wincmd w|1q - don't quit
7339 * :h|wincmd w|q - quit
7340 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007341 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007343 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007344#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007348 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350}
7351
7352/*
7353 * ":cquit".
7354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007356ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357{
7358 getout(1); /* this does not always pass on the exit code to the Manx
7359 compiler. why? */
7360}
7361
7362/*
7363 * ":qall": try to quit all windows
7364 */
7365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007366ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
7368# ifdef FEAT_CMDWIN
7369 if (cmdwin_type != 0)
7370 {
7371 if (eap->forceit)
7372 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7373 else
7374 cmdwin_result = K_XF2;
7375 return;
7376 }
7377# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007378
7379 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007380 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007381 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007382 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007383 return;
7384 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007385
7386 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007387 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007388
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007390 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 getout(0);
7392 not_exiting();
7393}
7394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395/*
7396 * ":close": close current window, unless it is the last one
7397 */
7398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007399ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007401 win_T *win;
7402 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007403#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007405 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007407#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007408 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007409 {
7410 if (eap->addr_count == 0)
7411 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007412 else
7413 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007414 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007415 {
7416 winnr++;
7417 if (winnr == eap->line2)
7418 break;
7419 }
7420 if (win == NULL)
7421 win = lastwin;
7422 ex_win_close(eap->forceit, win, NULL);
7423 }
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425}
7426
Bram Moolenaar4033c552017-09-16 20:54:51 +02007427#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428/*
7429 * ":pclose": Close any preview window.
7430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007432ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433{
7434 win_T *win;
7435
Bram Moolenaar29323592016-07-24 22:04:11 +02007436 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007437 if (win->w_p_pvw)
7438 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007439 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007440 break;
7441 }
7442}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007443#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007444
Bram Moolenaarf740b292006-02-16 22:11:02 +00007445/*
7446 * Close window "win" and take care of handling closing the last window for a
7447 * modified buffer.
7448 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007450ex_win_close(
7451 int forceit,
7452 win_T *win,
7453 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 int need_hide;
7456 buf_T *buf = win->w_buffer;
7457
7458 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007459 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007461#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if ((p_confirm || cmdmod.confirm) && p_write)
7463 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007464 bufref_T bufref;
7465
7466 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007468 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 return;
7470 need_hide = FALSE;
7471 }
7472 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007475 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 return;
7477 }
7478 }
7479
Bram Moolenaar4033c552017-09-16 20:54:51 +02007480#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007482#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007485 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007486 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007487 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007488 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007492 * Handle the argument for a tabpage related ex command.
7493 * Returns a tabpage number.
7494 * When an error is encountered then eap->errmsg is set.
7495 */
7496 static int
7497get_tabpage_arg(exarg_T *eap)
7498{
7499 int tab_number;
7500 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7501
7502 if (eap->arg && *eap->arg != NUL)
7503 {
7504 char_u *p = eap->arg;
7505 char_u *p_save;
7506 int relative = 0; /* argument +N/-N means: go to N places to the
7507 * right/left relative to the current position. */
7508
7509 if (*p == '-')
7510 {
7511 relative = -1;
7512 p++;
7513 }
7514 else if (*p == '+')
7515 {
7516 relative = 1;
7517 p++;
7518 }
7519
7520 p_save = p;
7521 tab_number = getdigits(&p);
7522
7523 if (relative == 0)
7524 {
7525 if (STRCMP(p, "$") == 0)
7526 tab_number = LAST_TAB_NR;
7527 else if (p == p_save || *p_save == '-' || *p != NUL
7528 || tab_number > LAST_TAB_NR)
7529 {
7530 /* No numbers as argument. */
7531 eap->errmsg = e_invarg;
7532 goto theend;
7533 }
7534 }
7535 else
7536 {
7537 if (*p_save == NUL)
7538 tab_number = 1;
7539 else if (p == p_save || *p_save == '-' || *p != NUL
7540 || tab_number == 0)
7541 {
7542 /* No numbers as argument. */
7543 eap->errmsg = e_invarg;
7544 goto theend;
7545 }
7546 tab_number = tab_number * relative + tabpage_index(curtab);
7547 if (!unaccept_arg0 && relative == -1)
7548 --tab_number;
7549 }
7550 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7551 eap->errmsg = e_invarg;
7552 }
7553 else if (eap->addr_count > 0)
7554 {
7555 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007556 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007557 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007558 tab_number = 0;
7559 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007560 else
7561 {
7562 tab_number = eap->line2;
7563 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7564 {
7565 --tab_number;
7566 if (tab_number < unaccept_arg0)
7567 eap->errmsg = e_invarg;
7568 }
7569 }
7570 }
7571 else
7572 {
7573 switch (eap->cmdidx)
7574 {
7575 case CMD_tabnext:
7576 tab_number = tabpage_index(curtab) + 1;
7577 if (tab_number > LAST_TAB_NR)
7578 tab_number = 1;
7579 break;
7580 case CMD_tabmove:
7581 tab_number = LAST_TAB_NR;
7582 break;
7583 default:
7584 tab_number = tabpage_index(curtab);
7585 }
7586 }
7587
7588theend:
7589 return tab_number;
7590}
7591
7592/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007593 * ":tabclose": close current tab page, unless it is the last one.
7594 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 */
7596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007597ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007599 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007600 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007601
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007602# ifdef FEAT_CMDWIN
7603 if (cmdwin_type != 0)
7604 cmdwin_result = K_IGNORE;
7605 else
7606# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007607 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007608 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007611 tab_number = get_tabpage_arg(eap);
7612 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007613 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007614 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007615 if (tp == NULL)
7616 {
7617 beep_flush();
7618 return;
7619 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007620 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007621 {
7622 tabpage_close_other(tp, eap->forceit);
7623 return;
7624 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007625 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007626 tabpage_close(eap->forceit);
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007629}
7630
7631/*
7632 * ":tabonly": close all tab pages except the current one
7633 */
7634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007635ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007636{
7637 tabpage_T *tp;
7638 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007639 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007640
7641# ifdef FEAT_CMDWIN
7642 if (cmdwin_type != 0)
7643 cmdwin_result = K_IGNORE;
7644 else
7645# endif
7646 if (first_tabpage->tp_next == NULL)
7647 MSG(_("Already only one tab page"));
7648 else
7649 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007650 tab_number = get_tabpage_arg(eap);
7651 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007652 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007653 goto_tabpage(tab_number);
7654 /* Repeat this up to a 1000 times, because autocommands may
7655 * mess up the lists. */
7656 for (done = 0; done < 1000; ++done)
7657 {
7658 FOR_ALL_TABPAGES(tp)
7659 if (tp->tp_topframe != topframe)
7660 {
7661 tabpage_close_other(tp, eap->forceit);
7662 /* if we failed to close it quit */
7663 if (valid_tabpage(tp))
7664 done = 1000;
7665 /* start over, "tp" is now invalid */
7666 break;
7667 }
7668 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007669 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007670 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007671 }
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
7675/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007676 * Close the current tab page.
7677 */
7678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007679tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007680{
7681 /* First close all the windows but the current one. If that worked then
7682 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007683 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007684 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007685 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007686 ex_win_close(forceit, curwin, NULL);
7687# ifdef FEAT_GUI
7688 need_mouse_correct = TRUE;
7689# endif
7690}
7691
7692/*
7693 * Close tab page "tp", which is not the current tab page.
7694 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007695 * Also takes care of the tab pages line disappearing when closing the
7696 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007697 */
7698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007700{
7701 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007702 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007703 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007704
7705 /* Limit to 1000 windows, autocommands may add a window while we close
7706 * one. OK, so I'm paranoid... */
7707 while (++done < 1000)
7708 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007709 wp = tp->tp_firstwin;
7710 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007711
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007712 /* Autocommands may delete the tab page under our fingers and we may
7713 * fail to close a window with a modified buffer. */
7714 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007715 break;
7716 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007717
Bram Moolenaar29323592016-07-24 22:04:11 +02007718 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007719
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007720 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007721 if (h != tabline_height())
7722 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007723}
7724
7725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 * ":only".
7727 */
7728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007729ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007731 win_T *wp;
7732 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733# ifdef FEAT_GUI
7734 need_mouse_correct = TRUE;
7735# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007736 if (eap->addr_count > 0)
7737 {
7738 wnr = eap->line2;
7739 for (wp = firstwin; --wnr > 0; )
7740 {
7741 if (wp->w_next == NULL)
7742 break;
7743 else
7744 wp = wp->w_next;
7745 }
7746 win_goto(wp);
7747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 close_others(TRUE, eap->forceit);
7749}
7750
7751/*
7752 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007753 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007755 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007756ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
7758 if (eap->addr_count == 0)
7759 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007760 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
7763 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007764ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007766 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007767 if (!eap->skip)
7768 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007769#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007770 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007771#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007772 if (eap->addr_count == 0)
7773 win_close(curwin, FALSE); /* don't free buffer */
7774 else
7775 {
7776 int winnr = 0;
7777 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007778
Bram Moolenaar2256c992016-11-15 21:17:07 +01007779 FOR_ALL_WINDOWS(win)
7780 {
7781 winnr++;
7782 if (winnr == eap->line2)
7783 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007784 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007785 if (win == NULL)
7786 win = lastwin;
7787 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
7790}
7791
7792/*
7793 * ":stop" and ":suspend": Suspend Vim.
7794 */
7795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007796ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 /*
7799 * Disallow suspending for "rvim".
7800 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007801 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {
7803 if (!eap->forceit)
7804 autowrite_all();
7805 windgoto((int)Rows - 1, 0);
7806 out_char('\n');
7807 out_flush();
7808 stoptermcap();
7809 out_flush(); /* needed for SUN to restore xterm buffer */
7810#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007811 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812#endif
7813 ui_suspend(); /* call machine specific function */
7814#ifdef FEAT_TITLE
7815 maketitle();
7816 resettitle(); /* force updating the title */
7817#endif
7818 starttermcap();
7819 scroll_start(); /* scroll screen before redrawing */
7820 redraw_later_clear();
7821 shell_resized(); /* may have resized window */
7822 }
7823}
7824
7825/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007826 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 */
7828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007829ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
7831#ifdef FEAT_CMDWIN
7832 if (cmdwin_type != 0)
7833 {
7834 cmdwin_result = Ctrl_C;
7835 return;
7836 }
7837#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007838 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007839 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007840 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007841 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007842 return;
7843 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007844
7845 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007846 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
7848 /*
7849 * if more files or windows we won't exit
7850 */
7851 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7852 exiting = TRUE;
7853 if ( ((eap->cmdidx == CMD_wq
7854 || curbufIsChanged())
7855 && do_write(eap) == FAIL)
7856 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007857 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {
7859 not_exiting();
7860 }
7861 else
7862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007865 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866# ifdef FEAT_GUI
7867 need_mouse_correct = TRUE;
7868# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007869 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007870 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872}
7873
7874/*
7875 * ":print", ":list", ":number".
7876 */
7877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007878ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007880 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7881 EMSG(_(e_emptybuf));
7882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007884 for ( ;!got_int; ui_breakcheck())
7885 {
7886 print_line(eap->line1,
7887 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7888 || (eap->flags & EXFLAG_NR)),
7889 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7890 if (++eap->line1 > eap->line2)
7891 break;
7892 out_flush(); /* show one line at a time */
7893 }
7894 setpcmark();
7895 /* put cursor at last line */
7896 curwin->w_cursor.lnum = eap->line2;
7897 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901}
7902
7903#ifdef FEAT_BYTEOFF
7904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007905ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 goto_byte(eap->line2);
7908}
7909#endif
7910
7911/*
7912 * ":shell".
7913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007915ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 do_shell(NULL, 0);
7918}
7919
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007920#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007922static int drop_busy = FALSE;
7923static int drop_filec;
7924static char_u **drop_filev = NULL;
7925static int drop_split;
7926static void (*drop_callback)(void *);
7927static void *drop_cookie;
7928
7929 static void
7930handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 exarg_T ea;
7933 int save_msg_scroll = msg_scroll;
7934
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007935 // Setting the argument list may cause screen updates and being called
7936 // recursively. Avoid that by setting drop_busy.
7937 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938
7939 /* Check whether the current buffer is changed. If so, we will need
7940 * to split the current window or data could be lost.
7941 * We don't need to check if the 'hidden' option is set, as in this
7942 * case the buffer won't be lost.
7943 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007944 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
7946 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007947 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 --emsg_off;
7949 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007950 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 if (win_split(0, 0) == FAIL)
7953 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007954 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
7956 /* When splitting the window, create a new alist. Otherwise the
7957 * existing one is overwritten. */
7958 alist_unlink(curwin->w_alist);
7959 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 }
7961
7962 /*
7963 * Set up the new argument list.
7964 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007965 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
7967 /*
7968 * Move to the first file.
7969 */
7970 /* Fake up a minimal "next" command for do_argfile() */
7971 vim_memset(&ea, 0, sizeof(ea));
7972 ea.cmd = (char_u *)"next";
7973 do_argfile(&ea, 0);
7974
7975 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7976 * mode that is not needed here. */
7977 need_start_insertmode = FALSE;
7978
7979 /* Restore msg_scroll, otherwise a following command may cause scrolling
7980 * unexpectedly. The screen will be redrawn by the caller, thus
7981 * msg_scroll being set by displaying a message is irrelevant. */
7982 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007983
7984 if (drop_callback != NULL)
7985 drop_callback(drop_cookie);
7986
7987 drop_filev = NULL;
7988 drop_busy = FALSE;
7989}
7990
7991/*
7992 * Handle a file drop. The code is here because a drop is *nearly* like an
7993 * :args command, but not quite (we have a list of exact filenames, so we
7994 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7995 * code is very similar to :args and hence needs access to a lot of the static
7996 * functions in this file.
7997 *
7998 * The "filev" list must have been allocated using alloc(), as should each item
7999 * in the list. This function takes over responsibility for freeing the "filev"
8000 * list.
8001 */
8002 void
8003handle_drop(
8004 int filec, // the number of files dropped
8005 char_u **filev, // the list of files dropped
8006 int split, // force splitting the window
8007 void (*callback)(void *), // to be called after setting the argument
8008 // list
8009 void *cookie) // argument for "callback" (allocated)
8010{
8011 // Cannot handle recursive drops, finish the pending one.
8012 if (drop_busy)
8013 {
8014 FreeWild(filec, filev);
8015 vim_free(cookie);
8016 return;
8017 }
8018
8019 // When calling handle_drop() more than once in a row we only use the last
8020 // one.
8021 if (drop_filev != NULL)
8022 {
8023 FreeWild(drop_filec, drop_filev);
8024 vim_free(drop_cookie);
8025 }
8026
8027 drop_filec = filec;
8028 drop_filev = filev;
8029 drop_split = split;
8030 drop_callback = callback;
8031 drop_cookie = cookie;
8032
8033 // Postpone this when:
8034 // - editing the command line
8035 // - not possible to change the current buffer
8036 // - updating the screen
8037 // As it may change buffers and window structures that are in use and cause
8038 // freed memory to be used.
8039 if (text_locked() || curbuf_locked() || updating_screen)
8040 return;
8041
8042 handle_drop_internal();
8043}
8044
8045/*
8046 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8047 * reset: Handle a postponed drop.
8048 */
8049 void
8050handle_any_postponed_drop(void)
8051{
8052 if (!drop_busy && drop_filev != NULL
8053 && !text_locked() && !curbuf_locked() && !updating_screen)
8054 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056#endif
8057
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058/*
8059 * Clear an argument list: free all file names and reset it to zero entries.
8060 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008061 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 while (--al->al_ga.ga_len >= 0)
8065 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8066 ga_clear(&al->al_ga);
8067}
8068
8069/*
8070 * Init an argument list.
8071 */
8072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008073alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8076}
8077
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078/*
8079 * Remove a reference from an argument list.
8080 * Ignored when the argument list is the global one.
8081 * If the argument list is no longer used by any window, free it.
8082 */
8083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 if (al != &global_alist && --al->al_refcount <= 0)
8087 {
8088 alist_clear(al);
8089 vim_free(al);
8090 }
8091}
8092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093/*
8094 * Create a new argument list and use it for the current window.
8095 */
8096 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8100 if (curwin->w_alist == NULL)
8101 {
8102 curwin->w_alist = &global_alist;
8103 ++global_alist.al_refcount;
8104 }
8105 else
8106 {
8107 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008108 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 alist_init(curwin->w_alist);
8110 }
8111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008113#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114/*
8115 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008116 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8117 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 */
8119 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008120alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008123 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 char_u **new_arg_files;
8125 int new_arg_file_count;
8126 char_u *save_p_su = p_su;
8127 int i;
8128
8129 /* Don't use 'suffixes' here. This should work like the shell did the
8130 * expansion. Also, the vimrc file isn't read yet, thus the user
8131 * can't set the options. */
8132 p_su = empty_option;
8133 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8134 if (old_arg_files != NULL)
8135 {
8136 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008137 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8138 old_arg_count = GARGCOUNT;
8139 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008141 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 && new_arg_file_count > 0)
8143 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008144 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8145 TRUE, fnum_list, fnum_len);
8146 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 }
8149 p_su = save_p_su;
8150}
8151#endif
8152
8153/*
8154 * Set the argument list for the current window.
8155 * Takes over the allocated files[] and the allocated fnames in it.
8156 */
8157 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158alist_set(
8159 alist_T *al,
8160 int count,
8161 char_u **files,
8162 int use_curbuf,
8163 int *fnum_list,
8164 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165{
8166 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008167 static int recursive = 0;
8168
8169 if (recursive)
8170 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008171 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008172 return;
8173 }
8174 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175
8176 alist_clear(al);
8177 if (ga_grow(&al->al_ga, count) == OK)
8178 {
8179 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008180 {
8181 if (got_int)
8182 {
8183 /* When adding many buffers this can take a long time. Allow
8184 * interrupting here. */
8185 while (i < count)
8186 vim_free(files[i++]);
8187 break;
8188 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008189
8190 /* May set buffer name of a buffer previously used for the
8191 * argument list, so that it's re-used by alist_add. */
8192 if (fnum_list != NULL && i < fnum_len)
8193 buf_set_name(fnum_list[i], files[i]);
8194
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008196 ui_breakcheck();
8197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 vim_free(files);
8199 }
8200 else
8201 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008204
8205 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206}
8207
8208/*
8209 * Add file "fname" to argument list "al".
8210 * "fname" must have been allocated and "al" must have been checked for room.
8211 */
8212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008213alist_add(
8214 alist_T *al,
8215 char_u *fname,
8216 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
8218 if (fname == NULL) /* don't add NULL file names */
8219 return;
8220#ifdef BACKSLASH_IN_FILENAME
8221 slash_adjust(fname);
8222#endif
8223 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8224 if (set_fnum > 0)
8225 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8226 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8227 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8231/*
8232 * Adjust slashes in file names. Called after 'shellslash' was set.
8233 */
8234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008235alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008239 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240
8241 for (i = 0; i < GARGCOUNT; ++i)
8242 if (GARGLIST[i].ae_fname != NULL)
8243 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008244 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (wp->w_alist != &global_alist)
8246 for (i = 0; i < WARGCOUNT(wp); ++i)
8247 if (WARGLIST(wp)[i].ae_fname != NULL)
8248 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249}
8250#endif
8251
8252/*
8253 * ":preserve".
8254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008258 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 ml_preserve(curbuf, TRUE);
8260}
8261
8262/*
8263 * ":recover".
8264 */
8265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008266ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267{
8268 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8269 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008270 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8271 | CCGD_MULTWIN
8272 | (eap->forceit ? CCGD_FORCEIT : 0)
8273 | CCGD_EXCMD)
8274
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 && (*eap->arg == NUL
8276 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8277 ml_recover();
8278 recoverymode = FALSE;
8279}
8280
8281/*
8282 * Command modifier used in a wrong way.
8283 */
8284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008285ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286{
8287 eap->errmsg = e_invcmd;
8288}
8289
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290/*
8291 * :sview [+command] file split window with new file, read-only
8292 * :split [[+command] file] split window with current or new file
8293 * :vsplit [[+command] file] split window vertically with current or new file
8294 * :new [[+command] file] split window with no or new file
8295 * :vnew [[+command] file] split vertically window with no or new file
8296 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008297 *
8298 * :tabedit open new Tab page with empty window
8299 * :tabedit [+command] file open new Tab page and edit "file"
8300 * :tabnew [[+command] file] just like :tabedit
8301 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 */
8303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008304ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008306 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008307#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008309#endif
8310#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008312#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008313 int use_tab = eap->cmdidx == CMD_tabedit
8314 || eap->cmdidx == CMD_tabfind
8315 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar4033c552017-09-16 20:54:51 +02008317#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
Bram Moolenaar4033c552017-09-16 20:54:51 +02008321#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008323 * quickfix windows. But it's OK when doing ":tab split". */
8324 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
8326 if (eap->cmdidx == CMD_split)
8327 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (eap->cmdidx == CMD_vsplit)
8329 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
Bram Moolenaar4033c552017-09-16 20:54:51 +02008333#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008334 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
8336 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8337 FNAME_MESS, TRUE, curbuf->b_ffname);
8338 if (fname == NULL)
8339 goto theend;
8340 eap->arg = fname;
8341 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008342# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008343 else
8344# endif
8345#endif
8346#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 && eap->cmdidx != CMD_new)
8350 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008351 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008352# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008353 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008354# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008355 au_has_group((char_u *)"FileExplorer"))
8356 {
8357 /* No browsing supported but we do have the file explorer:
8358 * Edit the directory. */
8359 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8360 eap->arg = (char_u *)".";
8361 }
8362 else
8363 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008364 fname = do_browse(0, (char_u *)(use_tab
8365 ? _("Edit File in new tab page")
8366 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008368 if (fname == NULL)
8369 goto theend;
8370 eap->arg = fname;
8371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 }
8373 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008376 /*
8377 * Either open new tab page or split the window.
8378 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008379 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008380 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008381 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8382 : eap->addr_count == 0 ? 0
8383 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008384 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008385 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008386
8387 /* set the alternate buffer for the window we came from */
8388 if (curwin != old_curwin
8389 && win_valid(old_curwin)
8390 && old_curwin->w_buffer != curbuf
8391 && !cmdmod.keepalt)
8392 old_curwin->w_alt_fnum = curbuf->b_fnum;
8393 }
8394 }
8395 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 /* Reset 'scrollbind' when editing another file, but keep it when
8399 * doing ":split" without arguments. */
8400 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008401# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008405 {
8406 RESET_BINDING(curwin);
8407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 else
8409 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 do_exedit(eap, old_curwin);
8411 }
8412
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008417# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418theend:
8419 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008422
8423/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008424 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008425 */
8426 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008427tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008428{
8429 exarg_T ea;
8430
8431 vim_memset(&ea, 0, sizeof(ea));
8432 ea.cmdidx = CMD_tabnew;
8433 ea.cmd = (char_u *)"tabn";
8434 ea.arg = (char_u *)"";
8435 ex_splitview(&ea);
8436}
8437
8438/*
8439 * :tabnext command
8440 */
8441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008443{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008444 int tab_number;
8445
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008446 switch (eap->cmdidx)
8447 {
8448 case CMD_tabfirst:
8449 case CMD_tabrewind:
8450 goto_tabpage(1);
8451 break;
8452 case CMD_tablast:
8453 goto_tabpage(9999);
8454 break;
8455 case CMD_tabprevious:
8456 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008457 if (eap->arg && *eap->arg != NUL)
8458 {
8459 char_u *p = eap->arg;
8460 char_u *p_save = p;
8461
8462 tab_number = getdigits(&p);
8463 if (p == p_save || *p_save == '-' || *p != NUL
8464 || tab_number == 0)
8465 {
8466 /* No numbers as argument. */
8467 eap->errmsg = e_invarg;
8468 return;
8469 }
8470 }
8471 else
8472 {
8473 if (eap->addr_count == 0)
8474 tab_number = 1;
8475 else
8476 {
8477 tab_number = eap->line2;
8478 if (tab_number < 1)
8479 {
8480 eap->errmsg = e_invrange;
8481 return;
8482 }
8483 }
8484 }
8485 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008486 break;
8487 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008488 tab_number = get_tabpage_arg(eap);
8489 if (eap->errmsg == NULL)
8490 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008491 break;
8492 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008493}
8494
8495/*
8496 * :tabmove command
8497 */
8498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008499ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008500{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008501 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008502
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008503 tab_number = get_tabpage_arg(eap);
8504 if (eap->errmsg == NULL)
8505 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008506}
8507
8508/*
8509 * :tabs command: List tabs and their contents.
8510 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008512ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008513{
8514 tabpage_T *tp;
8515 win_T *wp;
8516 int tabcount = 1;
8517
8518 msg_start();
8519 msg_scroll = TRUE;
8520 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8521 {
8522 msg_putchar('\n');
8523 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008524 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008525 out_flush(); /* output one line at a time */
8526 ui_breakcheck();
8527
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008528 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008529 wp = firstwin;
8530 else
8531 wp = tp->tp_firstwin;
8532 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8533 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008534 msg_putchar('\n');
8535 msg_putchar(wp == curwin ? '>' : ' ');
8536 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008537 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8538 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008539 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008540 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008541 else
8542 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8543 IObuff, IOSIZE, TRUE);
8544 msg_outtrans(IObuff);
8545 out_flush(); /* output one line at a time */
8546 ui_breakcheck();
8547 }
8548 }
8549}
8550
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551/*
8552 * ":mode": Set screen mode.
8553 * If no argument given, just get the screen size and redraw.
8554 */
8555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008556ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 if (*eap->arg == NUL)
8559 shell_resized();
8560 else
8561 mch_screenmode(eap->arg);
8562}
8563
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564/*
8565 * ":resize".
8566 * set, increment or decrement current window height
8567 */
8568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008569ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571 int n;
8572 win_T *wp = curwin;
8573
8574 if (eap->addr_count > 0)
8575 {
8576 n = eap->line2;
8577 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8578 ;
8579 }
8580
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008581# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (cmdmod.split & WSP_VERT)
8586 {
8587 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008588 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8590 n = 9999;
8591 win_setwidth_win((int)n, wp);
8592 }
8593 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {
8595 if (*eap->arg == '-' || *eap->arg == '+')
8596 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008597 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 n = 9999;
8599 win_setheight_win((int)n, wp);
8600 }
8601}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603/*
8604 * ":find [+command] <file>" command.
8605 */
8606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008607ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609#ifdef FEAT_SEARCHPATH
8610 char_u *fname;
8611 int count;
8612
8613 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8614 TRUE, curbuf->b_ffname);
8615 if (eap->addr_count > 0)
8616 {
8617 /* Repeat finding the file "count" times. This matters when it
8618 * appears several times in the path. */
8619 count = eap->line2;
8620 while (fname != NULL && --count > 0)
8621 {
8622 vim_free(fname);
8623 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8624 FALSE, curbuf->b_ffname);
8625 }
8626 }
8627
8628 if (fname != NULL)
8629 {
8630 eap->arg = fname;
8631#endif
8632 do_exedit(eap, NULL);
8633#ifdef FEAT_SEARCHPATH
8634 vim_free(fname);
8635 }
8636#endif
8637}
8638
8639/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008640 * ":open" simulation: for now just work like ":visual".
8641 */
8642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008643ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008644{
8645 regmatch_T regmatch;
8646 char_u *p;
8647
8648 curwin->w_cursor.lnum = eap->line2;
8649 beginline(BL_SOL | BL_FIX);
8650 if (*eap->arg == '/')
8651 {
8652 /* ":open /pattern/": put cursor in column found with pattern */
8653 ++eap->arg;
8654 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8655 *p = NUL;
8656 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8657 if (regmatch.regprog != NULL)
8658 {
8659 regmatch.rm_ic = p_ic;
8660 p = ml_get_curline();
8661 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008662 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008663 else
8664 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008665 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666 }
8667 /* Move to the NUL, ignore any other arguments. */
8668 eap->arg += STRLEN(eap->arg);
8669 }
8670 check_cursor();
8671
8672 eap->cmdidx = CMD_visual;
8673 do_exedit(eap, NULL);
8674}
8675
8676/*
8677 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 */
8679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008680ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 do_exedit(eap, NULL);
8683}
8684
8685/*
8686 * ":edit <file>" command and alikes.
8687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008689do_exedit(
8690 exarg_T *eap,
8691 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
8693 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008695 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
8697 /*
8698 * ":vi" command ends Ex mode.
8699 */
8700 if (exmode_active && (eap->cmdidx == CMD_visual
8701 || eap->cmdidx == CMD_view))
8702 {
8703 exmode_active = FALSE;
8704 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008705 {
8706 /* Special case: ":global/pat/visual\NLvi-commands" */
8707 if (global_busy)
8708 {
8709 int rd = RedrawingDisabled;
8710 int nwr = no_wait_return;
8711 int ms = msg_scroll;
8712#ifdef FEAT_GUI
8713 int he = hold_gui_events;
8714#endif
8715
8716 if (eap->nextcmd != NULL)
8717 {
8718 stuffReadbuff(eap->nextcmd);
8719 eap->nextcmd = NULL;
8720 }
8721
8722 if (exmode_was != EXMODE_VIM)
8723 settmode(TMODE_RAW);
8724 RedrawingDisabled = 0;
8725 no_wait_return = 0;
8726 need_wait_return = FALSE;
8727 msg_scroll = 0;
8728#ifdef FEAT_GUI
8729 hold_gui_events = 0;
8730#endif
8731 must_redraw = CLEAR;
8732
8733 main_loop(FALSE, TRUE);
8734
8735 RedrawingDisabled = rd;
8736 no_wait_return = nwr;
8737 msg_scroll = ms;
8738#ifdef FEAT_GUI
8739 hold_gui_events = he;
8740#endif
8741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 }
8745
8746 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008747 || eap->cmdidx == CMD_tabnew
8748 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008749 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008751 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 setpcmark();
8753 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008754 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8755 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008757 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 || *eap->arg != NUL
8759#ifdef FEAT_BROWSE
8760 || cmdmod.browse
8761#endif
8762 )
8763 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008764 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8765 * can bring us here, others are stopped earlier. */
8766 if (*eap->arg != NUL && curbuf_locked())
8767 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008768
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 n = readonlymode;
8770 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8771 readonlymode = TRUE;
8772 else if (eap->cmdidx == CMD_enew)
8773 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8774 empty buffer */
8775 setpcmark();
8776 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8777 NULL, eap,
8778 /* ":edit" goes to first line if Vi compatible */
8779 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8780 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8781 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008782 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008784 /* after a split we can use an existing buffer */
8785 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008787 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
8789 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 if (old_curwin != NULL)
8791 {
8792 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008793 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008795#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008796 cleanup_T cs;
8797
8798 /* Reset the error/interrupt/exception state here so that
8799 * aborting() returns FALSE when closing a window. */
8800 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008801#endif
8802#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008804#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008805 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008806
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008807#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008808 /* Restore the error/interrupt/exception state if not
8809 * discarded by a new aborting error, interrupt, or
8810 * uncaught exception. */
8811 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 }
8814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 }
8816 else if (readonlymode && curbuf->b_nwindows == 1)
8817 {
8818 /* When editing an already visited buffer, 'readonly' won't be set
8819 * but the previous value is kept. With ":view" and ":sview" we
8820 * want the file to be readonly, except when another window is
8821 * editing the same buffer. */
8822 curbuf->b_p_ro = TRUE;
8823 }
8824 readonlymode = n;
8825 }
8826 else
8827 {
8828 if (eap->do_ecmd_cmd != NULL)
8829 do_cmdline_cmd(eap->do_ecmd_cmd);
8830#ifdef FEAT_TITLE
8831 n = curwin->w_arg_idx_invalid;
8832#endif
8833 check_arg_idx(curwin);
8834#ifdef FEAT_TITLE
8835 if (n != curwin->w_arg_idx_invalid)
8836 maketitle();
8837#endif
8838 }
8839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 /*
8841 * if ":split file" worked, set alternate file name in old window to new
8842 * file
8843 */
8844 if (old_curwin != NULL
8845 && *eap->arg != NUL
8846 && curwin != old_curwin
8847 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008848 && old_curwin->w_buffer != curbuf
8849 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851
8852 ex_no_reprint = TRUE;
8853}
8854
8855#ifndef FEAT_GUI
8856/*
8857 * ":gui" and ":gvim" when there is no GUI.
8858 */
8859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008860ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862 eap->errmsg = e_nogvim;
8863}
8864#endif
8865
8866#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008868ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870 gui_make_tearoff(eap->arg);
8871}
8872#endif
8873
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008874#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8875 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008877ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008879# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8880 if (gui.in_use)
8881 gui_make_popup(eap->arg, eap->forceit);
8882# ifdef FEAT_TERM_POPUP_MENU
8883 else
8884# endif
8885# endif
8886# ifdef FEAT_TERM_POPUP_MENU
8887 pum_make_popup(eap->arg, eap->forceit);
8888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889}
8890#endif
8891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008893ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8896 MSG(_("No swap file"));
8897 else
8898 msg(curbuf->b_ml.ml_mfp->mf_fname);
8899}
8900
8901/*
8902 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8903 * offset.
8904 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008907ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008910 win_T *save_curwin = curwin;
8911 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 long topline;
8913 long y;
8914 linenr_T old_linenr = curwin->w_cursor.lnum;
8915
8916 setpcmark();
8917
8918 /*
8919 * determine max topline
8920 */
8921 if (curwin->w_p_scb)
8922 {
8923 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008924 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 {
8926 if (wp->w_p_scb && wp->w_buffer)
8927 {
8928 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8929 if (topline > y)
8930 topline = y;
8931 }
8932 }
8933 if (topline < 1)
8934 topline = 1;
8935 }
8936 else
8937 {
8938 topline = 1;
8939 }
8940
8941
8942 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008943 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008945 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
8947 if (curwin->w_p_scb)
8948 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008949 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 y = topline - curwin->w_topline;
8951 if (y > 0)
8952 scrollup(y, TRUE);
8953 else
8954 scrolldown(-y, TRUE);
8955 curwin->w_scbind_pos = topline;
8956 redraw_later(VALID);
8957 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 }
8960 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008961 curwin = save_curwin;
8962 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 if (curwin->w_p_scb)
8964 {
8965 did_syncbind = TRUE;
8966 checkpcmark();
8967 if (old_linenr != curwin->w_cursor.lnum)
8968 {
8969 char_u ctrl_o[2];
8970
8971 ctrl_o[0] = Ctrl_O;
8972 ctrl_o[1] = 0;
8973 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8974 }
8975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
8978
8979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008980ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008982 int i;
8983 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8984 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
8986 if (eap->usefilter) /* :r!cmd */
8987 do_bang(1, eap, FALSE, FALSE, TRUE);
8988 else
8989 {
8990 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8991 return;
8992
8993#ifdef FEAT_BROWSE
8994 if (cmdmod.browse)
8995 {
8996 char_u *browseFile;
8997
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008998 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 NULL, NULL, NULL, curbuf);
9000 if (browseFile != NULL)
9001 {
9002 i = readfile(browseFile, NULL,
9003 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9004 vim_free(browseFile);
9005 }
9006 else
9007 i = OK;
9008 }
9009 else
9010#endif
9011 if (*eap->arg == NUL)
9012 {
9013 if (check_fname() == FAIL) /* check for no file name */
9014 return;
9015 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9016 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9017 }
9018 else
9019 {
9020 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9021 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9022 i = readfile(eap->arg, NULL,
9023 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9024
9025 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009026 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009028#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 if (!aborting())
9030#endif
9031 EMSG2(_(e_notopen), eap->arg);
9032 }
9033 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009034 {
9035 if (empty && exmode_active)
9036 {
9037 /* Delete the empty line that remains. Historically ex does
9038 * this but vi doesn't. */
9039 if (eap->line2 == 0)
9040 lnum = curbuf->b_ml.ml_line_count;
9041 else
9042 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009043 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009044 {
9045 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009046 if (curwin->w_cursor.lnum > 1
9047 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009048 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009049 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009050 }
9051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
9055}
9056
Bram Moolenaarea408852005-06-25 22:49:46 +00009057static char_u *prev_dir = NULL;
9058
9059#if defined(EXITFREE) || defined(PROTO)
9060 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009061free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009062{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009063 VIM_CLEAR(prev_dir);
9064 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009065}
9066#endif
9067
Bram Moolenaarf4258302013-06-02 18:20:17 +02009068/*
9069 * Deal with the side effects of changing the current directory.
9070 * When "local" is TRUE then this was after an ":lcd" command.
9071 */
9072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009073post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009074{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009075 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009076 if (local)
9077 {
9078 /* If still in global directory, need to remember current
9079 * directory as global directory. */
9080 if (globaldir == NULL && prev_dir != NULL)
9081 globaldir = vim_strsave(prev_dir);
9082 /* Remember this local directory for the window. */
9083 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9084 curwin->w_localdir = vim_strsave(NameBuff);
9085 }
9086 else
9087 {
9088 /* We are now in the global directory, no need to remember its
9089 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009090 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009091 }
9092
9093 shorten_fnames(TRUE);
9094}
9095
Bram Moolenaarea408852005-06-25 22:49:46 +00009096
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097/*
9098 * ":cd", ":lcd", ":chdir" and ":lchdir".
9099 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009101ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 char_u *new_dir;
9104 char_u *tofree;
9105
9106 new_dir = eap->arg;
9107#if !defined(UNIX) && !defined(VMS)
9108 /* for non-UNIX ":cd" means: print current directory */
9109 if (*new_dir == NUL)
9110 ex_pwd(NULL);
9111 else
9112#endif
9113 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009114 if (allbuf_locked())
9115 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009116 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9117 && !eap->forceit)
9118 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009119 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009120 return;
9121 }
9122
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 /* ":cd -": Change to previous directory */
9124 if (STRCMP(new_dir, "-") == 0)
9125 {
9126 if (prev_dir == NULL)
9127 {
9128 EMSG(_("E186: No previous directory"));
9129 return;
9130 }
9131 new_dir = prev_dir;
9132 }
9133
9134 /* Save current directory for next ":cd -" */
9135 tofree = prev_dir;
9136 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9137 prev_dir = vim_strsave(NameBuff);
9138 else
9139 prev_dir = NULL;
9140
9141#if defined(UNIX) || defined(VMS)
9142 /* for UNIX ":cd" means: go to home directory */
9143 if (*new_dir == NUL)
9144 {
9145 /* use NameBuff for home directory name */
9146# ifdef VMS
9147 char_u *p;
9148
9149 p = mch_getenv((char_u *)"SYS$LOGIN");
9150 if (p == NULL || *p == NUL) /* empty is the same as not set */
9151 NameBuff[0] = NUL;
9152 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009153 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154# else
9155 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9156# endif
9157 new_dir = NameBuff;
9158 }
9159#endif
9160 if (new_dir == NULL || vim_chdir(new_dir))
9161 EMSG(_(e_failed));
9162 else
9163 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009164 int is_local_chdir = eap->cmdidx == CMD_lcd
9165 || eap->cmdidx == CMD_lchdir;
9166
9167 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168
9169 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009170 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009172 apply_autocmds(EVENT_DIRCHANGED,
9173 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9174 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 }
9176 vim_free(tofree);
9177 }
9178}
9179
9180/*
9181 * ":pwd".
9182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009184ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9187 {
9188#ifdef BACKSLASH_IN_FILENAME
9189 slash_adjust(NameBuff);
9190#endif
9191 msg(NameBuff);
9192 }
9193 else
9194 EMSG(_("E187: Unknown"));
9195}
9196
9197/*
9198 * ":=".
9199 */
9200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009201ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009204 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205}
9206
9207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009208ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009210 int n;
9211 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212
9213 if (cursor_valid())
9214 {
9215 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9216 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009217 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009219
9220 len = eap->line2;
9221 switch (*eap->arg)
9222 {
9223 case 'm': break;
9224 case NUL: len *= 1000L; break;
9225 default: EMSG2(_(e_invarg2), eap->arg); return;
9226 }
9227 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228}
9229
9230/*
9231 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9232 */
9233 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009234do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
9236 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009237 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
9239 cursor_on();
9240 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009241 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009243 wait_now = msec - done > 1000L ? 1000L : msec - done;
9244#ifdef FEAT_TIMERS
9245 {
9246 long due_time = check_due_timer();
9247
9248 if (due_time > 0 && due_time < wait_now)
9249 wait_now = due_time;
9250 }
9251#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009252#ifdef FEAT_JOB_CHANNEL
9253 if (has_any_channel() && wait_now > 100L)
9254 wait_now = 100L;
9255#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009256 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009257#ifdef FEAT_JOB_CHANNEL
9258 if (has_any_channel())
9259 ui_breakcheck_force(TRUE);
9260 else
9261#endif
9262 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009263#ifdef MESSAGE_QUEUE
9264 /* Process the netbeans and clientserver messages that may have been
9265 * received in the call to ui_breakcheck() when the GUI is in use. This
9266 * may occur when running a test case. */
9267 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009270
9271 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9272 // input buffer, otherwise a following call to input() fails.
9273 if (got_int)
9274 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275}
9276
9277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009278do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280 int mode;
9281 char_u *cmdp;
9282
9283 cmdp = eap->cmd;
9284 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9285
9286 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9287 eap->arg, mode, isabbrev))
9288 {
9289 case 1: EMSG(_(e_invarg));
9290 break;
9291 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9292 break;
9293 }
9294}
9295
9296/*
9297 * ":winsize" command (obsolete).
9298 */
9299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009300ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 int w, h;
9303 char_u *arg = eap->arg;
9304 char_u *p;
9305
9306 w = getdigits(&arg);
9307 arg = skipwhite(arg);
9308 p = arg;
9309 h = getdigits(&arg);
9310 if (*p != NUL && *arg == NUL)
9311 set_shellsize(w, h, TRUE);
9312 else
9313 EMSG(_("E465: :winsize requires two number arguments"));
9314}
9315
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009317ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318{
9319 int xchar = NUL;
9320 char_u *p;
9321
9322 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9323 {
9324 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9325 if (eap->arg[1] == NUL)
9326 {
9327 EMSG(_(e_invarg));
9328 return;
9329 }
9330 xchar = eap->arg[1];
9331 p = eap->arg + 2;
9332 }
9333 else
9334 p = eap->arg + 1;
9335
9336 eap->nextcmd = check_nextcmd(p);
9337 p = skipwhite(p);
9338 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9339 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009340 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 {
9342 /* Pass flags on for ":vertical wincmd ]". */
9343 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009344 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9346 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009347 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 }
9349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350
Bram Moolenaar843ee412004-06-30 16:16:41 +00009351#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352/*
9353 * ":winpos".
9354 */
9355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009356ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357{
9358 int x, y;
9359 char_u *arg = eap->arg;
9360 char_u *p;
9361
9362 if (*arg == NUL)
9363 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009364# if defined(FEAT_GUI) || defined(MSWIN)
9365# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009367# else
9368 if (mch_get_winpos(&x, &y) != FAIL)
9369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 {
9371 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9372 msg(IObuff);
9373 }
9374 else
9375# endif
9376 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9377 }
9378 else
9379 {
9380 x = getdigits(&arg);
9381 arg = skipwhite(arg);
9382 p = arg;
9383 y = getdigits(&arg);
9384 if (*p == NUL || *arg != NUL)
9385 {
9386 EMSG(_("E466: :winpos requires two number arguments"));
9387 return;
9388 }
9389# ifdef FEAT_GUI
9390 if (gui.in_use)
9391 gui_mch_set_winpos(x, y);
9392 else if (gui.starting)
9393 {
9394 /* Remember the coordinates for when the window is opened. */
9395 gui_win_x = x;
9396 gui_win_y = y;
9397 }
9398# ifdef HAVE_TGETENT
9399 else
9400# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009401# else
9402# ifdef MSWIN
9403 mch_set_winpos(x, y);
9404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405# endif
9406# ifdef HAVE_TGETENT
9407 if (*T_CWP)
9408 term_set_winpos(x, y);
9409# endif
9410 }
9411}
9412#endif
9413
9414/*
9415 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9416 */
9417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009418ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420 oparg_T oa;
9421
9422 clear_oparg(&oa);
9423 oa.regname = eap->regname;
9424 oa.start.lnum = eap->line1;
9425 oa.end.lnum = eap->line2;
9426 oa.line_count = eap->line2 - eap->line1 + 1;
9427 oa.motion_type = MLINE;
9428#ifdef FEAT_VIRTUALEDIT
9429 virtual_op = FALSE;
9430#endif
9431 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9432 {
9433 setpcmark();
9434 curwin->w_cursor.lnum = eap->line1;
9435 beginline(BL_SOL | BL_FIX);
9436 }
9437
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009438 if (VIsual_active)
9439 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009440
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 switch (eap->cmdidx)
9442 {
9443 case CMD_delete:
9444 oa.op_type = OP_DELETE;
9445 op_delete(&oa);
9446 break;
9447
9448 case CMD_yank:
9449 oa.op_type = OP_YANK;
9450 (void)op_yank(&oa, FALSE, TRUE);
9451 break;
9452
9453 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009454 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009456 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9457#else
9458 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009460 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 oa.op_type = OP_RSHIFT;
9462 else
9463 oa.op_type = OP_LSHIFT;
9464 op_shift(&oa, FALSE, eap->amount);
9465 break;
9466 }
9467#ifdef FEAT_VIRTUALEDIT
9468 virtual_op = MAYBE;
9469#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009470 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471}
9472
9473/*
9474 * ":put".
9475 */
9476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009477ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478{
9479 /* ":0put" works like ":1put!". */
9480 if (eap->line2 == 0)
9481 {
9482 eap->line2 = 1;
9483 eap->forceit = TRUE;
9484 }
9485 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009486 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9487 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488}
9489
9490/*
9491 * Handle ":copy" and ":move".
9492 */
9493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009494ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495{
9496 long n;
9497
Bram Moolenaarded27822017-01-02 14:27:34 +01009498 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (eap->arg == NULL) /* error detected */
9500 {
9501 eap->nextcmd = NULL;
9502 return;
9503 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009504 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505
9506 /*
9507 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9508 */
9509 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9510 {
9511 EMSG(_(e_invaddr));
9512 return;
9513 }
9514
9515 if (eap->cmdidx == CMD_move)
9516 {
9517 if (do_move(eap->line1, eap->line2, n) == FAIL)
9518 return;
9519 }
9520 else
9521 ex_copy(eap->line1, eap->line2, n);
9522 u_clearline();
9523 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009524 ex_may_print(eap);
9525}
9526
9527/*
9528 * Print the current line if flags were given to the Ex command.
9529 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009530 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009531ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009532{
9533 if (eap->flags != 0)
9534 {
9535 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9536 (eap->flags & EXFLAG_LIST));
9537 ex_no_reprint = TRUE;
9538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539}
9540
9541/*
9542 * ":smagic" and ":snomagic".
9543 */
9544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009545ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547 int magic_save = p_magic;
9548
9549 p_magic = (eap->cmdidx == CMD_smagic);
9550 do_sub(eap);
9551 p_magic = magic_save;
9552}
9553
9554/*
9555 * ":join".
9556 */
9557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009558ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560 curwin->w_cursor.lnum = eap->line1;
9561 if (eap->line1 == eap->line2)
9562 {
9563 if (eap->addr_count >= 2) /* :2,2join does nothing */
9564 return;
9565 if (eap->line2 == curbuf->b_ml.ml_line_count)
9566 {
9567 beep_flush();
9568 return;
9569 }
9570 ++eap->line2;
9571 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009572 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009574 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576
9577/*
9578 * ":[addr]@r" or ":[addr]*r": execute register
9579 */
9580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009581ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009584 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585
9586 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009587 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588
9589#ifdef USE_ON_FLY_SCROLL
9590 dont_scroll = TRUE; /* disallow scrolling here */
9591#endif
9592
9593 /* get the register name. No name means to use the previous one */
9594 c = *eap->arg;
9595 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9596 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009597 /* Put the register in the typeahead buffer with the "silent" flag. */
9598 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9599 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 else
9604 {
9605 int save_efr = exec_from_reg;
9606
9607 exec_from_reg = TRUE;
9608
9609 /*
9610 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009611 * Continue until the stuff buffer is empty and all added characters
9612 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009614 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9616
9617 exec_from_reg = save_efr;
9618 }
9619}
9620
9621/*
9622 * ":!".
9623 */
9624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009625ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626{
9627 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9628}
9629
9630/*
9631 * ":undo".
9632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009634ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009636 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009637 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009638 else
9639 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640}
9641
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009642#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009644ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009645{
9646 char_u hash[UNDO_HASH_SIZE];
9647
9648 u_compute_hash(hash);
9649 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9650}
9651
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009653ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009654{
9655 char_u hash[UNDO_HASH_SIZE];
9656
9657 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009658 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009659}
9660#endif
9661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662/*
9663 * ":redo".
9664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009666ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668 u_redo(1);
9669}
9670
9671/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009672 * ":earlier" and ":later".
9673 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009675ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009676{
9677 long count = 0;
9678 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009679 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009680 char_u *p = eap->arg;
9681
9682 if (*p == NUL)
9683 count = 1;
9684 else if (isdigit(*p))
9685 {
9686 count = getdigits(&p);
9687 switch (*p)
9688 {
9689 case 's': ++p; sec = TRUE; break;
9690 case 'm': ++p; sec = TRUE; count *= 60; break;
9691 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009692 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9693 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009694 }
9695 }
9696
9697 if (*p != NUL)
9698 EMSG2(_(e_invarg2), eap->arg);
9699 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009700 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9701 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009702}
9703
9704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 * ":redir": start/stop redirection.
9706 */
9707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009708ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 char *mode;
9711 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009712 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713
Bram Moolenaarba768492016-07-08 15:32:54 +02009714#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009715 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009716 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009717 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009718 return;
9719 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009720#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 if (STRICMP(eap->arg, "END") == 0)
9723 close_redir();
9724 else
9725 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009726 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009728 ++arg;
9729 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009731 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 mode = "a";
9733 }
9734 else
9735 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009736 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737
9738 close_redir();
9739
9740 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009741 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if (fname == NULL)
9743 return;
9744#ifdef FEAT_BROWSE
9745 if (cmdmod.browse)
9746 {
9747 char_u *browseFile;
9748
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009749 browseFile = do_browse(BROWSE_SAVE,
9750 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009751 fname, NULL, NULL,
9752 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 if (browseFile == NULL)
9754 return; /* operation cancelled */
9755 vim_free(fname);
9756 fname = browseFile;
9757 eap->forceit = TRUE; /* since dialog already asked */
9758 }
9759#endif
9760
9761 redir_fd = open_exfile(fname, eap->forceit, mode);
9762 vim_free(fname);
9763 }
9764#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009765 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
9767 /* redirect to a register a-z (resp. A-Z for appending) */
9768 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009769 ++arg;
9770 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009772 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009773 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009775 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009777 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009778 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009779 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009780 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009782 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009783 if (*arg == '>')
9784 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009785 /* Make register empty when not using @A-@Z and the
9786 * command is valid. */
9787 if (*arg == NUL && !isupper(redir_reg))
9788 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009790 }
9791 if (*arg != NUL)
9792 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009793 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009794 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009795 }
9796 }
9797 else if (*arg == '=' && arg[1] == '>')
9798 {
9799 int append;
9800
9801 /* redirect to a variable */
9802 close_redir();
9803 arg += 2;
9804
9805 if (*arg == '>')
9806 {
9807 ++arg;
9808 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 }
9810 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009811 append = FALSE;
9812
9813 if (var_redir_start(skipwhite(arg), append) == OK)
9814 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 }
9816#endif
9817
9818 /* TODO: redirect to a buffer */
9819
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009821 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009823
9824 /* Make sure redirection is not off. Can happen for cmdline completion
9825 * that indirectly invokes a command to catch its output. */
9826 if (redir_fd != NULL
9827#ifdef FEAT_EVAL
9828 || redir_reg || redir_vname
9829#endif
9830 )
9831 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832}
9833
9834/*
9835 * ":redraw": force redraw
9836 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009837 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009838ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840 int r = RedrawingDisabled;
9841 int p = p_lz;
9842
9843 RedrawingDisabled = 0;
9844 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009845 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009847 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848#ifdef FEAT_TITLE
9849 if (need_maketitle)
9850 maketitle();
9851#endif
9852 RedrawingDisabled = r;
9853 p_lz = p;
9854
9855 /* Reset msg_didout, so that a message that's there is overwritten. */
9856 msg_didout = FALSE;
9857 msg_col = 0;
9858
Bram Moolenaar56667a52013-07-17 11:54:28 +02009859 /* No need to wait after an intentional redraw. */
9860 need_wait_return = FALSE;
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 out_flush();
9863}
9864
9865/*
9866 * ":redrawstatus": force redraw of status line(s)
9867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009869ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 int r = RedrawingDisabled;
9872 int p = p_lz;
9873
9874 RedrawingDisabled = 0;
9875 p_lz = FALSE;
9876 if (eap->forceit)
9877 status_redraw_all();
9878 else
9879 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009880 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 RedrawingDisabled = r;
9882 p_lz = p;
9883 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884}
9885
9886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009887close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889 if (redir_fd != NULL)
9890 {
9891 fclose(redir_fd);
9892 redir_fd = NULL;
9893 }
9894#ifdef FEAT_EVAL
9895 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009896 if (redir_vname)
9897 {
9898 var_redir_stop();
9899 redir_vname = 0;
9900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901#endif
9902}
9903
9904#if defined(FEAT_SESSION) && defined(USE_CRNL)
9905# define MKSESSION_NL
9906static int mksession_nl = FALSE; /* use NL only in put_eol() */
9907#endif
9908
9909/*
9910 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9911 */
9912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009913ex_mkrc(
9914 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 FILE *fd;
9917 int failed = FALSE;
9918 char_u *fname;
9919#ifdef FEAT_BROWSE
9920 char_u *browseFile = NULL;
9921#endif
9922#ifdef FEAT_SESSION
9923 int view_session = FALSE;
9924 int using_vdir = FALSE; /* using 'viewdir'? */
9925 char_u *viewFile = NULL;
9926 unsigned *flagp;
9927#endif
9928
9929 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9930 {
9931#ifdef FEAT_SESSION
9932 view_session = TRUE;
9933#else
9934 ex_ni(eap);
9935 return;
9936#endif
9937 }
9938
9939#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009940 /* Use the short file name until ":lcd" is used. We also don't use the
9941 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009942 did_lcd = FALSE;
9943
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9945 if (eap->cmdidx == CMD_mkview
9946 && (*eap->arg == NUL
9947 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9948 {
9949 eap->forceit = TRUE;
9950 fname = get_view_file(*eap->arg);
9951 if (fname == NULL)
9952 return;
9953 viewFile = fname;
9954 using_vdir = TRUE;
9955 }
9956 else
9957#endif
9958 if (*eap->arg != NUL)
9959 fname = eap->arg;
9960 else if (eap->cmdidx == CMD_mkvimrc)
9961 fname = (char_u *)VIMRC_FILE;
9962#ifdef FEAT_SESSION
9963 else if (eap->cmdidx == CMD_mksession)
9964 fname = (char_u *)SESSION_FILE;
9965#endif
9966 else
9967 fname = (char_u *)EXRC_FILE;
9968
9969#ifdef FEAT_BROWSE
9970 if (cmdmod.browse)
9971 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009972 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973# ifdef FEAT_SESSION
9974 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9975 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9976# endif
9977 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009978 fname, (char_u *)"vim", NULL,
9979 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (browseFile == NULL)
9981 goto theend;
9982 fname = browseFile;
9983 eap->forceit = TRUE; /* since dialog already asked */
9984 }
9985#endif
9986
9987#if defined(FEAT_SESSION) && defined(vim_mkdir)
9988 /* When using 'viewdir' may have to create the directory. */
9989 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009990 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991#endif
9992
9993 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9994 if (fd != NULL)
9995 {
9996#ifdef FEAT_SESSION
9997 if (eap->cmdidx == CMD_mkview)
9998 flagp = &vop_flags;
9999 else
10000 flagp = &ssop_flags;
10001#endif
10002
10003#ifdef MKSESSION_NL
10004 /* "unix" in 'sessionoptions': use NL line separator */
10005 if (view_session && (*flagp & SSOP_UNIX))
10006 mksession_nl = TRUE;
10007#endif
10008
10009 /* Write the version command for :mkvimrc */
10010 if (eap->cmdidx == CMD_mkvimrc)
10011 (void)put_line(fd, "version 6.0");
10012
10013#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010014 if (eap->cmdidx == CMD_mksession)
10015 {
10016 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10017 failed = TRUE;
10018 }
10019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 if (eap->cmdidx != CMD_mkview)
10021#endif
10022 {
10023 /* Write setting 'compatible' first, because it has side effects.
10024 * For that same reason only do it when needed. */
10025 if (p_cp)
10026 (void)put_line(fd, "if !&cp | set cp | endif");
10027 else
10028 (void)put_line(fd, "if &cp | set nocp | endif");
10029 }
10030
10031#ifdef FEAT_SESSION
10032 if (!view_session
10033 || (eap->cmdidx == CMD_mksession
10034 && (*flagp & SSOP_OPTIONS)))
10035#endif
10036 failed |= (makemap(fd, NULL) == FAIL
10037 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10038
10039#ifdef FEAT_SESSION
10040 if (!failed && view_session)
10041 {
10042 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10043 failed = TRUE;
10044 if (eap->cmdidx == CMD_mksession)
10045 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010046 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaard9462e32011-04-11 21:35:11 +020010048 dirnow = alloc(MAXPATHL);
10049 if (dirnow == NULL)
10050 failed = TRUE;
10051 else
10052 {
10053 /*
10054 * Change to session file's dir.
10055 */
10056 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010058 *dirnow = NUL;
10059 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10060 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010061 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010062 shorten_fnames(TRUE);
10063 }
10064 else if (*dirnow != NUL
10065 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10066 {
10067 if (mch_chdir((char *)globaldir) == 0)
10068 shorten_fnames(TRUE);
10069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaard9462e32011-04-11 21:35:11 +020010071 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
Bram Moolenaard9462e32011-04-11 21:35:11 +020010073 /* restore original dir */
10074 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010076 {
10077 if (mch_chdir((char *)dirnow) != 0)
10078 EMSG(_(e_prev_dir));
10079 shorten_fnames(TRUE);
10080 }
10081 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 }
10083 }
10084 else
10085 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010086 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10087 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 }
10089 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10090 == FAIL)
10091 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010092 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10093 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010094 if (eap->cmdidx == CMD_mksession)
10095 {
10096 if (put_line(fd, "unlet SessionLoad") == FAIL)
10097 failed = TRUE;
10098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
10100#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010101 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10102 failed = TRUE;
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 failed |= fclose(fd);
10105
10106 if (failed)
10107 EMSG(_(e_write));
10108#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10109 else if (eap->cmdidx == CMD_mksession)
10110 {
10111 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010112 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113
Bram Moolenaard9462e32011-04-11 21:35:11 +020010114 tbuf = alloc(MAXPATHL);
10115 if (tbuf != NULL)
10116 {
10117 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10118 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10119 vim_free(tbuf);
10120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 }
10122#endif
10123#ifdef MKSESSION_NL
10124 mksession_nl = FALSE;
10125#endif
10126 }
10127
10128#ifdef FEAT_BROWSE
10129theend:
10130 vim_free(browseFile);
10131#endif
10132#ifdef FEAT_SESSION
10133 vim_free(viewFile);
10134#endif
10135}
10136
Bram Moolenaardf177f62005-02-22 08:39:57 +000010137#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10138 || defined(PROTO)
10139 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010140vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010141{
10142 if (vim_mkdir(name, prot) != 0)
10143 {
10144 EMSG2(_("E739: Cannot create directory: %s"), name);
10145 return FAIL;
10146 }
10147 return OK;
10148}
10149#endif
10150
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151/*
10152 * Open a file for writing for an Ex command, with some checks.
10153 * Return file descriptor, or NULL on failure.
10154 */
10155 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010156open_exfile(
10157 char_u *fname,
10158 int forceit,
10159 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160{
10161 FILE *fd;
10162
10163#ifdef UNIX
10164 /* with Unix it is possible to open a directory */
10165 if (mch_isdir(fname))
10166 {
10167 EMSG2(_(e_isadir2), fname);
10168 return NULL;
10169 }
10170#endif
10171 if (!forceit && *mode != 'a' && vim_fexists(fname))
10172 {
10173 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10174 return NULL;
10175 }
10176
10177 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10178 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10179
10180 return fd;
10181}
10182
10183/*
10184 * ":mark" and ":k".
10185 */
10186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010187ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188{
10189 pos_T pos;
10190
10191 if (*eap->arg == NUL) /* No argument? */
10192 EMSG(_(e_argreq));
10193 else if (eap->arg[1] != NUL) /* more than one character? */
10194 EMSG(_(e_trailing));
10195 else
10196 {
10197 pos = curwin->w_cursor; /* save curwin->w_cursor */
10198 curwin->w_cursor.lnum = eap->line2;
10199 beginline(BL_WHITE | BL_FIX);
10200 if (setmark(*eap->arg) == FAIL) /* set mark */
10201 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10202 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10203 }
10204}
10205
10206/*
10207 * Update w_topline, w_leftcol and the cursor position.
10208 */
10209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010210update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211{
10212 check_cursor(); /* put cursor on valid line */
10213 update_topline();
10214 if (!curwin->w_p_wrap)
10215 validate_cursor();
10216 update_curswant();
10217}
10218
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010220 * Save the current State and go to Normal mode.
10221 * Return TRUE if the typeahead could be saved.
10222 */
10223 int
10224save_current_state(save_state_T *sst)
10225{
10226 sst->save_msg_scroll = msg_scroll;
10227 sst->save_restart_edit = restart_edit;
10228 sst->save_msg_didout = msg_didout;
10229 sst->save_State = State;
10230 sst->save_insertmode = p_im;
10231 sst->save_finish_op = finish_op;
10232 sst->save_opcount = opcount;
10233
10234 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10235 restart_edit = 0; /* don't go to Insert mode */
10236 p_im = FALSE; /* don't use 'insertmode' */
10237
10238 /*
10239 * Save the current typeahead. This is required to allow using ":normal"
10240 * from an event handler and makes sure we don't hang when the argument
10241 * ends with half a command.
10242 */
10243 save_typeahead(&sst->tabuf);
10244 return sst->tabuf.typebuf_valid;
10245}
10246
10247 void
10248restore_current_state(save_state_T *sst)
10249{
10250 /* Restore the previous typeahead. */
10251 restore_typeahead(&sst->tabuf);
10252
10253 msg_scroll = sst->save_msg_scroll;
10254 restart_edit = sst->save_restart_edit;
10255 p_im = sst->save_insertmode;
10256 finish_op = sst->save_finish_op;
10257 opcount = sst->save_opcount;
10258 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10259
10260 /* Restore the state (needed when called from a function executed for
10261 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10262 State = sst->save_State;
10263#ifdef CURSOR_SHAPE
10264 ui_cursor_shape(); /* may show different cursor shape */
10265#endif
10266}
10267
10268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 * ":normal[!] {commands}": Execute normal mode commands.
10270 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010272ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010274 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275#ifdef FEAT_MBYTE
10276 char_u *arg = NULL;
10277 int l;
10278 char_u *p;
10279#endif
10280
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010281 if (ex_normal_lock > 0)
10282 {
10283 EMSG(_(e_secure));
10284 return;
10285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 if (ex_normal_busy >= p_mmd)
10287 {
10288 EMSG(_("E192: Recursive use of :normal too deep"));
10289 return;
10290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
10292#ifdef FEAT_MBYTE
10293 /*
10294 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10295 * this for the K_SPECIAL leading byte, otherwise special keys will not
10296 * work.
10297 */
10298 if (has_mbyte)
10299 {
10300 int len = 0;
10301
10302 /* Count the number of characters to be escaped. */
10303 for (p = eap->arg; *p != NUL; ++p)
10304 {
10305# ifdef FEAT_GUI
10306 if (*p == CSI) /* leadbyte CSI */
10307 len += 2;
10308# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010309 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10311# ifdef FEAT_GUI
10312 || *p == CSI
10313# endif
10314 )
10315 len += 2;
10316 }
10317 if (len > 0)
10318 {
10319 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10320 if (arg != NULL)
10321 {
10322 len = 0;
10323 for (p = eap->arg; *p != NUL; ++p)
10324 {
10325 arg[len++] = *p;
10326# ifdef FEAT_GUI
10327 if (*p == CSI)
10328 {
10329 arg[len++] = KS_EXTRA;
10330 arg[len++] = (int)KE_CSI;
10331 }
10332# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010333 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 {
10335 arg[len++] = *++p;
10336 if (*p == K_SPECIAL)
10337 {
10338 arg[len++] = KS_SPECIAL;
10339 arg[len++] = KE_FILLER;
10340 }
10341# ifdef FEAT_GUI
10342 else if (*p == CSI)
10343 {
10344 arg[len++] = KS_EXTRA;
10345 arg[len++] = (int)KE_CSI;
10346 }
10347# endif
10348 }
10349 arg[len] = NUL;
10350 }
10351 }
10352 }
10353 }
10354#endif
10355
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010356 ++ex_normal_busy;
10357 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 {
10359 /*
10360 * Repeat the :normal command for each line in the range. When no
10361 * range given, execute it just once, without positioning the cursor
10362 * first.
10363 */
10364 do
10365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 if (eap->addr_count != 0)
10367 {
10368 curwin->w_cursor.lnum = eap->line1++;
10369 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010370 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 }
10372
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010373 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374#ifdef FEAT_MBYTE
10375 arg != NULL ? arg :
10376#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010377 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 }
10379 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10380 }
10381
10382 /* Might not return to the main loop when in an event handler. */
10383 update_topline_cursor();
10384
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010385 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010387#ifdef FEAT_MOUSE
10388 setmouse();
10389#endif
10390#ifdef CURSOR_SHAPE
10391 ui_cursor_shape(); /* may show different cursor shape */
10392#endif
10393
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394#ifdef FEAT_MBYTE
10395 vim_free(arg);
10396#endif
10397}
10398
10399/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010400 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 */
10402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010403ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010405 if (eap->forceit)
10406 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010407 /* cursor line can be zero on startup */
10408 if (!curwin->w_cursor.lnum)
10409 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010410 coladvance((colnr_T)MAXCOL);
10411 curwin->w_curswant = MAXCOL;
10412 curwin->w_set_curswant = FALSE;
10413 }
10414
Bram Moolenaara40c5002005-01-09 21:16:21 +000010415 /* Ignore the command when already in Insert mode. Inserting an
10416 * expression register that invokes a function can do this. */
10417 if (State & INSERT)
10418 return;
10419
Bram Moolenaar64969662005-12-14 21:59:55 +000010420 if (eap->cmdidx == CMD_startinsert)
10421 restart_edit = 'a';
10422 else if (eap->cmdidx == CMD_startreplace)
10423 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010425 restart_edit = 'V';
10426
10427 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010429 if (eap->cmdidx == CMD_startinsert)
10430 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 curwin->w_curswant = 0; /* avoid MAXCOL */
10432 }
10433}
10434
10435/*
10436 * ":stopinsert"
10437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010439ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 restart_edit = 0;
10442 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010443 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010446/*
10447 * Execute normal mode command "cmd".
10448 * "remap" can be REMAP_NONE or REMAP_YES.
10449 */
10450 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010451exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010452{
Bram Moolenaar25281632016-01-21 23:32:32 +010010453 /* Stuff the argument into the typeahead buffer. */
10454 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010455 exec_normal(FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010456}
Bram Moolenaar25281632016-01-21 23:32:32 +010010457
Bram Moolenaar25281632016-01-21 23:32:32 +010010458/*
10459 * Execute normal_cmd() until there is no typeahead left.
10460 */
10461 void
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010462exec_normal(int was_typed, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010463{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010464 oparg_T oa;
10465
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010466 clear_oparg(&oa);
10467 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010468 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10469 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010470 {
10471 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010472#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010473 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010474 && oa.op_type == OP_NOP && oa.regname == NUL
10475 && !VIsual_active)
10476 {
10477 /* If terminal_loop() returns OK we got a key that is handled
10478 * in Normal model. With FAIL we first need to position the
10479 * cursor and the screen needs to be redrawn. */
10480 if (terminal_loop(TRUE) == OK)
10481 normal_cmd(&oa, TRUE);
10482 }
10483 else
10484#endif
10485 /* execute a Normal mode cmd */
10486 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010487 }
10488}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010489
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#ifdef FEAT_FIND_ID
10491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010492ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493{
10494 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10495 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10496 (linenr_T)1, (linenr_T)MAXLNUM);
10497}
10498
Bram Moolenaar4033c552017-09-16 20:54:51 +020010499#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500/*
10501 * ":psearch"
10502 */
10503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010504ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505{
10506 g_do_tagpreview = p_pvh;
10507 ex_findpat(eap);
10508 g_do_tagpreview = 0;
10509}
10510#endif
10511
10512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010513ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515 int whole = TRUE;
10516 long n;
10517 char_u *p;
10518 int action;
10519
10520 switch (cmdnames[eap->cmdidx].cmd_name[2])
10521 {
10522 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10523 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10524 action = ACTION_GOTO;
10525 else
10526 action = ACTION_SHOW;
10527 break;
10528 case 'i': /* ":ilist" and ":dlist" */
10529 action = ACTION_SHOW_ALL;
10530 break;
10531 case 'u': /* ":ijump" and ":djump" */
10532 action = ACTION_GOTO;
10533 break;
10534 default: /* ":isplit" and ":dsplit" */
10535 action = ACTION_SPLIT;
10536 break;
10537 }
10538
10539 n = 1;
10540 if (vim_isdigit(*eap->arg)) /* get count */
10541 {
10542 n = getdigits(&eap->arg);
10543 eap->arg = skipwhite(eap->arg);
10544 }
10545 if (*eap->arg == '/') /* Match regexp, not just whole words */
10546 {
10547 whole = FALSE;
10548 ++eap->arg;
10549 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10550 if (*p)
10551 {
10552 *p++ = NUL;
10553 p = skipwhite(p);
10554
10555 /* Check for trailing illegal characters */
10556 if (!ends_excmd(*p))
10557 eap->errmsg = e_trailing;
10558 else
10559 eap->nextcmd = check_nextcmd(p);
10560 }
10561 }
10562 if (!eap->skip)
10563 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10564 whole, !eap->forceit,
10565 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10566 n, action, eap->line1, eap->line2);
10567}
10568#endif
10569
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570
Bram Moolenaar4033c552017-09-16 20:54:51 +020010571#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572/*
10573 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10574 */
10575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010576ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010578 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10580}
10581
10582/*
10583 * ":pedit"
10584 */
10585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010586ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587{
10588 win_T *curwin_save = curwin;
10589
10590 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010591 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010592 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 do_exedit(eap, NULL);
10594 keep_help_flag = FALSE;
10595 if (curwin != curwin_save && win_valid(curwin_save))
10596 {
10597 /* Return cursor to where we were */
10598 validate_cursor();
10599 redraw_later(VALID);
10600 win_enter(curwin_save, TRUE);
10601 }
10602 g_do_tagpreview = 0;
10603}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
10606/*
10607 * ":stag", ":stselect" and ":stjump".
10608 */
10609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010610ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611{
10612 postponed_split = -1;
10613 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010614 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10616 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010617 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619
10620/*
10621 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10622 */
10623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010624ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
10626 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10627}
10628
10629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010630ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631{
10632 int cmd;
10633
10634 switch (name[1])
10635 {
10636 case 'j': cmd = DT_JUMP; /* ":tjump" */
10637 break;
10638 case 's': cmd = DT_SELECT; /* ":tselect" */
10639 break;
10640 case 'p': cmd = DT_PREV; /* ":tprevious" */
10641 break;
10642 case 'N': cmd = DT_PREV; /* ":tNext" */
10643 break;
10644 case 'n': cmd = DT_NEXT; /* ":tnext" */
10645 break;
10646 case 'o': cmd = DT_POP; /* ":pop" */
10647 break;
10648 case 'f': /* ":tfirst" */
10649 case 'r': cmd = DT_FIRST; /* ":trewind" */
10650 break;
10651 case 'l': cmd = DT_LAST; /* ":tlast" */
10652 break;
10653 default: /* ":tag" */
10654#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010655 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010657 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 return;
10659 }
10660#endif
10661 cmd = DT_TAG;
10662 break;
10663 }
10664
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010665 if (name[0] == 'l')
10666 {
10667#ifndef FEAT_QUICKFIX
10668 ex_ni(eap);
10669 return;
10670#else
10671 cmd = DT_LTAG;
10672#endif
10673 }
10674
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10676 eap->forceit, TRUE);
10677}
10678
10679/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010680 * Check "str" for starting with a special cmdline variable.
10681 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10682 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10683 */
10684 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010685find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010686{
10687 int len;
10688 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010689 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010690 "%",
10691#define SPEC_PERC 0
10692 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010693#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010694 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010695#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010696 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010697#define SPEC_CCWORD (SPEC_CWORD + 1)
10698 "<cexpr>", /* expr under cursor */
10699#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010700 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010701#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010702 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010703#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010704 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010705#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010706 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010707#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010708 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010709#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010710 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010711#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010712#ifdef FEAT_CLIENTSERVER
10713 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010714# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010715#endif
10716 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010717
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010718 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010719 {
10720 len = (int)STRLEN(spec_str[i]);
10721 if (STRNCMP(src, spec_str[i], len) == 0)
10722 {
10723 *usedlen = len;
10724 return i;
10725 }
10726 }
10727 return -1;
10728}
10729
10730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 * Evaluate cmdline variables.
10732 *
10733 * change '%' to curbuf->b_ffname
10734 * '#' to curwin->w_altfile
10735 * '<cword>' to word under the cursor
10736 * '<cWORD>' to WORD under the cursor
10737 * '<cfile>' to path name under the cursor
10738 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010739 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 * '<afile>' to file name for autocommand
10741 * '<abuf>' to buffer number for autocommand
10742 * '<amatch>' to matching name for autocommand
10743 *
10744 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10745 * "" for error without a message) and NULL is returned.
10746 * Returns an allocated string if a valid match was found.
10747 * Returns NULL if no match was found. "usedlen" then still contains the
10748 * number of characters to skip.
10749 */
10750 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010751eval_vars(
10752 char_u *src, /* pointer into commandline */
10753 char_u *srcstart, /* beginning of valid memory for src */
10754 int *usedlen, /* characters after src that are used */
10755 linenr_T *lnump, /* line number for :e command, or NULL */
10756 char_u **errormsg, /* pointer to error message */
10757 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010758 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759{
10760 int i;
10761 char_u *s;
10762 char_u *result;
10763 char_u *resultbuf = NULL;
10764 int resultlen;
10765 buf_T *buf;
10766 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10767 int spec_idx;
10768#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010769 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 int skip_mod = FALSE;
10771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773
10774 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010775 if (escaped != NULL)
10776 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777
10778 /*
10779 * Check if there is something to do.
10780 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010781 spec_idx = find_cmdline_var(src, usedlen);
10782 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 {
10784 *usedlen = 1;
10785 return NULL;
10786 }
10787
10788 /*
10789 * Skip when preceded with a backslash "\%" and "\#".
10790 * Note: In "\\%" the % is also not recognized!
10791 */
10792 if (src > srcstart && src[-1] == '\\')
10793 {
10794 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010795 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 return NULL;
10797 }
10798
10799 /*
10800 * word or WORD under cursor
10801 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010802 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10803 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010805 resultlen = find_ident_under_cursor(&result,
10806 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10807 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10808 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 if (resultlen == 0)
10810 {
10811 *errormsg = (char_u *)"";
10812 return NULL;
10813 }
10814 }
10815
10816 /*
10817 * '#': Alternate file name
10818 * '%': Current file name
10819 * File name under the cursor
10820 * File name for autocommand
10821 * and following modifiers
10822 */
10823 else
10824 {
10825 switch (spec_idx)
10826 {
10827 case SPEC_PERC: /* '%': current file */
10828 if (curbuf->b_fname == NULL)
10829 {
10830 result = (char_u *)"";
10831 valid = 0; /* Must have ":p:h" to be valid */
10832 }
10833 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010836#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010837 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010838#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 break;
10841
10842 case SPEC_HASH: /* '#' or "#99": alternate file */
10843 if (src[1] == '#') /* "##": the argument list */
10844 {
10845 result = arg_all();
10846 resultbuf = result;
10847 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010848 if (escaped != NULL)
10849 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850#ifdef FEAT_MODIFY_FNAME
10851 skip_mod = TRUE;
10852#endif
10853 break;
10854 }
10855 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010856 if (*s == '<') /* "#<99" uses v:oldfiles */
10857 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010859 if (s == src + 2 && src[1] == '-')
10860 /* just a minus sign, don't skip over it */
10861 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 *usedlen = (int)(s - src); /* length of what we expand */
10863
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010864 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010866 if (*usedlen < 2)
10867 {
10868 /* Should we give an error message for #<text? */
10869 *usedlen = 1;
10870 return NULL;
10871 }
10872#ifdef FEAT_EVAL
10873 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10874 (long)i);
10875 if (result == NULL)
10876 {
10877 *errormsg = (char_u *)"";
10878 return NULL;
10879 }
10880#else
10881 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 }
10885 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010886 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010887 if (i == 0 && src[1] == '<' && *usedlen > 1)
10888 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010889 buf = buflist_findnr(i);
10890 if (buf == NULL)
10891 {
10892 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10893 return NULL;
10894 }
10895 if (lnump != NULL)
10896 *lnump = ECMD_LAST;
10897 if (buf->b_fname == NULL)
10898 {
10899 result = (char_u *)"";
10900 valid = 0; /* Must have ":p:h" to be valid */
10901 }
10902 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010903 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010904 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010905#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010906 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010907#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010908 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 break;
10911
10912#ifdef FEAT_SEARCHPATH
10913 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010914 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 if (result == NULL)
10916 {
10917 *errormsg = (char_u *)"";
10918 return NULL;
10919 }
10920 resultbuf = result; /* remember allocated string */
10921 break;
10922#endif
10923
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 case SPEC_AFILE: /* file name for autocommand */
10925 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010926 if (result != NULL && !autocmd_fname_full)
10927 {
10928 /* Still need to turn the fname into a full path. It is
10929 * postponed to avoid a delay when <afile> is not used. */
10930 autocmd_fname_full = TRUE;
10931 result = FullName_save(autocmd_fname, FALSE);
10932 vim_free(autocmd_fname);
10933 autocmd_fname = result;
10934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 if (result == NULL)
10936 {
10937 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10938 return NULL;
10939 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010940 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 break;
10942
10943 case SPEC_ABUF: /* buffer number for autocommand */
10944 if (autocmd_bufnr <= 0)
10945 {
10946 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10947 return NULL;
10948 }
10949 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10950 result = strbuf;
10951 break;
10952
10953 case SPEC_AMATCH: /* match name for autocommand */
10954 result = autocmd_match;
10955 if (result == NULL)
10956 {
10957 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10958 return NULL;
10959 }
10960 break;
10961
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 case SPEC_SFILE: /* file name for ":so" command */
10963 result = sourcing_name;
10964 if (result == NULL)
10965 {
10966 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10967 return NULL;
10968 }
10969 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010970 case SPEC_SLNUM: /* line in file for ":so" command */
10971 if (sourcing_name == NULL || sourcing_lnum == 0)
10972 {
10973 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10974 return NULL;
10975 }
10976 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10977 result = strbuf;
10978 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979#if defined(FEAT_CLIENTSERVER)
10980 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010981 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10982 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 result = strbuf;
10984 break;
10985#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010986 default:
10987 result = (char_u *)""; /* avoid gcc warning */
10988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 }
10990
10991 resultlen = (int)STRLEN(result); /* length of new string */
10992 if (src[*usedlen] == '<') /* remove the file name extension */
10993 {
10994 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 resultlen = (int)(s - result);
10997 }
10998#ifdef FEAT_MODIFY_FNAME
10999 else if (!skip_mod)
11000 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011001 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 &resultlen);
11003 if (result == NULL)
11004 {
11005 *errormsg = (char_u *)"";
11006 return NULL;
11007 }
11008 }
11009#endif
11010 }
11011
11012 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11013 {
11014 if (valid != VALID_HEAD + VALID_PATH)
11015 /* xgettext:no-c-format */
11016 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
11017 else
11018 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
11019 result = NULL;
11020 }
11021 else
11022 result = vim_strnsave(result, resultlen);
11023 vim_free(resultbuf);
11024 return result;
11025}
11026
11027/*
11028 * Concatenate all files in the argument list, separated by spaces, and return
11029 * it in one allocated string.
11030 * Spaces and backslashes in the file names are escaped with a backslash.
11031 * Returns NULL when out of memory.
11032 */
11033 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011034arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035{
11036 int len;
11037 int idx;
11038 char_u *retval = NULL;
11039 char_u *p;
11040
11041 /*
11042 * Do this loop two times:
11043 * first time: compute the total length
11044 * second time: concatenate the names
11045 */
11046 for (;;)
11047 {
11048 len = 0;
11049 for (idx = 0; idx < ARGCOUNT; ++idx)
11050 {
11051 p = alist_name(&ARGLIST[idx]);
11052 if (p != NULL)
11053 {
11054 if (len > 0)
11055 {
11056 /* insert a space in between names */
11057 if (retval != NULL)
11058 retval[len] = ' ';
11059 ++len;
11060 }
11061 for ( ; *p != NUL; ++p)
11062 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011063 if (*p == ' '
11064#ifndef BACKSLASH_IN_FILENAME
11065 || *p == '\\'
11066#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011067 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 {
11069 /* insert a backslash */
11070 if (retval != NULL)
11071 retval[len] = '\\';
11072 ++len;
11073 }
11074 if (retval != NULL)
11075 retval[len] = *p;
11076 ++len;
11077 }
11078 }
11079 }
11080
11081 /* second time: break here */
11082 if (retval != NULL)
11083 {
11084 retval[len] = NUL;
11085 break;
11086 }
11087
11088 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011089 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 if (retval == NULL)
11091 break;
11092 }
11093
11094 return retval;
11095}
11096
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097/*
11098 * Expand the <sfile> string in "arg".
11099 *
11100 * Returns an allocated string, or NULL for any error.
11101 */
11102 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011103expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104{
11105 char_u *errormsg;
11106 int len;
11107 char_u *result;
11108 char_u *newres;
11109 char_u *repl;
11110 int srclen;
11111 char_u *p;
11112
11113 result = vim_strsave(arg);
11114 if (result == NULL)
11115 return NULL;
11116
11117 for (p = result; *p; )
11118 {
11119 if (STRNCMP(p, "<sfile>", 7) != 0)
11120 ++p;
11121 else
11122 {
11123 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011124 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 if (errormsg != NULL)
11126 {
11127 if (*errormsg)
11128 emsg(errormsg);
11129 vim_free(result);
11130 return NULL;
11131 }
11132 if (repl == NULL) /* no match (cannot happen) */
11133 {
11134 p += srclen;
11135 continue;
11136 }
11137 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11138 newres = alloc(len);
11139 if (newres == NULL)
11140 {
11141 vim_free(repl);
11142 vim_free(result);
11143 return NULL;
11144 }
11145 mch_memmove(newres, result, (size_t)(p - result));
11146 STRCPY(newres + (p - result), repl);
11147 len = (int)STRLEN(newres);
11148 STRCAT(newres, p + srclen);
11149 vim_free(repl);
11150 vim_free(result);
11151 result = newres;
11152 p = newres + len; /* continue after the match */
11153 }
11154 }
11155
11156 return result;
11157}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158
11159#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011160static int ses_winsizes(FILE *fd, int restore_size,
11161 win_T *tab_firstwin);
11162static int ses_win_rec(FILE *fd, frame_T *fr);
11163static frame_T *ses_skipframe(frame_T *fr);
11164static int ses_do_frame(frame_T *fr);
11165static int ses_do_win(win_T *wp);
11166static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11167static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011168static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169
11170/*
11171 * Write openfile commands for the current buffers to an .exrc file.
11172 * Return FAIL on error, OK otherwise.
11173 */
11174 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011175makeopens(
11176 FILE *fd,
11177 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178{
11179 buf_T *buf;
11180 int only_save_windows = TRUE;
11181 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 int restore_size = TRUE;
11183 win_T *wp;
11184 char_u *sname;
11185 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011186 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011187 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011188 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011189 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011190 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011191 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192
11193 if (ssop_flags & SSOP_BUFFERS)
11194 only_save_windows = FALSE; /* Save ALL buffers */
11195
11196 /*
11197 * Begin by setting the this_session variable, and then other
11198 * sessionable variables.
11199 */
11200#ifdef FEAT_EVAL
11201 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11202 return FAIL;
11203 if (ssop_flags & SSOP_GLOBALS)
11204 if (store_session_globals(fd) == FAIL)
11205 return FAIL;
11206#endif
11207
11208 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011209 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 */
11211 if (put_line(fd, "silent only") == FAIL)
11212 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011213 if ((ssop_flags & SSOP_TABPAGES)
11214 && put_line(fd, "silent tabonly") == FAIL)
11215 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216
11217 /*
11218 * Now a :cd command to the session directory or the current directory
11219 */
11220 if (ssop_flags & SSOP_SESDIR)
11221 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011222 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11223 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 return FAIL;
11225 }
11226 else if (ssop_flags & SSOP_CURDIR)
11227 {
11228 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11229 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011230 || fputs("cd ", fd) < 0
11231 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11232 || put_eol(fd) == FAIL)
11233 {
11234 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 vim_free(sname);
11238 }
11239
11240 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011241 * If there is an empty, unnamed buffer we will wipe it out later.
11242 * Remember the buffer number.
11243 */
11244 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11245 return FAIL;
11246 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11247 return FAIL;
11248 if (put_line(fd, "endif") == FAIL)
11249 return FAIL;
11250
11251 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 * Now save the current files, current buffer first.
11253 */
11254 if (put_line(fd, "set shortmess=aoO") == FAIL)
11255 return FAIL;
11256
11257 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011258 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 {
11260 if (!(only_save_windows && buf->b_nwindows == 0)
11261 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011262#ifdef FEAT_TERMINAL
11263 /* skip terminal buffers: finished ones are not useful, others
11264 * will be resurrected and result in a new buffer */
11265 && !bt_terminal(buf)
11266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 && buf->b_fname != NULL
11268 && buf->b_p_bl)
11269 {
11270 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11271 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011272 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 return FAIL;
11274 }
11275 }
11276
11277 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011278 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11280 return FAIL;
11281
11282 if (ssop_flags & SSOP_RESIZE)
11283 {
11284 /* Note: after the restore we still check it worked!*/
11285 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11286 || put_eol(fd) == FAIL)
11287 return FAIL;
11288 }
11289
11290#ifdef FEAT_GUI
11291 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11292 {
11293 int x, y;
11294
11295 if (gui_mch_get_winpos(&x, &y) == OK)
11296 {
11297 /* Note: after the restore we still check it worked!*/
11298 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11299 return FAIL;
11300 }
11301 }
11302#endif
11303
11304 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011305 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11306 * will be displayed when creating the next tab. That resizes the windows
11307 * in the first tab, which may cause problems. Set 'showtabline' to 2
11308 * temporarily to avoid that.
11309 */
11310 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11311 {
11312 if (put_line(fd, "set stal=2") == FAIL)
11313 return FAIL;
11314 restore_stal = TRUE;
11315 }
11316
11317 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011318 * May repeat putting Windows for each tab, when "tabpages" is in
11319 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011320 * Don't use goto_tabpage(), it may change directory and trigger
11321 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011323 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011324 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011325 if ((ssop_flags & SSOP_TABPAGES))
11326 {
11327 int num_tabs;
11328
11329 /*
11330 * Similar to ses_win_rec() below, populate the tab pages first so
11331 * later local options won't be copied to the new tabs.
11332 */
11333 for (tabnr = 1; ; ++tabnr)
11334 {
11335 tabpage_T *tp = find_tabpage(tabnr);
11336
11337 if (tp == NULL) /* done all tab pages */
11338 break;
11339
11340 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11341 return FAIL;
11342 }
11343
11344 num_tabs = tabnr - 1;
11345 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11346 || put_eol(fd) == FAIL))
11347 return FAIL;
11348 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011349 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011351 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011352 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011353
Bram Moolenaar18144c82006-04-12 21:52:12 +000011354 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011356 tabpage_T *tp = find_tabpage(tabnr);
11357
11358 if (tp == NULL)
11359 break; /* done all tab pages */
11360 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011361 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011362 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011363 tab_topframe = topframe;
11364 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011365 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011366 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011367 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011368 tab_topframe = tp->tp_topframe;
11369 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011370 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011371 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374 /*
11375 * Before creating the window layout, try loading one file. If this
11376 * is aborted we don't end up with a number of useless windows.
11377 * This may have side effects! (e.g., compressed or network file).
11378 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011379 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011380 {
11381 if (ses_do_win(wp)
11382 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011383 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011384#ifdef FEAT_QUICKFIX
11385 && !bt_nofile(wp->w_buffer)
11386#endif
11387 )
11388 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011389 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11390 return FAIL;
11391 need_tabnext = FALSE;
11392
11393 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011394 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11395 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011396 return FAIL;
11397 if (!wp->w_arg_idx_invalid)
11398 edited_win = wp;
11399 break;
11400 }
11401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011403 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011404 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011405 return FAIL;
11406
Bram Moolenaar18144c82006-04-12 21:52:12 +000011407 /*
11408 * Save current window layout.
11409 */
11410 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011412 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011414 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11415 return FAIL;
11416 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
Bram Moolenaar18144c82006-04-12 21:52:12 +000011419 /*
11420 * Check if window sizes can be restored (no windows omitted).
11421 * Remember the window number of the current window after restoring.
11422 */
11423 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011424 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011425 {
11426 if (ses_do_win(wp))
11427 ++nr;
11428 else
11429 restore_size = FALSE;
11430 if (curwin == wp)
11431 cnr = nr;
11432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433
Bram Moolenaar18144c82006-04-12 21:52:12 +000011434 /* Go to the first window. */
11435 if (put_line(fd, "wincmd t") == FAIL)
11436 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011437
Bram Moolenaar18144c82006-04-12 21:52:12 +000011438 /*
11439 * If more than one window, see if sizes can be restored.
11440 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11441 * resized when moving between windows.
11442 * Do this before restoring the view, so that the topline and the
11443 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011444 * winminheight and winminwidth need to be set to avoid an error if the
11445 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011446 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011447 if (put_line(fd, "set winminheight=0") == FAIL
11448 || put_line(fd, "set winheight=1") == FAIL
11449 || put_line(fd, "set winminwidth=0") == FAIL
11450 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011451 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011452 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011453 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454
Bram Moolenaar18144c82006-04-12 21:52:12 +000011455 /*
11456 * Restore the view of the window (options, file, cursor, etc.).
11457 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011458 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011459 {
11460 if (!ses_do_win(wp))
11461 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011462 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11463 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011464 return FAIL;
11465 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11466 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011467 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011468 }
11469
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011470 /* The argument index in the first tab page is zero, need to set it in
11471 * each window. For further tab pages it's the window where we do
11472 * "tabedit". */
11473 cur_arg_idx = next_arg_idx;
11474
Bram Moolenaar18144c82006-04-12 21:52:12 +000011475 /*
11476 * Restore cursor to the current window if it's not the first one.
11477 */
11478 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11479 || put_eol(fd) == FAIL))
11480 return FAIL;
11481
11482 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011483 * Restore window sizes again after jumping around in windows, because
11484 * the current window has a minimum size while others may not.
11485 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011486 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011487 return FAIL;
11488
Bram Moolenaar18144c82006-04-12 21:52:12 +000011489 /* Don't continue in another tab page when doing only the current one
11490 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011491 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011492 break;
11493 }
11494
11495 if (ssop_flags & SSOP_TABPAGES)
11496 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011497 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11498 || put_eol(fd) == FAIL)
11499 return FAIL;
11500 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011501 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11502 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011503
Bram Moolenaar9c102382006-05-03 21:26:49 +000011504 /*
11505 * Wipe out an empty unnamed buffer we started in.
11506 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011507 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011508 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011509 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011510 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011511 return FAIL;
11512 if (put_line(fd, "endif") == FAIL)
11513 return FAIL;
11514 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11515 return FAIL;
11516
11517 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11518 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11519 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11520 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011521 /* Re-apply 'winminheight' and 'winminwidth'. */
11522 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11523 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11524 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525
11526 /*
11527 * Lastly, execute the x.vim file if it exists.
11528 */
11529 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11530 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011531 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 || put_line(fd, "endif") == FAIL)
11533 return FAIL;
11534
11535 return OK;
11536}
11537
11538 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011539ses_winsizes(
11540 FILE *fd,
11541 int restore_size,
11542 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543{
11544 int n = 0;
11545 win_T *wp;
11546
11547 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11548 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011549 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 {
11551 if (!ses_do_win(wp))
11552 continue;
11553 ++n;
11554
11555 /* restore height when not full height */
11556 if (wp->w_height + wp->w_status_height < topframe->fr_height
11557 && (fprintf(fd,
11558 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11559 n, (long)wp->w_height, Rows / 2, Rows) < 0
11560 || put_eol(fd) == FAIL))
11561 return FAIL;
11562
11563 /* restore width when not full width */
11564 if (wp->w_width < Columns && (fprintf(fd,
11565 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11566 n, (long)wp->w_width, Columns / 2, Columns) < 0
11567 || put_eol(fd) == FAIL))
11568 return FAIL;
11569 }
11570 }
11571 else
11572 {
11573 /* Just equalise window sizes */
11574 if (put_line(fd, "wincmd =") == FAIL)
11575 return FAIL;
11576 }
11577 return OK;
11578}
11579
11580/*
11581 * Write commands to "fd" to recursively create windows for frame "fr",
11582 * horizontally and vertically split.
11583 * After the commands the last window in the frame is the current window.
11584 * Returns FAIL when writing the commands to "fd" fails.
11585 */
11586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011587ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
11589 frame_T *frc;
11590 int count = 0;
11591
11592 if (fr->fr_layout != FR_LEAF)
11593 {
11594 /* Find first frame that's not skipped and then create a window for
11595 * each following one (first frame is already there). */
11596 frc = ses_skipframe(fr->fr_child);
11597 if (frc != NULL)
11598 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11599 {
11600 /* Make window as big as possible so that we have lots of room
11601 * to split. */
11602 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11603 || put_line(fd, fr->fr_layout == FR_COL
11604 ? "split" : "vsplit") == FAIL)
11605 return FAIL;
11606 ++count;
11607 }
11608
11609 /* Go back to the first window. */
11610 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11611 ? "%dwincmd k" : "%dwincmd h", count) < 0
11612 || put_eol(fd) == FAIL))
11613 return FAIL;
11614
11615 /* Recursively create frames/windows in each window of this column or
11616 * row. */
11617 frc = ses_skipframe(fr->fr_child);
11618 while (frc != NULL)
11619 {
11620 ses_win_rec(fd, frc);
11621 frc = ses_skipframe(frc->fr_next);
11622 /* Go to next window. */
11623 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11624 return FAIL;
11625 }
11626 }
11627 return OK;
11628}
11629
11630/*
11631 * Skip frames that don't contain windows we want to save in the Session.
11632 * Returns NULL when there none.
11633 */
11634 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011635ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636{
11637 frame_T *frc;
11638
11639 for (frc = fr; frc != NULL; frc = frc->fr_next)
11640 if (ses_do_frame(frc))
11641 break;
11642 return frc;
11643}
11644
11645/*
11646 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11647 * the Session.
11648 */
11649 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011650ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651{
11652 frame_T *frc;
11653
11654 if (fr->fr_layout == FR_LEAF)
11655 return ses_do_win(fr->fr_win);
11656 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11657 if (ses_do_frame(frc))
11658 return TRUE;
11659 return FALSE;
11660}
11661
11662/*
11663 * Return non-zero if window "wp" is to be stored in the Session.
11664 */
11665 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011666ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011668#ifdef FEAT_TERMINAL
11669 if (bt_terminal(wp->w_buffer))
11670 return !term_is_finished(wp->w_buffer)
11671 && (ssop_flags & SSOP_TERMINAL)
11672 && term_should_restore(wp->w_buffer);
11673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 if (wp->w_buffer->b_fname == NULL
11675#ifdef FEAT_QUICKFIX
11676 /* When 'buftype' is "nofile" can't restore the window contents. */
11677 || bt_nofile(wp->w_buffer)
11678#endif
11679 )
11680 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011681 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 return (ssop_flags & SSOP_HELP);
11683 return TRUE;
11684}
11685
11686/*
11687 * Write commands to "fd" to restore the view of a window.
11688 * Caller must make sure 'scrolloff' is zero.
11689 */
11690 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011691put_view(
11692 FILE *fd,
11693 win_T *wp,
11694 int add_edit, /* add ":edit" command to view */
11695 unsigned *flagp, /* vop_flags or ssop_flags */
11696 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011697 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698{
11699 win_T *save_curwin;
11700 int f;
11701 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011702 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703
11704 /* Always restore cursor position for ":mksession". For ":mkview" only
11705 * when 'viewoptions' contains "cursor". */
11706 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11707
11708 /*
11709 * Local argument list.
11710 */
11711 if (wp->w_alist == &global_alist)
11712 {
11713 if (put_line(fd, "argglobal") == FAIL)
11714 return FAIL;
11715 }
11716 else
11717 {
11718 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11719 flagp == &vop_flags
11720 || !(*flagp & SSOP_CURDIR)
11721 || wp->w_localdir != NULL, flagp) == FAIL)
11722 return FAIL;
11723 }
11724
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011725 /* Only when part of a session: restore the argument index. Some
11726 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011727 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011728 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011730 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 || put_eol(fd) == FAIL)
11732 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011733 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 }
11735
11736 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011737 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011739# ifdef FEAT_TERMINAL
11740 if (bt_terminal(wp->w_buffer))
11741 {
11742 if (term_write_session(fd, wp) == FAIL)
11743 return FAIL;
11744 }
11745 else
11746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 /*
11748 * Load the file.
11749 */
11750 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011751# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 )
11755 {
11756 /*
11757 * Editing a file in this buffer: use ":edit file".
11758 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011759 *
11760 * Note, if a buffer for that file already exists, use :badd to
11761 * edit that buffer, to not lose folding information (:edit resets
11762 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011764 if (fputs("if bufexists('", fd) < 0
11765 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11766 || fputs("') | buffer ", fd) < 0
11767 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11768 || fputs(" | else | edit ", fd) < 0
11769 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11770 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011771 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 return FAIL;
11773 }
11774 else
11775 {
11776 /* No file in this buffer, just make it empty. */
11777 if (put_line(fd, "enew") == FAIL)
11778 return FAIL;
11779#ifdef FEAT_QUICKFIX
11780 if (wp->w_buffer->b_ffname != NULL)
11781 {
11782 /* The buffer does have a name, but it's not a file name. */
11783 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011784 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 return FAIL;
11786 }
11787#endif
11788 do_cursor = FALSE;
11789 }
11790 }
11791
11792 /*
11793 * Local mappings and abbreviations.
11794 */
11795 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11796 && makemap(fd, wp->w_buffer) == FAIL)
11797 return FAIL;
11798
11799 /*
11800 * Local options. Need to go to the window temporarily.
11801 * Store only local values when using ":mkview" and when ":mksession" is
11802 * used and 'sessionoptions' doesn't include "options".
11803 * Some folding options are always stored when "folds" is included,
11804 * otherwise the folds would not be restored correctly.
11805 */
11806 save_curwin = curwin;
11807 curwin = wp;
11808 curbuf = curwin->w_buffer;
11809 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11810 f = makeset(fd, OPT_LOCAL,
11811 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11812#ifdef FEAT_FOLDING
11813 else if (*flagp & SSOP_FOLDS)
11814 f = makefoldset(fd);
11815#endif
11816 else
11817 f = OK;
11818 curwin = save_curwin;
11819 curbuf = curwin->w_buffer;
11820 if (f == FAIL)
11821 return FAIL;
11822
11823#ifdef FEAT_FOLDING
11824 /*
11825 * Save Folds when 'buftype' is empty and for help files.
11826 */
11827 if ((*flagp & SSOP_FOLDS)
11828 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011829 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 {
11831 if (put_folds(fd, wp) == FAIL)
11832 return FAIL;
11833 }
11834#endif
11835
11836 /*
11837 * Set the cursor after creating folds, since that moves the cursor.
11838 */
11839 if (do_cursor)
11840 {
11841
11842 /* Restore the cursor line in the file and relatively in the
11843 * window. Don't use "G", it changes the jumplist. */
11844 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11845 (long)wp->w_cursor.lnum,
11846 (long)(wp->w_cursor.lnum - wp->w_topline),
11847 (long)wp->w_height / 2, (long)wp->w_height) < 0
11848 || put_eol(fd) == FAIL
11849 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11850 || put_line(fd, "exe s:l") == FAIL
11851 || put_line(fd, "normal! zt") == FAIL
11852 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11853 || put_eol(fd) == FAIL)
11854 return FAIL;
11855 /* Restore the cursor column and left offset when not wrapping. */
11856 if (wp->w_cursor.col == 0)
11857 {
11858 if (put_line(fd, "normal! 0") == FAIL)
11859 return FAIL;
11860 }
11861 else
11862 {
11863 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11864 {
11865 if (fprintf(fd,
11866 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011867 (long)wp->w_virtcol + 1,
11868 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 (long)wp->w_width / 2, (long)wp->w_width) < 0
11870 || put_eol(fd) == FAIL
11871 || put_line(fd, "if s:c > 0") == FAIL
11872 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011873 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11874 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 || put_eol(fd) == FAIL
11876 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011877 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 || put_eol(fd) == FAIL
11879 || put_line(fd, "endif") == FAIL)
11880 return FAIL;
11881 }
11882 else
11883 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011884 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 || put_eol(fd) == FAIL)
11886 return FAIL;
11887 }
11888 }
11889 }
11890
11891 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011892 * Local directory, if the current flag is not view options or the "curdir"
11893 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011895 if (wp->w_localdir != NULL
11896 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 {
11898 if (fputs("lcd ", fd) < 0
11899 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11900 || put_eol(fd) == FAIL)
11901 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011902 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 }
11904
11905 return OK;
11906}
11907
11908/*
11909 * Write an argument list to the session file.
11910 * Returns FAIL if writing fails.
11911 */
11912 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011913ses_arglist(
11914 FILE *fd,
11915 char *cmd,
11916 garray_T *gap,
11917 int fullname, /* TRUE: use full path name */
11918 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919{
11920 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011921 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 char_u *s;
11923
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011924 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11925 return FAIL;
11926 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 return FAIL;
11928 for (i = 0; i < gap->ga_len; ++i)
11929 {
11930 /* NULL file names are skipped (only happens when out of memory). */
11931 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11932 if (s != NULL)
11933 {
11934 if (fullname)
11935 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011936 buf = alloc(MAXPATHL);
11937 if (buf != NULL)
11938 {
11939 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11940 s = buf;
11941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011943 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011944 || ses_put_fname(fd, s, flagp) == FAIL
11945 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011946 {
11947 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011949 }
11950 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 }
11952 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011953 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954}
11955
11956/*
11957 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011958 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 * Returns FAIL if writing fails.
11960 */
11961 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011962ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963{
11964 char_u *name;
11965
11966 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011967 * the session file will be sourced.
11968 * Don't do this for ":mkview", we don't know the current directory.
11969 * Don't do this after ":lcd", we don't keep track of what the current
11970 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 if (buf->b_sfname != NULL
11972 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011973 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011974#ifdef FEAT_AUTOCHDIR
11975 && !p_acd
11976#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011977 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 name = buf->b_sfname;
11979 else
11980 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011981 if (ses_put_fname(fd, name, flagp) == FAIL
11982 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 return FAIL;
11984 return OK;
11985}
11986
11987/*
11988 * Write a file name to the session file.
11989 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11990 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011991 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 */
11993 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011994ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995{
11996 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011997 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
12000 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012001 if (sname == NULL)
12002 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012004 if (*flagp & SSOP_SLASH)
12005 {
12006 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012007 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012008 if (*p == '\\')
12009 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012011
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012012 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012013 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012015 if (p == NULL)
12016 return FAIL;
12017
12018 /* write the result */
12019 if (fputs((char *)p, fd) < 0)
12020 retval = FAIL;
12021
12022 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 return retval;
12024}
12025
12026/*
12027 * ":loadview [nr]"
12028 */
12029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012030ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031{
12032 char_u *fname;
12033
12034 fname = get_view_file(*eap->arg);
12035 if (fname != NULL)
12036 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012037 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 vim_free(fname);
12039 }
12040}
12041
12042/*
12043 * Get the name of the view file for the current buffer.
12044 */
12045 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012046get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047{
12048 int len = 0;
12049 char_u *p, *s;
12050 char_u *retval;
12051 char_u *sname;
12052
12053 if (curbuf->b_ffname == NULL)
12054 {
12055 EMSG(_(e_noname));
12056 return NULL;
12057 }
12058 sname = home_replace_save(NULL, curbuf->b_ffname);
12059 if (sname == NULL)
12060 return NULL;
12061
12062 /*
12063 * We want a file name without separators, because we're not going to make
12064 * a directory.
12065 * "normal" path separator -> "=+"
12066 * "=" -> "=="
12067 * ":" path separator -> "=-"
12068 */
12069 for (p = sname; *p; ++p)
12070 if (*p == '=' || vim_ispathsep(*p))
12071 ++len;
12072 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12073 if (retval != NULL)
12074 {
12075 STRCPY(retval, p_vdir);
12076 add_pathsep(retval);
12077 s = retval + STRLEN(retval);
12078 for (p = sname; *p; ++p)
12079 {
12080 if (*p == '=')
12081 {
12082 *s++ = '=';
12083 *s++ = '=';
12084 }
12085 else if (vim_ispathsep(*p))
12086 {
12087 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012088#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 if (*p == ':')
12090 *s++ = '-';
12091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012093 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 }
12095 else
12096 *s++ = *p;
12097 }
12098 *s++ = '=';
12099 *s++ = c;
12100 STRCPY(s, ".vim");
12101 }
12102
12103 vim_free(sname);
12104 return retval;
12105}
12106
12107#endif /* FEAT_SESSION */
12108
12109/*
12110 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12111 * Return FAIL for a write error.
12112 */
12113 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012114put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115{
12116 if (
12117#ifdef USE_CRNL
12118 (
12119# ifdef MKSESSION_NL
12120 !mksession_nl &&
12121# endif
12122 (putc('\r', fd) < 0)) ||
12123#endif
12124 (putc('\n', fd) < 0))
12125 return FAIL;
12126 return OK;
12127}
12128
12129/*
12130 * Write a line to "fd".
12131 * Return FAIL for a write error.
12132 */
12133 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012134put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135{
12136 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12137 return FAIL;
12138 return OK;
12139}
12140
12141#ifdef FEAT_VIMINFO
12142/*
12143 * ":rviminfo" and ":wviminfo".
12144 */
12145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012146ex_viminfo(
12147 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148{
12149 char_u *save_viminfo;
12150
12151 save_viminfo = p_viminfo;
12152 if (*p_viminfo == NUL)
12153 p_viminfo = (char_u *)"'100";
12154 if (eap->cmdidx == CMD_rviminfo)
12155 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012156 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12157 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 EMSG(_("E195: Cannot open viminfo file for reading"));
12159 }
12160 else
12161 write_viminfo(eap->arg, eap->forceit);
12162 p_viminfo = save_viminfo;
12163}
12164#endif
12165
12166#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012167/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012168 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012169 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012172dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 if (fname == NULL)
12175 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012176 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177}
12178#endif
12179
12180/*
12181 * ":behave {mswin,xterm}"
12182 */
12183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012184ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185{
12186 if (STRCMP(eap->arg, "mswin") == 0)
12187 {
12188 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12189 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12190 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12191 set_option_value((char_u *)"keymodel", 0L,
12192 (char_u *)"startsel,stopsel", 0);
12193 }
12194 else if (STRCMP(eap->arg, "xterm") == 0)
12195 {
12196 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12197 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12198 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12199 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12200 }
12201 else
12202 EMSG2(_(e_invarg2), eap->arg);
12203}
12204
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012205#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12206/*
12207 * Function given to ExpandGeneric() to obtain the possible arguments of the
12208 * ":behave {mswin,xterm}" command.
12209 */
12210 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012211get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012212{
12213 if (idx == 0)
12214 return (char_u *)"mswin";
12215 if (idx == 1)
12216 return (char_u *)"xterm";
12217 return NULL;
12218}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012219
12220/*
12221 * Function given to ExpandGeneric() to obtain the possible arguments of the
12222 * ":messages {clear}" command.
12223 */
12224 char_u *
12225get_messages_arg(expand_T *xp UNUSED, int idx)
12226{
12227 if (idx == 0)
12228 return (char_u *)"clear";
12229 return NULL;
12230}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012231#endif
12232
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012233 char_u *
12234get_mapclear_arg(expand_T *xp UNUSED, int idx)
12235{
12236 if (idx == 0)
12237 return (char_u *)"<buffer>";
12238 return NULL;
12239}
12240
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241static int filetype_detect = FALSE;
12242static int filetype_plugin = FALSE;
12243static int filetype_indent = FALSE;
12244
12245/*
12246 * ":filetype [plugin] [indent] {on,off,detect}"
12247 * on: Load the filetype.vim file to install autocommands for file types.
12248 * off: Load the ftoff.vim file to remove all autocommands for file types.
12249 * plugin on: load filetype.vim and ftplugin.vim
12250 * plugin off: load ftplugof.vim
12251 * indent on: load filetype.vim and indent.vim
12252 * indent off: load indoff.vim
12253 */
12254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012255ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256{
12257 char_u *arg = eap->arg;
12258 int plugin = FALSE;
12259 int indent = FALSE;
12260
12261 if (*eap->arg == NUL)
12262 {
12263 /* Print current status. */
12264 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12265 filetype_detect ? "ON" : "OFF",
12266 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12267 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12268 return;
12269 }
12270
12271 /* Accept "plugin" and "indent" in any order. */
12272 for (;;)
12273 {
12274 if (STRNCMP(arg, "plugin", 6) == 0)
12275 {
12276 plugin = TRUE;
12277 arg = skipwhite(arg + 6);
12278 continue;
12279 }
12280 if (STRNCMP(arg, "indent", 6) == 0)
12281 {
12282 indent = TRUE;
12283 arg = skipwhite(arg + 6);
12284 continue;
12285 }
12286 break;
12287 }
12288 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12289 {
12290 if (*arg == 'o' || !filetype_detect)
12291 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012292 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 filetype_detect = TRUE;
12294 if (plugin)
12295 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012296 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 filetype_plugin = TRUE;
12298 }
12299 if (indent)
12300 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012301 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 filetype_indent = TRUE;
12303 }
12304 }
12305 if (*arg == 'd')
12306 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012307 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012308 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 }
12310 }
12311 else if (STRCMP(arg, "off") == 0)
12312 {
12313 if (plugin || indent)
12314 {
12315 if (plugin)
12316 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012317 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 filetype_plugin = FALSE;
12319 }
12320 if (indent)
12321 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012322 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 filetype_indent = FALSE;
12324 }
12325 }
12326 else
12327 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012328 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 filetype_detect = FALSE;
12330 }
12331 }
12332 else
12333 EMSG2(_(e_invarg2), arg);
12334}
12335
12336/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012337 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 */
12339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012340ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341{
12342 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012343 {
12344 char_u *arg = eap->arg;
12345
12346 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12347 arg += 9;
12348
12349 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12350 if (arg != eap->arg)
12351 did_filetype = FALSE;
12352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012356ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357{
12358#ifdef FEAT_DIGRAPHS
12359 if (*eap->arg != NUL)
12360 putdigraph(eap->arg);
12361 else
12362 listdigraphs();
12363#else
12364 EMSG(_("E196: No digraphs in this version"));
12365#endif
12366}
12367
12368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012369ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370{
12371 int flags = 0;
12372
12373 if (eap->cmdidx == CMD_setlocal)
12374 flags = OPT_LOCAL;
12375 else if (eap->cmdidx == CMD_setglobal)
12376 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012377#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 if (cmdmod.browse && flags == 0)
12379 ex_options(eap);
12380 else
12381#endif
12382 (void)do_set(eap->arg, flags);
12383}
12384
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012385#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12386 void
12387set_no_hlsearch(int flag)
12388{
12389 no_hlsearch = flag;
12390# ifdef FEAT_EVAL
12391 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12392# endif
12393}
12394
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395/*
12396 * ":nohlsearch"
12397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012399ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012401 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012402 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403}
12404
12405/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012406 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 * Sets nextcmd to the start of the next command, if any. Also called when
12408 * skipping commands to find the next command.
12409 */
12410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012411ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412{
12413 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012414 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 char_u *end;
12416 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012417 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012418
12419 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012420 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012421 else
12422 {
12423 EMSG(e_invcmd);
12424 return;
12425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426
12427 /* First clear any old pattern. */
12428 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012429 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430
12431 if (ends_excmd(*eap->arg))
12432 end = eap->arg;
12433 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012434 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 end = eap->arg + 4;
12436 else
12437 {
12438 p = skiptowhite(eap->arg);
12439 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012440 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 p = skipwhite(p);
12442 if (*p == NUL)
12443 {
12444 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012445 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 EMSG2(_(e_invarg2), eap->arg);
12447 return;
12448 }
12449 end = skip_regexp(p + 1, *p, TRUE, NULL);
12450 if (!eap->skip)
12451 {
12452 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12453 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012454 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 eap->errmsg = e_trailing;
12456 return;
12457 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012458 if (*end != *p)
12459 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012460 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012461 EMSG2(_(e_invarg2), p);
12462 return;
12463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464
12465 c = *end;
12466 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012467 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012468 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012469 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 }
12471 }
12472 eap->nextcmd = find_nextcmd(end);
12473}
12474#endif
12475
12476#ifdef FEAT_CRYPT
12477/*
12478 * ":X": Get crypt key
12479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012481ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012483 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012484 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485}
12486#endif
12487
12488#ifdef FEAT_FOLDING
12489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012490ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491{
12492 if (foldManualAllowed(TRUE))
12493 foldCreate(eap->line1, eap->line2);
12494}
12495
12496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012497ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498{
12499 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12500 eap->forceit, FALSE);
12501}
12502
12503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012504ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505{
12506 linenr_T lnum;
12507
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012508#ifdef FEAT_CLIPBOARD
12509 start_global_changes();
12510#endif
12511
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 /* First set the marks for all lines closed/open. */
12513 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12514 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12515 ml_setmarked(lnum);
12516
12517 /* Execute the command on the marked lines. */
12518 global_exe(eap->arg);
12519 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012520#ifdef FEAT_CLIPBOARD
12521 end_global_changes();
12522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523}
12524#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012525
Bram Moolenaar39665952018-08-15 20:59:48 +020012526#ifdef FEAT_QUICKFIX
12527/*
12528 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12529 * instead of a quickfix command.
12530 */
12531 int
12532is_loclist_cmd(int cmdidx)
12533{
12534 if (cmdidx < 0 || cmdidx > CMD_SIZE)
12535 return FALSE;
12536 return cmdnames[cmdidx].cmd_name[0] == 'l';
12537}
12538#endif
12539
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012540# if defined(FEAT_TIMERS) || defined(PROTO)
12541 int
12542get_pressedreturn(void)
12543{
12544 return ex_pressedreturn;
12545}
12546
12547 void
12548set_pressedreturn(int val)
12549{
12550 ex_pressedreturn = val;
12551}
12552#endif