blob: 169cb7a64e96481bf11d3c7bffa99838bc3b9ef5 [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
5752 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005753 vim_strncpy(buff,
5754 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005755 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005757 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 _("%d more files to edit. Quit anyway?"), n);
5759 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5760 return OK;
5761 return FAIL;
5762 }
5763#endif
5764 if (n == 1)
5765 EMSG(_("E173: 1 more file to edit"));
5766 else
5767 EMSGN(_("E173: %ld more files to edit"), n);
5768 quitmore = 2; /* next try to quit is allowed */
5769 }
5770 return FAIL;
5771 }
5772 return OK;
5773}
5774
5775#ifdef FEAT_CMDL_COMPL
5776/*
5777 * Function given to ExpandGeneric() to obtain the list of command names.
5778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005780get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 if (idx >= (int)CMD_SIZE)
5783# ifdef FEAT_USR_CMDS
5784 return get_user_command_name(idx);
5785# else
5786 return NULL;
5787# endif
5788 return cmdnames[idx].cmd_name;
5789}
5790#endif
5791
5792#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005793static 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);
5794static void uc_list(char_u *name, size_t name_len);
5795static 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);
5796static char_u *uc_split_args(char_u *arg, size_t *lenp);
5797static 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 +00005798
5799 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005800uc_add_command(
5801 char_u *name,
5802 size_t name_len,
5803 char_u *rep,
5804 long argt,
5805 long def,
5806 int flags,
5807 int compl,
5808 char_u *compl_arg,
5809 int addr_type,
5810 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 ucmd_T *cmd = NULL;
5813 char_u *p;
5814 int i;
5815 int cmp = 1;
5816 char_u *rep_buf = NULL;
5817 garray_T *gap;
5818
Bram Moolenaar9c102382006-05-03 21:26:49 +00005819 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (rep_buf == NULL)
5821 {
5822 /* Can't replace termcodes - try using the string as is */
5823 rep_buf = vim_strsave(rep);
5824
5825 /* Give up if out of memory */
5826 if (rep_buf == NULL)
5827 return FAIL;
5828 }
5829
5830 /* get address of growarray: global or in curbuf */
5831 if (flags & UC_BUFFER)
5832 {
5833 gap = &curbuf->b_ucmds;
5834 if (gap->ga_itemsize == 0)
5835 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5836 }
5837 else
5838 gap = &ucmds;
5839
5840 /* Search for the command in the already defined commands. */
5841 for (i = 0; i < gap->ga_len; ++i)
5842 {
5843 size_t len;
5844
5845 cmd = USER_CMD_GA(gap, i);
5846 len = STRLEN(cmd->uc_name);
5847 cmp = STRNCMP(name, cmd->uc_name, name_len);
5848 if (cmp == 0)
5849 {
5850 if (name_len < len)
5851 cmp = -1;
5852 else if (name_len > len)
5853 cmp = 1;
5854 }
5855
5856 if (cmp == 0)
5857 {
5858 if (!force)
5859 {
5860 EMSG(_("E174: Command already exists: add ! to replace it"));
5861 goto fail;
5862 }
5863
Bram Moolenaard23a8232018-02-10 18:45:26 +01005864 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005865#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005866 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 break;
5869 }
5870
5871 /* Stop as soon as we pass the name to add */
5872 if (cmp < 0)
5873 break;
5874 }
5875
5876 /* Extend the array unless we're replacing an existing command */
5877 if (cmp != 0)
5878 {
5879 if (ga_grow(gap, 1) != OK)
5880 goto fail;
5881 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5882 goto fail;
5883
5884 cmd = USER_CMD_GA(gap, i);
5885 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5886
5887 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
5889 cmd->uc_name = p;
5890 }
5891
5892 cmd->uc_rep = rep_buf;
5893 cmd->uc_argt = argt;
5894 cmd->uc_def = def;
5895 cmd->uc_compl = compl;
5896#ifdef FEAT_EVAL
5897 cmd->uc_scriptID = current_SID;
5898# ifdef FEAT_CMDL_COMPL
5899 cmd->uc_compl_arg = compl_arg;
5900# endif
5901#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005902 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904 return OK;
5905
5906fail:
5907 vim_free(rep_buf);
5908#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5909 vim_free(compl_arg);
5910#endif
5911 return FAIL;
5912}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005915#if defined(FEAT_USR_CMDS)
5916static struct
5917{
5918 int expand;
5919 char *name;
5920} addr_type_complete[] =
5921{
5922 {ADDR_ARGUMENTS, "arguments"},
5923 {ADDR_LINES, "lines"},
5924 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5925 {ADDR_TABS, "tabs"},
5926 {ADDR_BUFFERS, "buffers"},
5927 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005928 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005929 {-1, NULL}
5930};
5931#endif
5932
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005933#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934/*
5935 * List of names for completion for ":command" with the EXPAND_ flag.
5936 * Must be alphabetical for completion.
5937 */
5938static struct
5939{
5940 int expand;
5941 char *name;
5942} command_complete[] =
5943{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005944 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005946 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005948 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005950 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005951#if defined(FEAT_CSCOPE)
5952 {EXPAND_CSCOPE, "cscope"},
5953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5955 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005956 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#endif
5958 {EXPAND_DIRECTORIES, "dir"},
5959 {EXPAND_ENV_VARS, "environment"},
5960 {EXPAND_EVENTS, "event"},
5961 {EXPAND_EXPRESSION, "expression"},
5962 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005963 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005964 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {EXPAND_FUNCTIONS, "function"},
5966 {EXPAND_HELP, "help"},
5967 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005968#if defined(FEAT_CMDHIST)
5969 {EXPAND_HISTORY, "history"},
5970#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005971#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005972 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005973 {EXPAND_LOCALES, "locale"},
5974#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005975 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 {EXPAND_MAPPINGS, "mapping"},
5977 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005978 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005979 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005980#if defined(FEAT_PROFILE)
5981 {EXPAND_SYNTIME, "syntime"},
5982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005984 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005985 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005986#if defined(FEAT_SIGNS)
5987 {EXPAND_SIGN, "sign"},
5988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 {EXPAND_TAGS, "tag"},
5990 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005991 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {EXPAND_USER_VARS, "var"},
5993 {0, NULL}
5994};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005997#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005999uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 int i, j;
6002 int found = FALSE;
6003 ucmd_T *cmd;
6004 int len;
6005 long a;
6006 garray_T *gap;
6007
6008 gap = &curbuf->b_ucmds;
6009 for (;;)
6010 {
6011 for (i = 0; i < gap->ga_len; ++i)
6012 {
6013 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006014 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaard29459b2016-08-26 22:29:11 +02006016 /* Skip commands which don't match the requested prefix and
6017 * commands filtered out. */
6018 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6019 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 continue;
6021
6022 /* Put out the title first time */
6023 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006024 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 found = TRUE;
6026 msg_putchar('\n');
6027 if (got_int)
6028 break;
6029
6030 /* Special cases */
6031 msg_putchar(a & BANG ? '!' : ' ');
6032 msg_putchar(a & REGSTR ? '"' : ' ');
6033 msg_putchar(gap != &ucmds ? 'b' : ' ');
6034 msg_putchar(' ');
6035
Bram Moolenaar8820b482017-03-16 17:23:31 +01006036 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 len = (int)STRLEN(cmd->uc_name) + 4;
6038
6039 do {
6040 msg_putchar(' ');
6041 ++len;
6042 } while (len < 16);
6043
6044 len = 0;
6045
6046 /* Arguments */
6047 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6048 {
6049 case 0: IObuff[len++] = '0'; break;
6050 case (EXTRA): IObuff[len++] = '*'; break;
6051 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6052 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6053 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6054 }
6055
6056 do {
6057 IObuff[len++] = ' ';
6058 } while (len < 5);
6059
6060 /* Range */
6061 if (a & (RANGE|COUNT))
6062 {
6063 if (a & COUNT)
6064 {
6065 /* -count=N */
6066 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6067 len += (int)STRLEN(IObuff + len);
6068 }
6069 else if (a & DFLALL)
6070 IObuff[len++] = '%';
6071 else if (cmd->uc_def >= 0)
6072 {
6073 /* -range=N */
6074 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6075 len += (int)STRLEN(IObuff + len);
6076 }
6077 else
6078 IObuff[len++] = '.';
6079 }
6080
6081 do {
6082 IObuff[len++] = ' ';
6083 } while (len < 11);
6084
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006085 /* Address Type */
6086 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6087 if (addr_type_complete[j].expand != ADDR_LINES
6088 && addr_type_complete[j].expand == cmd->uc_addr_type)
6089 {
6090 STRCPY(IObuff + len, addr_type_complete[j].name);
6091 len += (int)STRLEN(IObuff + len);
6092 break;
6093 }
6094
6095 do {
6096 IObuff[len++] = ' ';
6097 } while (len < 21);
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 /* Completion */
6100 for (j = 0; command_complete[j].expand != 0; ++j)
6101 if (command_complete[j].expand == cmd->uc_compl)
6102 {
6103 STRCPY(IObuff + len, command_complete[j].name);
6104 len += (int)STRLEN(IObuff + len);
6105 break;
6106 }
6107
6108 do {
6109 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006110 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 IObuff[len] = '\0';
6113 msg_outtrans(IObuff);
6114
6115 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006116#ifdef FEAT_EVAL
6117 if (p_verbose > 0)
6118 last_set_msg(cmd->uc_scriptID);
6119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 out_flush();
6121 ui_breakcheck();
6122 if (got_int)
6123 break;
6124 }
6125 if (gap == &ucmds || i < gap->ga_len)
6126 break;
6127 gap = &ucmds;
6128 }
6129
6130 if (!found)
6131 MSG(_("No user-defined commands found"));
6132}
6133
6134 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006135uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6138 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6139 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6140 0xb9, 0x7f, 0};
6141 int i;
6142
6143 for (i = 0; fcmd[i]; ++i)
6144 IObuff[i] = fcmd[i] - 0x40;
6145 IObuff[i] = 0;
6146 return IObuff;
6147}
6148
6149 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006150uc_scan_attr(
6151 char_u *attr,
6152 size_t len,
6153 long *argt,
6154 long *def,
6155 int *flags,
6156 int *compl,
6157 char_u **compl_arg,
6158 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
6160 char_u *p;
6161
6162 if (len == 0)
6163 {
6164 EMSG(_("E175: No attribute specified"));
6165 return FAIL;
6166 }
6167
6168 /* First, try the simple attributes (no arguments) */
6169 if (STRNICMP(attr, "bang", len) == 0)
6170 *argt |= BANG;
6171 else if (STRNICMP(attr, "buffer", len) == 0)
6172 *flags |= UC_BUFFER;
6173 else if (STRNICMP(attr, "register", len) == 0)
6174 *argt |= REGSTR;
6175 else if (STRNICMP(attr, "bar", len) == 0)
6176 *argt |= TRLBAR;
6177 else
6178 {
6179 int i;
6180 char_u *val = NULL;
6181 size_t vallen = 0;
6182 size_t attrlen = len;
6183
6184 /* Look for the attribute name - which is the part before any '=' */
6185 for (i = 0; i < (int)len; ++i)
6186 {
6187 if (attr[i] == '=')
6188 {
6189 val = &attr[i + 1];
6190 vallen = len - i - 1;
6191 attrlen = i;
6192 break;
6193 }
6194 }
6195
6196 if (STRNICMP(attr, "nargs", attrlen) == 0)
6197 {
6198 if (vallen == 1)
6199 {
6200 if (*val == '0')
6201 /* Do nothing - this is the default */;
6202 else if (*val == '1')
6203 *argt |= (EXTRA | NOSPC | NEEDARG);
6204 else if (*val == '*')
6205 *argt |= EXTRA;
6206 else if (*val == '?')
6207 *argt |= (EXTRA | NOSPC);
6208 else if (*val == '+')
6209 *argt |= (EXTRA | NEEDARG);
6210 else
6211 goto wrong_nargs;
6212 }
6213 else
6214 {
6215wrong_nargs:
6216 EMSG(_("E176: Invalid number of arguments"));
6217 return FAIL;
6218 }
6219 }
6220 else if (STRNICMP(attr, "range", attrlen) == 0)
6221 {
6222 *argt |= RANGE;
6223 if (vallen == 1 && *val == '%')
6224 *argt |= DFLALL;
6225 else if (val != NULL)
6226 {
6227 p = val;
6228 if (*def >= 0)
6229 {
6230two_count:
6231 EMSG(_("E177: Count cannot be specified twice"));
6232 return FAIL;
6233 }
6234
6235 *def = getdigits(&p);
6236 *argt |= (ZEROR | NOTADR);
6237
6238 if (p != val + vallen || vallen == 0)
6239 {
6240invalid_count:
6241 EMSG(_("E178: Invalid default value for count"));
6242 return FAIL;
6243 }
6244 }
6245 }
6246 else if (STRNICMP(attr, "count", attrlen) == 0)
6247 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006248 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250 if (val != NULL)
6251 {
6252 p = val;
6253 if (*def >= 0)
6254 goto two_count;
6255
6256 *def = getdigits(&p);
6257
6258 if (p != val + vallen)
6259 goto invalid_count;
6260 }
6261
6262 if (*def < 0)
6263 *def = 0;
6264 }
6265 else if (STRNICMP(attr, "complete", attrlen) == 0)
6266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (val == NULL)
6268 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006269 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 return FAIL;
6271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006273 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6274 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006277 else if (STRNICMP(attr, "addr", attrlen) == 0)
6278 {
6279 *argt |= RANGE;
6280 if (val == NULL)
6281 {
6282 EMSG(_("E179: argument required for -addr"));
6283 return FAIL;
6284 }
6285 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6286 == FAIL)
6287 return FAIL;
6288 if (addr_type_arg != ADDR_LINES)
6289 *argt |= (ZEROR | NOTADR) ;
6290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 else
6292 {
6293 char_u ch = attr[len];
6294 attr[len] = '\0';
6295 EMSG2(_("E181: Invalid attribute: %s"), attr);
6296 attr[len] = ch;
6297 return FAIL;
6298 }
6299 }
6300
6301 return OK;
6302}
6303
Bram Moolenaara850a712009-01-28 14:42:59 +00006304/*
6305 * ":command ..."
6306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006308ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 char_u *name;
6311 char_u *end;
6312 char_u *p;
6313 long argt = 0;
6314 long def = -1;
6315 int flags = 0;
6316 int compl = EXPAND_NOTHING;
6317 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006318 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006320 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
6322 p = eap->arg;
6323
6324 /* Check for attributes */
6325 while (*p == '-')
6326 {
6327 ++p;
6328 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006329 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6330 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 == FAIL)
6332 return;
6333 p = skipwhite(end);
6334 }
6335
6336 /* Get the name (if any) and skip to the following argument */
6337 name = p;
6338 if (ASCII_ISALPHA(*p))
6339 while (ASCII_ISALNUM(*p))
6340 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006341 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
6343 EMSG(_("E182: Invalid command name"));
6344 return;
6345 }
6346 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006347 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349 /* If there is nothing after the name, and no attributes were specified,
6350 * we are listing commands
6351 */
6352 p = skipwhite(end);
6353 if (!has_attr && ends_excmd(*p))
6354 {
6355 uc_list(name, end - name);
6356 }
6357 else if (!ASCII_ISUPPER(*name))
6358 {
6359 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6360 return;
6361 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006362 else if ((name_len == 1 && *name == 'X')
6363 || (name_len <= 4
6364 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6365 {
6366 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6367 return;
6368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 else
6370 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006371 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372}
6373
6374/*
6375 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * Clear all user commands, global and for current buffer.
6377 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006378 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006379ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381 uc_clear(&ucmds);
6382 uc_clear(&curbuf->b_ucmds);
6383}
6384
6385/*
6386 * Clear all user commands for "gap".
6387 */
6388 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006389uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391 int i;
6392 ucmd_T *cmd;
6393
6394 for (i = 0; i < gap->ga_len; ++i)
6395 {
6396 cmd = USER_CMD_GA(gap, i);
6397 vim_free(cmd->uc_name);
6398 vim_free(cmd->uc_rep);
6399# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6400 vim_free(cmd->uc_compl_arg);
6401# endif
6402 }
6403 ga_clear(gap);
6404}
6405
6406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006407ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 int i = 0;
6410 ucmd_T *cmd = NULL;
6411 int cmp = -1;
6412 garray_T *gap;
6413
6414 gap = &curbuf->b_ucmds;
6415 for (;;)
6416 {
6417 for (i = 0; i < gap->ga_len; ++i)
6418 {
6419 cmd = USER_CMD_GA(gap, i);
6420 cmp = STRCMP(eap->arg, cmd->uc_name);
6421 if (cmp <= 0)
6422 break;
6423 }
6424 if (gap == &ucmds || cmp == 0)
6425 break;
6426 gap = &ucmds;
6427 }
6428
6429 if (cmp != 0)
6430 {
6431 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6432 return;
6433 }
6434
6435 vim_free(cmd->uc_name);
6436 vim_free(cmd->uc_rep);
6437# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6438 vim_free(cmd->uc_compl_arg);
6439# endif
6440
6441 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443 if (i < gap->ga_len)
6444 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6445}
6446
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006447/*
6448 * split and quote args for <f-args>
6449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006451uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 char_u *buf;
6454 char_u *p;
6455 char_u *q;
6456 int len;
6457
6458 /* Precalculate length */
6459 p = arg;
6460 len = 2; /* Initial and final quotes */
6461
6462 while (*p)
6463 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006464 if (p[0] == '\\' && p[1] == '\\')
6465 {
6466 len += 2;
6467 p += 2;
6468 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006469 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 len += 1;
6472 p += 2;
6473 }
6474 else if (*p == '\\' || *p == '"')
6475 {
6476 len += 2;
6477 p += 1;
6478 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006479 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
6481 p = skipwhite(p);
6482 if (*p == NUL)
6483 break;
6484 len += 3; /* "," */
6485 }
6486 else
6487 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006488#ifdef FEAT_MBYTE
6489 int charlen = (*mb_ptr2len)(p);
6490 len += charlen;
6491 p += charlen;
6492#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 ++len;
6494 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
6497 }
6498
6499 buf = alloc(len + 1);
6500 if (buf == NULL)
6501 {
6502 *lenp = 0;
6503 return buf;
6504 }
6505
6506 p = arg;
6507 q = buf;
6508 *q++ = '"';
6509 while (*p)
6510 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006511 if (p[0] == '\\' && p[1] == '\\')
6512 {
6513 *q++ = '\\';
6514 *q++ = '\\';
6515 p += 2;
6516 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006517 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
6519 *q++ = p[1];
6520 p += 2;
6521 }
6522 else if (*p == '\\' || *p == '"')
6523 {
6524 *q++ = '\\';
6525 *q++ = *p++;
6526 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006527 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
6529 p = skipwhite(p);
6530 if (*p == NUL)
6531 break;
6532 *q++ = '"';
6533 *q++ = ',';
6534 *q++ = '"';
6535 }
6536 else
6537 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006538 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 }
6540 }
6541 *q++ = '"';
6542 *q = 0;
6543
6544 *lenp = len;
6545 return buf;
6546}
6547
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006548 static size_t
6549add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6550{
6551 size_t result;
6552
6553 result = STRLEN(mod_str);
6554 if (*multi_mods)
6555 result += 1;
6556 if (buf != NULL)
6557 {
6558 if (*multi_mods)
6559 STRCAT(buf, " ");
6560 STRCAT(buf, mod_str);
6561 }
6562
6563 *multi_mods = 1;
6564
6565 return result;
6566}
6567
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568/*
6569 * Check for a <> code in a user command.
6570 * "code" points to the '<'. "len" the length of the <> (inclusive).
6571 * "buf" is where the result is to be added.
6572 * "split_buf" points to a buffer used for splitting, caller should free it.
6573 * "split_len" is the length of what "split_buf" contains.
6574 * Returns the length of the replacement, which has been added to "buf".
6575 * Returns -1 if there was no match, and only the "<" has been copied.
6576 */
6577 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006578uc_check_code(
6579 char_u *code,
6580 size_t len,
6581 char_u *buf,
6582 ucmd_T *cmd, /* the user command we're expanding */
6583 exarg_T *eap, /* ex arguments */
6584 char_u **split_buf,
6585 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 size_t result = 0;
6588 char_u *p = code + 1;
6589 size_t l = len - 2;
6590 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006591 enum {
6592 ct_ARGS,
6593 ct_BANG,
6594 ct_COUNT,
6595 ct_LINE1,
6596 ct_LINE2,
6597 ct_RANGE,
6598 ct_MODS,
6599 ct_REGISTER,
6600 ct_LT,
6601 ct_NONE
6602 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6605 {
6606 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6607 p += 2;
6608 l -= 2;
6609 }
6610
Bram Moolenaar371d5402006-03-20 21:47:49 +00006611 ++l;
6612 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006614 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006616 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006618 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006620 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006622 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006624 else if (STRNICMP(p, "range>", l) == 0)
6625 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006626 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006628 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006630 else if (STRNICMP(p, "mods>", l) == 0)
6631 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
6633 switch (type)
6634 {
6635 case ct_ARGS:
6636 /* Simple case first */
6637 if (*eap->arg == NUL)
6638 {
6639 if (quote == 1)
6640 {
6641 result = 2;
6642 if (buf != NULL)
6643 STRCPY(buf, "''");
6644 }
6645 else
6646 result = 0;
6647 break;
6648 }
6649
6650 /* When specified there is a single argument don't split it.
6651 * Works for ":Cmd %" when % is "a b c". */
6652 if ((eap->argt & NOSPC) && quote == 2)
6653 quote = 1;
6654
6655 switch (quote)
6656 {
6657 case 0: /* No quoting, no splitting */
6658 result = STRLEN(eap->arg);
6659 if (buf != NULL)
6660 STRCPY(buf, eap->arg);
6661 break;
6662 case 1: /* Quote, but don't split */
6663 result = STRLEN(eap->arg) + 2;
6664 for (p = eap->arg; *p; ++p)
6665 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006666#ifdef FEAT_MBYTE
6667 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6668 /* DBCS can contain \ in a trail byte, skip the
6669 * double-byte character. */
6670 ++p;
6671 else
6672#endif
6673 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 ++result;
6675 }
6676
6677 if (buf != NULL)
6678 {
6679 *buf++ = '"';
6680 for (p = eap->arg; *p; ++p)
6681 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006682#ifdef FEAT_MBYTE
6683 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6684 /* DBCS can contain \ in a trail byte, copy the
6685 * double-byte character to avoid escaping. */
6686 *buf++ = *p++;
6687 else
6688#endif
6689 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 *buf++ = '\\';
6691 *buf++ = *p;
6692 }
6693 *buf = '"';
6694 }
6695
6696 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006697 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 /* This is hard, so only do it once, and cache the result */
6699 if (*split_buf == NULL)
6700 *split_buf = uc_split_args(eap->arg, split_len);
6701
6702 result = *split_len;
6703 if (buf != NULL && result != 0)
6704 STRCPY(buf, *split_buf);
6705
6706 break;
6707 }
6708 break;
6709
6710 case ct_BANG:
6711 result = eap->forceit ? 1 : 0;
6712 if (quote)
6713 result += 2;
6714 if (buf != NULL)
6715 {
6716 if (quote)
6717 *buf++ = '"';
6718 if (eap->forceit)
6719 *buf++ = '!';
6720 if (quote)
6721 *buf = '"';
6722 }
6723 break;
6724
6725 case ct_LINE1:
6726 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006727 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 case ct_COUNT:
6729 {
6730 char num_buf[20];
6731 long num = (type == ct_LINE1) ? eap->line1 :
6732 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006733 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6735 size_t num_len;
6736
6737 sprintf(num_buf, "%ld", num);
6738 num_len = STRLEN(num_buf);
6739 result = num_len;
6740
6741 if (quote)
6742 result += 2;
6743
6744 if (buf != NULL)
6745 {
6746 if (quote)
6747 *buf++ = '"';
6748 STRCPY(buf, num_buf);
6749 buf += num_len;
6750 if (quote)
6751 *buf = '"';
6752 }
6753
6754 break;
6755 }
6756
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006757 case ct_MODS:
6758 {
6759 int multi_mods = 0;
6760 typedef struct {
6761 int *varp;
6762 char *name;
6763 } mod_entry_T;
6764 static mod_entry_T mod_entries[] = {
6765#ifdef FEAT_BROWSE_CMD
6766 {&cmdmod.browse, "browse"},
6767#endif
6768#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6769 {&cmdmod.confirm, "confirm"},
6770#endif
6771 {&cmdmod.hide, "hide"},
6772 {&cmdmod.keepalt, "keepalt"},
6773 {&cmdmod.keepjumps, "keepjumps"},
6774 {&cmdmod.keepmarks, "keepmarks"},
6775 {&cmdmod.keeppatterns, "keeppatterns"},
6776 {&cmdmod.lockmarks, "lockmarks"},
6777 {&cmdmod.noswapfile, "noswapfile"},
6778 {NULL, NULL}
6779 };
6780 int i;
6781
6782 result = quote ? 2 : 0;
6783 if (buf != NULL)
6784 {
6785 if (quote)
6786 *buf++ = '"';
6787 *buf = '\0';
6788 }
6789
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006790 /* :aboveleft and :leftabove */
6791 if (cmdmod.split & WSP_ABOVE)
6792 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6793 /* :belowright and :rightbelow */
6794 if (cmdmod.split & WSP_BELOW)
6795 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6796 /* :botright */
6797 if (cmdmod.split & WSP_BOT)
6798 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006799
6800 /* the modifiers that are simple flags */
6801 for (i = 0; mod_entries[i].varp != NULL; ++i)
6802 if (*mod_entries[i].varp)
6803 result += add_cmd_modifier(buf, mod_entries[i].name,
6804 &multi_mods);
6805
6806 /* TODO: How to support :noautocmd? */
6807#ifdef HAVE_SANDBOX
6808 /* TODO: How to support :sandbox?*/
6809#endif
6810 /* :silent */
6811 if (msg_silent > 0)
6812 result += add_cmd_modifier(buf,
6813 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006814 /* :tab */
6815 if (cmdmod.tab > 0)
6816 result += add_cmd_modifier(buf, "tab", &multi_mods);
6817 /* :topleft */
6818 if (cmdmod.split & WSP_TOP)
6819 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006820 /* TODO: How to support :unsilent?*/
6821 /* :verbose */
6822 if (p_verbose > 0)
6823 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006824 /* :vertical */
6825 if (cmdmod.split & WSP_VERT)
6826 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006827 if (quote && buf != NULL)
6828 {
6829 buf += result - 2;
6830 *buf = '"';
6831 }
6832 break;
6833 }
6834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 case ct_REGISTER:
6836 result = eap->regname ? 1 : 0;
6837 if (quote)
6838 result += 2;
6839 if (buf != NULL)
6840 {
6841 if (quote)
6842 *buf++ = '\'';
6843 if (eap->regname)
6844 *buf++ = eap->regname;
6845 if (quote)
6846 *buf = '\'';
6847 }
6848 break;
6849
6850 case ct_LT:
6851 result = 1;
6852 if (buf != NULL)
6853 *buf = '<';
6854 break;
6855
6856 default:
6857 /* Not recognized: just copy the '<' and return -1. */
6858 result = (size_t)-1;
6859 if (buf != NULL)
6860 *buf = '<';
6861 break;
6862 }
6863
6864 return result;
6865}
6866
6867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006868do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 char_u *buf;
6871 char_u *p;
6872 char_u *q;
6873
6874 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006875 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006876 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 size_t len, totlen;
6878
6879 size_t split_len = 0;
6880 char_u *split_buf = NULL;
6881 ucmd_T *cmd;
6882#ifdef FEAT_EVAL
6883 scid_T save_current_SID = current_SID;
6884#endif
6885
6886 if (eap->cmdidx == CMD_USER)
6887 cmd = USER_CMD(eap->useridx);
6888 else
6889 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6890
6891 /*
6892 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006893 * First round: "buf" is NULL, compute length, allocate "buf".
6894 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 */
6896 buf = NULL;
6897 for (;;)
6898 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006899 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006900 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006902
6903 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006905 start = vim_strchr(p, '<');
6906 if (start != NULL)
6907 end = vim_strchr(start + 1, '>');
6908 if (buf != NULL)
6909 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006910 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6911 ;
6912 if (*ksp == K_SPECIAL
6913 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006914 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6915# ifdef FEAT_GUI
6916 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6917# endif
6918 ))
6919 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006920 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006921 * KS_SPECIAL KE_FILLER, like for mappings, but
6922 * do_cmdline() doesn't handle that, so convert it back.
6923 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6924 len = ksp - p;
6925 if (len > 0)
6926 {
6927 mch_memmove(q, p, len);
6928 q += len;
6929 }
6930 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6931 p = ksp + 3;
6932 continue;
6933 }
6934 }
6935
6936 /* break if there no <item> is found */
6937 if (start == NULL || end == NULL)
6938 break;
6939
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 /* Include the '>' */
6941 ++end;
6942
6943 /* Take everything up to the '<' */
6944 len = start - p;
6945 if (buf == NULL)
6946 totlen += len;
6947 else
6948 {
6949 mch_memmove(q, p, len);
6950 q += len;
6951 }
6952
6953 len = uc_check_code(start, end - start, q, cmd, eap,
6954 &split_buf, &split_len);
6955 if (len == (size_t)-1)
6956 {
6957 /* no match, continue after '<' */
6958 p = start + 1;
6959 len = 1;
6960 }
6961 else
6962 p = end;
6963 if (buf == NULL)
6964 totlen += len;
6965 else
6966 q += len;
6967 }
6968 if (buf != NULL) /* second time here, finished */
6969 {
6970 STRCPY(q, p);
6971 break;
6972 }
6973
6974 totlen += STRLEN(p); /* Add on the trailing characters */
6975 buf = alloc((unsigned)(totlen + 1));
6976 if (buf == NULL)
6977 {
6978 vim_free(split_buf);
6979 return;
6980 }
6981 }
6982
6983#ifdef FEAT_EVAL
6984 current_SID = cmd->uc_scriptID;
6985#endif
6986 (void)do_cmdline(buf, eap->getline, eap->cookie,
6987 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6988#ifdef FEAT_EVAL
6989 current_SID = save_current_SID;
6990#endif
6991 vim_free(buf);
6992 vim_free(split_buf);
6993}
6994
6995# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6996 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006997get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7000}
7001
7002/*
7003 * Function given to ExpandGeneric() to obtain the list of user command names.
7004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007006get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 if (idx < curbuf->b_ucmds.ga_len)
7009 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7010 idx -= curbuf->b_ucmds.ga_len;
7011 if (idx < ucmds.ga_len)
7012 return USER_CMD(idx)->uc_name;
7013 return NULL;
7014}
7015
7016/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007017 * Function given to ExpandGeneric() to obtain the list of user address type names.
7018 */
7019 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007020get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007021{
7022 return (char_u *)addr_type_complete[idx].name;
7023}
7024
7025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 * Function given to ExpandGeneric() to obtain the list of user command
7027 * attributes.
7028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007030get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007033 {"addr", "bang", "bar", "buffer", "complete",
7034 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007036 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 return NULL;
7038 return (char_u *)user_cmd_flags[idx];
7039}
7040
7041/*
7042 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007045get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7048
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007049 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 return NULL;
7051 return (char_u *)user_cmd_nargs[idx];
7052}
7053
7054/*
7055 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007058get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 return (char_u *)command_complete[idx].name;
7061}
7062# endif /* FEAT_CMDL_COMPL */
7063
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007064/*
7065 * Parse address type argument
7066 */
7067 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007068parse_addr_type_arg(
7069 char_u *value,
7070 int vallen,
7071 long *argt,
7072 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007073{
7074 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007075
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007076 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7077 {
7078 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7079 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7080 if (a && b)
7081 {
7082 *addr_type_arg = addr_type_complete[i].expand;
7083 break;
7084 }
7085 }
7086
7087 if (addr_type_complete[i].expand == -1)
7088 {
7089 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007090
Bram Moolenaar1c465442017-03-12 20:10:05 +01007091 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007092 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007093 err[i] = NUL;
7094 EMSG2(_("E180: Invalid address type value: %s"), err);
7095 return FAIL;
7096 }
7097
7098 if (*addr_type_arg != ADDR_LINES)
7099 *argt |= NOTADR;
7100
7101 return OK;
7102}
7103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104#endif /* FEAT_USR_CMDS */
7105
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007106#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7107/*
7108 * Parse a completion argument "value[vallen]".
7109 * The detected completion goes in "*complp", argument type in "*argt".
7110 * When there is an argument, for function and user defined completion, it's
7111 * copied to allocated memory and stored in "*compl_arg".
7112 * Returns FAIL if something is wrong.
7113 */
7114 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007115parse_compl_arg(
7116 char_u *value,
7117 int vallen,
7118 int *complp,
7119 long *argt,
7120 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121{
7122 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007123# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007124 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007125# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007126 int i;
7127 int valend = vallen;
7128
7129 /* Look for any argument part - which is the part after any ',' */
7130 for (i = 0; i < vallen; ++i)
7131 {
7132 if (value[i] == ',')
7133 {
7134 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007135# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007136 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007137# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007138 valend = i;
7139 break;
7140 }
7141 }
7142
7143 for (i = 0; command_complete[i].expand != 0; ++i)
7144 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007145 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007146 && STRNCMP(value, command_complete[i].name, valend) == 0)
7147 {
7148 *complp = command_complete[i].expand;
7149 if (command_complete[i].expand == EXPAND_BUFFERS)
7150 *argt |= BUFNAME;
7151 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7152 || command_complete[i].expand == EXPAND_FILES)
7153 *argt |= XFILE;
7154 break;
7155 }
7156 }
7157
7158 if (command_complete[i].expand == 0)
7159 {
7160 EMSG2(_("E180: Invalid complete value: %s"), value);
7161 return FAIL;
7162 }
7163
7164# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7165 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7166 && arg != NULL)
7167# else
7168 if (arg != NULL)
7169# endif
7170 {
7171 EMSG(_("E468: Completion argument only allowed for custom completion"));
7172 return FAIL;
7173 }
7174
7175# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7176 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7177 && arg == NULL)
7178 {
7179 EMSG(_("E467: Custom completion requires a function argument"));
7180 return FAIL;
7181 }
7182
7183 if (arg != NULL)
7184 *compl_arg = vim_strnsave(arg, (int)arglen);
7185# endif
7186 return OK;
7187}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007188
7189 int
7190cmdcomplete_str_to_type(char_u *complete_str)
7191{
7192 int i;
7193
7194 for (i = 0; command_complete[i].expand != 0; ++i)
7195 if (STRCMP(complete_str, command_complete[i].name) == 0)
7196 return command_complete[i].expand;
7197
7198 return EXPAND_NOTHING;
7199}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007200#endif
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007203ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
Bram Moolenaare6850792010-05-14 15:28:44 +02007205 if (*eap->arg == NUL)
7206 {
7207#ifdef FEAT_EVAL
7208 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7209 char_u *p = NULL;
7210
7211 if (expr != NULL)
7212 {
7213 ++emsg_off;
7214 p = eval_to_string(expr, NULL, FALSE);
7215 --emsg_off;
7216 vim_free(expr);
7217 }
7218 if (p != NULL)
7219 {
7220 MSG(p);
7221 vim_free(p);
7222 }
7223 else
7224 MSG("default");
7225#else
7226 MSG(_("unknown"));
7227#endif
7228 }
7229 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007230 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231}
7232
7233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007234ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
7236 if (*eap->arg == NUL && eap->cmd[2] == '!')
7237 MSG(_("Greetings, Vim user!"));
7238 do_highlight(eap->arg, eap->forceit, FALSE);
7239}
7240
7241
7242/*
7243 * Call this function if we thought we were going to exit, but we won't
7244 * (because of an error). May need to restore the terminal mode.
7245 */
7246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007247not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
7249 exiting = FALSE;
7250 settmode(TMODE_RAW);
7251}
7252
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007253 static int
7254before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7255{
7256 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7257
7258 /* Bail out when autocommands closed the window.
7259 * Refuse to quit when the buffer in the last window is being closed (can
7260 * only happen in autocommands). */
7261 if (!win_valid(wp)
7262 || curbuf_locked()
7263 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7264 return TRUE;
7265
7266 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7267 {
7268 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7269 /* Refuse to quit when locked or when the buffer in the last window is
7270 * being closed (can only happen in autocommands). */
7271 if (curbuf_locked()
7272 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7273 return TRUE;
7274 }
7275
7276 return FALSE;
7277}
7278
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007280 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007281 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 */
7283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007284ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007286 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007287
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288#ifdef FEAT_CMDWIN
7289 if (cmdwin_type != 0)
7290 {
7291 cmdwin_result = Ctrl_C;
7292 return;
7293 }
7294#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007295 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007296 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007297 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007298 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007299 return;
7300 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007301 if (eap->addr_count > 0)
7302 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007303 int wnr = eap->line2;
7304
7305 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7306 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007307 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007308 }
7309 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007310 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007311
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007312 /* Refuse to quit when locked. */
7313 if (curbuf_locked())
7314 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007315
7316 /* Trigger QuitPre and maybe ExitPre */
7317 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007318 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320#ifdef FEAT_NETBEANS_INTG
7321 netbeansForcedQuit = eap->forceit;
7322#endif
7323
7324 /*
7325 * If there are more files or windows we won't exit.
7326 */
7327 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7328 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007329 if ((!buf_hide(wp->w_buffer)
7330 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007331 | (eap->forceit ? CCGD_FORCEIT : 0)
7332 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007334 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 not_exiting();
7337 }
7338 else
7339 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007340 /* quit last window
7341 * Note: only_one_window() returns true, even so a help window is
7342 * still open. In that case only quit, if no address has been
7343 * specified. Example:
7344 * :h|wincmd w|1q - don't quit
7345 * :h|wincmd w|q - quit
7346 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007347 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007349 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007350#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007354 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
7356}
7357
7358/*
7359 * ":cquit".
7360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007362ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
7364 getout(1); /* this does not always pass on the exit code to the Manx
7365 compiler. why? */
7366}
7367
7368/*
7369 * ":qall": try to quit all windows
7370 */
7371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007372ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374# ifdef FEAT_CMDWIN
7375 if (cmdwin_type != 0)
7376 {
7377 if (eap->forceit)
7378 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7379 else
7380 cmdwin_result = K_XF2;
7381 return;
7382 }
7383# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007384
7385 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007386 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007387 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007388 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007389 return;
7390 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007391
7392 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007393 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007396 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 getout(0);
7398 not_exiting();
7399}
7400
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401/*
7402 * ":close": close current window, unless it is the last one
7403 */
7404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007405ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007407 win_T *win;
7408 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007409#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007411 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007413#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007414 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007415 {
7416 if (eap->addr_count == 0)
7417 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007418 else
7419 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007420 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007421 {
7422 winnr++;
7423 if (winnr == eap->line2)
7424 break;
7425 }
7426 if (win == NULL)
7427 win = lastwin;
7428 ex_win_close(eap->forceit, win, NULL);
7429 }
7430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432
Bram Moolenaar4033c552017-09-16 20:54:51 +02007433#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007434/*
7435 * ":pclose": Close any preview window.
7436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007438ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007439{
7440 win_T *win;
7441
Bram Moolenaar29323592016-07-24 22:04:11 +02007442 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443 if (win->w_p_pvw)
7444 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007445 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446 break;
7447 }
7448}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007449#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007450
Bram Moolenaarf740b292006-02-16 22:11:02 +00007451/*
7452 * Close window "win" and take care of handling closing the last window for a
7453 * modified buffer.
7454 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007456ex_win_close(
7457 int forceit,
7458 win_T *win,
7459 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461 int need_hide;
7462 buf_T *buf = win->w_buffer;
7463
7464 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007465 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007467#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if ((p_confirm || cmdmod.confirm) && p_write)
7469 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007470 bufref_T bufref;
7471
7472 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007474 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 return;
7476 need_hide = FALSE;
7477 }
7478 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007481 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 return;
7483 }
7484 }
7485
Bram Moolenaar4033c552017-09-16 20:54:51 +02007486#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007488#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007489
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007491 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007492 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007493 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007494 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495}
7496
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007498 * Handle the argument for a tabpage related ex command.
7499 * Returns a tabpage number.
7500 * When an error is encountered then eap->errmsg is set.
7501 */
7502 static int
7503get_tabpage_arg(exarg_T *eap)
7504{
7505 int tab_number;
7506 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7507
7508 if (eap->arg && *eap->arg != NUL)
7509 {
7510 char_u *p = eap->arg;
7511 char_u *p_save;
7512 int relative = 0; /* argument +N/-N means: go to N places to the
7513 * right/left relative to the current position. */
7514
7515 if (*p == '-')
7516 {
7517 relative = -1;
7518 p++;
7519 }
7520 else if (*p == '+')
7521 {
7522 relative = 1;
7523 p++;
7524 }
7525
7526 p_save = p;
7527 tab_number = getdigits(&p);
7528
7529 if (relative == 0)
7530 {
7531 if (STRCMP(p, "$") == 0)
7532 tab_number = LAST_TAB_NR;
7533 else if (p == p_save || *p_save == '-' || *p != NUL
7534 || tab_number > LAST_TAB_NR)
7535 {
7536 /* No numbers as argument. */
7537 eap->errmsg = e_invarg;
7538 goto theend;
7539 }
7540 }
7541 else
7542 {
7543 if (*p_save == NUL)
7544 tab_number = 1;
7545 else if (p == p_save || *p_save == '-' || *p != NUL
7546 || tab_number == 0)
7547 {
7548 /* No numbers as argument. */
7549 eap->errmsg = e_invarg;
7550 goto theend;
7551 }
7552 tab_number = tab_number * relative + tabpage_index(curtab);
7553 if (!unaccept_arg0 && relative == -1)
7554 --tab_number;
7555 }
7556 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7557 eap->errmsg = e_invarg;
7558 }
7559 else if (eap->addr_count > 0)
7560 {
7561 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007562 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007563 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007564 tab_number = 0;
7565 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007566 else
7567 {
7568 tab_number = eap->line2;
7569 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7570 {
7571 --tab_number;
7572 if (tab_number < unaccept_arg0)
7573 eap->errmsg = e_invarg;
7574 }
7575 }
7576 }
7577 else
7578 {
7579 switch (eap->cmdidx)
7580 {
7581 case CMD_tabnext:
7582 tab_number = tabpage_index(curtab) + 1;
7583 if (tab_number > LAST_TAB_NR)
7584 tab_number = 1;
7585 break;
7586 case CMD_tabmove:
7587 tab_number = LAST_TAB_NR;
7588 break;
7589 default:
7590 tab_number = tabpage_index(curtab);
7591 }
7592 }
7593
7594theend:
7595 return tab_number;
7596}
7597
7598/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007599 * ":tabclose": close current tab page, unless it is the last one.
7600 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 */
7602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007603ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007605 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007606 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007607
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007608# ifdef FEAT_CMDWIN
7609 if (cmdwin_type != 0)
7610 cmdwin_result = K_IGNORE;
7611 else
7612# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007613 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007614 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007617 tab_number = get_tabpage_arg(eap);
7618 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007619 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007620 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007621 if (tp == NULL)
7622 {
7623 beep_flush();
7624 return;
7625 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007626 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007627 {
7628 tabpage_close_other(tp, eap->forceit);
7629 return;
7630 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007631 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007632 tabpage_close(eap->forceit);
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007635}
7636
7637/*
7638 * ":tabonly": close all tab pages except the current one
7639 */
7640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007642{
7643 tabpage_T *tp;
7644 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007645 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007646
7647# ifdef FEAT_CMDWIN
7648 if (cmdwin_type != 0)
7649 cmdwin_result = K_IGNORE;
7650 else
7651# endif
7652 if (first_tabpage->tp_next == NULL)
7653 MSG(_("Already only one tab page"));
7654 else
7655 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007656 tab_number = get_tabpage_arg(eap);
7657 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007658 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007659 goto_tabpage(tab_number);
7660 /* Repeat this up to a 1000 times, because autocommands may
7661 * mess up the lists. */
7662 for (done = 0; done < 1000; ++done)
7663 {
7664 FOR_ALL_TABPAGES(tp)
7665 if (tp->tp_topframe != topframe)
7666 {
7667 tabpage_close_other(tp, eap->forceit);
7668 /* if we failed to close it quit */
7669 if (valid_tabpage(tp))
7670 done = 1000;
7671 /* start over, "tp" is now invalid */
7672 break;
7673 }
7674 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007675 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007676 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007677 }
7678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007682 * Close the current tab page.
7683 */
7684 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007685tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007686{
7687 /* First close all the windows but the current one. If that worked then
7688 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007689 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007690 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007691 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007692 ex_win_close(forceit, curwin, NULL);
7693# ifdef FEAT_GUI
7694 need_mouse_correct = TRUE;
7695# endif
7696}
7697
7698/*
7699 * Close tab page "tp", which is not the current tab page.
7700 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007701 * Also takes care of the tab pages line disappearing when closing the
7702 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007703 */
7704 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007705tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007706{
7707 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007708 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007709 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007710
7711 /* Limit to 1000 windows, autocommands may add a window while we close
7712 * one. OK, so I'm paranoid... */
7713 while (++done < 1000)
7714 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007715 wp = tp->tp_firstwin;
7716 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007717
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007718 /* Autocommands may delete the tab page under our fingers and we may
7719 * fail to close a window with a modified buffer. */
7720 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007721 break;
7722 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007723
Bram Moolenaar29323592016-07-24 22:04:11 +02007724 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007725
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007726 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007727 if (h != tabline_height())
7728 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007729}
7730
7731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 * ":only".
7733 */
7734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007737 win_T *wp;
7738 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739# ifdef FEAT_GUI
7740 need_mouse_correct = TRUE;
7741# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007742 if (eap->addr_count > 0)
7743 {
7744 wnr = eap->line2;
7745 for (wp = firstwin; --wnr > 0; )
7746 {
7747 if (wp->w_next == NULL)
7748 break;
7749 else
7750 wp = wp->w_next;
7751 }
7752 win_goto(wp);
7753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 close_others(TRUE, eap->forceit);
7755}
7756
7757/*
7758 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007759 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007762ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
7764 if (eap->addr_count == 0)
7765 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007766 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
7769 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007770ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007772 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007773 if (!eap->skip)
7774 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007775#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007776 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007777#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007778 if (eap->addr_count == 0)
7779 win_close(curwin, FALSE); /* don't free buffer */
7780 else
7781 {
7782 int winnr = 0;
7783 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007784
Bram Moolenaar2256c992016-11-15 21:17:07 +01007785 FOR_ALL_WINDOWS(win)
7786 {
7787 winnr++;
7788 if (winnr == eap->line2)
7789 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007790 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007791 if (win == NULL)
7792 win = lastwin;
7793 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 }
7796}
7797
7798/*
7799 * ":stop" and ":suspend": Suspend Vim.
7800 */
7801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007802ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 /*
7805 * Disallow suspending for "rvim".
7806 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007807 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {
7809 if (!eap->forceit)
7810 autowrite_all();
7811 windgoto((int)Rows - 1, 0);
7812 out_char('\n');
7813 out_flush();
7814 stoptermcap();
7815 out_flush(); /* needed for SUN to restore xterm buffer */
7816#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007817 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818#endif
7819 ui_suspend(); /* call machine specific function */
7820#ifdef FEAT_TITLE
7821 maketitle();
7822 resettitle(); /* force updating the title */
7823#endif
7824 starttermcap();
7825 scroll_start(); /* scroll screen before redrawing */
7826 redraw_later_clear();
7827 shell_resized(); /* may have resized window */
7828 }
7829}
7830
7831/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007832 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 */
7834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007835ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
7837#ifdef FEAT_CMDWIN
7838 if (cmdwin_type != 0)
7839 {
7840 cmdwin_result = Ctrl_C;
7841 return;
7842 }
7843#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007844 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007845 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007846 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007847 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007848 return;
7849 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007850
7851 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007852 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
7854 /*
7855 * if more files or windows we won't exit
7856 */
7857 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7858 exiting = TRUE;
7859 if ( ((eap->cmdidx == CMD_wq
7860 || curbufIsChanged())
7861 && do_write(eap) == FAIL)
7862 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007863 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {
7865 not_exiting();
7866 }
7867 else
7868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007871 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872# ifdef FEAT_GUI
7873 need_mouse_correct = TRUE;
7874# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007875 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007876 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
7878}
7879
7880/*
7881 * ":print", ":list", ":number".
7882 */
7883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007884ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007886 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7887 EMSG(_(e_emptybuf));
7888 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007890 for ( ;!got_int; ui_breakcheck())
7891 {
7892 print_line(eap->line1,
7893 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7894 || (eap->flags & EXFLAG_NR)),
7895 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7896 if (++eap->line1 > eap->line2)
7897 break;
7898 out_flush(); /* show one line at a time */
7899 }
7900 setpcmark();
7901 /* put cursor at last line */
7902 curwin->w_cursor.lnum = eap->line2;
7903 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 }
7905
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907}
7908
7909#ifdef FEAT_BYTEOFF
7910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007911ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 goto_byte(eap->line2);
7914}
7915#endif
7916
7917/*
7918 * ":shell".
7919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 do_shell(NULL, 0);
7924}
7925
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007926#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007928static int drop_busy = FALSE;
7929static int drop_filec;
7930static char_u **drop_filev = NULL;
7931static int drop_split;
7932static void (*drop_callback)(void *);
7933static void *drop_cookie;
7934
7935 static void
7936handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 exarg_T ea;
7939 int save_msg_scroll = msg_scroll;
7940
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007941 // Setting the argument list may cause screen updates and being called
7942 // recursively. Avoid that by setting drop_busy.
7943 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
7945 /* Check whether the current buffer is changed. If so, we will need
7946 * to split the current window or data could be lost.
7947 * We don't need to check if the 'hidden' option is set, as in this
7948 * case the buffer won't be lost.
7949 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007950 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
7952 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007953 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 --emsg_off;
7955 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007956 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 if (win_split(0, 0) == FAIL)
7959 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007960 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
7962 /* When splitting the window, create a new alist. Otherwise the
7963 * existing one is overwritten. */
7964 alist_unlink(curwin->w_alist);
7965 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 }
7967
7968 /*
7969 * Set up the new argument list.
7970 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007971 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
7973 /*
7974 * Move to the first file.
7975 */
7976 /* Fake up a minimal "next" command for do_argfile() */
7977 vim_memset(&ea, 0, sizeof(ea));
7978 ea.cmd = (char_u *)"next";
7979 do_argfile(&ea, 0);
7980
7981 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7982 * mode that is not needed here. */
7983 need_start_insertmode = FALSE;
7984
7985 /* Restore msg_scroll, otherwise a following command may cause scrolling
7986 * unexpectedly. The screen will be redrawn by the caller, thus
7987 * msg_scroll being set by displaying a message is irrelevant. */
7988 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007989
7990 if (drop_callback != NULL)
7991 drop_callback(drop_cookie);
7992
7993 drop_filev = NULL;
7994 drop_busy = FALSE;
7995}
7996
7997/*
7998 * Handle a file drop. The code is here because a drop is *nearly* like an
7999 * :args command, but not quite (we have a list of exact filenames, so we
8000 * don't want to (a) parse a command line, or (b) expand wildcards. So the
8001 * code is very similar to :args and hence needs access to a lot of the static
8002 * functions in this file.
8003 *
8004 * The "filev" list must have been allocated using alloc(), as should each item
8005 * in the list. This function takes over responsibility for freeing the "filev"
8006 * list.
8007 */
8008 void
8009handle_drop(
8010 int filec, // the number of files dropped
8011 char_u **filev, // the list of files dropped
8012 int split, // force splitting the window
8013 void (*callback)(void *), // to be called after setting the argument
8014 // list
8015 void *cookie) // argument for "callback" (allocated)
8016{
8017 // Cannot handle recursive drops, finish the pending one.
8018 if (drop_busy)
8019 {
8020 FreeWild(filec, filev);
8021 vim_free(cookie);
8022 return;
8023 }
8024
8025 // When calling handle_drop() more than once in a row we only use the last
8026 // one.
8027 if (drop_filev != NULL)
8028 {
8029 FreeWild(drop_filec, drop_filev);
8030 vim_free(drop_cookie);
8031 }
8032
8033 drop_filec = filec;
8034 drop_filev = filev;
8035 drop_split = split;
8036 drop_callback = callback;
8037 drop_cookie = cookie;
8038
8039 // Postpone this when:
8040 // - editing the command line
8041 // - not possible to change the current buffer
8042 // - updating the screen
8043 // As it may change buffers and window structures that are in use and cause
8044 // freed memory to be used.
8045 if (text_locked() || curbuf_locked() || updating_screen)
8046 return;
8047
8048 handle_drop_internal();
8049}
8050
8051/*
8052 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8053 * reset: Handle a postponed drop.
8054 */
8055 void
8056handle_any_postponed_drop(void)
8057{
8058 if (!drop_busy && drop_filev != NULL
8059 && !text_locked() && !curbuf_locked() && !updating_screen)
8060 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061}
8062#endif
8063
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064/*
8065 * Clear an argument list: free all file names and reset it to zero entries.
8066 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008067 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008068alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 while (--al->al_ga.ga_len >= 0)
8071 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8072 ga_clear(&al->al_ga);
8073}
8074
8075/*
8076 * Init an argument list.
8077 */
8078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8082}
8083
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084/*
8085 * Remove a reference from an argument list.
8086 * Ignored when the argument list is the global one.
8087 * If the argument list is no longer used by any window, free it.
8088 */
8089 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 if (al != &global_alist && --al->al_refcount <= 0)
8093 {
8094 alist_clear(al);
8095 vim_free(al);
8096 }
8097}
8098
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099/*
8100 * Create a new argument list and use it for the current window.
8101 */
8102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008103alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8106 if (curwin->w_alist == NULL)
8107 {
8108 curwin->w_alist = &global_alist;
8109 ++global_alist.al_refcount;
8110 }
8111 else
8112 {
8113 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008114 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 alist_init(curwin->w_alist);
8116 }
8117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008119#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120/*
8121 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008122 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8123 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 */
8125 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008126alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008129 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 char_u **new_arg_files;
8131 int new_arg_file_count;
8132 char_u *save_p_su = p_su;
8133 int i;
8134
8135 /* Don't use 'suffixes' here. This should work like the shell did the
8136 * expansion. Also, the vimrc file isn't read yet, thus the user
8137 * can't set the options. */
8138 p_su = empty_option;
8139 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8140 if (old_arg_files != NULL)
8141 {
8142 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008143 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8144 old_arg_count = GARGCOUNT;
8145 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008147 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 && new_arg_file_count > 0)
8149 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008150 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8151 TRUE, fnum_list, fnum_len);
8152 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 }
8155 p_su = save_p_su;
8156}
8157#endif
8158
8159/*
8160 * Set the argument list for the current window.
8161 * Takes over the allocated files[] and the allocated fnames in it.
8162 */
8163 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008164alist_set(
8165 alist_T *al,
8166 int count,
8167 char_u **files,
8168 int use_curbuf,
8169 int *fnum_list,
8170 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171{
8172 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008173 static int recursive = 0;
8174
8175 if (recursive)
8176 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008177 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008178 return;
8179 }
8180 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181
8182 alist_clear(al);
8183 if (ga_grow(&al->al_ga, count) == OK)
8184 {
8185 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008186 {
8187 if (got_int)
8188 {
8189 /* When adding many buffers this can take a long time. Allow
8190 * interrupting here. */
8191 while (i < count)
8192 vim_free(files[i++]);
8193 break;
8194 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008195
8196 /* May set buffer name of a buffer previously used for the
8197 * argument list, so that it's re-used by alist_add. */
8198 if (fnum_list != NULL && i < fnum_len)
8199 buf_set_name(fnum_list[i], files[i]);
8200
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008202 ui_breakcheck();
8203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 vim_free(files);
8205 }
8206 else
8207 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008210
8211 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212}
8213
8214/*
8215 * Add file "fname" to argument list "al".
8216 * "fname" must have been allocated and "al" must have been checked for room.
8217 */
8218 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008219alist_add(
8220 alist_T *al,
8221 char_u *fname,
8222 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
8224 if (fname == NULL) /* don't add NULL file names */
8225 return;
8226#ifdef BACKSLASH_IN_FILENAME
8227 slash_adjust(fname);
8228#endif
8229 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8230 if (set_fnum > 0)
8231 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8232 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8233 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234}
8235
8236#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8237/*
8238 * Adjust slashes in file names. Called after 'shellslash' was set.
8239 */
8240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008241alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242{
8243 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008245 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246
8247 for (i = 0; i < GARGCOUNT; ++i)
8248 if (GARGLIST[i].ae_fname != NULL)
8249 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008250 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (wp->w_alist != &global_alist)
8252 for (i = 0; i < WARGCOUNT(wp); ++i)
8253 if (WARGLIST(wp)[i].ae_fname != NULL)
8254 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255}
8256#endif
8257
8258/*
8259 * ":preserve".
8260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008262ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008264 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 ml_preserve(curbuf, TRUE);
8266}
8267
8268/*
8269 * ":recover".
8270 */
8271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008272ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8275 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008276 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8277 | CCGD_MULTWIN
8278 | (eap->forceit ? CCGD_FORCEIT : 0)
8279 | CCGD_EXCMD)
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 && (*eap->arg == NUL
8282 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8283 ml_recover();
8284 recoverymode = FALSE;
8285}
8286
8287/*
8288 * Command modifier used in a wrong way.
8289 */
8290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008291ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
8293 eap->errmsg = e_invcmd;
8294}
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296/*
8297 * :sview [+command] file split window with new file, read-only
8298 * :split [[+command] file] split window with current or new file
8299 * :vsplit [[+command] file] split window vertically with current or new file
8300 * :new [[+command] file] split window with no or new file
8301 * :vnew [[+command] file] split vertically window with no or new file
8302 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008303 *
8304 * :tabedit open new Tab page with empty window
8305 * :tabedit [+command] file open new Tab page and edit "file"
8306 * :tabnew [[+command] file] just like :tabedit
8307 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 */
8309 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008310ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008312 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008313#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008315#endif
8316#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008318#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008319 int use_tab = eap->cmdidx == CMD_tabedit
8320 || eap->cmdidx == CMD_tabfind
8321 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
Bram Moolenaar4033c552017-09-16 20:54:51 +02008323#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326
Bram Moolenaar4033c552017-09-16 20:54:51 +02008327#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008329 * quickfix windows. But it's OK when doing ":tab split". */
8330 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {
8332 if (eap->cmdidx == CMD_split)
8333 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 if (eap->cmdidx == CMD_vsplit)
8335 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
Bram Moolenaar4033c552017-09-16 20:54:51 +02008339#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008340 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {
8342 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8343 FNAME_MESS, TRUE, curbuf->b_ffname);
8344 if (fname == NULL)
8345 goto theend;
8346 eap->arg = fname;
8347 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008348# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008349 else
8350# endif
8351#endif
8352#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 && eap->cmdidx != CMD_new)
8356 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008357 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008358# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008359 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008360# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008361 au_has_group((char_u *)"FileExplorer"))
8362 {
8363 /* No browsing supported but we do have the file explorer:
8364 * Edit the directory. */
8365 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8366 eap->arg = (char_u *)".";
8367 }
8368 else
8369 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008370 fname = do_browse(0, (char_u *)(use_tab
8371 ? _("Edit File in new tab page")
8372 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008374 if (fname == NULL)
8375 goto theend;
8376 eap->arg = fname;
8377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 }
8379 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008382 /*
8383 * Either open new tab page or split the window.
8384 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008385 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008386 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008387 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8388 : eap->addr_count == 0 ? 0
8389 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008390 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008391 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008392
8393 /* set the alternate buffer for the window we came from */
8394 if (curwin != old_curwin
8395 && win_valid(old_curwin)
8396 && old_curwin->w_buffer != curbuf
8397 && !cmdmod.keepalt)
8398 old_curwin->w_alt_fnum = curbuf->b_fnum;
8399 }
8400 }
8401 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 /* Reset 'scrollbind' when editing another file, but keep it when
8405 * doing ":split" without arguments. */
8406 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008407# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008411 {
8412 RESET_BINDING(curwin);
8413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 else
8415 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 do_exedit(eap, old_curwin);
8417 }
8418
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008419# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008423# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424theend:
8425 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008428
8429/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008430 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008431 */
8432 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008433tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008434{
8435 exarg_T ea;
8436
8437 vim_memset(&ea, 0, sizeof(ea));
8438 ea.cmdidx = CMD_tabnew;
8439 ea.cmd = (char_u *)"tabn";
8440 ea.arg = (char_u *)"";
8441 ex_splitview(&ea);
8442}
8443
8444/*
8445 * :tabnext command
8446 */
8447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008448ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008449{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008450 int tab_number;
8451
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008452 switch (eap->cmdidx)
8453 {
8454 case CMD_tabfirst:
8455 case CMD_tabrewind:
8456 goto_tabpage(1);
8457 break;
8458 case CMD_tablast:
8459 goto_tabpage(9999);
8460 break;
8461 case CMD_tabprevious:
8462 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008463 if (eap->arg && *eap->arg != NUL)
8464 {
8465 char_u *p = eap->arg;
8466 char_u *p_save = p;
8467
8468 tab_number = getdigits(&p);
8469 if (p == p_save || *p_save == '-' || *p != NUL
8470 || tab_number == 0)
8471 {
8472 /* No numbers as argument. */
8473 eap->errmsg = e_invarg;
8474 return;
8475 }
8476 }
8477 else
8478 {
8479 if (eap->addr_count == 0)
8480 tab_number = 1;
8481 else
8482 {
8483 tab_number = eap->line2;
8484 if (tab_number < 1)
8485 {
8486 eap->errmsg = e_invrange;
8487 return;
8488 }
8489 }
8490 }
8491 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008492 break;
8493 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008494 tab_number = get_tabpage_arg(eap);
8495 if (eap->errmsg == NULL)
8496 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008497 break;
8498 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008499}
8500
8501/*
8502 * :tabmove command
8503 */
8504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008505ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008506{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008507 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008508
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008509 tab_number = get_tabpage_arg(eap);
8510 if (eap->errmsg == NULL)
8511 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008512}
8513
8514/*
8515 * :tabs command: List tabs and their contents.
8516 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008518ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008519{
8520 tabpage_T *tp;
8521 win_T *wp;
8522 int tabcount = 1;
8523
8524 msg_start();
8525 msg_scroll = TRUE;
8526 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8527 {
8528 msg_putchar('\n');
8529 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008530 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008531 out_flush(); /* output one line at a time */
8532 ui_breakcheck();
8533
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008534 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008535 wp = firstwin;
8536 else
8537 wp = tp->tp_firstwin;
8538 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8539 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008540 msg_putchar('\n');
8541 msg_putchar(wp == curwin ? '>' : ' ');
8542 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008543 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8544 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008545 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008546 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008547 else
8548 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8549 IObuff, IOSIZE, TRUE);
8550 msg_outtrans(IObuff);
8551 out_flush(); /* output one line at a time */
8552 ui_breakcheck();
8553 }
8554 }
8555}
8556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557/*
8558 * ":mode": Set screen mode.
8559 * If no argument given, just get the screen size and redraw.
8560 */
8561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008562ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 if (*eap->arg == NUL)
8565 shell_resized();
8566 else
8567 mch_screenmode(eap->arg);
8568}
8569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570/*
8571 * ":resize".
8572 * set, increment or decrement current window height
8573 */
8574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008575ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577 int n;
8578 win_T *wp = curwin;
8579
8580 if (eap->addr_count > 0)
8581 {
8582 n = eap->line2;
8583 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8584 ;
8585 }
8586
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008587# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 if (cmdmod.split & WSP_VERT)
8592 {
8593 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008594 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8596 n = 9999;
8597 win_setwidth_win((int)n, wp);
8598 }
8599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {
8601 if (*eap->arg == '-' || *eap->arg == '+')
8602 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008603 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 n = 9999;
8605 win_setheight_win((int)n, wp);
8606 }
8607}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609/*
8610 * ":find [+command] <file>" command.
8611 */
8612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008613ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615#ifdef FEAT_SEARCHPATH
8616 char_u *fname;
8617 int count;
8618
8619 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8620 TRUE, curbuf->b_ffname);
8621 if (eap->addr_count > 0)
8622 {
8623 /* Repeat finding the file "count" times. This matters when it
8624 * appears several times in the path. */
8625 count = eap->line2;
8626 while (fname != NULL && --count > 0)
8627 {
8628 vim_free(fname);
8629 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8630 FALSE, curbuf->b_ffname);
8631 }
8632 }
8633
8634 if (fname != NULL)
8635 {
8636 eap->arg = fname;
8637#endif
8638 do_exedit(eap, NULL);
8639#ifdef FEAT_SEARCHPATH
8640 vim_free(fname);
8641 }
8642#endif
8643}
8644
8645/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008646 * ":open" simulation: for now just work like ":visual".
8647 */
8648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008649ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008650{
8651 regmatch_T regmatch;
8652 char_u *p;
8653
8654 curwin->w_cursor.lnum = eap->line2;
8655 beginline(BL_SOL | BL_FIX);
8656 if (*eap->arg == '/')
8657 {
8658 /* ":open /pattern/": put cursor in column found with pattern */
8659 ++eap->arg;
8660 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8661 *p = NUL;
8662 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8663 if (regmatch.regprog != NULL)
8664 {
8665 regmatch.rm_ic = p_ic;
8666 p = ml_get_curline();
8667 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008668 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 else
8670 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008671 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008672 }
8673 /* Move to the NUL, ignore any other arguments. */
8674 eap->arg += STRLEN(eap->arg);
8675 }
8676 check_cursor();
8677
8678 eap->cmdidx = CMD_visual;
8679 do_exedit(eap, NULL);
8680}
8681
8682/*
8683 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 */
8685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008686ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 do_exedit(eap, NULL);
8689}
8690
8691/*
8692 * ":edit <file>" command and alikes.
8693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008695do_exedit(
8696 exarg_T *eap,
8697 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008701 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
8703 /*
8704 * ":vi" command ends Ex mode.
8705 */
8706 if (exmode_active && (eap->cmdidx == CMD_visual
8707 || eap->cmdidx == CMD_view))
8708 {
8709 exmode_active = FALSE;
8710 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008711 {
8712 /* Special case: ":global/pat/visual\NLvi-commands" */
8713 if (global_busy)
8714 {
8715 int rd = RedrawingDisabled;
8716 int nwr = no_wait_return;
8717 int ms = msg_scroll;
8718#ifdef FEAT_GUI
8719 int he = hold_gui_events;
8720#endif
8721
8722 if (eap->nextcmd != NULL)
8723 {
8724 stuffReadbuff(eap->nextcmd);
8725 eap->nextcmd = NULL;
8726 }
8727
8728 if (exmode_was != EXMODE_VIM)
8729 settmode(TMODE_RAW);
8730 RedrawingDisabled = 0;
8731 no_wait_return = 0;
8732 need_wait_return = FALSE;
8733 msg_scroll = 0;
8734#ifdef FEAT_GUI
8735 hold_gui_events = 0;
8736#endif
8737 must_redraw = CLEAR;
8738
8739 main_loop(FALSE, TRUE);
8740
8741 RedrawingDisabled = rd;
8742 no_wait_return = nwr;
8743 msg_scroll = ms;
8744#ifdef FEAT_GUI
8745 hold_gui_events = he;
8746#endif
8747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 }
8751
8752 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008753 || eap->cmdidx == CMD_tabnew
8754 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008755 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008757 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 setpcmark();
8759 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008760 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8761 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008763 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 || *eap->arg != NUL
8765#ifdef FEAT_BROWSE
8766 || cmdmod.browse
8767#endif
8768 )
8769 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008770 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8771 * can bring us here, others are stopped earlier. */
8772 if (*eap->arg != NUL && curbuf_locked())
8773 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 n = readonlymode;
8776 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8777 readonlymode = TRUE;
8778 else if (eap->cmdidx == CMD_enew)
8779 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8780 empty buffer */
8781 setpcmark();
8782 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8783 NULL, eap,
8784 /* ":edit" goes to first line if Vi compatible */
8785 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8786 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8787 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008788 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008790 /* after a split we can use an existing buffer */
8791 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008793 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 {
8795 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 if (old_curwin != NULL)
8797 {
8798 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008799 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008801#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008802 cleanup_T cs;
8803
8804 /* Reset the error/interrupt/exception state here so that
8805 * aborting() returns FALSE when closing a window. */
8806 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008807#endif
8808#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008810#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008811 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008812
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008813#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008814 /* Restore the error/interrupt/exception state if not
8815 * discarded by a new aborting error, interrupt, or
8816 * uncaught exception. */
8817 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
8820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
8822 else if (readonlymode && curbuf->b_nwindows == 1)
8823 {
8824 /* When editing an already visited buffer, 'readonly' won't be set
8825 * but the previous value is kept. With ":view" and ":sview" we
8826 * want the file to be readonly, except when another window is
8827 * editing the same buffer. */
8828 curbuf->b_p_ro = TRUE;
8829 }
8830 readonlymode = n;
8831 }
8832 else
8833 {
8834 if (eap->do_ecmd_cmd != NULL)
8835 do_cmdline_cmd(eap->do_ecmd_cmd);
8836#ifdef FEAT_TITLE
8837 n = curwin->w_arg_idx_invalid;
8838#endif
8839 check_arg_idx(curwin);
8840#ifdef FEAT_TITLE
8841 if (n != curwin->w_arg_idx_invalid)
8842 maketitle();
8843#endif
8844 }
8845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 /*
8847 * if ":split file" worked, set alternate file name in old window to new
8848 * file
8849 */
8850 if (old_curwin != NULL
8851 && *eap->arg != NUL
8852 && curwin != old_curwin
8853 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008854 && old_curwin->w_buffer != curbuf
8855 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857
8858 ex_no_reprint = TRUE;
8859}
8860
8861#ifndef FEAT_GUI
8862/*
8863 * ":gui" and ":gvim" when there is no GUI.
8864 */
8865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008866ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868 eap->errmsg = e_nogvim;
8869}
8870#endif
8871
8872#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008874ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
8876 gui_make_tearoff(eap->arg);
8877}
8878#endif
8879
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008880#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8881 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008883ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008885# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8886 if (gui.in_use)
8887 gui_make_popup(eap->arg, eap->forceit);
8888# ifdef FEAT_TERM_POPUP_MENU
8889 else
8890# endif
8891# endif
8892# ifdef FEAT_TERM_POPUP_MENU
8893 pum_make_popup(eap->arg, eap->forceit);
8894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895}
8896#endif
8897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008899ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
8901 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8902 MSG(_("No swap file"));
8903 else
8904 msg(curbuf->b_ml.ml_mfp->mf_fname);
8905}
8906
8907/*
8908 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8909 * offset.
8910 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008913ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008916 win_T *save_curwin = curwin;
8917 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 long topline;
8919 long y;
8920 linenr_T old_linenr = curwin->w_cursor.lnum;
8921
8922 setpcmark();
8923
8924 /*
8925 * determine max topline
8926 */
8927 if (curwin->w_p_scb)
8928 {
8929 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008930 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 {
8932 if (wp->w_p_scb && wp->w_buffer)
8933 {
8934 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8935 if (topline > y)
8936 topline = y;
8937 }
8938 }
8939 if (topline < 1)
8940 topline = 1;
8941 }
8942 else
8943 {
8944 topline = 1;
8945 }
8946
8947
8948 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008949 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008951 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 {
8953 if (curwin->w_p_scb)
8954 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008955 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 y = topline - curwin->w_topline;
8957 if (y > 0)
8958 scrollup(y, TRUE);
8959 else
8960 scrolldown(-y, TRUE);
8961 curwin->w_scbind_pos = topline;
8962 redraw_later(VALID);
8963 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 }
8966 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008967 curwin = save_curwin;
8968 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 if (curwin->w_p_scb)
8970 {
8971 did_syncbind = TRUE;
8972 checkpcmark();
8973 if (old_linenr != curwin->w_cursor.lnum)
8974 {
8975 char_u ctrl_o[2];
8976
8977 ctrl_o[0] = Ctrl_O;
8978 ctrl_o[1] = 0;
8979 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8980 }
8981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
8984
8985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008986ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008988 int i;
8989 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8990 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991
8992 if (eap->usefilter) /* :r!cmd */
8993 do_bang(1, eap, FALSE, FALSE, TRUE);
8994 else
8995 {
8996 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8997 return;
8998
8999#ifdef FEAT_BROWSE
9000 if (cmdmod.browse)
9001 {
9002 char_u *browseFile;
9003
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009004 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 NULL, NULL, NULL, curbuf);
9006 if (browseFile != NULL)
9007 {
9008 i = readfile(browseFile, NULL,
9009 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9010 vim_free(browseFile);
9011 }
9012 else
9013 i = OK;
9014 }
9015 else
9016#endif
9017 if (*eap->arg == NUL)
9018 {
9019 if (check_fname() == FAIL) /* check for no file name */
9020 return;
9021 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9022 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9023 }
9024 else
9025 {
9026 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9027 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9028 i = readfile(eap->arg, NULL,
9029 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9030
9031 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009032 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009034#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 if (!aborting())
9036#endif
9037 EMSG2(_(e_notopen), eap->arg);
9038 }
9039 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009040 {
9041 if (empty && exmode_active)
9042 {
9043 /* Delete the empty line that remains. Historically ex does
9044 * this but vi doesn't. */
9045 if (eap->line2 == 0)
9046 lnum = curbuf->b_ml.ml_line_count;
9047 else
9048 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009049 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009050 {
9051 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009052 if (curwin->w_cursor.lnum > 1
9053 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009054 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009055 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009056 }
9057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061}
9062
Bram Moolenaarea408852005-06-25 22:49:46 +00009063static char_u *prev_dir = NULL;
9064
9065#if defined(EXITFREE) || defined(PROTO)
9066 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009067free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009068{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009069 VIM_CLEAR(prev_dir);
9070 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009071}
9072#endif
9073
Bram Moolenaarf4258302013-06-02 18:20:17 +02009074/*
9075 * Deal with the side effects of changing the current directory.
9076 * When "local" is TRUE then this was after an ":lcd" command.
9077 */
9078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009079post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009080{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009081 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009082 if (local)
9083 {
9084 /* If still in global directory, need to remember current
9085 * directory as global directory. */
9086 if (globaldir == NULL && prev_dir != NULL)
9087 globaldir = vim_strsave(prev_dir);
9088 /* Remember this local directory for the window. */
9089 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9090 curwin->w_localdir = vim_strsave(NameBuff);
9091 }
9092 else
9093 {
9094 /* We are now in the global directory, no need to remember its
9095 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009096 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009097 }
9098
9099 shorten_fnames(TRUE);
9100}
9101
Bram Moolenaarea408852005-06-25 22:49:46 +00009102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103/*
9104 * ":cd", ":lcd", ":chdir" and ":lchdir".
9105 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009106 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 char_u *new_dir;
9110 char_u *tofree;
9111
9112 new_dir = eap->arg;
9113#if !defined(UNIX) && !defined(VMS)
9114 /* for non-UNIX ":cd" means: print current directory */
9115 if (*new_dir == NUL)
9116 ex_pwd(NULL);
9117 else
9118#endif
9119 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009120 if (allbuf_locked())
9121 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009122 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9123 && !eap->forceit)
9124 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009125 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009126 return;
9127 }
9128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 /* ":cd -": Change to previous directory */
9130 if (STRCMP(new_dir, "-") == 0)
9131 {
9132 if (prev_dir == NULL)
9133 {
9134 EMSG(_("E186: No previous directory"));
9135 return;
9136 }
9137 new_dir = prev_dir;
9138 }
9139
9140 /* Save current directory for next ":cd -" */
9141 tofree = prev_dir;
9142 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9143 prev_dir = vim_strsave(NameBuff);
9144 else
9145 prev_dir = NULL;
9146
9147#if defined(UNIX) || defined(VMS)
9148 /* for UNIX ":cd" means: go to home directory */
9149 if (*new_dir == NUL)
9150 {
9151 /* use NameBuff for home directory name */
9152# ifdef VMS
9153 char_u *p;
9154
9155 p = mch_getenv((char_u *)"SYS$LOGIN");
9156 if (p == NULL || *p == NUL) /* empty is the same as not set */
9157 NameBuff[0] = NUL;
9158 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009159 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160# else
9161 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9162# endif
9163 new_dir = NameBuff;
9164 }
9165#endif
9166 if (new_dir == NULL || vim_chdir(new_dir))
9167 EMSG(_(e_failed));
9168 else
9169 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009170 int is_local_chdir = eap->cmdidx == CMD_lcd
9171 || eap->cmdidx == CMD_lchdir;
9172
9173 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174
9175 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009176 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009178 apply_autocmds(EVENT_DIRCHANGED,
9179 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9180 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
9182 vim_free(tofree);
9183 }
9184}
9185
9186/*
9187 * ":pwd".
9188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009190ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9193 {
9194#ifdef BACKSLASH_IN_FILENAME
9195 slash_adjust(NameBuff);
9196#endif
9197 msg(NameBuff);
9198 }
9199 else
9200 EMSG(_("E187: Unknown"));
9201}
9202
9203/*
9204 * ":=".
9205 */
9206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009207ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009210 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211}
9212
9213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009214ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009216 int n;
9217 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
9219 if (cursor_valid())
9220 {
9221 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9222 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009223 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009225
9226 len = eap->line2;
9227 switch (*eap->arg)
9228 {
9229 case 'm': break;
9230 case NUL: len *= 1000L; break;
9231 default: EMSG2(_(e_invarg2), eap->arg); return;
9232 }
9233 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234}
9235
9236/*
9237 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9238 */
9239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009240do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241{
9242 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009243 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
9245 cursor_on();
9246 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009247 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009249 wait_now = msec - done > 1000L ? 1000L : msec - done;
9250#ifdef FEAT_TIMERS
9251 {
9252 long due_time = check_due_timer();
9253
9254 if (due_time > 0 && due_time < wait_now)
9255 wait_now = due_time;
9256 }
9257#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009258#ifdef FEAT_JOB_CHANNEL
9259 if (has_any_channel() && wait_now > 100L)
9260 wait_now = 100L;
9261#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009262 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009263#ifdef FEAT_JOB_CHANNEL
9264 if (has_any_channel())
9265 ui_breakcheck_force(TRUE);
9266 else
9267#endif
9268 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009269#ifdef MESSAGE_QUEUE
9270 /* Process the netbeans and clientserver messages that may have been
9271 * received in the call to ui_breakcheck() when the GUI is in use. This
9272 * may occur when running a test case. */
9273 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009276
9277 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9278 // input buffer, otherwise a following call to input() fails.
9279 if (got_int)
9280 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281}
9282
9283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009284do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 int mode;
9287 char_u *cmdp;
9288
9289 cmdp = eap->cmd;
9290 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9291
9292 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9293 eap->arg, mode, isabbrev))
9294 {
9295 case 1: EMSG(_(e_invarg));
9296 break;
9297 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9298 break;
9299 }
9300}
9301
9302/*
9303 * ":winsize" command (obsolete).
9304 */
9305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009306ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 int w, h;
9309 char_u *arg = eap->arg;
9310 char_u *p;
9311
9312 w = getdigits(&arg);
9313 arg = skipwhite(arg);
9314 p = arg;
9315 h = getdigits(&arg);
9316 if (*p != NUL && *arg == NUL)
9317 set_shellsize(w, h, TRUE);
9318 else
9319 EMSG(_("E465: :winsize requires two number arguments"));
9320}
9321
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009323ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 int xchar = NUL;
9326 char_u *p;
9327
9328 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9329 {
9330 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9331 if (eap->arg[1] == NUL)
9332 {
9333 EMSG(_(e_invarg));
9334 return;
9335 }
9336 xchar = eap->arg[1];
9337 p = eap->arg + 2;
9338 }
9339 else
9340 p = eap->arg + 1;
9341
9342 eap->nextcmd = check_nextcmd(p);
9343 p = skipwhite(p);
9344 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9345 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009346 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
9348 /* Pass flags on for ":vertical wincmd ]". */
9349 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009350 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9352 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009353 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 }
9355}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356
Bram Moolenaar843ee412004-06-30 16:16:41 +00009357#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358/*
9359 * ":winpos".
9360 */
9361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009362ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 int x, y;
9365 char_u *arg = eap->arg;
9366 char_u *p;
9367
9368 if (*arg == NUL)
9369 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009370# if defined(FEAT_GUI) || defined(MSWIN)
9371# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009373# else
9374 if (mch_get_winpos(&x, &y) != FAIL)
9375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
9377 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9378 msg(IObuff);
9379 }
9380 else
9381# endif
9382 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9383 }
9384 else
9385 {
9386 x = getdigits(&arg);
9387 arg = skipwhite(arg);
9388 p = arg;
9389 y = getdigits(&arg);
9390 if (*p == NUL || *arg != NUL)
9391 {
9392 EMSG(_("E466: :winpos requires two number arguments"));
9393 return;
9394 }
9395# ifdef FEAT_GUI
9396 if (gui.in_use)
9397 gui_mch_set_winpos(x, y);
9398 else if (gui.starting)
9399 {
9400 /* Remember the coordinates for when the window is opened. */
9401 gui_win_x = x;
9402 gui_win_y = y;
9403 }
9404# ifdef HAVE_TGETENT
9405 else
9406# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009407# else
9408# ifdef MSWIN
9409 mch_set_winpos(x, y);
9410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411# endif
9412# ifdef HAVE_TGETENT
9413 if (*T_CWP)
9414 term_set_winpos(x, y);
9415# endif
9416 }
9417}
9418#endif
9419
9420/*
9421 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9422 */
9423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009424ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
9426 oparg_T oa;
9427
9428 clear_oparg(&oa);
9429 oa.regname = eap->regname;
9430 oa.start.lnum = eap->line1;
9431 oa.end.lnum = eap->line2;
9432 oa.line_count = eap->line2 - eap->line1 + 1;
9433 oa.motion_type = MLINE;
9434#ifdef FEAT_VIRTUALEDIT
9435 virtual_op = FALSE;
9436#endif
9437 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9438 {
9439 setpcmark();
9440 curwin->w_cursor.lnum = eap->line1;
9441 beginline(BL_SOL | BL_FIX);
9442 }
9443
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009444 if (VIsual_active)
9445 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009446
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 switch (eap->cmdidx)
9448 {
9449 case CMD_delete:
9450 oa.op_type = OP_DELETE;
9451 op_delete(&oa);
9452 break;
9453
9454 case CMD_yank:
9455 oa.op_type = OP_YANK;
9456 (void)op_yank(&oa, FALSE, TRUE);
9457 break;
9458
9459 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009460 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009462 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9463#else
9464 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009466 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 oa.op_type = OP_RSHIFT;
9468 else
9469 oa.op_type = OP_LSHIFT;
9470 op_shift(&oa, FALSE, eap->amount);
9471 break;
9472 }
9473#ifdef FEAT_VIRTUALEDIT
9474 virtual_op = MAYBE;
9475#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009476 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477}
9478
9479/*
9480 * ":put".
9481 */
9482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009483ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 /* ":0put" works like ":1put!". */
9486 if (eap->line2 == 0)
9487 {
9488 eap->line2 = 1;
9489 eap->forceit = TRUE;
9490 }
9491 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009492 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9493 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
9497 * Handle ":copy" and ":move".
9498 */
9499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 long n;
9503
Bram Moolenaarded27822017-01-02 14:27:34 +01009504 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 if (eap->arg == NULL) /* error detected */
9506 {
9507 eap->nextcmd = NULL;
9508 return;
9509 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009510 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
9512 /*
9513 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9514 */
9515 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9516 {
9517 EMSG(_(e_invaddr));
9518 return;
9519 }
9520
9521 if (eap->cmdidx == CMD_move)
9522 {
9523 if (do_move(eap->line1, eap->line2, n) == FAIL)
9524 return;
9525 }
9526 else
9527 ex_copy(eap->line1, eap->line2, n);
9528 u_clearline();
9529 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009530 ex_may_print(eap);
9531}
9532
9533/*
9534 * Print the current line if flags were given to the Ex command.
9535 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009537ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009538{
9539 if (eap->flags != 0)
9540 {
9541 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9542 (eap->flags & EXFLAG_LIST));
9543 ex_no_reprint = TRUE;
9544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545}
9546
9547/*
9548 * ":smagic" and ":snomagic".
9549 */
9550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009551ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 int magic_save = p_magic;
9554
9555 p_magic = (eap->cmdidx == CMD_smagic);
9556 do_sub(eap);
9557 p_magic = magic_save;
9558}
9559
9560/*
9561 * ":join".
9562 */
9563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009564ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566 curwin->w_cursor.lnum = eap->line1;
9567 if (eap->line1 == eap->line2)
9568 {
9569 if (eap->addr_count >= 2) /* :2,2join does nothing */
9570 return;
9571 if (eap->line2 == curbuf->b_ml.ml_line_count)
9572 {
9573 beep_flush();
9574 return;
9575 }
9576 ++eap->line2;
9577 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009578 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009580 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581}
9582
9583/*
9584 * ":[addr]@r" or ":[addr]*r": execute register
9585 */
9586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009587ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009590 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591
9592 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009593 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
9595#ifdef USE_ON_FLY_SCROLL
9596 dont_scroll = TRUE; /* disallow scrolling here */
9597#endif
9598
9599 /* get the register name. No name means to use the previous one */
9600 c = *eap->arg;
9601 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9602 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009603 /* Put the register in the typeahead buffer with the "silent" flag. */
9604 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9605 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 else
9610 {
9611 int save_efr = exec_from_reg;
9612
9613 exec_from_reg = TRUE;
9614
9615 /*
9616 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009617 * Continue until the stuff buffer is empty and all added characters
9618 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009620 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9622
9623 exec_from_reg = save_efr;
9624 }
9625}
9626
9627/*
9628 * ":!".
9629 */
9630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009631ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9634}
9635
9636/*
9637 * ":undo".
9638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009640ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009642 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009643 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009644 else
9645 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646}
9647
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009648#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009650ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009651{
9652 char_u hash[UNDO_HASH_SIZE];
9653
9654 u_compute_hash(hash);
9655 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9656}
9657
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009659ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009660{
9661 char_u hash[UNDO_HASH_SIZE];
9662
9663 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009664 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009665}
9666#endif
9667
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668/*
9669 * ":redo".
9670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009672ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
9674 u_redo(1);
9675}
9676
9677/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009678 * ":earlier" and ":later".
9679 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009681ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009682{
9683 long count = 0;
9684 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009685 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009686 char_u *p = eap->arg;
9687
9688 if (*p == NUL)
9689 count = 1;
9690 else if (isdigit(*p))
9691 {
9692 count = getdigits(&p);
9693 switch (*p)
9694 {
9695 case 's': ++p; sec = TRUE; break;
9696 case 'm': ++p; sec = TRUE; count *= 60; break;
9697 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009698 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9699 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009700 }
9701 }
9702
9703 if (*p != NUL)
9704 EMSG2(_(e_invarg2), eap->arg);
9705 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009706 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9707 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009708}
9709
9710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 * ":redir": start/stop redirection.
9712 */
9713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009714ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 char *mode;
9717 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009718 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
Bram Moolenaarba768492016-07-08 15:32:54 +02009720#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009721 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009722 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009723 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009724 return;
9725 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009726#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009727
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 if (STRICMP(eap->arg, "END") == 0)
9729 close_redir();
9730 else
9731 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009732 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009734 ++arg;
9735 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009737 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 mode = "a";
9739 }
9740 else
9741 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009742 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743
9744 close_redir();
9745
9746 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009747 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 if (fname == NULL)
9749 return;
9750#ifdef FEAT_BROWSE
9751 if (cmdmod.browse)
9752 {
9753 char_u *browseFile;
9754
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009755 browseFile = do_browse(BROWSE_SAVE,
9756 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009757 fname, NULL, NULL,
9758 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (browseFile == NULL)
9760 return; /* operation cancelled */
9761 vim_free(fname);
9762 fname = browseFile;
9763 eap->forceit = TRUE; /* since dialog already asked */
9764 }
9765#endif
9766
9767 redir_fd = open_exfile(fname, eap->forceit, mode);
9768 vim_free(fname);
9769 }
9770#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009771 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 {
9773 /* redirect to a register a-z (resp. A-Z for appending) */
9774 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009775 ++arg;
9776 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009778 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009779 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009781 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009783 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009784 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009785 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009786 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009788 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009789 if (*arg == '>')
9790 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009791 /* Make register empty when not using @A-@Z and the
9792 * command is valid. */
9793 if (*arg == NUL && !isupper(redir_reg))
9794 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009796 }
9797 if (*arg != NUL)
9798 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009799 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009800 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009801 }
9802 }
9803 else if (*arg == '=' && arg[1] == '>')
9804 {
9805 int append;
9806
9807 /* redirect to a variable */
9808 close_redir();
9809 arg += 2;
9810
9811 if (*arg == '>')
9812 {
9813 ++arg;
9814 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 }
9816 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009817 append = FALSE;
9818
9819 if (var_redir_start(skipwhite(arg), append) == OK)
9820 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 }
9822#endif
9823
9824 /* TODO: redirect to a buffer */
9825
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009827 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009829
9830 /* Make sure redirection is not off. Can happen for cmdline completion
9831 * that indirectly invokes a command to catch its output. */
9832 if (redir_fd != NULL
9833#ifdef FEAT_EVAL
9834 || redir_reg || redir_vname
9835#endif
9836 )
9837 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838}
9839
9840/*
9841 * ":redraw": force redraw
9842 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009844ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 int r = RedrawingDisabled;
9847 int p = p_lz;
9848
9849 RedrawingDisabled = 0;
9850 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009851 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009853 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854#ifdef FEAT_TITLE
9855 if (need_maketitle)
9856 maketitle();
9857#endif
9858 RedrawingDisabled = r;
9859 p_lz = p;
9860
9861 /* Reset msg_didout, so that a message that's there is overwritten. */
9862 msg_didout = FALSE;
9863 msg_col = 0;
9864
Bram Moolenaar56667a52013-07-17 11:54:28 +02009865 /* No need to wait after an intentional redraw. */
9866 need_wait_return = FALSE;
9867
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 out_flush();
9869}
9870
9871/*
9872 * ":redrawstatus": force redraw of status line(s)
9873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009875ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 int r = RedrawingDisabled;
9878 int p = p_lz;
9879
9880 RedrawingDisabled = 0;
9881 p_lz = FALSE;
9882 if (eap->forceit)
9883 status_redraw_all();
9884 else
9885 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009886 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 RedrawingDisabled = r;
9888 p_lz = p;
9889 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009893close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895 if (redir_fd != NULL)
9896 {
9897 fclose(redir_fd);
9898 redir_fd = NULL;
9899 }
9900#ifdef FEAT_EVAL
9901 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009902 if (redir_vname)
9903 {
9904 var_redir_stop();
9905 redir_vname = 0;
9906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907#endif
9908}
9909
9910#if defined(FEAT_SESSION) && defined(USE_CRNL)
9911# define MKSESSION_NL
9912static int mksession_nl = FALSE; /* use NL only in put_eol() */
9913#endif
9914
9915/*
9916 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9917 */
9918 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009919ex_mkrc(
9920 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921{
9922 FILE *fd;
9923 int failed = FALSE;
9924 char_u *fname;
9925#ifdef FEAT_BROWSE
9926 char_u *browseFile = NULL;
9927#endif
9928#ifdef FEAT_SESSION
9929 int view_session = FALSE;
9930 int using_vdir = FALSE; /* using 'viewdir'? */
9931 char_u *viewFile = NULL;
9932 unsigned *flagp;
9933#endif
9934
9935 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9936 {
9937#ifdef FEAT_SESSION
9938 view_session = TRUE;
9939#else
9940 ex_ni(eap);
9941 return;
9942#endif
9943 }
9944
9945#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009946 /* Use the short file name until ":lcd" is used. We also don't use the
9947 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009948 did_lcd = FALSE;
9949
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9951 if (eap->cmdidx == CMD_mkview
9952 && (*eap->arg == NUL
9953 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9954 {
9955 eap->forceit = TRUE;
9956 fname = get_view_file(*eap->arg);
9957 if (fname == NULL)
9958 return;
9959 viewFile = fname;
9960 using_vdir = TRUE;
9961 }
9962 else
9963#endif
9964 if (*eap->arg != NUL)
9965 fname = eap->arg;
9966 else if (eap->cmdidx == CMD_mkvimrc)
9967 fname = (char_u *)VIMRC_FILE;
9968#ifdef FEAT_SESSION
9969 else if (eap->cmdidx == CMD_mksession)
9970 fname = (char_u *)SESSION_FILE;
9971#endif
9972 else
9973 fname = (char_u *)EXRC_FILE;
9974
9975#ifdef FEAT_BROWSE
9976 if (cmdmod.browse)
9977 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009978 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979# ifdef FEAT_SESSION
9980 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9981 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9982# endif
9983 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009984 fname, (char_u *)"vim", NULL,
9985 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 if (browseFile == NULL)
9987 goto theend;
9988 fname = browseFile;
9989 eap->forceit = TRUE; /* since dialog already asked */
9990 }
9991#endif
9992
9993#if defined(FEAT_SESSION) && defined(vim_mkdir)
9994 /* When using 'viewdir' may have to create the directory. */
9995 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009996 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997#endif
9998
9999 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10000 if (fd != NULL)
10001 {
10002#ifdef FEAT_SESSION
10003 if (eap->cmdidx == CMD_mkview)
10004 flagp = &vop_flags;
10005 else
10006 flagp = &ssop_flags;
10007#endif
10008
10009#ifdef MKSESSION_NL
10010 /* "unix" in 'sessionoptions': use NL line separator */
10011 if (view_session && (*flagp & SSOP_UNIX))
10012 mksession_nl = TRUE;
10013#endif
10014
10015 /* Write the version command for :mkvimrc */
10016 if (eap->cmdidx == CMD_mkvimrc)
10017 (void)put_line(fd, "version 6.0");
10018
10019#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010020 if (eap->cmdidx == CMD_mksession)
10021 {
10022 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10023 failed = TRUE;
10024 }
10025
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 if (eap->cmdidx != CMD_mkview)
10027#endif
10028 {
10029 /* Write setting 'compatible' first, because it has side effects.
10030 * For that same reason only do it when needed. */
10031 if (p_cp)
10032 (void)put_line(fd, "if !&cp | set cp | endif");
10033 else
10034 (void)put_line(fd, "if &cp | set nocp | endif");
10035 }
10036
10037#ifdef FEAT_SESSION
10038 if (!view_session
10039 || (eap->cmdidx == CMD_mksession
10040 && (*flagp & SSOP_OPTIONS)))
10041#endif
10042 failed |= (makemap(fd, NULL) == FAIL
10043 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10044
10045#ifdef FEAT_SESSION
10046 if (!failed && view_session)
10047 {
10048 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10049 failed = TRUE;
10050 if (eap->cmdidx == CMD_mksession)
10051 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010052 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
Bram Moolenaard9462e32011-04-11 21:35:11 +020010054 dirnow = alloc(MAXPATHL);
10055 if (dirnow == NULL)
10056 failed = TRUE;
10057 else
10058 {
10059 /*
10060 * Change to session file's dir.
10061 */
10062 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010064 *dirnow = NUL;
10065 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10066 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010067 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010068 shorten_fnames(TRUE);
10069 }
10070 else if (*dirnow != NUL
10071 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10072 {
10073 if (mch_chdir((char *)globaldir) == 0)
10074 shorten_fnames(TRUE);
10075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
Bram Moolenaard9462e32011-04-11 21:35:11 +020010077 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078
Bram Moolenaard9462e32011-04-11 21:35:11 +020010079 /* restore original dir */
10080 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010082 {
10083 if (mch_chdir((char *)dirnow) != 0)
10084 EMSG(_(e_prev_dir));
10085 shorten_fnames(TRUE);
10086 }
10087 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 }
10089 }
10090 else
10091 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010092 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10093 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 }
10095 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10096 == FAIL)
10097 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010098 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10099 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010100 if (eap->cmdidx == CMD_mksession)
10101 {
10102 if (put_line(fd, "unlet SessionLoad") == FAIL)
10103 failed = TRUE;
10104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 }
10106#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010107 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10108 failed = TRUE;
10109
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 failed |= fclose(fd);
10111
10112 if (failed)
10113 EMSG(_(e_write));
10114#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10115 else if (eap->cmdidx == CMD_mksession)
10116 {
10117 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010118 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaard9462e32011-04-11 21:35:11 +020010120 tbuf = alloc(MAXPATHL);
10121 if (tbuf != NULL)
10122 {
10123 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10124 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10125 vim_free(tbuf);
10126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 }
10128#endif
10129#ifdef MKSESSION_NL
10130 mksession_nl = FALSE;
10131#endif
10132 }
10133
10134#ifdef FEAT_BROWSE
10135theend:
10136 vim_free(browseFile);
10137#endif
10138#ifdef FEAT_SESSION
10139 vim_free(viewFile);
10140#endif
10141}
10142
Bram Moolenaardf177f62005-02-22 08:39:57 +000010143#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10144 || defined(PROTO)
10145 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010146vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010147{
10148 if (vim_mkdir(name, prot) != 0)
10149 {
10150 EMSG2(_("E739: Cannot create directory: %s"), name);
10151 return FAIL;
10152 }
10153 return OK;
10154}
10155#endif
10156
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157/*
10158 * Open a file for writing for an Ex command, with some checks.
10159 * Return file descriptor, or NULL on failure.
10160 */
10161 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010162open_exfile(
10163 char_u *fname,
10164 int forceit,
10165 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166{
10167 FILE *fd;
10168
10169#ifdef UNIX
10170 /* with Unix it is possible to open a directory */
10171 if (mch_isdir(fname))
10172 {
10173 EMSG2(_(e_isadir2), fname);
10174 return NULL;
10175 }
10176#endif
10177 if (!forceit && *mode != 'a' && vim_fexists(fname))
10178 {
10179 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10180 return NULL;
10181 }
10182
10183 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10184 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10185
10186 return fd;
10187}
10188
10189/*
10190 * ":mark" and ":k".
10191 */
10192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010193ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194{
10195 pos_T pos;
10196
10197 if (*eap->arg == NUL) /* No argument? */
10198 EMSG(_(e_argreq));
10199 else if (eap->arg[1] != NUL) /* more than one character? */
10200 EMSG(_(e_trailing));
10201 else
10202 {
10203 pos = curwin->w_cursor; /* save curwin->w_cursor */
10204 curwin->w_cursor.lnum = eap->line2;
10205 beginline(BL_WHITE | BL_FIX);
10206 if (setmark(*eap->arg) == FAIL) /* set mark */
10207 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10208 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10209 }
10210}
10211
10212/*
10213 * Update w_topline, w_leftcol and the cursor position.
10214 */
10215 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010216update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217{
10218 check_cursor(); /* put cursor on valid line */
10219 update_topline();
10220 if (!curwin->w_p_wrap)
10221 validate_cursor();
10222 update_curswant();
10223}
10224
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010226 * Save the current State and go to Normal mode.
10227 * Return TRUE if the typeahead could be saved.
10228 */
10229 int
10230save_current_state(save_state_T *sst)
10231{
10232 sst->save_msg_scroll = msg_scroll;
10233 sst->save_restart_edit = restart_edit;
10234 sst->save_msg_didout = msg_didout;
10235 sst->save_State = State;
10236 sst->save_insertmode = p_im;
10237 sst->save_finish_op = finish_op;
10238 sst->save_opcount = opcount;
10239
10240 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10241 restart_edit = 0; /* don't go to Insert mode */
10242 p_im = FALSE; /* don't use 'insertmode' */
10243
10244 /*
10245 * Save the current typeahead. This is required to allow using ":normal"
10246 * from an event handler and makes sure we don't hang when the argument
10247 * ends with half a command.
10248 */
10249 save_typeahead(&sst->tabuf);
10250 return sst->tabuf.typebuf_valid;
10251}
10252
10253 void
10254restore_current_state(save_state_T *sst)
10255{
10256 /* Restore the previous typeahead. */
10257 restore_typeahead(&sst->tabuf);
10258
10259 msg_scroll = sst->save_msg_scroll;
10260 restart_edit = sst->save_restart_edit;
10261 p_im = sst->save_insertmode;
10262 finish_op = sst->save_finish_op;
10263 opcount = sst->save_opcount;
10264 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10265
10266 /* Restore the state (needed when called from a function executed for
10267 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10268 State = sst->save_State;
10269#ifdef CURSOR_SHAPE
10270 ui_cursor_shape(); /* may show different cursor shape */
10271#endif
10272}
10273
10274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 * ":normal[!] {commands}": Execute normal mode commands.
10276 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010277 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010278ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010280 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#ifdef FEAT_MBYTE
10282 char_u *arg = NULL;
10283 int l;
10284 char_u *p;
10285#endif
10286
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010287 if (ex_normal_lock > 0)
10288 {
10289 EMSG(_(e_secure));
10290 return;
10291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 if (ex_normal_busy >= p_mmd)
10293 {
10294 EMSG(_("E192: Recursive use of :normal too deep"));
10295 return;
10296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297
10298#ifdef FEAT_MBYTE
10299 /*
10300 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10301 * this for the K_SPECIAL leading byte, otherwise special keys will not
10302 * work.
10303 */
10304 if (has_mbyte)
10305 {
10306 int len = 0;
10307
10308 /* Count the number of characters to be escaped. */
10309 for (p = eap->arg; *p != NUL; ++p)
10310 {
10311# ifdef FEAT_GUI
10312 if (*p == CSI) /* leadbyte CSI */
10313 len += 2;
10314# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010315 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10317# ifdef FEAT_GUI
10318 || *p == CSI
10319# endif
10320 )
10321 len += 2;
10322 }
10323 if (len > 0)
10324 {
10325 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10326 if (arg != NULL)
10327 {
10328 len = 0;
10329 for (p = eap->arg; *p != NUL; ++p)
10330 {
10331 arg[len++] = *p;
10332# ifdef FEAT_GUI
10333 if (*p == CSI)
10334 {
10335 arg[len++] = KS_EXTRA;
10336 arg[len++] = (int)KE_CSI;
10337 }
10338# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010339 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 {
10341 arg[len++] = *++p;
10342 if (*p == K_SPECIAL)
10343 {
10344 arg[len++] = KS_SPECIAL;
10345 arg[len++] = KE_FILLER;
10346 }
10347# ifdef FEAT_GUI
10348 else if (*p == CSI)
10349 {
10350 arg[len++] = KS_EXTRA;
10351 arg[len++] = (int)KE_CSI;
10352 }
10353# endif
10354 }
10355 arg[len] = NUL;
10356 }
10357 }
10358 }
10359 }
10360#endif
10361
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010362 ++ex_normal_busy;
10363 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 {
10365 /*
10366 * Repeat the :normal command for each line in the range. When no
10367 * range given, execute it just once, without positioning the cursor
10368 * first.
10369 */
10370 do
10371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 if (eap->addr_count != 0)
10373 {
10374 curwin->w_cursor.lnum = eap->line1++;
10375 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010376 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 }
10378
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010379 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380#ifdef FEAT_MBYTE
10381 arg != NULL ? arg :
10382#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010383 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 }
10385 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10386 }
10387
10388 /* Might not return to the main loop when in an event handler. */
10389 update_topline_cursor();
10390
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010391 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010393#ifdef FEAT_MOUSE
10394 setmouse();
10395#endif
10396#ifdef CURSOR_SHAPE
10397 ui_cursor_shape(); /* may show different cursor shape */
10398#endif
10399
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400#ifdef FEAT_MBYTE
10401 vim_free(arg);
10402#endif
10403}
10404
10405/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010406 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 */
10408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010409ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010411 if (eap->forceit)
10412 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010413 /* cursor line can be zero on startup */
10414 if (!curwin->w_cursor.lnum)
10415 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010416 coladvance((colnr_T)MAXCOL);
10417 curwin->w_curswant = MAXCOL;
10418 curwin->w_set_curswant = FALSE;
10419 }
10420
Bram Moolenaara40c5002005-01-09 21:16:21 +000010421 /* Ignore the command when already in Insert mode. Inserting an
10422 * expression register that invokes a function can do this. */
10423 if (State & INSERT)
10424 return;
10425
Bram Moolenaar64969662005-12-14 21:59:55 +000010426 if (eap->cmdidx == CMD_startinsert)
10427 restart_edit = 'a';
10428 else if (eap->cmdidx == CMD_startreplace)
10429 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010431 restart_edit = 'V';
10432
10433 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010435 if (eap->cmdidx == CMD_startinsert)
10436 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 curwin->w_curswant = 0; /* avoid MAXCOL */
10438 }
10439}
10440
10441/*
10442 * ":stopinsert"
10443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010445ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446{
10447 restart_edit = 0;
10448 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010449 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010452/*
10453 * Execute normal mode command "cmd".
10454 * "remap" can be REMAP_NONE or REMAP_YES.
10455 */
10456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010457exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010458{
Bram Moolenaar25281632016-01-21 23:32:32 +010010459 /* Stuff the argument into the typeahead buffer. */
10460 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010461 exec_normal(FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010462}
Bram Moolenaar25281632016-01-21 23:32:32 +010010463
Bram Moolenaar25281632016-01-21 23:32:32 +010010464/*
10465 * Execute normal_cmd() until there is no typeahead left.
10466 */
10467 void
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010468exec_normal(int was_typed, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010469{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010470 oparg_T oa;
10471
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010472 clear_oparg(&oa);
10473 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010474 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10475 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010476 {
10477 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010478#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010479 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010480 && oa.op_type == OP_NOP && oa.regname == NUL
10481 && !VIsual_active)
10482 {
10483 /* If terminal_loop() returns OK we got a key that is handled
10484 * in Normal model. With FAIL we first need to position the
10485 * cursor and the screen needs to be redrawn. */
10486 if (terminal_loop(TRUE) == OK)
10487 normal_cmd(&oa, TRUE);
10488 }
10489 else
10490#endif
10491 /* execute a Normal mode cmd */
10492 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010493 }
10494}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010495
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496#ifdef FEAT_FIND_ID
10497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010498ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499{
10500 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10501 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10502 (linenr_T)1, (linenr_T)MAXLNUM);
10503}
10504
Bram Moolenaar4033c552017-09-16 20:54:51 +020010505#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506/*
10507 * ":psearch"
10508 */
10509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010510ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512 g_do_tagpreview = p_pvh;
10513 ex_findpat(eap);
10514 g_do_tagpreview = 0;
10515}
10516#endif
10517
10518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010519ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521 int whole = TRUE;
10522 long n;
10523 char_u *p;
10524 int action;
10525
10526 switch (cmdnames[eap->cmdidx].cmd_name[2])
10527 {
10528 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10529 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10530 action = ACTION_GOTO;
10531 else
10532 action = ACTION_SHOW;
10533 break;
10534 case 'i': /* ":ilist" and ":dlist" */
10535 action = ACTION_SHOW_ALL;
10536 break;
10537 case 'u': /* ":ijump" and ":djump" */
10538 action = ACTION_GOTO;
10539 break;
10540 default: /* ":isplit" and ":dsplit" */
10541 action = ACTION_SPLIT;
10542 break;
10543 }
10544
10545 n = 1;
10546 if (vim_isdigit(*eap->arg)) /* get count */
10547 {
10548 n = getdigits(&eap->arg);
10549 eap->arg = skipwhite(eap->arg);
10550 }
10551 if (*eap->arg == '/') /* Match regexp, not just whole words */
10552 {
10553 whole = FALSE;
10554 ++eap->arg;
10555 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10556 if (*p)
10557 {
10558 *p++ = NUL;
10559 p = skipwhite(p);
10560
10561 /* Check for trailing illegal characters */
10562 if (!ends_excmd(*p))
10563 eap->errmsg = e_trailing;
10564 else
10565 eap->nextcmd = check_nextcmd(p);
10566 }
10567 }
10568 if (!eap->skip)
10569 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10570 whole, !eap->forceit,
10571 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10572 n, action, eap->line1, eap->line2);
10573}
10574#endif
10575
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576
Bram Moolenaar4033c552017-09-16 20:54:51 +020010577#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578/*
10579 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10580 */
10581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010582ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010584 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10586}
10587
10588/*
10589 * ":pedit"
10590 */
10591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010592ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593{
10594 win_T *curwin_save = curwin;
10595
10596 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010597 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010598 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 do_exedit(eap, NULL);
10600 keep_help_flag = FALSE;
10601 if (curwin != curwin_save && win_valid(curwin_save))
10602 {
10603 /* Return cursor to where we were */
10604 validate_cursor();
10605 redraw_later(VALID);
10606 win_enter(curwin_save, TRUE);
10607 }
10608 g_do_tagpreview = 0;
10609}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611
10612/*
10613 * ":stag", ":stselect" and ":stjump".
10614 */
10615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010616ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617{
10618 postponed_split = -1;
10619 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010620 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10622 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010623 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625
10626/*
10627 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10628 */
10629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010630ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631{
10632 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10633}
10634
10635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010636ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 int cmd;
10639
10640 switch (name[1])
10641 {
10642 case 'j': cmd = DT_JUMP; /* ":tjump" */
10643 break;
10644 case 's': cmd = DT_SELECT; /* ":tselect" */
10645 break;
10646 case 'p': cmd = DT_PREV; /* ":tprevious" */
10647 break;
10648 case 'N': cmd = DT_PREV; /* ":tNext" */
10649 break;
10650 case 'n': cmd = DT_NEXT; /* ":tnext" */
10651 break;
10652 case 'o': cmd = DT_POP; /* ":pop" */
10653 break;
10654 case 'f': /* ":tfirst" */
10655 case 'r': cmd = DT_FIRST; /* ":trewind" */
10656 break;
10657 case 'l': cmd = DT_LAST; /* ":tlast" */
10658 break;
10659 default: /* ":tag" */
10660#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010661 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010663 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 return;
10665 }
10666#endif
10667 cmd = DT_TAG;
10668 break;
10669 }
10670
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010671 if (name[0] == 'l')
10672 {
10673#ifndef FEAT_QUICKFIX
10674 ex_ni(eap);
10675 return;
10676#else
10677 cmd = DT_LTAG;
10678#endif
10679 }
10680
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10682 eap->forceit, TRUE);
10683}
10684
10685/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010686 * Check "str" for starting with a special cmdline variable.
10687 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10688 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10689 */
10690 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010691find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010692{
10693 int len;
10694 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010695 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010696 "%",
10697#define SPEC_PERC 0
10698 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010699#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010700 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010701#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010702 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010703#define SPEC_CCWORD (SPEC_CWORD + 1)
10704 "<cexpr>", /* expr under cursor */
10705#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010706 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010707#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010708 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010709#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010710 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010711#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010712 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010713#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010714 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010715#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010716 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010717#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010718#ifdef FEAT_CLIENTSERVER
10719 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010720# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010721#endif
10722 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010723
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010724 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010725 {
10726 len = (int)STRLEN(spec_str[i]);
10727 if (STRNCMP(src, spec_str[i], len) == 0)
10728 {
10729 *usedlen = len;
10730 return i;
10731 }
10732 }
10733 return -1;
10734}
10735
10736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 * Evaluate cmdline variables.
10738 *
10739 * change '%' to curbuf->b_ffname
10740 * '#' to curwin->w_altfile
10741 * '<cword>' to word under the cursor
10742 * '<cWORD>' to WORD under the cursor
10743 * '<cfile>' to path name under the cursor
10744 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010745 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 * '<afile>' to file name for autocommand
10747 * '<abuf>' to buffer number for autocommand
10748 * '<amatch>' to matching name for autocommand
10749 *
10750 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10751 * "" for error without a message) and NULL is returned.
10752 * Returns an allocated string if a valid match was found.
10753 * Returns NULL if no match was found. "usedlen" then still contains the
10754 * number of characters to skip.
10755 */
10756 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010757eval_vars(
10758 char_u *src, /* pointer into commandline */
10759 char_u *srcstart, /* beginning of valid memory for src */
10760 int *usedlen, /* characters after src that are used */
10761 linenr_T *lnump, /* line number for :e command, or NULL */
10762 char_u **errormsg, /* pointer to error message */
10763 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010764 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765{
10766 int i;
10767 char_u *s;
10768 char_u *result;
10769 char_u *resultbuf = NULL;
10770 int resultlen;
10771 buf_T *buf;
10772 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10773 int spec_idx;
10774#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010775 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 int skip_mod = FALSE;
10777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779
10780 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010781 if (escaped != NULL)
10782 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
10784 /*
10785 * Check if there is something to do.
10786 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010787 spec_idx = find_cmdline_var(src, usedlen);
10788 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 {
10790 *usedlen = 1;
10791 return NULL;
10792 }
10793
10794 /*
10795 * Skip when preceded with a backslash "\%" and "\#".
10796 * Note: In "\\%" the % is also not recognized!
10797 */
10798 if (src > srcstart && src[-1] == '\\')
10799 {
10800 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010801 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 return NULL;
10803 }
10804
10805 /*
10806 * word or WORD under cursor
10807 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010808 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10809 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010811 resultlen = find_ident_under_cursor(&result,
10812 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10813 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10814 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 if (resultlen == 0)
10816 {
10817 *errormsg = (char_u *)"";
10818 return NULL;
10819 }
10820 }
10821
10822 /*
10823 * '#': Alternate file name
10824 * '%': Current file name
10825 * File name under the cursor
10826 * File name for autocommand
10827 * and following modifiers
10828 */
10829 else
10830 {
10831 switch (spec_idx)
10832 {
10833 case SPEC_PERC: /* '%': current file */
10834 if (curbuf->b_fname == NULL)
10835 {
10836 result = (char_u *)"";
10837 valid = 0; /* Must have ":p:h" to be valid */
10838 }
10839 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010842#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010843 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010844#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 break;
10847
10848 case SPEC_HASH: /* '#' or "#99": alternate file */
10849 if (src[1] == '#') /* "##": the argument list */
10850 {
10851 result = arg_all();
10852 resultbuf = result;
10853 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010854 if (escaped != NULL)
10855 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856#ifdef FEAT_MODIFY_FNAME
10857 skip_mod = TRUE;
10858#endif
10859 break;
10860 }
10861 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010862 if (*s == '<') /* "#<99" uses v:oldfiles */
10863 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010865 if (s == src + 2 && src[1] == '-')
10866 /* just a minus sign, don't skip over it */
10867 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 *usedlen = (int)(s - src); /* length of what we expand */
10869
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010870 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010872 if (*usedlen < 2)
10873 {
10874 /* Should we give an error message for #<text? */
10875 *usedlen = 1;
10876 return NULL;
10877 }
10878#ifdef FEAT_EVAL
10879 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10880 (long)i);
10881 if (result == NULL)
10882 {
10883 *errormsg = (char_u *)"";
10884 return NULL;
10885 }
10886#else
10887 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 }
10891 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010892 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010893 if (i == 0 && src[1] == '<' && *usedlen > 1)
10894 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010895 buf = buflist_findnr(i);
10896 if (buf == NULL)
10897 {
10898 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10899 return NULL;
10900 }
10901 if (lnump != NULL)
10902 *lnump = ECMD_LAST;
10903 if (buf->b_fname == NULL)
10904 {
10905 result = (char_u *)"";
10906 valid = 0; /* Must have ":p:h" to be valid */
10907 }
10908 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010909 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010910 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010911#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010912 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010913#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010914 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 break;
10917
10918#ifdef FEAT_SEARCHPATH
10919 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010920 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 if (result == NULL)
10922 {
10923 *errormsg = (char_u *)"";
10924 return NULL;
10925 }
10926 resultbuf = result; /* remember allocated string */
10927 break;
10928#endif
10929
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 case SPEC_AFILE: /* file name for autocommand */
10931 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010932 if (result != NULL && !autocmd_fname_full)
10933 {
10934 /* Still need to turn the fname into a full path. It is
10935 * postponed to avoid a delay when <afile> is not used. */
10936 autocmd_fname_full = TRUE;
10937 result = FullName_save(autocmd_fname, FALSE);
10938 vim_free(autocmd_fname);
10939 autocmd_fname = result;
10940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 if (result == NULL)
10942 {
10943 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10944 return NULL;
10945 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010946 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 break;
10948
10949 case SPEC_ABUF: /* buffer number for autocommand */
10950 if (autocmd_bufnr <= 0)
10951 {
10952 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10953 return NULL;
10954 }
10955 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10956 result = strbuf;
10957 break;
10958
10959 case SPEC_AMATCH: /* match name for autocommand */
10960 result = autocmd_match;
10961 if (result == NULL)
10962 {
10963 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10964 return NULL;
10965 }
10966 break;
10967
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 case SPEC_SFILE: /* file name for ":so" command */
10969 result = sourcing_name;
10970 if (result == NULL)
10971 {
10972 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10973 return NULL;
10974 }
10975 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010976 case SPEC_SLNUM: /* line in file for ":so" command */
10977 if (sourcing_name == NULL || sourcing_lnum == 0)
10978 {
10979 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10980 return NULL;
10981 }
10982 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10983 result = strbuf;
10984 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985#if defined(FEAT_CLIENTSERVER)
10986 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010987 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10988 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 result = strbuf;
10990 break;
10991#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010992 default:
10993 result = (char_u *)""; /* avoid gcc warning */
10994 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 }
10996
10997 resultlen = (int)STRLEN(result); /* length of new string */
10998 if (src[*usedlen] == '<') /* remove the file name extension */
10999 {
11000 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 resultlen = (int)(s - result);
11003 }
11004#ifdef FEAT_MODIFY_FNAME
11005 else if (!skip_mod)
11006 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011007 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 &resultlen);
11009 if (result == NULL)
11010 {
11011 *errormsg = (char_u *)"";
11012 return NULL;
11013 }
11014 }
11015#endif
11016 }
11017
11018 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11019 {
11020 if (valid != VALID_HEAD + VALID_PATH)
11021 /* xgettext:no-c-format */
11022 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
11023 else
11024 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
11025 result = NULL;
11026 }
11027 else
11028 result = vim_strnsave(result, resultlen);
11029 vim_free(resultbuf);
11030 return result;
11031}
11032
11033/*
11034 * Concatenate all files in the argument list, separated by spaces, and return
11035 * it in one allocated string.
11036 * Spaces and backslashes in the file names are escaped with a backslash.
11037 * Returns NULL when out of memory.
11038 */
11039 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011040arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041{
11042 int len;
11043 int idx;
11044 char_u *retval = NULL;
11045 char_u *p;
11046
11047 /*
11048 * Do this loop two times:
11049 * first time: compute the total length
11050 * second time: concatenate the names
11051 */
11052 for (;;)
11053 {
11054 len = 0;
11055 for (idx = 0; idx < ARGCOUNT; ++idx)
11056 {
11057 p = alist_name(&ARGLIST[idx]);
11058 if (p != NULL)
11059 {
11060 if (len > 0)
11061 {
11062 /* insert a space in between names */
11063 if (retval != NULL)
11064 retval[len] = ' ';
11065 ++len;
11066 }
11067 for ( ; *p != NUL; ++p)
11068 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011069 if (*p == ' '
11070#ifndef BACKSLASH_IN_FILENAME
11071 || *p == '\\'
11072#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011073 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 {
11075 /* insert a backslash */
11076 if (retval != NULL)
11077 retval[len] = '\\';
11078 ++len;
11079 }
11080 if (retval != NULL)
11081 retval[len] = *p;
11082 ++len;
11083 }
11084 }
11085 }
11086
11087 /* second time: break here */
11088 if (retval != NULL)
11089 {
11090 retval[len] = NUL;
11091 break;
11092 }
11093
11094 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011095 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 if (retval == NULL)
11097 break;
11098 }
11099
11100 return retval;
11101}
11102
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103/*
11104 * Expand the <sfile> string in "arg".
11105 *
11106 * Returns an allocated string, or NULL for any error.
11107 */
11108 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011109expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110{
11111 char_u *errormsg;
11112 int len;
11113 char_u *result;
11114 char_u *newres;
11115 char_u *repl;
11116 int srclen;
11117 char_u *p;
11118
11119 result = vim_strsave(arg);
11120 if (result == NULL)
11121 return NULL;
11122
11123 for (p = result; *p; )
11124 {
11125 if (STRNCMP(p, "<sfile>", 7) != 0)
11126 ++p;
11127 else
11128 {
11129 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011130 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 if (errormsg != NULL)
11132 {
11133 if (*errormsg)
11134 emsg(errormsg);
11135 vim_free(result);
11136 return NULL;
11137 }
11138 if (repl == NULL) /* no match (cannot happen) */
11139 {
11140 p += srclen;
11141 continue;
11142 }
11143 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11144 newres = alloc(len);
11145 if (newres == NULL)
11146 {
11147 vim_free(repl);
11148 vim_free(result);
11149 return NULL;
11150 }
11151 mch_memmove(newres, result, (size_t)(p - result));
11152 STRCPY(newres + (p - result), repl);
11153 len = (int)STRLEN(newres);
11154 STRCAT(newres, p + srclen);
11155 vim_free(repl);
11156 vim_free(result);
11157 result = newres;
11158 p = newres + len; /* continue after the match */
11159 }
11160 }
11161
11162 return result;
11163}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164
11165#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011166static int ses_winsizes(FILE *fd, int restore_size,
11167 win_T *tab_firstwin);
11168static int ses_win_rec(FILE *fd, frame_T *fr);
11169static frame_T *ses_skipframe(frame_T *fr);
11170static int ses_do_frame(frame_T *fr);
11171static int ses_do_win(win_T *wp);
11172static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11173static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011174static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175
11176/*
11177 * Write openfile commands for the current buffers to an .exrc file.
11178 * Return FAIL on error, OK otherwise.
11179 */
11180 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011181makeopens(
11182 FILE *fd,
11183 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
11185 buf_T *buf;
11186 int only_save_windows = TRUE;
11187 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 int restore_size = TRUE;
11189 win_T *wp;
11190 char_u *sname;
11191 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011192 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011193 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011194 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011195 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011196 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011197 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198
11199 if (ssop_flags & SSOP_BUFFERS)
11200 only_save_windows = FALSE; /* Save ALL buffers */
11201
11202 /*
11203 * Begin by setting the this_session variable, and then other
11204 * sessionable variables.
11205 */
11206#ifdef FEAT_EVAL
11207 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11208 return FAIL;
11209 if (ssop_flags & SSOP_GLOBALS)
11210 if (store_session_globals(fd) == FAIL)
11211 return FAIL;
11212#endif
11213
11214 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011215 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 */
11217 if (put_line(fd, "silent only") == FAIL)
11218 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011219 if ((ssop_flags & SSOP_TABPAGES)
11220 && put_line(fd, "silent tabonly") == FAIL)
11221 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222
11223 /*
11224 * Now a :cd command to the session directory or the current directory
11225 */
11226 if (ssop_flags & SSOP_SESDIR)
11227 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011228 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11229 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 return FAIL;
11231 }
11232 else if (ssop_flags & SSOP_CURDIR)
11233 {
11234 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11235 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011236 || fputs("cd ", fd) < 0
11237 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11238 || put_eol(fd) == FAIL)
11239 {
11240 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 vim_free(sname);
11244 }
11245
11246 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011247 * If there is an empty, unnamed buffer we will wipe it out later.
11248 * Remember the buffer number.
11249 */
11250 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11251 return FAIL;
11252 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11253 return FAIL;
11254 if (put_line(fd, "endif") == FAIL)
11255 return FAIL;
11256
11257 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 * Now save the current files, current buffer first.
11259 */
11260 if (put_line(fd, "set shortmess=aoO") == FAIL)
11261 return FAIL;
11262
11263 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011264 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 {
11266 if (!(only_save_windows && buf->b_nwindows == 0)
11267 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011268#ifdef FEAT_TERMINAL
11269 /* skip terminal buffers: finished ones are not useful, others
11270 * will be resurrected and result in a new buffer */
11271 && !bt_terminal(buf)
11272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 && buf->b_fname != NULL
11274 && buf->b_p_bl)
11275 {
11276 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11277 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011278 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 return FAIL;
11280 }
11281 }
11282
11283 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011284 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11286 return FAIL;
11287
11288 if (ssop_flags & SSOP_RESIZE)
11289 {
11290 /* Note: after the restore we still check it worked!*/
11291 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11292 || put_eol(fd) == FAIL)
11293 return FAIL;
11294 }
11295
11296#ifdef FEAT_GUI
11297 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11298 {
11299 int x, y;
11300
11301 if (gui_mch_get_winpos(&x, &y) == OK)
11302 {
11303 /* Note: after the restore we still check it worked!*/
11304 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11305 return FAIL;
11306 }
11307 }
11308#endif
11309
11310 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011311 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11312 * will be displayed when creating the next tab. That resizes the windows
11313 * in the first tab, which may cause problems. Set 'showtabline' to 2
11314 * temporarily to avoid that.
11315 */
11316 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11317 {
11318 if (put_line(fd, "set stal=2") == FAIL)
11319 return FAIL;
11320 restore_stal = TRUE;
11321 }
11322
11323 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011324 * May repeat putting Windows for each tab, when "tabpages" is in
11325 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011326 * Don't use goto_tabpage(), it may change directory and trigger
11327 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011329 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011330 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011331 if ((ssop_flags & SSOP_TABPAGES))
11332 {
11333 int num_tabs;
11334
11335 /*
11336 * Similar to ses_win_rec() below, populate the tab pages first so
11337 * later local options won't be copied to the new tabs.
11338 */
11339 for (tabnr = 1; ; ++tabnr)
11340 {
11341 tabpage_T *tp = find_tabpage(tabnr);
11342
11343 if (tp == NULL) /* done all tab pages */
11344 break;
11345
11346 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11347 return FAIL;
11348 }
11349
11350 num_tabs = tabnr - 1;
11351 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11352 || put_eol(fd) == FAIL))
11353 return FAIL;
11354 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011355 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011357 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011358 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011359
Bram Moolenaar18144c82006-04-12 21:52:12 +000011360 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011362 tabpage_T *tp = find_tabpage(tabnr);
11363
11364 if (tp == NULL)
11365 break; /* done all tab pages */
11366 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011367 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011368 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011369 tab_topframe = topframe;
11370 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011371 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011372 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011373 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011374 tab_topframe = tp->tp_topframe;
11375 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011376 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011377 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379
Bram Moolenaar18144c82006-04-12 21:52:12 +000011380 /*
11381 * Before creating the window layout, try loading one file. If this
11382 * is aborted we don't end up with a number of useless windows.
11383 * This may have side effects! (e.g., compressed or network file).
11384 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011385 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011386 {
11387 if (ses_do_win(wp)
11388 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011389 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011390#ifdef FEAT_QUICKFIX
11391 && !bt_nofile(wp->w_buffer)
11392#endif
11393 )
11394 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011395 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11396 return FAIL;
11397 need_tabnext = FALSE;
11398
11399 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011400 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11401 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011402 return FAIL;
11403 if (!wp->w_arg_idx_invalid)
11404 edited_win = wp;
11405 break;
11406 }
11407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011409 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011410 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011411 return FAIL;
11412
Bram Moolenaar18144c82006-04-12 21:52:12 +000011413 /*
11414 * Save current window layout.
11415 */
11416 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011418 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011420 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11421 return FAIL;
11422 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11423 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424
Bram Moolenaar18144c82006-04-12 21:52:12 +000011425 /*
11426 * Check if window sizes can be restored (no windows omitted).
11427 * Remember the window number of the current window after restoring.
11428 */
11429 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011430 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011431 {
11432 if (ses_do_win(wp))
11433 ++nr;
11434 else
11435 restore_size = FALSE;
11436 if (curwin == wp)
11437 cnr = nr;
11438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439
Bram Moolenaar18144c82006-04-12 21:52:12 +000011440 /* Go to the first window. */
11441 if (put_line(fd, "wincmd t") == FAIL)
11442 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011443
Bram Moolenaar18144c82006-04-12 21:52:12 +000011444 /*
11445 * If more than one window, see if sizes can be restored.
11446 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11447 * resized when moving between windows.
11448 * Do this before restoring the view, so that the topline and the
11449 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011450 * winminheight and winminwidth need to be set to avoid an error if the
11451 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011452 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011453 if (put_line(fd, "set winminheight=0") == FAIL
11454 || put_line(fd, "set winheight=1") == FAIL
11455 || put_line(fd, "set winminwidth=0") == FAIL
11456 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011457 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011458 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460
Bram Moolenaar18144c82006-04-12 21:52:12 +000011461 /*
11462 * Restore the view of the window (options, file, cursor, etc.).
11463 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011464 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011465 {
11466 if (!ses_do_win(wp))
11467 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011468 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11469 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011470 return FAIL;
11471 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11472 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011473 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011474 }
11475
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011476 /* The argument index in the first tab page is zero, need to set it in
11477 * each window. For further tab pages it's the window where we do
11478 * "tabedit". */
11479 cur_arg_idx = next_arg_idx;
11480
Bram Moolenaar18144c82006-04-12 21:52:12 +000011481 /*
11482 * Restore cursor to the current window if it's not the first one.
11483 */
11484 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11485 || put_eol(fd) == FAIL))
11486 return FAIL;
11487
11488 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011489 * Restore window sizes again after jumping around in windows, because
11490 * the current window has a minimum size while others may not.
11491 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011492 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011493 return FAIL;
11494
Bram Moolenaar18144c82006-04-12 21:52:12 +000011495 /* Don't continue in another tab page when doing only the current one
11496 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011497 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011498 break;
11499 }
11500
11501 if (ssop_flags & SSOP_TABPAGES)
11502 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011503 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11504 || put_eol(fd) == FAIL)
11505 return FAIL;
11506 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011507 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11508 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011509
Bram Moolenaar9c102382006-05-03 21:26:49 +000011510 /*
11511 * Wipe out an empty unnamed buffer we started in.
11512 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011513 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011514 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011515 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011516 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011517 return FAIL;
11518 if (put_line(fd, "endif") == FAIL)
11519 return FAIL;
11520 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11521 return FAIL;
11522
11523 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11524 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11525 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11526 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011527 /* Re-apply 'winminheight' and 'winminwidth'. */
11528 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11529 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11530 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531
11532 /*
11533 * Lastly, execute the x.vim file if it exists.
11534 */
11535 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11536 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011537 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 || put_line(fd, "endif") == FAIL)
11539 return FAIL;
11540
11541 return OK;
11542}
11543
11544 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011545ses_winsizes(
11546 FILE *fd,
11547 int restore_size,
11548 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
11550 int n = 0;
11551 win_T *wp;
11552
11553 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11554 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011555 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 {
11557 if (!ses_do_win(wp))
11558 continue;
11559 ++n;
11560
11561 /* restore height when not full height */
11562 if (wp->w_height + wp->w_status_height < topframe->fr_height
11563 && (fprintf(fd,
11564 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11565 n, (long)wp->w_height, Rows / 2, Rows) < 0
11566 || put_eol(fd) == FAIL))
11567 return FAIL;
11568
11569 /* restore width when not full width */
11570 if (wp->w_width < Columns && (fprintf(fd,
11571 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11572 n, (long)wp->w_width, Columns / 2, Columns) < 0
11573 || put_eol(fd) == FAIL))
11574 return FAIL;
11575 }
11576 }
11577 else
11578 {
11579 /* Just equalise window sizes */
11580 if (put_line(fd, "wincmd =") == FAIL)
11581 return FAIL;
11582 }
11583 return OK;
11584}
11585
11586/*
11587 * Write commands to "fd" to recursively create windows for frame "fr",
11588 * horizontally and vertically split.
11589 * After the commands the last window in the frame is the current window.
11590 * Returns FAIL when writing the commands to "fd" fails.
11591 */
11592 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011593ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594{
11595 frame_T *frc;
11596 int count = 0;
11597
11598 if (fr->fr_layout != FR_LEAF)
11599 {
11600 /* Find first frame that's not skipped and then create a window for
11601 * each following one (first frame is already there). */
11602 frc = ses_skipframe(fr->fr_child);
11603 if (frc != NULL)
11604 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11605 {
11606 /* Make window as big as possible so that we have lots of room
11607 * to split. */
11608 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11609 || put_line(fd, fr->fr_layout == FR_COL
11610 ? "split" : "vsplit") == FAIL)
11611 return FAIL;
11612 ++count;
11613 }
11614
11615 /* Go back to the first window. */
11616 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11617 ? "%dwincmd k" : "%dwincmd h", count) < 0
11618 || put_eol(fd) == FAIL))
11619 return FAIL;
11620
11621 /* Recursively create frames/windows in each window of this column or
11622 * row. */
11623 frc = ses_skipframe(fr->fr_child);
11624 while (frc != NULL)
11625 {
11626 ses_win_rec(fd, frc);
11627 frc = ses_skipframe(frc->fr_next);
11628 /* Go to next window. */
11629 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11630 return FAIL;
11631 }
11632 }
11633 return OK;
11634}
11635
11636/*
11637 * Skip frames that don't contain windows we want to save in the Session.
11638 * Returns NULL when there none.
11639 */
11640 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011641ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642{
11643 frame_T *frc;
11644
11645 for (frc = fr; frc != NULL; frc = frc->fr_next)
11646 if (ses_do_frame(frc))
11647 break;
11648 return frc;
11649}
11650
11651/*
11652 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11653 * the Session.
11654 */
11655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011656ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657{
11658 frame_T *frc;
11659
11660 if (fr->fr_layout == FR_LEAF)
11661 return ses_do_win(fr->fr_win);
11662 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11663 if (ses_do_frame(frc))
11664 return TRUE;
11665 return FALSE;
11666}
11667
11668/*
11669 * Return non-zero if window "wp" is to be stored in the Session.
11670 */
11671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011672ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011674#ifdef FEAT_TERMINAL
11675 if (bt_terminal(wp->w_buffer))
11676 return !term_is_finished(wp->w_buffer)
11677 && (ssop_flags & SSOP_TERMINAL)
11678 && term_should_restore(wp->w_buffer);
11679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 if (wp->w_buffer->b_fname == NULL
11681#ifdef FEAT_QUICKFIX
11682 /* When 'buftype' is "nofile" can't restore the window contents. */
11683 || bt_nofile(wp->w_buffer)
11684#endif
11685 )
11686 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011687 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 return (ssop_flags & SSOP_HELP);
11689 return TRUE;
11690}
11691
11692/*
11693 * Write commands to "fd" to restore the view of a window.
11694 * Caller must make sure 'scrolloff' is zero.
11695 */
11696 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011697put_view(
11698 FILE *fd,
11699 win_T *wp,
11700 int add_edit, /* add ":edit" command to view */
11701 unsigned *flagp, /* vop_flags or ssop_flags */
11702 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011703 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704{
11705 win_T *save_curwin;
11706 int f;
11707 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011708 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709
11710 /* Always restore cursor position for ":mksession". For ":mkview" only
11711 * when 'viewoptions' contains "cursor". */
11712 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11713
11714 /*
11715 * Local argument list.
11716 */
11717 if (wp->w_alist == &global_alist)
11718 {
11719 if (put_line(fd, "argglobal") == FAIL)
11720 return FAIL;
11721 }
11722 else
11723 {
11724 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11725 flagp == &vop_flags
11726 || !(*flagp & SSOP_CURDIR)
11727 || wp->w_localdir != NULL, flagp) == FAIL)
11728 return FAIL;
11729 }
11730
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011731 /* Only when part of a session: restore the argument index. Some
11732 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011733 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011734 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011736 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 || put_eol(fd) == FAIL)
11738 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011739 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 }
11741
11742 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011743 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011745# ifdef FEAT_TERMINAL
11746 if (bt_terminal(wp->w_buffer))
11747 {
11748 if (term_write_session(fd, wp) == FAIL)
11749 return FAIL;
11750 }
11751 else
11752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 /*
11754 * Load the file.
11755 */
11756 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011757# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 )
11761 {
11762 /*
11763 * Editing a file in this buffer: use ":edit file".
11764 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011765 *
11766 * Note, if a buffer for that file already exists, use :badd to
11767 * edit that buffer, to not lose folding information (:edit resets
11768 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011770 if (fputs("if bufexists('", fd) < 0
11771 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11772 || fputs("') | buffer ", fd) < 0
11773 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11774 || fputs(" | else | edit ", fd) < 0
11775 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11776 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011777 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 return FAIL;
11779 }
11780 else
11781 {
11782 /* No file in this buffer, just make it empty. */
11783 if (put_line(fd, "enew") == FAIL)
11784 return FAIL;
11785#ifdef FEAT_QUICKFIX
11786 if (wp->w_buffer->b_ffname != NULL)
11787 {
11788 /* The buffer does have a name, but it's not a file name. */
11789 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011790 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 return FAIL;
11792 }
11793#endif
11794 do_cursor = FALSE;
11795 }
11796 }
11797
11798 /*
11799 * Local mappings and abbreviations.
11800 */
11801 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11802 && makemap(fd, wp->w_buffer) == FAIL)
11803 return FAIL;
11804
11805 /*
11806 * Local options. Need to go to the window temporarily.
11807 * Store only local values when using ":mkview" and when ":mksession" is
11808 * used and 'sessionoptions' doesn't include "options".
11809 * Some folding options are always stored when "folds" is included,
11810 * otherwise the folds would not be restored correctly.
11811 */
11812 save_curwin = curwin;
11813 curwin = wp;
11814 curbuf = curwin->w_buffer;
11815 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11816 f = makeset(fd, OPT_LOCAL,
11817 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11818#ifdef FEAT_FOLDING
11819 else if (*flagp & SSOP_FOLDS)
11820 f = makefoldset(fd);
11821#endif
11822 else
11823 f = OK;
11824 curwin = save_curwin;
11825 curbuf = curwin->w_buffer;
11826 if (f == FAIL)
11827 return FAIL;
11828
11829#ifdef FEAT_FOLDING
11830 /*
11831 * Save Folds when 'buftype' is empty and for help files.
11832 */
11833 if ((*flagp & SSOP_FOLDS)
11834 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011835 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 {
11837 if (put_folds(fd, wp) == FAIL)
11838 return FAIL;
11839 }
11840#endif
11841
11842 /*
11843 * Set the cursor after creating folds, since that moves the cursor.
11844 */
11845 if (do_cursor)
11846 {
11847
11848 /* Restore the cursor line in the file and relatively in the
11849 * window. Don't use "G", it changes the jumplist. */
11850 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11851 (long)wp->w_cursor.lnum,
11852 (long)(wp->w_cursor.lnum - wp->w_topline),
11853 (long)wp->w_height / 2, (long)wp->w_height) < 0
11854 || put_eol(fd) == FAIL
11855 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11856 || put_line(fd, "exe s:l") == FAIL
11857 || put_line(fd, "normal! zt") == FAIL
11858 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11859 || put_eol(fd) == FAIL)
11860 return FAIL;
11861 /* Restore the cursor column and left offset when not wrapping. */
11862 if (wp->w_cursor.col == 0)
11863 {
11864 if (put_line(fd, "normal! 0") == FAIL)
11865 return FAIL;
11866 }
11867 else
11868 {
11869 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11870 {
11871 if (fprintf(fd,
11872 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011873 (long)wp->w_virtcol + 1,
11874 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 (long)wp->w_width / 2, (long)wp->w_width) < 0
11876 || put_eol(fd) == FAIL
11877 || put_line(fd, "if s:c > 0") == FAIL
11878 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011879 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11880 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 || put_eol(fd) == FAIL
11882 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011883 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 || put_eol(fd) == FAIL
11885 || put_line(fd, "endif") == FAIL)
11886 return FAIL;
11887 }
11888 else
11889 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011890 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 || put_eol(fd) == FAIL)
11892 return FAIL;
11893 }
11894 }
11895 }
11896
11897 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011898 * Local directory, if the current flag is not view options or the "curdir"
11899 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011901 if (wp->w_localdir != NULL
11902 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 {
11904 if (fputs("lcd ", fd) < 0
11905 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11906 || put_eol(fd) == FAIL)
11907 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011908 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 }
11910
11911 return OK;
11912}
11913
11914/*
11915 * Write an argument list to the session file.
11916 * Returns FAIL if writing fails.
11917 */
11918 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011919ses_arglist(
11920 FILE *fd,
11921 char *cmd,
11922 garray_T *gap,
11923 int fullname, /* TRUE: use full path name */
11924 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925{
11926 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011927 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 char_u *s;
11929
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011930 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11931 return FAIL;
11932 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 return FAIL;
11934 for (i = 0; i < gap->ga_len; ++i)
11935 {
11936 /* NULL file names are skipped (only happens when out of memory). */
11937 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11938 if (s != NULL)
11939 {
11940 if (fullname)
11941 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011942 buf = alloc(MAXPATHL);
11943 if (buf != NULL)
11944 {
11945 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11946 s = buf;
11947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011949 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011950 || ses_put_fname(fd, s, flagp) == FAIL
11951 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011952 {
11953 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011955 }
11956 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 }
11958 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011959 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960}
11961
11962/*
11963 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011964 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 * Returns FAIL if writing fails.
11966 */
11967 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011968ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970 char_u *name;
11971
11972 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011973 * the session file will be sourced.
11974 * Don't do this for ":mkview", we don't know the current directory.
11975 * Don't do this after ":lcd", we don't keep track of what the current
11976 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 if (buf->b_sfname != NULL
11978 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011979 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011980#ifdef FEAT_AUTOCHDIR
11981 && !p_acd
11982#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011983 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 name = buf->b_sfname;
11985 else
11986 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011987 if (ses_put_fname(fd, name, flagp) == FAIL
11988 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 return FAIL;
11990 return OK;
11991}
11992
11993/*
11994 * Write a file name to the session file.
11995 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11996 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011997 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 */
11999 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012000ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001{
12002 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012003 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005
12006 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012007 if (sname == NULL)
12008 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012010 if (*flagp & SSOP_SLASH)
12011 {
12012 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012013 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012014 if (*p == '\\')
12015 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012017
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012018 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012019 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012021 if (p == NULL)
12022 return FAIL;
12023
12024 /* write the result */
12025 if (fputs((char *)p, fd) < 0)
12026 retval = FAIL;
12027
12028 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 return retval;
12030}
12031
12032/*
12033 * ":loadview [nr]"
12034 */
12035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012036ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037{
12038 char_u *fname;
12039
12040 fname = get_view_file(*eap->arg);
12041 if (fname != NULL)
12042 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012043 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 vim_free(fname);
12045 }
12046}
12047
12048/*
12049 * Get the name of the view file for the current buffer.
12050 */
12051 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012052get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053{
12054 int len = 0;
12055 char_u *p, *s;
12056 char_u *retval;
12057 char_u *sname;
12058
12059 if (curbuf->b_ffname == NULL)
12060 {
12061 EMSG(_(e_noname));
12062 return NULL;
12063 }
12064 sname = home_replace_save(NULL, curbuf->b_ffname);
12065 if (sname == NULL)
12066 return NULL;
12067
12068 /*
12069 * We want a file name without separators, because we're not going to make
12070 * a directory.
12071 * "normal" path separator -> "=+"
12072 * "=" -> "=="
12073 * ":" path separator -> "=-"
12074 */
12075 for (p = sname; *p; ++p)
12076 if (*p == '=' || vim_ispathsep(*p))
12077 ++len;
12078 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12079 if (retval != NULL)
12080 {
12081 STRCPY(retval, p_vdir);
12082 add_pathsep(retval);
12083 s = retval + STRLEN(retval);
12084 for (p = sname; *p; ++p)
12085 {
12086 if (*p == '=')
12087 {
12088 *s++ = '=';
12089 *s++ = '=';
12090 }
12091 else if (vim_ispathsep(*p))
12092 {
12093 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012094#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 if (*p == ':')
12096 *s++ = '-';
12097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012099 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 }
12101 else
12102 *s++ = *p;
12103 }
12104 *s++ = '=';
12105 *s++ = c;
12106 STRCPY(s, ".vim");
12107 }
12108
12109 vim_free(sname);
12110 return retval;
12111}
12112
12113#endif /* FEAT_SESSION */
12114
12115/*
12116 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12117 * Return FAIL for a write error.
12118 */
12119 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012120put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121{
12122 if (
12123#ifdef USE_CRNL
12124 (
12125# ifdef MKSESSION_NL
12126 !mksession_nl &&
12127# endif
12128 (putc('\r', fd) < 0)) ||
12129#endif
12130 (putc('\n', fd) < 0))
12131 return FAIL;
12132 return OK;
12133}
12134
12135/*
12136 * Write a line to "fd".
12137 * Return FAIL for a write error.
12138 */
12139 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012140put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141{
12142 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12143 return FAIL;
12144 return OK;
12145}
12146
12147#ifdef FEAT_VIMINFO
12148/*
12149 * ":rviminfo" and ":wviminfo".
12150 */
12151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012152ex_viminfo(
12153 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154{
12155 char_u *save_viminfo;
12156
12157 save_viminfo = p_viminfo;
12158 if (*p_viminfo == NUL)
12159 p_viminfo = (char_u *)"'100";
12160 if (eap->cmdidx == CMD_rviminfo)
12161 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012162 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12163 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 EMSG(_("E195: Cannot open viminfo file for reading"));
12165 }
12166 else
12167 write_viminfo(eap->arg, eap->forceit);
12168 p_viminfo = save_viminfo;
12169}
12170#endif
12171
12172#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012173/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012174 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012175 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012178dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 if (fname == NULL)
12181 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012182 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183}
12184#endif
12185
12186/*
12187 * ":behave {mswin,xterm}"
12188 */
12189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012190ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
12192 if (STRCMP(eap->arg, "mswin") == 0)
12193 {
12194 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12195 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12196 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12197 set_option_value((char_u *)"keymodel", 0L,
12198 (char_u *)"startsel,stopsel", 0);
12199 }
12200 else if (STRCMP(eap->arg, "xterm") == 0)
12201 {
12202 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12203 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12204 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12205 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12206 }
12207 else
12208 EMSG2(_(e_invarg2), eap->arg);
12209}
12210
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012211#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12212/*
12213 * Function given to ExpandGeneric() to obtain the possible arguments of the
12214 * ":behave {mswin,xterm}" command.
12215 */
12216 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012217get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012218{
12219 if (idx == 0)
12220 return (char_u *)"mswin";
12221 if (idx == 1)
12222 return (char_u *)"xterm";
12223 return NULL;
12224}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012225
12226/*
12227 * Function given to ExpandGeneric() to obtain the possible arguments of the
12228 * ":messages {clear}" command.
12229 */
12230 char_u *
12231get_messages_arg(expand_T *xp UNUSED, int idx)
12232{
12233 if (idx == 0)
12234 return (char_u *)"clear";
12235 return NULL;
12236}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012237#endif
12238
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012239 char_u *
12240get_mapclear_arg(expand_T *xp UNUSED, int idx)
12241{
12242 if (idx == 0)
12243 return (char_u *)"<buffer>";
12244 return NULL;
12245}
12246
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247static int filetype_detect = FALSE;
12248static int filetype_plugin = FALSE;
12249static int filetype_indent = FALSE;
12250
12251/*
12252 * ":filetype [plugin] [indent] {on,off,detect}"
12253 * on: Load the filetype.vim file to install autocommands for file types.
12254 * off: Load the ftoff.vim file to remove all autocommands for file types.
12255 * plugin on: load filetype.vim and ftplugin.vim
12256 * plugin off: load ftplugof.vim
12257 * indent on: load filetype.vim and indent.vim
12258 * indent off: load indoff.vim
12259 */
12260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012261ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262{
12263 char_u *arg = eap->arg;
12264 int plugin = FALSE;
12265 int indent = FALSE;
12266
12267 if (*eap->arg == NUL)
12268 {
12269 /* Print current status. */
12270 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12271 filetype_detect ? "ON" : "OFF",
12272 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12273 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12274 return;
12275 }
12276
12277 /* Accept "plugin" and "indent" in any order. */
12278 for (;;)
12279 {
12280 if (STRNCMP(arg, "plugin", 6) == 0)
12281 {
12282 plugin = TRUE;
12283 arg = skipwhite(arg + 6);
12284 continue;
12285 }
12286 if (STRNCMP(arg, "indent", 6) == 0)
12287 {
12288 indent = TRUE;
12289 arg = skipwhite(arg + 6);
12290 continue;
12291 }
12292 break;
12293 }
12294 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12295 {
12296 if (*arg == 'o' || !filetype_detect)
12297 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012298 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 filetype_detect = TRUE;
12300 if (plugin)
12301 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012302 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 filetype_plugin = TRUE;
12304 }
12305 if (indent)
12306 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012307 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 filetype_indent = TRUE;
12309 }
12310 }
12311 if (*arg == 'd')
12312 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012313 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012314 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 }
12316 }
12317 else if (STRCMP(arg, "off") == 0)
12318 {
12319 if (plugin || indent)
12320 {
12321 if (plugin)
12322 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012323 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 filetype_plugin = FALSE;
12325 }
12326 if (indent)
12327 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012328 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 filetype_indent = FALSE;
12330 }
12331 }
12332 else
12333 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012334 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 filetype_detect = FALSE;
12336 }
12337 }
12338 else
12339 EMSG2(_(e_invarg2), arg);
12340}
12341
12342/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012343 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 */
12345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012346ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347{
12348 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012349 {
12350 char_u *arg = eap->arg;
12351
12352 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12353 arg += 9;
12354
12355 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12356 if (arg != eap->arg)
12357 did_filetype = FALSE;
12358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012362ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363{
12364#ifdef FEAT_DIGRAPHS
12365 if (*eap->arg != NUL)
12366 putdigraph(eap->arg);
12367 else
12368 listdigraphs();
12369#else
12370 EMSG(_("E196: No digraphs in this version"));
12371#endif
12372}
12373
12374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012375ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376{
12377 int flags = 0;
12378
12379 if (eap->cmdidx == CMD_setlocal)
12380 flags = OPT_LOCAL;
12381 else if (eap->cmdidx == CMD_setglobal)
12382 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012383#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 if (cmdmod.browse && flags == 0)
12385 ex_options(eap);
12386 else
12387#endif
12388 (void)do_set(eap->arg, flags);
12389}
12390
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012391#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12392 void
12393set_no_hlsearch(int flag)
12394{
12395 no_hlsearch = flag;
12396# ifdef FEAT_EVAL
12397 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12398# endif
12399}
12400
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401/*
12402 * ":nohlsearch"
12403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012405ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012407 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012408 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409}
12410
12411/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012412 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 * Sets nextcmd to the start of the next command, if any. Also called when
12414 * skipping commands to find the next command.
12415 */
12416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012417ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418{
12419 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012420 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 char_u *end;
12422 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012423 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012424
12425 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012426 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012427 else
12428 {
12429 EMSG(e_invcmd);
12430 return;
12431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432
12433 /* First clear any old pattern. */
12434 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012435 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436
12437 if (ends_excmd(*eap->arg))
12438 end = eap->arg;
12439 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012440 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 end = eap->arg + 4;
12442 else
12443 {
12444 p = skiptowhite(eap->arg);
12445 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012446 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 p = skipwhite(p);
12448 if (*p == NUL)
12449 {
12450 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012451 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452 EMSG2(_(e_invarg2), eap->arg);
12453 return;
12454 }
12455 end = skip_regexp(p + 1, *p, TRUE, NULL);
12456 if (!eap->skip)
12457 {
12458 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12459 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012460 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 eap->errmsg = e_trailing;
12462 return;
12463 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012464 if (*end != *p)
12465 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012466 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012467 EMSG2(_(e_invarg2), p);
12468 return;
12469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470
12471 c = *end;
12472 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012473 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012474 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012475 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 }
12477 }
12478 eap->nextcmd = find_nextcmd(end);
12479}
12480#endif
12481
12482#ifdef FEAT_CRYPT
12483/*
12484 * ":X": Get crypt key
12485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012487ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012489 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012490 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491}
12492#endif
12493
12494#ifdef FEAT_FOLDING
12495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012496ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497{
12498 if (foldManualAllowed(TRUE))
12499 foldCreate(eap->line1, eap->line2);
12500}
12501
12502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012503ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
12505 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12506 eap->forceit, FALSE);
12507}
12508
12509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012510ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511{
12512 linenr_T lnum;
12513
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012514#ifdef FEAT_CLIPBOARD
12515 start_global_changes();
12516#endif
12517
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 /* First set the marks for all lines closed/open. */
12519 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12520 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12521 ml_setmarked(lnum);
12522
12523 /* Execute the command on the marked lines. */
12524 global_exe(eap->arg);
12525 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012526#ifdef FEAT_CLIPBOARD
12527 end_global_changes();
12528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529}
12530#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012531
12532# if defined(FEAT_TIMERS) || defined(PROTO)
12533 int
12534get_pressedreturn(void)
12535{
12536 return ex_pressedreturn;
12537}
12538
12539 void
12540set_pressedreturn(int val)
12541{
12542 ex_pressedreturn = val;
12543}
12544#endif