blob: c8a1c3f23854d03765c2d1dd162286105f92c592 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001770 * 3. Skip over the range to find the command. Let "p" point to after it.
1771 *
1772 * We need the command to know what kind of range it uses.
1773 */
1774 cmd = ea.cmd;
1775 ea.cmd = skip_range(ea.cmd, NULL);
1776 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1777 ea.cmd = skipwhite(ea.cmd + 1);
1778 p = find_command(&ea, NULL);
1779
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001780#ifdef FEAT_EVAL
1781# ifdef FEAT_PROFILE
1782 // Count this line for profiling if skip is TRUE.
1783 if (do_profiling == PROF_YES
1784 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1785 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1786 {
1787 int skip = did_emsg || got_int || did_throw;
1788
1789 if (ea.cmdidx == CMD_catch)
1790 skip = !skip && !(cstack->cs_idx >= 0
1791 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1792 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1793 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1794 skip = skip || !(cstack->cs_idx >= 0
1795 && !(cstack->cs_flags[cstack->cs_idx]
1796 & (CSF_ACTIVE | CSF_TRUE)));
1797 else if (ea.cmdidx == CMD_finally)
1798 skip = FALSE;
1799 else if (ea.cmdidx != CMD_endif
1800 && ea.cmdidx != CMD_endfor
1801 && ea.cmdidx != CMD_endtry
1802 && ea.cmdidx != CMD_endwhile)
1803 skip = ea.skip;
1804
1805 if (!skip)
1806 {
1807 if (getline_equal(fgetline, cookie, get_func_line))
1808 func_line_exec(getline_cookie(fgetline, cookie));
1809 else if (getline_equal(fgetline, cookie, getsourceline))
1810 script_line_exec();
1811 }
1812 }
1813# endif
1814
1815 /* May go to debug mode. If this happens and the ">quit" debug command is
1816 * used, throw an interrupt exception and skip the next command. */
1817 dbg_check_breakpoint(&ea);
1818 if (!ea.skip && got_int)
1819 {
1820 ea.skip = TRUE;
1821 (void)do_intthrow(cstack);
1822 }
1823#endif
1824
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001825/*
1826 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 *
1828 * where 'addr' is:
1829 *
1830 * % (entire file)
1831 * $ [+-NUM]
1832 * 'x [+-NUM] (where x denotes a currently defined mark)
1833 * . [+-NUM]
1834 * [+-NUM]..
1835 * NUM
1836 *
1837 * The ea.cmd pointer is updated to point to the first character following the
1838 * range spec. If an initial address is found, but no second, the upper bound
1839 * is equal to the lower.
1840 */
1841
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001842 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001843 if (!IS_USER_CMDIDX(ea.cmdidx))
1844 {
1845 if (ea.cmdidx != CMD_SIZE)
1846 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1847 else
1848 ea.addr_type = ADDR_LINES;
1849
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001850 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001851 if (ea.cmdidx == CMD_wincmd && p != NULL)
1852 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001853 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001854
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001855 ea.cmd = cmd;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001856 if (parse_cmd_address(&ea, &errormsg) == FAIL)
1857 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 */
1862
1863 /*
1864 * Skip ':' and any white space
1865 */
1866 ea.cmd = skipwhite(ea.cmd);
1867 while (*ea.cmd == ':')
1868 ea.cmd = skipwhite(ea.cmd + 1);
1869
1870 /*
1871 * If we got a line, but no command, then go to the line.
1872 * If we find a '|' or '\n' we set ea.nextcmd.
1873 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001874 if (*ea.cmd == NUL || *ea.cmd == '"'
1875 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 /*
1878 * strange vi behaviour:
1879 * ":3" jumps to line 3
1880 * ":3|..." prints line 3
1881 * ":|" prints current line
1882 */
1883 if (ea.skip) /* skip this if inside :if */
1884 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001885 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 ea.cmdidx = CMD_print;
1888 ea.argt = RANGE+COUNT+TRLBAR;
1889 if ((errormsg = invalid_range(&ea)) == NULL)
1890 {
1891 correct_range(&ea);
1892 ex_print(&ea);
1893 }
1894 }
1895 else if (ea.addr_count != 0)
1896 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001897 if (ea.line2 > curbuf->b_ml.ml_line_count)
1898 {
1899 /* With '-' in 'cpoptions' a line number past the file is an
1900 * error, otherwise put it at the end of the file. */
1901 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1902 ea.line2 = -1;
1903 else
1904 ea.line2 = curbuf->b_ml.ml_line_count;
1905 }
1906
1907 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001908 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 else
1910 {
1911 if (ea.line2 == 0)
1912 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else
1914 curwin->w_cursor.lnum = ea.line2;
1915 beginline(BL_SOL | BL_FIX);
1916 }
1917 }
1918 goto doend;
1919 }
1920
Bram Moolenaard5005162014-08-22 23:05:54 +02001921 /* If this looks like an undefined user command and there are CmdUndefined
1922 * autocommands defined, trigger the matching autocommands. */
1923 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1924 && ASCII_ISUPPER(*ea.cmd)
1925 && has_cmdundefined())
1926 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001927 int ret;
1928
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001929 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001930 while (ASCII_ISALNUM(*p))
1931 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001932 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001933 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1934 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001935 /* If the autocommands did something and didn't cause an error, try
1936 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001937 p = (ret
1938#ifdef FEAT_EVAL
1939 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001940#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001941 ) ? find_command(&ea, NULL) : ea.cmd;
1942 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944#ifdef FEAT_USR_CMDS
1945 if (p == NULL)
1946 {
1947 if (!ea.skip)
1948 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
1949 goto doend;
1950 }
1951 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001952 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1953 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 errormsg = uc_fun_cmd();
1956 goto doend;
1957 }
1958#endif
1959 if (ea.cmdidx == CMD_SIZE)
1960 {
1961 if (!ea.skip)
1962 {
1963 STRCPY(IObuff, _("E492: Not an editor command"));
1964 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001965 {
1966 /* If the modifier was parsed OK the error must be in the
1967 * following command */
1968 if (after_modifier != NULL)
1969 append_command(after_modifier);
1970 else
1971 append_command(*cmdlinep);
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001974 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 goto doend;
1977 }
1978
Bram Moolenaar958636c2014-10-21 20:01:58 +02001979 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1980 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001981#ifdef HAVE_EX_SCRIPT_NI
1982 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1983#endif
1984 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
1986#ifndef FEAT_EVAL
1987 /*
1988 * When the expression evaluation is disabled, recognize the ":if" and
1989 * ":endif" commands and ignore everything in between it.
1990 */
1991 if (ea.cmdidx == CMD_if)
1992 ++if_level;
1993 if (if_level)
1994 {
1995 if (ea.cmdidx == CMD_endif)
1996 --if_level;
1997 goto doend;
1998 }
1999
2000#endif
2001
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002002 /* forced commands */
2003 if (*p == '!' && ea.cmdidx != CMD_substitute
2004 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
2006 ++p;
2007 ea.forceit = TRUE;
2008 }
2009 else
2010 ea.forceit = FALSE;
2011
2012/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002013 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002015 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002016 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 if (!ea.skip)
2019 {
2020#ifdef HAVE_SANDBOX
2021 if (sandbox != 0 && !(ea.argt & SBOXOK))
2022 {
2023 /* Command not allowed in sandbox. */
2024 errormsg = (char_u *)_(e_sandbox);
2025 goto doend;
2026 }
2027#endif
2028 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2029 {
2030 /* Command not allowed in non-'modifiable' buffer */
2031 errormsg = (char_u *)_(e_modifiable);
2032 goto doend;
2033 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002034
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002035 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002036 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002038 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002039 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 goto doend;
2041 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002042 /* Disallow editing another buffer when "curbuf_lock" is set.
2043 * Do allow ":edit" (check for argument later).
2044 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002045 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002046 && ea.cmdidx != CMD_edit
2047 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002048 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002049 && curbuf_locked())
2050 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2053 {
2054 /* no range allowed */
2055 errormsg = (char_u *)_(e_norange);
2056 goto doend;
2057 }
2058 }
2059
2060 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2061 {
2062 errormsg = (char_u *)_(e_nobang);
2063 goto doend;
2064 }
2065
2066 /*
2067 * Don't complain about the range if it is not used
2068 * (could happen if line_count is accidentally set to 0).
2069 */
2070 if (!ea.skip && !ni)
2071 {
2072 /*
2073 * If the range is backwards, ask for confirmation and, if given, swap
2074 * ea.line1 & ea.line2 so it's forwards again.
2075 * When global command is busy, don't ask, will fail below.
2076 */
2077 if (!global_busy && ea.line1 > ea.line2)
2078 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002079 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002081 if (sourcing || exmode_active)
2082 {
2083 errormsg = (char_u *)_("E493: Backwards range given");
2084 goto doend;
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (ask_yesno((char_u *)
2087 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002088 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 lnum = ea.line1;
2091 ea.line1 = ea.line2;
2092 ea.line2 = lnum;
2093 }
2094 if ((errormsg = invalid_range(&ea)) != NULL)
2095 goto doend;
2096 }
2097
2098 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2099 ea.line2 = 1;
2100
2101 correct_range(&ea);
2102
2103#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002104 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2105 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
2107 /* Put the first line at the start of a closed fold, put the last line
2108 * at the end of a closed fold. */
2109 (void)hasFolding(ea.line1, &ea.line1, NULL);
2110 (void)hasFolding(ea.line2, NULL, &ea.line2);
2111 }
2112#endif
2113
2114#ifdef FEAT_QUICKFIX
2115 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002116 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 * option here, so things like % get expanded.
2118 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002119 p = replace_makeprg(&ea, p, cmdlinep);
2120 if (p == NULL)
2121 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#endif
2123
2124 /*
2125 * Skip to start of argument.
2126 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2127 */
2128 if (ea.cmdidx == CMD_bang)
2129 ea.arg = p;
2130 else
2131 ea.arg = skipwhite(p);
2132
2133 /*
2134 * Check for "++opt=val" argument.
2135 * Must be first, allow ":w ++enc=utf8 !cmd"
2136 */
2137 if (ea.argt & ARGOPT)
2138 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2139 if (getargopt(&ea) == FAIL && !ni)
2140 {
2141 errormsg = (char_u *)_(e_invarg);
2142 goto doend;
2143 }
2144
2145 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2146 {
2147 if (*ea.arg == '>') /* append */
2148 {
2149 if (*++ea.arg != '>') /* typed wrong */
2150 {
2151 errormsg = (char_u *)_("E494: Use w or w>>");
2152 goto doend;
2153 }
2154 ea.arg = skipwhite(ea.arg + 1);
2155 ea.append = TRUE;
2156 }
2157 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2158 {
2159 ++ea.arg;
2160 ea.usefilter = TRUE;
2161 }
2162 }
2163
2164 if (ea.cmdidx == CMD_read)
2165 {
2166 if (ea.forceit)
2167 {
2168 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2169 ea.forceit = FALSE;
2170 }
2171 else if (*ea.arg == '!') /* :r !filter */
2172 {
2173 ++ea.arg;
2174 ea.usefilter = TRUE;
2175 }
2176 }
2177
2178 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2179 {
2180 ea.amount = 1;
2181 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2182 {
2183 ++ea.arg;
2184 ++ea.amount;
2185 }
2186 ea.arg = skipwhite(ea.arg);
2187 }
2188
2189 /*
2190 * Check for "+command" argument, before checking for next command.
2191 * Don't do this for ":read !cmd" and ":write !cmd".
2192 */
2193 if ((ea.argt & EDITCMD) && !ea.usefilter)
2194 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2195
2196 /*
2197 * Check for '|' to separate commands and '"' to start comments.
2198 * Don't do this for ":read !cmd" and ":write !cmd".
2199 */
2200 if ((ea.argt & TRLBAR) && !ea.usefilter)
2201 separate_nextcmd(&ea);
2202
2203 /*
2204 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002205 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2206 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002208 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002209 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002210 || ea.cmdidx == CMD_global
2211 || ea.cmdidx == CMD_vglobal
2212 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 for (p = ea.arg; *p; ++p)
2215 {
2216 /* Remove one backslash before a newline, so that it's possible to
2217 * pass a newline to the shell and also a newline that is preceded
2218 * with a backslash. This makes it impossible to end a shell
2219 * command in a backslash, but that doesn't appear useful.
2220 * Halving the number of backslashes is incompatible with previous
2221 * versions. */
2222 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002223 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 else if (*p == '\n')
2225 {
2226 ea.nextcmd = p + 1;
2227 *p = NUL;
2228 break;
2229 }
2230 }
2231 }
2232
2233 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2234 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002235 buf_T *buf;
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002238 switch (ea.addr_type)
2239 {
2240 case ADDR_LINES:
2241 ea.line2 = curbuf->b_ml.ml_line_count;
2242 break;
2243 case ADDR_LOADED_BUFFERS:
2244 buf = firstbuf;
2245 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2246 buf = buf->b_next;
2247 ea.line1 = buf->b_fnum;
2248 buf = lastbuf;
2249 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2250 buf = buf->b_prev;
2251 ea.line2 = buf->b_fnum;
2252 break;
2253 case ADDR_BUFFERS:
2254 ea.line1 = firstbuf->b_fnum;
2255 ea.line2 = lastbuf->b_fnum;
2256 break;
2257 case ADDR_WINDOWS:
2258 ea.line2 = LAST_WIN_NR;
2259 break;
2260 case ADDR_TABS:
2261 ea.line2 = LAST_TAB_NR;
2262 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002263 case ADDR_TABS_RELATIVE:
2264 ea.line2 = 1;
2265 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002266 case ADDR_ARGUMENTS:
2267 if (ARGCOUNT == 0)
2268 ea.line1 = ea.line2 = 0;
2269 else
2270 ea.line2 = ARGCOUNT;
2271 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002272#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002273 case ADDR_QUICKFIX:
2274 ea.line2 = qf_get_size(&ea);
2275 if (ea.line2 == 0)
2276 ea.line2 = 1;
2277 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002278#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281
2282 /* accept numbered register only when no count allowed (:put) */
2283 if ( (ea.argt & REGSTR)
2284 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002285 /* Do not allow register = for user commands */
2286 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2288 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002289#ifndef FEAT_CLIPBOARD
2290 /* check these explicitly for a more specific error message */
2291 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002293 errormsg = (char_u *)_(e_invalidreg);
2294 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002297 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2298 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002299 {
2300 ea.regname = *ea.arg++;
2301#ifdef FEAT_EVAL
2302 /* for '=' register: accept the rest of the line as an expression */
2303 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2304 {
2305 set_expr_line(vim_strsave(ea.arg));
2306 ea.arg += STRLEN(ea.arg);
2307 }
2308#endif
2309 ea.arg = skipwhite(ea.arg);
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312
2313 /*
2314 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2315 * count, it's a buffer name.
2316 */
2317 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2318 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002319 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 n = getdigits(&ea.arg);
2322 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002323 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 errormsg = (char_u *)_(e_zerocount);
2326 goto doend;
2327 }
2328 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2329 {
2330 ea.line2 = n;
2331 if (ea.addr_count == 0)
2332 ea.addr_count = 1;
2333 }
2334 else
2335 {
2336 ea.line1 = ea.line2;
2337 ea.line2 += n - 1;
2338 ++ea.addr_count;
2339 /*
2340 * Be vi compatible: no error message for out of range.
2341 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002342 if (ea.addr_type == ADDR_LINES
2343 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 ea.line2 = curbuf->b_ml.ml_line_count;
2345 }
2346 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002347
2348 /*
2349 * Check for flags: 'l', 'p' and '#'.
2350 */
2351 if (ea.argt & EXFLAGS)
2352 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002354 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002355 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
2357 errormsg = (char_u *)_(e_trailing);
2358 goto doend;
2359 }
2360
2361 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2362 {
2363 errormsg = (char_u *)_(e_argreq);
2364 goto doend;
2365 }
2366
2367#ifdef FEAT_EVAL
2368 /*
2369 * Skip the command when it's not going to be executed.
2370 * The commands like :if, :endif, etc. always need to be executed.
2371 * Also make an exception for commands that handle a trailing command
2372 * themselves.
2373 */
2374 if (ea.skip)
2375 {
2376 switch (ea.cmdidx)
2377 {
2378 /* commands that need evaluation */
2379 case CMD_while:
2380 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002381 case CMD_for:
2382 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 case CMD_if:
2384 case CMD_elseif:
2385 case CMD_else:
2386 case CMD_endif:
2387 case CMD_try:
2388 case CMD_catch:
2389 case CMD_finally:
2390 case CMD_endtry:
2391 case CMD_function:
2392 break;
2393
2394 /* Commands that handle '|' themselves. Check: A command should
2395 * either have the TRLBAR flag, appear in this list or appear in
2396 * the list at ":help :bar". */
2397 case CMD_aboveleft:
2398 case CMD_and:
2399 case CMD_belowright:
2400 case CMD_botright:
2401 case CMD_browse:
2402 case CMD_call:
2403 case CMD_confirm:
2404 case CMD_delfunction:
2405 case CMD_djump:
2406 case CMD_dlist:
2407 case CMD_dsearch:
2408 case CMD_dsplit:
2409 case CMD_echo:
2410 case CMD_echoerr:
2411 case CMD_echomsg:
2412 case CMD_echon:
2413 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002414 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_help:
2416 case CMD_hide:
2417 case CMD_ijump:
2418 case CMD_ilist:
2419 case CMD_isearch:
2420 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002421 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_keepjumps:
2423 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002424 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_leftabove:
2426 case CMD_let:
2427 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002428 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002430 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002431 case CMD_noautocmd:
2432 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_perl:
2434 case CMD_psearch:
2435 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002436 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002437 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 case CMD_return:
2439 case CMD_rightbelow:
2440 case CMD_ruby:
2441 case CMD_silent:
2442 case CMD_smagic:
2443 case CMD_snomagic:
2444 case CMD_substitute:
2445 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002446 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_tcl:
2448 case CMD_throw:
2449 case CMD_tilde:
2450 case CMD_topleft:
2451 case CMD_unlet:
2452 case CMD_verbose:
2453 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002454 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 break;
2456
2457 default: goto doend;
2458 }
2459 }
2460#endif
2461
2462 if (ea.argt & XFILE)
2463 {
2464 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2465 goto doend;
2466 }
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 /*
2469 * Accept buffer name. Cannot be used at the same time with a buffer
2470 * number. Don't do this for a user command.
2471 */
2472 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 /*
2476 * :bdelete, :bwipeout and :bunload take several arguments, separated
2477 * by spaces: find next space (skipping over escaped characters).
2478 * The others take one argument: ignore trailing spaces.
2479 */
2480 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2481 || ea.cmdidx == CMD_bunload)
2482 p = skiptowhite_esc(ea.arg);
2483 else
2484 {
2485 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002486 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 --p;
2488 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002489 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2490 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (ea.line2 < 0) /* failed */
2492 goto doend;
2493 ea.addr_count = 1;
2494 ea.arg = skipwhite(p);
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
Bram Moolenaar2be57332018-02-13 18:05:18 +01002497 /* The :try command saves the emsg_silent flag, reset it here when
2498 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002499 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002500 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002501 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002502 if (emsg_silent < 0)
2503 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002504 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002505 }
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002508 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002512 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 /*
2515 * Execute a user-defined command.
2516 */
2517 do_ucmd(&ea);
2518 }
2519 else
2520#endif
2521 {
2522 /*
2523 * Call the function to execute the command.
2524 */
2525 ea.errmsg = NULL;
2526 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2527 if (ea.errmsg != NULL)
2528 errormsg = (char_u *)_(ea.errmsg);
2529 }
2530
2531#ifdef FEAT_EVAL
2532 /*
2533 * If the command just executed called do_cmdline(), any throw or ":return"
2534 * or ":finish" encountered there must also check the cstack of the still
2535 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2536 * exception, or reanimate a returned function or finished script file and
2537 * return or finish it again.
2538 */
2539 if (need_rethrow)
2540 do_throw(cstack);
2541 else if (check_cstack)
2542 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002543 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 && current_func_returned())
2547 do_return(&ea, TRUE, FALSE, NULL);
2548 }
2549 need_rethrow = check_cstack = FALSE;
2550#endif
2551
2552doend:
2553 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002556 curwin->w_cursor.col = 0;
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2560 {
2561 if (sourcing)
2562 {
2563 if (errormsg != IObuff)
2564 {
2565 STRCPY(IObuff, errormsg);
2566 errormsg = IObuff;
2567 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002568 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 emsg(errormsg);
2571 }
2572#ifdef FEAT_EVAL
2573 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002574 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2575 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
2577
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 if (ea.verbose_save >= 0)
2579 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002580
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002581 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 cmdmod = save_cmdmod;
2583
Bram Moolenaareffed932018-08-14 13:38:17 +02002584 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 /* messages could be enabled for a serious error, need to check if the
2587 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002588 if (!did_emsg || msg_silent > ea.save_msg_silent)
2589 msg_silent = ea.save_msg_silent;
2590 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (emsg_silent < 0)
2592 emsg_silent = 0;
2593 /* Restore msg_scroll, it's set by file I/O commands, even when no
2594 * message is actually displayed. */
2595 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002596
2597 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2598 * somewhere in the line. Put it back in the first column. */
2599 if (redirecting())
2600 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002603#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002605 --sandbox;
2606#endif
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2609 ea.nextcmd = NULL;
2610
2611#ifdef FEAT_EVAL
2612 --ex_nesting_level;
2613#endif
2614
2615 return ea.nextcmd;
2616}
2617#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002618 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619#endif
2620
2621/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 * Parse and skip over command modifiers:
2623 * - update eap->cmd
2624 * - store flags in "cmdmod".
2625 * - Set ex_pressedreturn for an empty command line.
2626 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002627 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002628 * - set p_verbose for ":verbose"
2629 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002630 * When "skip_only" is TRUE the global variables are not changed, except for
2631 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 * Return FAIL when the command is not to be executed.
2633 * May set "errormsg" to an error message.
2634 */
2635 int
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002636parse_command_modifiers(exarg_T *eap, char_u **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002637{
2638 char_u *p;
2639
2640 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2641 eap->verbose_save = -1;
2642 eap->save_msg_silent = -1;
2643
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002644 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 for (;;)
2646 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2648 ++eap->cmd;
2649
2650 /* in ex mode, an empty line works like :+ */
2651 if (*eap->cmd == NUL && exmode_active
2652 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2653 || getline_equal(eap->getline, eap->cookie, getexline))
2654 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2655 {
2656 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002657 if (!skip_only)
2658 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002659 }
2660
2661 /* ignore comment and empty lines */
2662 if (*eap->cmd == '"')
2663 return FAIL;
2664 if (*eap->cmd == NUL)
2665 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002666 if (!skip_only)
2667 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002668 return FAIL;
2669 }
2670
Bram Moolenaareffed932018-08-14 13:38:17 +02002671 p = skip_range(eap->cmd, NULL);
2672 switch (*p)
2673 {
2674 /* When adding an entry, also modify cmd_exists(). */
2675 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2676 break;
2677 cmdmod.split |= WSP_ABOVE;
2678 continue;
2679
2680 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2681 {
2682 cmdmod.split |= WSP_BELOW;
2683 continue;
2684 }
2685 if (checkforcmd(&eap->cmd, "browse", 3))
2686 {
2687#ifdef FEAT_BROWSE_CMD
2688 cmdmod.browse = TRUE;
2689#endif
2690 continue;
2691 }
2692 if (!checkforcmd(&eap->cmd, "botright", 2))
2693 break;
2694 cmdmod.split |= WSP_BOT;
2695 continue;
2696
2697 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2698 break;
2699#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2700 cmdmod.confirm = TRUE;
2701#endif
2702 continue;
2703
2704 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2705 {
2706 cmdmod.keepmarks = TRUE;
2707 continue;
2708 }
2709 if (checkforcmd(&eap->cmd, "keepalt", 5))
2710 {
2711 cmdmod.keepalt = TRUE;
2712 continue;
2713 }
2714 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2715 {
2716 cmdmod.keeppatterns = TRUE;
2717 continue;
2718 }
2719 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2720 break;
2721 cmdmod.keepjumps = TRUE;
2722 continue;
2723
2724 case 'f': /* only accept ":filter {pat} cmd" */
2725 {
2726 char_u *reg_pat;
2727
2728 if (!checkforcmd(&p, "filter", 4)
2729 || *p == NUL || ends_excmd(*p))
2730 break;
2731 if (*p == '!')
2732 {
2733 cmdmod.filter_force = TRUE;
2734 p = skipwhite(p + 1);
2735 if (*p == NUL || ends_excmd(*p))
2736 break;
2737 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002738 if (skip_only)
2739 p = skip_vimgrep_pat(p, NULL, NULL);
2740 else
2741 // NOTE: This puts a NUL after the pattern.
2742 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 if (p == NULL || *p == NUL)
2744 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002745 if (!skip_only)
2746 {
2747 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002748 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002749 if (cmdmod.filter_regmatch.regprog == NULL)
2750 break;
2751 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 eap->cmd = p;
2753 continue;
2754 }
2755
2756 /* ":hide" and ":hide | cmd" are not modifiers */
2757 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2758 || *p == NUL || ends_excmd(*p))
2759 break;
2760 eap->cmd = p;
2761 cmdmod.hide = TRUE;
2762 continue;
2763
2764 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2765 {
2766 cmdmod.lockmarks = TRUE;
2767 continue;
2768 }
2769
2770 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2771 break;
2772 cmdmod.split |= WSP_ABOVE;
2773 continue;
2774
2775 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2776 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002777 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 {
2779 /* Set 'eventignore' to "all". Restore the
2780 * existing option value later. */
2781 cmdmod.save_ei = vim_strsave(p_ei);
2782 set_string_option_direct((char_u *)"ei", -1,
2783 (char_u *)"all", OPT_FREE, SID_NONE);
2784 }
2785 continue;
2786 }
2787 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2788 break;
2789 cmdmod.noswapfile = TRUE;
2790 continue;
2791
2792 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2793 break;
2794 cmdmod.split |= WSP_BELOW;
2795 continue;
2796
2797 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2798 {
2799#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002800 if (!skip_only)
2801 {
2802 if (!eap->did_sandbox)
2803 ++sandbox;
2804 eap->did_sandbox = TRUE;
2805 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002806#endif
2807 continue;
2808 }
2809 if (!checkforcmd(&eap->cmd, "silent", 3))
2810 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002811 if (!skip_only)
2812 {
2813 if (eap->save_msg_silent == -1)
2814 eap->save_msg_silent = msg_silent;
2815 ++msg_silent;
2816 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2818 {
2819 /* ":silent!", but not "silent !cmd" */
2820 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002821 if (!skip_only)
2822 {
2823 ++emsg_silent;
2824 ++eap->did_esilent;
2825 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 }
2827 continue;
2828
2829 case 't': if (checkforcmd(&p, "tab", 3))
2830 {
2831 long tabnr = get_address(eap, &eap->cmd, ADDR_TABS,
2832 eap->skip, FALSE, 1);
2833 if (tabnr == MAXLNUM)
2834 cmdmod.tab = tabpage_index(curtab) + 1;
2835 else
2836 {
2837 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2838 {
2839 *errormsg = (char_u *)_(e_invrange);
2840 return FAIL;
2841 }
2842 cmdmod.tab = tabnr + 1;
2843 }
2844 eap->cmd = p;
2845 continue;
2846 }
2847 if (!checkforcmd(&eap->cmd, "topleft", 2))
2848 break;
2849 cmdmod.split |= WSP_TOP;
2850 continue;
2851
2852 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2853 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002854 if (!skip_only)
2855 {
2856 if (eap->save_msg_silent == -1)
2857 eap->save_msg_silent = msg_silent;
2858 msg_silent = 0;
2859 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 continue;
2861
2862 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2863 {
2864 cmdmod.split |= WSP_VERT;
2865 continue;
2866 }
2867 if (!checkforcmd(&p, "verbose", 4))
2868 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002869 if (!skip_only)
2870 {
2871 if (eap->verbose_save < 0)
2872 eap->verbose_save = p_verbose;
2873 if (vim_isdigit(*eap->cmd))
2874 p_verbose = atoi((char *)eap->cmd);
2875 else
2876 p_verbose = 1;
2877 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 eap->cmd = p;
2879 continue;
2880 }
2881 break;
2882 }
2883
2884 return OK;
2885}
2886
2887/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002888 * Free contents of "cmdmod".
2889 */
2890 static void
2891free_cmdmod(void)
2892{
2893 if (cmdmod.save_ei != NULL)
2894 {
2895 /* Restore 'eventignore' to the value before ":noautocmd". */
2896 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2897 OPT_FREE, SID_NONE);
2898 free_string_option(cmdmod.save_ei);
2899 }
2900
2901 if (cmdmod.filter_regmatch.regprog != NULL)
2902 vim_regfree(cmdmod.filter_regmatch.regprog);
2903}
2904
2905/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002906 * Parse the address range, if any, in "eap".
2907 * Return FAIL and set "errormsg" or return OK.
2908 */
2909 int
2910parse_cmd_address(exarg_T *eap, char_u **errormsg)
2911{
2912 int address_count = 1;
2913 linenr_T lnum;
2914
2915 // Repeat for all ',' or ';' separated addresses.
2916 for (;;)
2917 {
2918 eap->line1 = eap->line2;
2919 switch (eap->addr_type)
2920 {
2921 case ADDR_LINES:
2922 // default is current line number
2923 eap->line2 = curwin->w_cursor.lnum;
2924 break;
2925 case ADDR_WINDOWS:
2926 eap->line2 = CURRENT_WIN_NR;
2927 break;
2928 case ADDR_ARGUMENTS:
2929 eap->line2 = curwin->w_arg_idx + 1;
2930 if (eap->line2 > ARGCOUNT)
2931 eap->line2 = ARGCOUNT;
2932 break;
2933 case ADDR_LOADED_BUFFERS:
2934 case ADDR_BUFFERS:
2935 eap->line2 = curbuf->b_fnum;
2936 break;
2937 case ADDR_TABS:
2938 eap->line2 = CURRENT_TAB_NR;
2939 break;
2940 case ADDR_TABS_RELATIVE:
2941 eap->line2 = 1;
2942 break;
2943#ifdef FEAT_QUICKFIX
2944 case ADDR_QUICKFIX:
2945 eap->line2 = qf_get_cur_valid_idx(eap);
2946 break;
2947#endif
2948 }
2949 eap->cmd = skipwhite(eap->cmd);
2950 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip,
2951 eap->addr_count == 0, address_count++);
2952 if (eap->cmd == NULL) // error detected
2953 return FAIL;
2954 if (lnum == MAXLNUM)
2955 {
2956 if (*eap->cmd == '%') // '%' - all lines
2957 {
2958 ++eap->cmd;
2959 switch (eap->addr_type)
2960 {
2961 case ADDR_LINES:
2962 eap->line1 = 1;
2963 eap->line2 = curbuf->b_ml.ml_line_count;
2964 break;
2965 case ADDR_LOADED_BUFFERS:
2966 {
2967 buf_T *buf = firstbuf;
2968
2969 while (buf->b_next != NULL
2970 && buf->b_ml.ml_mfp == NULL)
2971 buf = buf->b_next;
2972 eap->line1 = buf->b_fnum;
2973 buf = lastbuf;
2974 while (buf->b_prev != NULL
2975 && buf->b_ml.ml_mfp == NULL)
2976 buf = buf->b_prev;
2977 eap->line2 = buf->b_fnum;
2978 break;
2979 }
2980 case ADDR_BUFFERS:
2981 eap->line1 = firstbuf->b_fnum;
2982 eap->line2 = lastbuf->b_fnum;
2983 break;
2984 case ADDR_WINDOWS:
2985 case ADDR_TABS:
2986 if (IS_USER_CMDIDX(eap->cmdidx))
2987 {
2988 eap->line1 = 1;
2989 eap->line2 = eap->addr_type == ADDR_WINDOWS
2990 ? LAST_WIN_NR : LAST_TAB_NR;
2991 }
2992 else
2993 {
2994 // there is no Vim command which uses '%' and
2995 // ADDR_WINDOWS or ADDR_TABS
2996 *errormsg = (char_u *)_(e_invrange);
2997 return FAIL;
2998 }
2999 break;
3000 case ADDR_TABS_RELATIVE:
3001 *errormsg = (char_u *)_(e_invrange);
3002 return FAIL;
3003 case ADDR_ARGUMENTS:
3004 if (ARGCOUNT == 0)
3005 eap->line1 = eap->line2 = 0;
3006 else
3007 {
3008 eap->line1 = 1;
3009 eap->line2 = ARGCOUNT;
3010 }
3011 break;
3012#ifdef FEAT_QUICKFIX
3013 case ADDR_QUICKFIX:
3014 eap->line1 = 1;
3015 eap->line2 = qf_get_size(eap);
3016 if (eap->line2 == 0)
3017 eap->line2 = 1;
3018 break;
3019#endif
3020 }
3021 ++eap->addr_count;
3022 }
3023 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3024 {
3025 pos_T *fp;
3026
3027 // '*' - visual area
3028 if (eap->addr_type != ADDR_LINES)
3029 {
3030 *errormsg = (char_u *)_(e_invrange);
3031 return FAIL;
3032 }
3033
3034 ++eap->cmd;
3035 if (!eap->skip)
3036 {
3037 fp = getmark('<', FALSE);
3038 if (check_mark(fp) == FAIL)
3039 return FAIL;
3040 eap->line1 = fp->lnum;
3041 fp = getmark('>', FALSE);
3042 if (check_mark(fp) == FAIL)
3043 return FAIL;
3044 eap->line2 = fp->lnum;
3045 ++eap->addr_count;
3046 }
3047 }
3048 }
3049 else
3050 eap->line2 = lnum;
3051 eap->addr_count++;
3052
3053 if (*eap->cmd == ';')
3054 {
3055 if (!eap->skip)
3056 {
3057 curwin->w_cursor.lnum = eap->line2;
3058 // don't leave the cursor on an illegal line or column
3059 check_cursor();
3060 }
3061 }
3062 else if (*eap->cmd != ',')
3063 break;
3064 ++eap->cmd;
3065 }
3066
3067 // One address given: set start and end lines.
3068 if (eap->addr_count == 1)
3069 {
3070 eap->line1 = eap->line2;
3071 // ... but only implicit: really no address given
3072 if (lnum == MAXLNUM)
3073 eap->addr_count = 0;
3074 }
3075 return OK;
3076}
3077
3078/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003079 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 * If there is a match advance "pp" to the argument and return TRUE.
3081 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003082 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003083checkforcmd(
3084 char_u **pp, /* start of command */
3085 char *cmd, /* name of command */
3086 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 int i;
3089
3090 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003091 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 break;
3093 if (i >= len && !isalpha((*pp)[i]))
3094 {
3095 *pp = skipwhite(*pp + i);
3096 return TRUE;
3097 }
3098 return FALSE;
3099}
3100
3101/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003102 * Append "cmd" to the error message in IObuff.
3103 * Takes care of limiting the length and handling 0xa0, which would be
3104 * invisible otherwise.
3105 */
3106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003108{
3109 char_u *s = cmd;
3110 char_u *d;
3111
3112 STRCAT(IObuff, ": ");
3113 d = IObuff + STRLEN(IObuff);
3114 while (*s != NUL && d - IObuff < IOSIZE - 7)
3115 {
3116 if (
3117#ifdef FEAT_MBYTE
3118 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3119#endif
3120 *s == 0xa0)
3121 {
3122 s +=
3123#ifdef FEAT_MBYTE
3124 enc_utf8 ? 2 :
3125#endif
3126 1;
3127 STRCPY(d, "<a0>");
3128 d += 4;
3129 }
3130 else
3131 MB_COPY_CHAR(s, d);
3132 }
3133 *d = NUL;
3134}
3135
3136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003138 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003140 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * Returns NULL for an ambiguous user command.
3142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003144find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
3146 int len;
3147 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003148 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 /*
3151 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003152 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 * - the 'k' command can directly be followed by any character.
3154 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003155 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003157 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 */
3159 p = eap->cmd;
3160 if (*p == 'k')
3161 {
3162 eap->cmdidx = CMD_k;
3163 ++p;
3164 }
3165 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003166 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3167 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 || p[1] == 'g'
3169 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3170 || p[1] == 'I'
3171 || (p[1] == 'r' && p[2] != 'e')))
3172 {
3173 eap->cmdidx = CMD_substitute;
3174 ++p;
3175 }
3176 else
3177 {
3178 while (ASCII_ISALPHA(*p))
3179 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003180 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003181 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003182 while (ASCII_ISALNUM(*p))
3183 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 /* check for non-alpha command */
3186 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3187 ++p;
3188 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003189 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3190 {
3191 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3192 * :delete with the 'l' flag. Same for 'p'. */
3193 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003194 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003195 break;
3196 if (i == len - 1)
3197 {
3198 --len;
3199 if (p[-1] == 'l')
3200 eap->flags |= EXFLAG_LIST;
3201 else
3202 eap->flags |= EXFLAG_PRINT;
3203 }
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003206 if (ASCII_ISLOWER(eap->cmd[0]))
3207 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003208 int c1 = eap->cmd[0];
3209 int c2 = eap->cmd[1];
3210
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003211 if (command_count != (int)CMD_SIZE)
3212 {
3213 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3214 getout(1);
3215 }
3216
3217 /* Use a precomputed index for fast look-up in cmdnames[]
3218 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003219 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3220 if (ASCII_ISLOWER(c2))
3221 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003224 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3227 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3228 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3229 (size_t)len) == 0)
3230 {
3231#ifdef FEAT_EVAL
3232 if (full != NULL
3233 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3234 *full = TRUE;
3235#endif
3236 break;
3237 }
3238
3239#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003240 /* Look for a user defined command as a last resort. Let ":Print" be
3241 * overruled by a user defined command. */
3242 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3243 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 while (ASCII_ISALNUM(*p))
3247 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 eap->cmdidx = CMD_SIZE;
3253 }
3254
3255 return p;
3256}
3257
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258#ifdef FEAT_USR_CMDS
3259/*
3260 * Search for a user command that matches "eap->cmd".
3261 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3262 * Return a pointer to just after the command.
3263 * Return NULL if there is no matching command.
3264 */
3265 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003266find_ucmd(
3267 exarg_T *eap,
3268 char_u *p, /* end of the command (possibly including count) */
3269 int *full, /* set to TRUE for a full match */
3270 expand_T *xp, /* used for completion, NULL otherwise */
3271 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272{
3273 int len = (int)(p - eap->cmd);
3274 int j, k, matchlen = 0;
3275 ucmd_T *uc;
3276 int found = FALSE;
3277 int possible = FALSE;
3278 char_u *cp, *np; /* Point into typed cmd and test name */
3279 garray_T *gap;
3280 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3281 only full match global is accepted. */
3282
3283 /*
3284 * Look for buffer-local user commands first, then global ones.
3285 */
3286 gap = &curbuf->b_ucmds;
3287 for (;;)
3288 {
3289 for (j = 0; j < gap->ga_len; ++j)
3290 {
3291 uc = USER_CMD_GA(gap, j);
3292 cp = eap->cmd;
3293 np = uc->uc_name;
3294 k = 0;
3295 while (k < len && *np != NUL && *cp++ == *np++)
3296 k++;
3297 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3298 {
3299 /* If finding a second match, the command is ambiguous. But
3300 * not if a buffer-local command wasn't a full match and a
3301 * global command is a full match. */
3302 if (k == len && found && *np != NUL)
3303 {
3304 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003306 amb_local = TRUE;
3307 }
3308
3309 if (!found || (k == len && *np == NUL))
3310 {
3311 /* If we matched up to a digit, then there could
3312 * be another command including the digit that we
3313 * should use instead.
3314 */
3315 if (k == len)
3316 found = TRUE;
3317 else
3318 possible = TRUE;
3319
3320 if (gap == &ucmds)
3321 eap->cmdidx = CMD_USER;
3322 else
3323 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003324 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003325 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003326 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327
3328# ifdef FEAT_CMDL_COMPL
3329 if (compl != NULL)
3330 *compl = uc->uc_compl;
3331# ifdef FEAT_EVAL
3332 if (xp != NULL)
3333 {
3334 xp->xp_arg = uc->uc_compl_arg;
3335 xp->xp_scriptID = uc->uc_scriptID;
3336 }
3337# endif
3338# endif
3339 /* Do not search for further abbreviations
3340 * if this is an exact match. */
3341 matchlen = k;
3342 if (k == len && *np == NUL)
3343 {
3344 if (full != NULL)
3345 *full = TRUE;
3346 amb_local = FALSE;
3347 break;
3348 }
3349 }
3350 }
3351 }
3352
3353 /* Stop if we found a full match or searched all. */
3354 if (j < gap->ga_len || gap == &ucmds)
3355 break;
3356 gap = &ucmds;
3357 }
3358
3359 /* Only found ambiguous matches. */
3360 if (amb_local)
3361 {
3362 if (xp != NULL)
3363 xp->xp_context = EXPAND_UNSUCCESSFUL;
3364 return NULL;
3365 }
3366
3367 /* The match we found may be followed immediately by a number. Move "p"
3368 * back to point to it. */
3369 if (found || possible)
3370 return p + (matchlen - len);
3371 return p;
3372}
3373#endif
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003376static struct cmdmod
3377{
3378 char *name;
3379 int minlen;
3380 int has_count; /* :123verbose :3tab */
3381} cmdmods[] = {
3382 {"aboveleft", 3, FALSE},
3383 {"belowright", 3, FALSE},
3384 {"botright", 2, FALSE},
3385 {"browse", 3, FALSE},
3386 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003387 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003388 {"hide", 3, FALSE},
3389 {"keepalt", 5, FALSE},
3390 {"keepjumps", 5, FALSE},
3391 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003392 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003393 {"leftabove", 5, FALSE},
3394 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003395 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003396 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 {"rightbelow", 6, FALSE},
3398 {"sandbox", 3, FALSE},
3399 {"silent", 3, FALSE},
3400 {"tab", 3, TRUE},
3401 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003402 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 {"verbose", 4, TRUE},
3404 {"vertical", 4, FALSE},
3405};
3406
3407/*
3408 * Return length of a command modifier (including optional count).
3409 * Return zero when it's not a modifier.
3410 */
3411 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003412modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003413{
3414 int i, j;
3415 char_u *p = cmd;
3416
3417 if (VIM_ISDIGIT(*cmd))
3418 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003419 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003420 {
3421 for (j = 0; p[j] != NUL; ++j)
3422 if (p[j] != cmdmods[i].name[j])
3423 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003424 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003425 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003426 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427 }
3428 return 0;
3429}
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431/*
3432 * Return > 0 if an Ex command "name" exists.
3433 * Return 2 if there is an exact match.
3434 * Return 3 if there is an ambiguous match.
3435 */
3436 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003437cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 exarg_T ea;
3440 int full = FALSE;
3441 int i;
3442 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003443 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003446 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 for (j = 0; name[j] != NUL; ++j)
3449 if (name[j] != cmdmods[i].name[j])
3450 break;
3451 if (name[j] == NUL && j >= cmdmods[i].minlen)
3452 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3453 }
3454
Bram Moolenaara9587612006-05-04 21:47:50 +00003455 /* Check built-in commands and user defined commands.
3456 * For ":2match" and ":3match" we need to skip the number. */
3457 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003459 p = find_command(&ea, &full);
3460 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003462 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3463 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003464 if (*skipwhite(p) != NUL)
3465 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3467}
3468#endif
3469
3470/*
3471 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3472 * we don't need/want deleted. Maybe this could be done better if we didn't
3473 * repeat all this stuff. The only problem is that they may not stay
3474 * perfectly compatible with each other, but then the command line syntax
3475 * probably won't change that much -- webb.
3476 */
3477 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003478set_one_cmd_context(
3479 expand_T *xp,
3480 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
3482 char_u *p;
3483 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484 int len = 0;
3485 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3487 int compl = EXPAND_NOTHING;
3488#endif
3489#ifdef FEAT_CMDL_COMPL
3490 int delim;
3491#endif
3492 int forceit = FALSE;
3493 int usefilter = FALSE; /* filter instead of file name */
3494
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003495 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 xp->xp_pattern = buff;
3497 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003501 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 */
3503 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3504 ;
3505 xp->xp_pattern = cmd;
3506
3507 if (*cmd == NUL)
3508 return NULL;
3509 if (*cmd == '"') /* ignore comment lines */
3510 {
3511 xp->xp_context = EXPAND_NOTHING;
3512 return NULL;
3513 }
3514
3515/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003516 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
3518 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 xp->xp_pattern = cmd;
3520 if (*cmd == NUL)
3521 return NULL;
3522 if (*cmd == '"')
3523 {
3524 xp->xp_context = EXPAND_NOTHING;
3525 return NULL;
3526 }
3527
3528 if (*cmd == '|' || *cmd == '\n')
3529 return cmd + 1; /* There's another command */
3530
3531 /*
3532 * Isolate the command and search for it in the command table.
3533 * Exceptions:
3534 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003535 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3537 */
3538 if (*cmd == 'k' && cmd[1] != 'e')
3539 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 p = cmd + 1;
3542 }
3543 else
3544 {
3545 p = cmd;
3546 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3547 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003548 /* a user command may contain digits */
3549 if (ASCII_ISUPPER(cmd[0]))
3550 while (ASCII_ISALNUM(*p) || *p == '*')
3551 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003552 /* for python 3.x: ":py3*" commands completion */
3553 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003554 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003555 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003556 while (ASCII_ISALPHA(*p) || *p == '*')
3557 ++p;
3558 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003559 /* check for non-alpha command */
3560 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3561 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 xp->xp_context = EXPAND_UNSUCCESSFUL;
3567 return NULL;
3568 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003569 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003570 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3571 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3572 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 break;
3574
3575#ifdef FEAT_USR_CMDS
3576 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3578 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579#endif
3580 }
3581
3582 /*
3583 * If the cursor is touching the command, and it ends in an alpha-numeric
3584 * character, complete the command name.
3585 */
3586 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3587 return NULL;
3588
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003589 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3592 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 p = cmd + 1;
3595 }
3596#ifdef FEAT_USR_CMDS
3597 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3598 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 ea.cmd = cmd;
3600 p = find_ucmd(&ea, p, NULL, xp,
3601# if defined(FEAT_CMDL_COMPL)
3602 &compl
3603# else
3604 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003606 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003607 if (p == NULL)
3608 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
3610#endif
3611 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 /* Not still touching the command and it was an illegal one */
3615 xp->xp_context = EXPAND_UNSUCCESSFUL;
3616 return NULL;
3617 }
3618
3619 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3620
3621 if (*p == '!') /* forced commands */
3622 {
3623 forceit = TRUE;
3624 ++p;
3625 }
3626
3627/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003628 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003630 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003631 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 arg = skipwhite(p);
3634
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 if (*arg == '>') /* append */
3638 {
3639 if (*++arg == '>')
3640 ++arg;
3641 arg = skipwhite(arg);
3642 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 ++arg;
3646 usefilter = TRUE;
3647 }
3648 }
3649
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 usefilter = forceit; /* :r! filter if forced */
3653 if (*arg == '!') /* :r !filter */
3654 {
3655 ++arg;
3656 usefilter = TRUE;
3657 }
3658 }
3659
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
3662 while (*arg == *cmd) /* allow any number of '>' or '<' */
3663 ++arg;
3664 arg = skipwhite(arg);
3665 }
3666
3667 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003668 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 /* Check if we're in the +command */
3671 p = arg + 1;
3672 arg = skip_cmd_arg(arg, FALSE);
3673
3674 /* Still touching the command after '+'? */
3675 if (*arg == NUL)
3676 return p;
3677
3678 /* Skip space(s) after +command to get to the real argument */
3679 arg = skipwhite(arg);
3680 }
3681
3682 /*
3683 * Check for '|' to separate commands and '"' to start comments.
3684 * Don't do this for ":read !cmd" and ":write !cmd".
3685 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003686 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
3688 p = arg;
3689 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003690 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 p += 2;
3692 while (*p)
3693 {
3694 if (*p == Ctrl_V)
3695 {
3696 if (p[1] != NUL)
3697 ++p;
3698 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003699 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 || *p == '|' || *p == '\n')
3701 {
3702 if (*(p - 1) != '\\')
3703 {
3704 if (*p == '|' || *p == '\n')
3705 return p + 1;
3706 return NULL; /* It's a comment */
3707 }
3708 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003709 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 }
3712
3713 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003714 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 vim_strchr((char_u *)"|\"", *arg) == NULL)
3716 return NULL;
3717
3718 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003719 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003721 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003722 while (*p && p < buff + len)
3723 {
3724 if (*p == ' ' || *p == TAB)
3725 {
3726 /* argument starts after a space */
3727 xp->xp_pattern = ++p;
3728 }
3729 else
3730 {
3731 if (*p == '\\' && *(p + 1) != NUL)
3732 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003733 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003734 }
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003737 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003739 int c;
3740 int in_quote = FALSE;
3741 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743 /*
3744 * Allow spaces within back-quotes to count as part of the argument
3745 * being expanded.
3746 */
3747 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003748 p = xp->xp_pattern;
3749 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003751#ifdef FEAT_MBYTE
3752 if (has_mbyte)
3753 c = mb_ptr2char(p);
3754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003756 c = *p;
3757 if (c == '\\' && p[1] != NUL)
3758 ++p;
3759 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
3761 if (!in_quote)
3762 {
3763 xp->xp_pattern = p;
3764 bow = p + 1;
3765 }
3766 in_quote = !in_quote;
3767 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003768 /* An argument can contain just about everything, except
3769 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003770 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003771#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003772 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003773#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003774 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003775 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003776 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003777 while (*p != NUL)
3778 {
3779#ifdef FEAT_MBYTE
3780 if (has_mbyte)
3781 c = mb_ptr2char(p);
3782 else
3783#endif
3784 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003785 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003786 break;
3787#ifdef FEAT_MBYTE
3788 if (has_mbyte)
3789 len = (*mb_ptr2len)(p);
3790 else
3791#endif
3792 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003793 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003794 }
3795 if (in_quote)
3796 bow = p;
3797 else
3798 xp->xp_pattern = p;
3799 p -= len;
3800 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003801 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803
3804 /*
3805 * If we are still inside the quotes, and we passed a space, just
3806 * expand from there.
3807 */
3808 if (bow != NULL && in_quote)
3809 xp->xp_pattern = bow;
3810 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003811
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003812 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003813 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003814 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003815#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003816 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003817#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003818 /* When still after the command name expand executables. */
3819 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003820 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 /* Check for environment variable */
3824 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003825#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 || *xp->xp_pattern == '%'
3827#endif
3828 )
3829 {
3830 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3831 if (!vim_isIDc(*p))
3832 break;
3833 if (*p == NUL)
3834 {
3835 xp->xp_context = EXPAND_ENV_VARS;
3836 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003837#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3838 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003839 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003840 compl = EXPAND_ENV_VARS;
3841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003844#if defined(FEAT_CMDL_COMPL)
3845 /* Check for user names */
3846 if (*xp->xp_pattern == '~')
3847 {
3848 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3849 ;
3850 /* Complete ~user only if it partially matches a user name.
3851 * A full match ~user<Tab> will be replaced by user's home
3852 * directory i.e. something like ~user<Tab> -> /home/user/ */
3853 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003854 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003855 {
3856 xp->xp_context = EXPAND_USER;
3857 ++xp->xp_pattern;
3858 }
3859 }
3860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862
3863/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003864 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003866 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003868 case CMD_find:
3869 case CMD_sfind:
3870 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003871 if (xp->xp_context == EXPAND_FILES)
3872 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003873 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 case CMD_cd:
3875 case CMD_chdir:
3876 case CMD_lcd:
3877 case CMD_lchdir:
3878 if (xp->xp_context == EXPAND_FILES)
3879 xp->xp_context = EXPAND_DIRECTORIES;
3880 break;
3881 case CMD_help:
3882 xp->xp_context = EXPAND_HELP;
3883 xp->xp_pattern = arg;
3884 break;
3885
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003886 /* Command modifiers: return the argument.
3887 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003889 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_belowright:
3891 case CMD_botright:
3892 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003893 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003894 case CMD_cdo:
3895 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003897 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_folddoclosed:
3899 case CMD_folddoopen:
3900 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003901 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 case CMD_keepjumps:
3903 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003904 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003905 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003907 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003909 case CMD_noautocmd:
3910 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003912 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003914 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003915 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 case CMD_topleft:
3917 case CMD_verbose:
3918 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003919 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return arg;
3921
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003922 case CMD_filter:
3923 if (*arg != NUL)
3924 arg = skip_vimgrep_pat(arg, NULL, NULL);
3925 if (arg == NULL || *arg == NUL)
3926 {
3927 xp->xp_context = EXPAND_NOTHING;
3928 return NULL;
3929 }
3930 return skipwhite(arg);
3931
Bram Moolenaar4f688582007-07-24 12:34:30 +00003932#ifdef FEAT_CMDL_COMPL
3933# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 case CMD_match:
3935 if (*arg == NUL || !ends_excmd(*arg))
3936 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003937 /* also complete "None" */
3938 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 arg = skipwhite(skiptowhite(arg));
3940 if (*arg != NUL)
3941 {
3942 xp->xp_context = EXPAND_NOTHING;
3943 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3944 }
3945 }
3946 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949/*
3950 * All completion for the +cmdline_compl feature goes here.
3951 */
3952
3953# ifdef FEAT_USR_CMDS
3954 case CMD_command:
3955 /* Check for attributes */
3956 while (*arg == '-')
3957 {
3958 arg++; /* Skip "-" */
3959 p = skiptowhite(arg);
3960 if (*p == NUL)
3961 {
3962 /* Cursor is still in the attribute */
3963 p = vim_strchr(arg, '=');
3964 if (p == NULL)
3965 {
3966 /* No "=", so complete attribute names */
3967 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3968 xp->xp_pattern = arg;
3969 return NULL;
3970 }
3971
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003972 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 * their arguments as well.
3974 */
3975 if (STRNICMP(arg, "complete", p - arg) == 0)
3976 {
3977 xp->xp_context = EXPAND_USER_COMPLETE;
3978 xp->xp_pattern = p + 1;
3979 return NULL;
3980 }
3981 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3982 {
3983 xp->xp_context = EXPAND_USER_NARGS;
3984 xp->xp_pattern = p + 1;
3985 return NULL;
3986 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003987 else if (STRNICMP(arg, "addr", p - arg) == 0)
3988 {
3989 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3990 xp->xp_pattern = p + 1;
3991 return NULL;
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 return NULL;
3994 }
3995 arg = skipwhite(p);
3996 }
3997
3998 /* After the attributes comes the new command name */
3999 p = skiptowhite(arg);
4000 if (*p == NUL)
4001 {
4002 xp->xp_context = EXPAND_USER_COMMANDS;
4003 xp->xp_pattern = arg;
4004 break;
4005 }
4006
4007 /* And finally comes a normal command */
4008 return skipwhite(p);
4009
4010 case CMD_delcommand:
4011 xp->xp_context = EXPAND_USER_COMMANDS;
4012 xp->xp_pattern = arg;
4013 break;
4014# endif
4015
4016 case CMD_global:
4017 case CMD_vglobal:
4018 delim = *arg; /* get the delimiter */
4019 if (delim)
4020 ++arg; /* skip delimiter if there is one */
4021
4022 while (arg[0] != NUL && arg[0] != delim)
4023 {
4024 if (arg[0] == '\\' && arg[1] != NUL)
4025 ++arg;
4026 ++arg;
4027 }
4028 if (arg[0] != NUL)
4029 return arg + 1;
4030 break;
4031 case CMD_and:
4032 case CMD_substitute:
4033 delim = *arg;
4034 if (delim)
4035 {
4036 /* skip "from" part */
4037 ++arg;
4038 arg = skip_regexp(arg, delim, p_magic, NULL);
4039 }
4040 /* skip "to" part */
4041 while (arg[0] != NUL && arg[0] != delim)
4042 {
4043 if (arg[0] == '\\' && arg[1] != NUL)
4044 ++arg;
4045 ++arg;
4046 }
4047 if (arg[0] != NUL) /* skip delimiter */
4048 ++arg;
4049 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4050 ++arg;
4051 if (arg[0] != NUL)
4052 return arg;
4053 break;
4054 case CMD_isearch:
4055 case CMD_dsearch:
4056 case CMD_ilist:
4057 case CMD_dlist:
4058 case CMD_ijump:
4059 case CMD_psearch:
4060 case CMD_djump:
4061 case CMD_isplit:
4062 case CMD_dsplit:
4063 arg = skipwhite(skipdigits(arg)); /* skip count */
4064 if (*arg == '/') /* Match regexp, not just whole words */
4065 {
4066 for (++arg; *arg && *arg != '/'; arg++)
4067 if (*arg == '\\' && arg[1] != NUL)
4068 arg++;
4069 if (*arg)
4070 {
4071 arg = skipwhite(arg + 1);
4072
4073 /* Check for trailing illegal characters */
4074 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4075 xp->xp_context = EXPAND_NOTHING;
4076 else
4077 return arg;
4078 }
4079 }
4080 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 case CMD_autocmd:
4083 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004085 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 case CMD_set:
4088 set_context_in_set_cmd(xp, arg, 0);
4089 break;
4090 case CMD_setglobal:
4091 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4092 break;
4093 case CMD_setlocal:
4094 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4095 break;
4096 case CMD_tag:
4097 case CMD_stag:
4098 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004099 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 case CMD_tselect:
4101 case CMD_stselect:
4102 case CMD_ptselect:
4103 case CMD_tjump:
4104 case CMD_stjump:
4105 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004106 if (*p_wop != NUL)
4107 xp->xp_context = EXPAND_TAGS_LISTFILES;
4108 else
4109 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 xp->xp_pattern = arg;
4111 break;
4112 case CMD_augroup:
4113 xp->xp_context = EXPAND_AUGROUP;
4114 xp->xp_pattern = arg;
4115 break;
4116#ifdef FEAT_SYN_HL
4117 case CMD_syntax:
4118 set_context_in_syntax_cmd(xp, arg);
4119 break;
4120#endif
4121#ifdef FEAT_EVAL
4122 case CMD_let:
4123 case CMD_if:
4124 case CMD_elseif:
4125 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004126 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 case CMD_echo:
4128 case CMD_echon:
4129 case CMD_execute:
4130 case CMD_echomsg:
4131 case CMD_echoerr:
4132 case CMD_call:
4133 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004134 case CMD_cexpr:
4135 case CMD_caddexpr:
4136 case CMD_cgetexpr:
4137 case CMD_lexpr:
4138 case CMD_laddexpr:
4139 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004140 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 break;
4142
4143 case CMD_unlet:
4144 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4145 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004146
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 xp->xp_context = EXPAND_USER_VARS;
4148 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004149
4150 if (*xp->xp_pattern == '$')
4151 {
4152 xp->xp_context = EXPAND_ENV_VARS;
4153 ++xp->xp_pattern;
4154 }
4155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 break;
4157
4158 case CMD_function:
4159 case CMD_delfunction:
4160 xp->xp_context = EXPAND_USER_FUNC;
4161 xp->xp_pattern = arg;
4162 break;
4163
4164 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004165 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 break;
4167#endif
4168 case CMD_highlight:
4169 set_context_in_highlight_cmd(xp, arg);
4170 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004171#ifdef FEAT_CSCOPE
4172 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004173 case CMD_lcscope:
4174 case CMD_scscope:
4175 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004176 break;
4177#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004178#ifdef FEAT_SIGNS
4179 case CMD_sign:
4180 set_context_in_sign_cmd(xp, arg);
4181 break;
4182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 case CMD_bdelete:
4184 case CMD_bwipeout:
4185 case CMD_bunload:
4186 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4187 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004188 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 case CMD_buffer:
4190 case CMD_sbuffer:
4191 case CMD_checktime:
4192 xp->xp_context = EXPAND_BUFFERS;
4193 xp->xp_pattern = arg;
4194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195#ifdef FEAT_USR_CMDS
4196 case CMD_USER:
4197 case CMD_USER_BUF:
4198 if (compl != EXPAND_NOTHING)
4199 {
4200 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004201 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203# ifdef FEAT_MENU
4204 if (compl == EXPAND_MENUS)
4205 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4206# endif
4207 if (compl == EXPAND_COMMANDS)
4208 return arg;
4209 if (compl == EXPAND_MAPPINGS)
4210 return set_context_in_map_cmd(xp, (char_u *)"map",
4211 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004212 /* Find start of last argument. */
4213 p = arg;
4214 while (*p)
4215 {
4216 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004217 /* argument starts after a space */
4218 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004219 else if (*p == '\\' && *(p + 1) != NUL)
4220 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004221 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 xp->xp_pattern = arg;
4224 }
4225 xp->xp_context = compl;
4226 }
4227 break;
4228#endif
4229 case CMD_map: case CMD_noremap:
4230 case CMD_nmap: case CMD_nnoremap:
4231 case CMD_vmap: case CMD_vnoremap:
4232 case CMD_omap: case CMD_onoremap:
4233 case CMD_imap: case CMD_inoremap:
4234 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004235 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004236 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004237 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004238 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004240 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 case CMD_unmap:
4242 case CMD_nunmap:
4243 case CMD_vunmap:
4244 case CMD_ounmap:
4245 case CMD_iunmap:
4246 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004247 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004248 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004249 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004250 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004252 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004253 case CMD_mapclear:
4254 case CMD_nmapclear:
4255 case CMD_vmapclear:
4256 case CMD_omapclear:
4257 case CMD_imapclear:
4258 case CMD_cmapclear:
4259 case CMD_lmapclear:
4260 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004261 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004262 case CMD_xmapclear:
4263 xp->xp_context = EXPAND_MAPCLEAR;
4264 xp->xp_pattern = arg;
4265 break;
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 case CMD_abbreviate: case CMD_noreabbrev:
4268 case CMD_cabbrev: case CMD_cnoreabbrev:
4269 case CMD_iabbrev: case CMD_inoreabbrev:
4270 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004271 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 case CMD_unabbreviate:
4273 case CMD_cunabbrev:
4274 case CMD_iunabbrev:
4275 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004276 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277#ifdef FEAT_MENU
4278 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4279 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4280 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4281 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4282 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4283 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4284 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4285 case CMD_tmenu: case CMD_tunmenu:
4286 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4287 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4288#endif
4289
4290 case CMD_colorscheme:
4291 xp->xp_context = EXPAND_COLORS;
4292 xp->xp_pattern = arg;
4293 break;
4294
4295 case CMD_compiler:
4296 xp->xp_context = EXPAND_COMPILER;
4297 xp->xp_pattern = arg;
4298 break;
4299
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004300 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004301 xp->xp_context = EXPAND_OWNSYNTAX;
4302 xp->xp_pattern = arg;
4303 break;
4304
4305 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004306 xp->xp_context = EXPAND_FILETYPE;
4307 xp->xp_pattern = arg;
4308 break;
4309
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004310 case CMD_packadd:
4311 xp->xp_context = EXPAND_PACKADD;
4312 xp->xp_pattern = arg;
4313 break;
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4316 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4317 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004318 p = skiptowhite(arg);
4319 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 xp->xp_context = EXPAND_LANGUAGE;
4322 xp->xp_pattern = arg;
4323 }
4324 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004325 {
4326 if ( STRNCMP(arg, "messages", p - arg) == 0
4327 || STRNCMP(arg, "ctype", p - arg) == 0
4328 || STRNCMP(arg, "time", p - arg) == 0)
4329 {
4330 xp->xp_context = EXPAND_LOCALES;
4331 xp->xp_pattern = skipwhite(p);
4332 }
4333 else
4334 xp->xp_context = EXPAND_NOTHING;
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 break;
4337#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004338#if defined(FEAT_PROFILE)
4339 case CMD_profile:
4340 set_context_in_profile_cmd(xp, arg);
4341 break;
4342#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004343 case CMD_behave:
4344 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004345 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004348 case CMD_messages:
4349 xp->xp_context = EXPAND_MESSAGES;
4350 xp->xp_pattern = arg;
4351 break;
4352
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004353#if defined(FEAT_CMDHIST)
4354 case CMD_history:
4355 xp->xp_context = EXPAND_HISTORY;
4356 xp->xp_pattern = arg;
4357 break;
4358#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004359#if defined(FEAT_PROFILE)
4360 case CMD_syntime:
4361 xp->xp_context = EXPAND_SYNTIME;
4362 xp->xp_pattern = arg;
4363 break;
4364#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004365
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004366 case CMD_argdelete:
4367 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4368 arg = xp->xp_pattern + 1;
4369 xp->xp_context = EXPAND_ARGLIST;
4370 xp->xp_pattern = arg;
4371 break;
4372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#endif /* FEAT_CMDL_COMPL */
4374
4375 default:
4376 break;
4377 }
4378 return NULL;
4379}
4380
4381/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004382 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 *
4384 * Backslashed delimiters after / or ? will be skipped, and commands will
4385 * not be expanded between /'s and ?'s or after "'".
4386 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004387 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 * Returns the "cmd" pointer advanced to beyond the range.
4389 */
4390 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004391skip_range(
4392 char_u *cmd,
4393 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004395 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004397 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004399 if (*cmd == '\\')
4400 {
4401 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4402 ++cmd;
4403 else
4404 break;
4405 }
4406 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
4408 if (*++cmd == NUL && ctx != NULL)
4409 *ctx = EXPAND_NOTHING;
4410 }
4411 else if (*cmd == '/' || *cmd == '?')
4412 {
4413 delim = *cmd++;
4414 while (*cmd != NUL && *cmd != delim)
4415 if (*cmd++ == '\\' && *cmd != NUL)
4416 ++cmd;
4417 if (*cmd == NUL && ctx != NULL)
4418 *ctx = EXPAND_NOTHING;
4419 }
4420 if (*cmd != NUL)
4421 ++cmd;
4422 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004423
4424 /* Skip ":" and white space. */
4425 while (*cmd == ':')
4426 cmd = skipwhite(cmd + 1);
4427
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 return cmd;
4429}
4430
4431/*
4432 * get a single EX address
4433 *
4434 * Set ptr to the next character after the part that was interpreted.
4435 * Set ptr to NULL when an error is encountered.
4436 *
4437 * Return MAXLNUM when no Ex address was found.
4438 */
4439 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004440get_address(
4441 exarg_T *eap UNUSED,
4442 char_u **ptr,
4443 int addr_type, /* flag: one of ADDR_LINES, ... */
4444 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004445 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004446 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447{
4448 int c;
4449 int i;
4450 long n;
4451 char_u *cmd;
4452 pos_T pos;
4453 pos_T *fp;
4454 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004455 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
4457 cmd = skipwhite(*ptr);
4458 lnum = MAXLNUM;
4459 do
4460 {
4461 switch (*cmd)
4462 {
4463 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004464 ++cmd;
4465 switch (addr_type)
4466 {
4467 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 lnum = curwin->w_cursor.lnum;
4469 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004471 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 break;
4473 case ADDR_ARGUMENTS:
4474 lnum = curwin->w_arg_idx + 1;
4475 break;
4476 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004477 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 lnum = curbuf->b_fnum;
4479 break;
4480 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004481 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004483 case ADDR_TABS_RELATIVE:
4484 EMSG(_(e_invrange));
4485 cmd = NULL;
4486 goto error;
4487 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004488#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004489 case ADDR_QUICKFIX:
4490 lnum = qf_get_cur_valid_idx(eap);
4491 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004492#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 }
4494 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 ++cmd;
4498 switch (addr_type)
4499 {
4500 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 lnum = curbuf->b_ml.ml_line_count;
4502 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004504 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 break;
4506 case ADDR_ARGUMENTS:
4507 lnum = ARGCOUNT;
4508 break;
4509 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004510 buf = lastbuf;
4511 while (buf->b_ml.ml_mfp == NULL)
4512 {
4513 if (buf->b_prev == NULL)
4514 break;
4515 buf = buf->b_prev;
4516 }
4517 lnum = buf->b_fnum;
4518 break;
4519 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 lnum = lastbuf->b_fnum;
4521 break;
4522 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004523 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004524 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004525 case ADDR_TABS_RELATIVE:
4526 EMSG(_(e_invrange));
4527 cmd = NULL;
4528 goto error;
4529 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004530#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004531 case ADDR_QUICKFIX:
4532 lnum = qf_get_size(eap);
4533 if (lnum == 0)
4534 lnum = 1;
4535 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004536#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 }
4538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004541 if (*++cmd == NUL)
4542 {
4543 cmd = NULL;
4544 goto error;
4545 }
4546 if (addr_type != ADDR_LINES)
4547 {
4548 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004549 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 goto error;
4551 }
4552 if (skip)
4553 ++cmd;
4554 else
4555 {
4556 /* Only accept a mark in another file when it is
4557 * used by itself: ":'M". */
4558 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4559 ++cmd;
4560 if (fp == (pos_T *)-1)
4561 /* Jumped to another file. */
4562 lnum = curwin->w_cursor.lnum;
4563 else
4564 {
4565 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
4567 cmd = NULL;
4568 goto error;
4569 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 lnum = fp->lnum;
4571 }
4572 }
4573 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 case '/':
4576 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 c = *cmd++;
4578 if (addr_type != ADDR_LINES)
4579 {
4580 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004581 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 goto error;
4583 }
4584 if (skip) /* skip "/pat/" */
4585 {
4586 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4587 if (*cmd == c)
4588 ++cmd;
4589 }
4590 else
4591 {
4592 pos = curwin->w_cursor; /* save curwin->w_cursor */
4593 /*
4594 * When '/' or '?' follows another address, start
4595 * from there.
4596 */
4597 if (lnum != MAXLNUM)
4598 curwin->w_cursor.lnum = lnum;
4599 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004600 * Start a forward search at the end of the line (unless
4601 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 * Start a backward search at the start of the line.
4603 * This makes sure we never match in the current
4604 * line, and can match anywhere in the
4605 * next/previous line.
4606 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004607 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 curwin->w_cursor.col = MAXCOL;
4609 else
4610 curwin->w_cursor.col = 0;
4611 searchcmdlen = 0;
4612 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004613 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004614 {
4615 curwin->w_cursor = pos;
4616 cmd = NULL;
4617 goto error;
4618 }
4619 lnum = curwin->w_cursor.lnum;
4620 curwin->w_cursor = pos;
4621 /* adjust command string pointer */
4622 cmd += searchcmdlen;
4623 }
4624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004627 ++cmd;
4628 if (addr_type != ADDR_LINES)
4629 {
4630 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004631 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004632 goto error;
4633 }
4634 if (*cmd == '&')
4635 i = RE_SUBST;
4636 else if (*cmd == '?' || *cmd == '/')
4637 i = RE_SEARCH;
4638 else
4639 {
4640 EMSG(_(e_backslash));
4641 cmd = NULL;
4642 goto error;
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004645 if (!skip)
4646 {
4647 /*
4648 * When search follows another address, start from
4649 * there.
4650 */
4651 if (lnum != MAXLNUM)
4652 pos.lnum = lnum;
4653 else
4654 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004656 /*
4657 * Start the search just like for the above
4658 * do_search().
4659 */
4660 if (*cmd != '?')
4661 pos.col = MAXCOL;
4662 else
4663 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004664#ifdef FEAT_VIRTUALEDIT
4665 pos.coladd = 0;
4666#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004667 if (searchit(curwin, curbuf, &pos,
4668 *cmd == '?' ? BACKWARD : FORWARD,
4669 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004670 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004671 lnum = pos.lnum;
4672 else
4673 {
4674 cmd = NULL;
4675 goto error;
4676 }
4677 }
4678 ++cmd;
4679 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
4681 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004682 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4683 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
4685
4686 for (;;)
4687 {
4688 cmd = skipwhite(cmd);
4689 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4690 break;
4691
4692 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004693 {
4694 switch (addr_type)
4695 {
4696 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004697 /* "+1" is same as ".+1" */
4698 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004699 break;
4700 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004701 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004702 break;
4703 case ADDR_ARGUMENTS:
4704 lnum = curwin->w_arg_idx + 1;
4705 break;
4706 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004707 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004708 lnum = curbuf->b_fnum;
4709 break;
4710 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004711 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004712 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004713 case ADDR_TABS_RELATIVE:
4714 lnum = 1;
4715 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004716#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004717 case ADDR_QUICKFIX:
4718 lnum = qf_get_cur_valid_idx(eap);
4719 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004720#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004721 }
4722 }
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 if (VIM_ISDIGIT(*cmd))
4725 i = '+'; /* "number" is same as "+number" */
4726 else
4727 i = *cmd++;
4728 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4729 n = 1;
4730 else
4731 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004732
4733 if (addr_type == ADDR_TABS_RELATIVE)
4734 {
4735 EMSG(_(e_invrange));
4736 cmd = NULL;
4737 goto error;
4738 }
4739 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004740 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004741 lnum = compute_buffer_local_count(
4742 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004744 {
4745#ifdef FEAT_FOLDING
4746 /* Relative line addressing, need to adjust for folded lines
4747 * now, but only do it after the first address. */
4748 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4749 && address_count >= 2)
4750 (void)hasFolding(lnum, NULL, &lnum);
4751#endif
4752 if (i == '-')
4753 lnum -= n;
4754 else
4755 lnum += n;
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758 } while (*cmd == '/' || *cmd == '?');
4759
4760error:
4761 *ptr = cmd;
4762 return lnum;
4763}
4764
4765/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004766 * Get flags from an Ex command argument.
4767 */
4768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004770{
4771 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4772 {
4773 if (*eap->arg == 'l')
4774 eap->flags |= EXFLAG_LIST;
4775 else if (*eap->arg == 'p')
4776 eap->flags |= EXFLAG_PRINT;
4777 else
4778 eap->flags |= EXFLAG_NR;
4779 eap->arg = skipwhite(eap->arg + 1);
4780 }
4781}
4782
4783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 * Function called for command which is Not Implemented. NI!
4785 */
4786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004787ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
4789 if (!eap->skip)
4790 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4791}
4792
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004793#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794/*
4795 * Function called for script command which is Not Implemented. NI!
4796 * Skips over ":perl <<EOF" constructs.
4797 */
4798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004799ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
4801 if (!eap->skip)
4802 ex_ni(eap);
4803 else
4804 vim_free(script_get(eap, eap->arg));
4805}
4806#endif
4807
4808/*
4809 * Check range in Ex command for validity.
4810 * Return NULL when valid, error message when invalid.
4811 */
4812 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004813invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004815 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if ( eap->line1 < 0
4817 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004818 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004820
4821 if (eap->argt & RANGE)
4822 {
4823 switch(eap->addr_type)
4824 {
4825 case ADDR_LINES:
4826 if (!(eap->argt & NOTADR)
4827 && eap->line2 > curbuf->b_ml.ml_line_count
4828#ifdef FEAT_DIFF
4829 + (eap->cmdidx == CMD_diffget)
4830#endif
4831 )
4832 return (char_u *)_(e_invrange);
4833 break;
4834 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004835 /* add 1 if ARGCOUNT is 0 */
4836 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837 return (char_u *)_(e_invrange);
4838 break;
4839 case ADDR_BUFFERS:
4840 if (eap->line1 < firstbuf->b_fnum
4841 || eap->line2 > lastbuf->b_fnum)
4842 return (char_u *)_(e_invrange);
4843 break;
4844 case ADDR_LOADED_BUFFERS:
4845 buf = firstbuf;
4846 while (buf->b_ml.ml_mfp == NULL)
4847 {
4848 if (buf->b_next == NULL)
4849 return (char_u *)_(e_invrange);
4850 buf = buf->b_next;
4851 }
4852 if (eap->line1 < buf->b_fnum)
4853 return (char_u *)_(e_invrange);
4854 buf = lastbuf;
4855 while (buf->b_ml.ml_mfp == NULL)
4856 {
4857 if (buf->b_prev == NULL)
4858 return (char_u *)_(e_invrange);
4859 buf = buf->b_prev;
4860 }
4861 if (eap->line2 > buf->b_fnum)
4862 return (char_u *)_(e_invrange);
4863 break;
4864 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004865 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004866 return (char_u *)_(e_invrange);
4867 break;
4868 case ADDR_TABS:
4869 if (eap->line2 > LAST_TAB_NR)
4870 return (char_u *)_(e_invrange);
4871 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004872 case ADDR_TABS_RELATIVE:
4873 /* Do nothing */
4874 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004875#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004876 case ADDR_QUICKFIX:
4877 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4878 return (char_u *)_(e_invrange);
4879 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004880#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004881 }
4882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return NULL;
4884}
4885
4886/*
4887 * Correct the range for zero line number, if required.
4888 */
4889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004890correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4893 {
4894 if (eap->line1 == 0)
4895 eap->line1 = 1;
4896 if (eap->line2 == 0)
4897 eap->line2 = 1;
4898 }
4899}
4900
Bram Moolenaar748bf032005-02-02 23:04:36 +00004901#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004902static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004903
4904/*
4905 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4906 * pattern. Otherwise return eap->arg.
4907 */
4908 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004909skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004910{
4911 char_u *p = eap->arg;
4912
Bram Moolenaara37420f2006-02-04 22:37:47 +00004913 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4914 || eap->cmdidx == CMD_vimgrepadd
4915 || eap->cmdidx == CMD_lvimgrepadd
4916 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004917 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004918 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004919 if (p == NULL)
4920 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004921 }
4922 return p;
4923}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004924
4925/*
4926 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4927 * in the command line, so that things like % get expanded.
4928 */
4929 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004931{
4932 char_u *new_cmdline;
4933 char_u *program;
4934 char_u *pos;
4935 char_u *ptr;
4936 int len;
4937 int i;
4938
4939 /*
4940 * Don't do it when ":vimgrep" is used for ":grep".
4941 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004942 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4943 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4944 || eap->cmdidx == CMD_grepadd
4945 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004946 && !grep_internal(eap->cmdidx))
4947 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004948 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4949 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004950 {
4951 if (*curbuf->b_p_gp == NUL)
4952 program = p_gp;
4953 else
4954 program = curbuf->b_p_gp;
4955 }
4956 else
4957 {
4958 if (*curbuf->b_p_mp == NUL)
4959 program = p_mp;
4960 else
4961 program = curbuf->b_p_mp;
4962 }
4963
4964 p = skipwhite(p);
4965
4966 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4967 {
4968 /* replace $* by given arguments */
4969 i = 1;
4970 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4971 ++i;
4972 len = (int)STRLEN(p);
4973 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4974 if (new_cmdline == NULL)
4975 return NULL; /* out of memory */
4976 ptr = new_cmdline;
4977 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4978 {
4979 i = (int)(pos - program);
4980 STRNCPY(ptr, program, i);
4981 STRCPY(ptr += i, p);
4982 ptr += len;
4983 program = pos + 2;
4984 }
4985 STRCPY(ptr, program);
4986 }
4987 else
4988 {
4989 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4990 if (new_cmdline == NULL)
4991 return NULL; /* out of memory */
4992 STRCPY(new_cmdline, program);
4993 STRCAT(new_cmdline, " ");
4994 STRCAT(new_cmdline, p);
4995 }
4996 msg_make(p);
4997
4998 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4999 vim_free(*cmdlinep);
5000 *cmdlinep = new_cmdline;
5001 p = new_cmdline;
5002 }
5003 return p;
5004}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005005#endif
5006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007/*
5008 * Expand file name in Ex command argument.
5009 * Return FAIL for failure, OK otherwise.
5010 */
5011 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005012expand_filename(
5013 exarg_T *eap,
5014 char_u **cmdlinep,
5015 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016{
5017 int has_wildcards; /* need to expand wildcards */
5018 char_u *repl;
5019 int srclen;
5020 char_u *p;
5021 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005022 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
Bram Moolenaar748bf032005-02-02 23:04:36 +00005024#ifdef FEAT_QUICKFIX
5025 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5026 p = skip_grep_pat(eap);
5027#else
5028 p = eap->arg;
5029#endif
5030
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 /*
5032 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5033 * the file name contains a wildcard it should not cause expanding.
5034 * (it will be expanded anyway if there is a wildcard before replacing).
5035 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005036 has_wildcards = mch_has_wildcard(p);
5037 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005039#ifdef FEAT_EVAL
5040 /* Skip over `=expr`, wildcards in it are not expanded. */
5041 if (p[0] == '`' && p[1] == '=')
5042 {
5043 p += 2;
5044 (void)skip_expr(&p);
5045 if (*p == '`')
5046 ++p;
5047 continue;
5048 }
5049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /*
5051 * Quick check if this cannot be the start of a special string.
5052 * Also removes backslash before '%', '#' and '<'.
5053 */
5054 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5055 {
5056 ++p;
5057 continue;
5058 }
5059
5060 /*
5061 * Try to find a match at this position.
5062 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005063 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5064 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (*errormsgp != NULL) /* error detected */
5066 return FAIL;
5067 if (repl == NULL) /* no match found */
5068 {
5069 p += srclen;
5070 continue;
5071 }
5072
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005073 /* Wildcards won't be expanded below, the replacement is taken
5074 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5075 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5076 {
5077 char_u *l = repl;
5078
5079 repl = expand_env_save(repl);
5080 vim_free(l);
5081 }
5082
Bram Moolenaar63b92542007-03-27 14:57:09 +00005083 /* Need to escape white space et al. with a backslash.
5084 * Don't do this for:
5085 * - replacement that already has been escaped: "##"
5086 * - shell commands (may have to use quotes instead).
5087 * - non-unix systems when there is a single argument (spaces don't
5088 * separate arguments then).
5089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005091 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 && eap->cmdidx != CMD_grep
5094 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005095 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005096 && eap->cmdidx != CMD_lgrep
5097 && eap->cmdidx != CMD_lgrepadd
5098 && eap->cmdidx != CMD_lmake
5099 && eap->cmdidx != CMD_make
5100 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101#ifndef UNIX
5102 && !(eap->argt & NOSPC)
5103#endif
5104 )
5105 {
5106 char_u *l;
5107#ifdef BACKSLASH_IN_FILENAME
5108 /* Don't escape a backslash here, because rem_backslash() doesn't
5109 * remove it later. */
5110 static char_u *nobslash = (char_u *)" \t\"|";
5111# define ESCAPE_CHARS nobslash
5112#else
5113# define ESCAPE_CHARS escape_chars
5114#endif
5115
5116 for (l = repl; *l; ++l)
5117 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5118 {
5119 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5120 if (l != NULL)
5121 {
5122 vim_free(repl);
5123 repl = l;
5124 }
5125 break;
5126 }
5127 }
5128
5129 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005130 if ((eap->usefilter || eap->cmdidx == CMD_bang
5131 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005132 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 char_u *l;
5135
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005136 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (l != NULL)
5138 {
5139 vim_free(repl);
5140 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 }
5143
5144 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5145 vim_free(repl);
5146 if (p == NULL)
5147 return FAIL;
5148 }
5149
5150 /*
5151 * One file argument: Expand wildcards.
5152 * Don't do this with ":r !command" or ":w !command".
5153 */
5154 if ((eap->argt & NOSPC) && !eap->usefilter)
5155 {
5156 /*
5157 * May do this twice:
5158 * 1. Replace environment variables.
5159 * 2. Replace any other wildcards, remove backslashes.
5160 */
5161 for (n = 1; n <= 2; ++n)
5162 {
5163 if (n == 2)
5164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 /*
5166 * Halve the number of backslashes (this is Vi compatible).
5167 * For Unix and OS/2, when wildcards are expanded, this is
5168 * done by ExpandOne() below.
5169 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005170#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (!has_wildcards)
5172#endif
5173 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175
5176 if (has_wildcards)
5177 {
5178 if (n == 1)
5179 {
5180 /*
5181 * First loop: May expand environment variables. This
5182 * can be done much faster with expand_env() than with
5183 * something else (e.g., calling a shell).
5184 * After expanding environment variables, check again
5185 * if there are still wildcards present.
5186 */
5187 if (vim_strchr(eap->arg, '$') != NULL
5188 || vim_strchr(eap->arg, '~') != NULL)
5189 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005190 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005191 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 has_wildcards = mch_has_wildcard(NameBuff);
5193 p = NameBuff;
5194 }
5195 else
5196 p = NULL;
5197 }
5198 else /* n == 2 */
5199 {
5200 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005201 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 ExpandInit(&xpc);
5204 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005205 if (p_wic)
5206 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005208 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (p == NULL)
5210 return FAIL;
5211 }
5212 if (p != NULL)
5213 {
5214 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5215 p, cmdlinep);
5216 if (n == 2) /* p came from ExpandOne() */
5217 vim_free(p);
5218 }
5219 }
5220 }
5221 }
5222 return OK;
5223}
5224
5225/*
5226 * Replace part of the command line, keeping eap->cmd, eap->arg and
5227 * eap->nextcmd correct.
5228 * "src" points to the part that is to be replaced, of length "srclen".
5229 * "repl" is the replacement string.
5230 * Returns a pointer to the character after the replaced string.
5231 * Returns NULL for failure.
5232 */
5233 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005234repl_cmdline(
5235 exarg_T *eap,
5236 char_u *src,
5237 int srclen,
5238 char_u *repl,
5239 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
5241 int len;
5242 int i;
5243 char_u *new_cmdline;
5244
5245 /*
5246 * The new command line is build in new_cmdline[].
5247 * First allocate it.
5248 * Careful: a "+cmd" argument may have been NUL terminated.
5249 */
5250 len = (int)STRLEN(repl);
5251 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005252 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5254 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5255 return NULL; /* out of memory! */
5256
5257 /*
5258 * Copy the stuff before the expanded part.
5259 * Copy the expanded stuff.
5260 * Copy what came after the expanded part.
5261 * Copy the next commands, if there are any.
5262 */
5263 i = (int)(src - *cmdlinep); /* length of part before match */
5264 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 mch_memmove(new_cmdline + i, repl, (size_t)len);
5267 i += len; /* remember the end of the string */
5268 STRCPY(new_cmdline + i, src + srclen);
5269 src = new_cmdline + i; /* remember where to continue */
5270
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005271 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 i = (int)STRLEN(new_cmdline) + 1;
5274 STRCPY(new_cmdline + i, eap->nextcmd);
5275 eap->nextcmd = new_cmdline + i;
5276 }
5277 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5278 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5279 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5280 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5281 vim_free(*cmdlinep);
5282 *cmdlinep = new_cmdline;
5283
5284 return src;
5285}
5286
5287/*
5288 * Check for '|' to separate commands and '"' to start comments.
5289 */
5290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005291separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293 char_u *p;
5294
Bram Moolenaar86b68352004-12-27 21:59:20 +00005295#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005296 p = skip_grep_pat(eap);
5297#else
5298 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005299#endif
5300
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005301 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
5303 if (*p == Ctrl_V)
5304 {
5305 if (eap->argt & (USECTRLV | XFILE))
5306 ++p; /* skip CTRL-V and next char */
5307 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005308 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005309 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 if (*p == NUL) /* stop at NUL after CTRL-V */
5311 break;
5312 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005313
5314#ifdef FEAT_EVAL
5315 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005316 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005317 {
5318 p += 2;
5319 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005320 }
5321#endif
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 /* Check for '"': start of comment or '|': next command */
5324 /* :@" and :*" do not start a comment!
5325 * :redir @" doesn't either. */
5326 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5327 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5328 || p != eap->arg)
5329 && (eap->cmdidx != CMD_redir
5330 || p != eap->arg + 1 || p[-1] != '@'))
5331 || *p == '|' || *p == '\n')
5332 {
5333 /*
5334 * We remove the '\' before the '|', unless USECTRLV is used
5335 * AND 'b' is present in 'cpoptions'.
5336 */
5337 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5338 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5339 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005340 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 --p;
5342 }
5343 else
5344 {
5345 eap->nextcmd = check_nextcmd(p);
5346 *p = NUL;
5347 break;
5348 }
5349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5353 del_trailing_spaces(eap->arg);
5354}
5355
5356/*
5357 * get + command from ex argument
5358 */
5359 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005360getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361{
5362 char_u *arg = *argp;
5363 char_u *command = NULL;
5364
5365 if (*arg == '+') /* +[command] */
5366 {
5367 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005368 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 command = dollar_command;
5370 else
5371 {
5372 command = arg;
5373 arg = skip_cmd_arg(command, TRUE);
5374 if (*arg != NUL)
5375 *arg++ = NUL; /* terminate command with NUL */
5376 }
5377
5378 arg = skipwhite(arg); /* skip over spaces */
5379 *argp = arg;
5380 }
5381 return command;
5382}
5383
5384/*
5385 * Find end of "+command" argument. Skip over "\ " and "\\".
5386 */
5387 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005388skip_cmd_arg(
5389 char_u *p,
5390 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 while (*p && !vim_isspace(*p))
5393 {
5394 if (*p == '\\' && p[1] != NUL)
5395 {
5396 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005397 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 else
5399 ++p;
5400 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005401 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 return p;
5404}
5405
Bram Moolenaar86676c92018-04-05 18:56:48 +02005406#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005407 int
5408get_bad_opt(char_u *p, exarg_T *eap)
5409{
5410 if (STRICMP(p, "keep") == 0)
5411 eap->bad_char = BAD_KEEP;
5412 else if (STRICMP(p, "drop") == 0)
5413 eap->bad_char = BAD_DROP;
5414 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5415 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005416 else
5417 return FAIL;
5418 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005419}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005420#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005421
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422/*
5423 * Get "++opt=arg" argument.
5424 * Return FAIL or OK.
5425 */
5426 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005427getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 char_u *arg = eap->arg + 2;
5430 int *pp = NULL;
5431#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005432 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 char_u *p;
5434#endif
5435
5436 /* ":edit ++[no]bin[ary] file" */
5437 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5438 {
5439 if (*arg == 'n')
5440 {
5441 arg += 2;
5442 eap->force_bin = FORCE_NOBIN;
5443 }
5444 else
5445 eap->force_bin = FORCE_BIN;
5446 if (!checkforcmd(&arg, "binary", 3))
5447 return FAIL;
5448 eap->arg = skipwhite(arg);
5449 return OK;
5450 }
5451
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005452 /* ":read ++edit file" */
5453 if (STRNCMP(arg, "edit", 4) == 0)
5454 {
5455 eap->read_edit = TRUE;
5456 eap->arg = skipwhite(arg + 4);
5457 return OK;
5458 }
5459
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 if (STRNCMP(arg, "ff", 2) == 0)
5461 {
5462 arg += 2;
5463 pp = &eap->force_ff;
5464 }
5465 else if (STRNCMP(arg, "fileformat", 10) == 0)
5466 {
5467 arg += 10;
5468 pp = &eap->force_ff;
5469 }
5470#ifdef FEAT_MBYTE
5471 else if (STRNCMP(arg, "enc", 3) == 0)
5472 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005473 if (STRNCMP(arg, "encoding", 8) == 0)
5474 arg += 8;
5475 else
5476 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 pp = &eap->force_enc;
5478 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005479 else if (STRNCMP(arg, "bad", 3) == 0)
5480 {
5481 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005482 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#endif
5485
5486 if (pp == NULL || *arg != '=')
5487 return FAIL;
5488
5489 ++arg;
5490 *pp = (int)(arg - eap->cmd);
5491 arg = skip_cmd_arg(arg, FALSE);
5492 eap->arg = skipwhite(arg);
5493 *arg = NUL;
5494
5495#ifdef FEAT_MBYTE
5496 if (pp == &eap->force_ff)
5497 {
5498#endif
5499 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5500 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005501 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#ifdef FEAT_MBYTE
5503 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005504 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 /* Make 'fileencoding' lower case. */
5507 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5508 *p = TOLOWER_ASC(*p);
5509 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005510 else
5511 {
5512 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5513 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005514 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005515 return FAIL;
5516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#endif
5518
5519 return OK;
5520}
5521
5522/*
5523 * ":abbreviate" and friends.
5524 */
5525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005526ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 do_exmap(eap, TRUE); /* almost the same as mapping */
5529}
5530
5531/*
5532 * ":map" and friends.
5533 */
5534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 /*
5538 * If we are sourcing .exrc or .vimrc in current directory we
5539 * print the mappings for security reasons.
5540 */
5541 if (secure)
5542 {
5543 secure = 2;
5544 msg_outtrans(eap->cmd);
5545 msg_putchar('\n');
5546 }
5547 do_exmap(eap, FALSE);
5548}
5549
5550/*
5551 * ":unmap" and friends.
5552 */
5553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005554ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
5556 do_exmap(eap, FALSE);
5557}
5558
5559/*
5560 * ":mapclear" and friends.
5561 */
5562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5566}
5567
5568/*
5569 * ":abclear" and friends.
5570 */
5571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
5574 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5575}
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005581 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 * directory for security reasons.
5583 */
5584 if (secure)
5585 {
5586 secure = 2;
5587 eap->errmsg = e_curdir;
5588 }
5589 else if (eap->cmdidx == CMD_autocmd)
5590 do_autocmd(eap->arg, eap->forceit);
5591 else
5592 do_augroup(eap->arg, eap->forceit);
5593}
5594
5595/*
5596 * ":doautocmd": Apply the automatic commands to the current buffer.
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005601 char_u *arg = eap->arg;
5602 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005603 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005604
Bram Moolenaar1610d052016-06-09 22:53:01 +02005605 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5606 /* Only when there is no <nomodeline>. */
5607 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005608 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611/*
5612 * :[N]bunload[!] [N] [bufname] unload buffer
5613 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5614 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5615 */
5616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 eap->errmsg = do_bufdel(
5620 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5621 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5622 : DOBUF_UNLOAD, eap->arg,
5623 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5624}
5625
5626/*
5627 * :[N]buffer [N] to buffer N
5628 * :[N]sbuffer [N] to buffer N
5629 */
5630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005631ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 if (*eap->arg)
5634 eap->errmsg = e_trailing;
5635 else
5636 {
5637 if (eap->addr_count == 0) /* default is current buffer */
5638 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5639 else
5640 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005641 if (eap->do_ecmd_cmd != NULL)
5642 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
5644}
5645
5646/*
5647 * :[N]bmodified [N] to next mod. buffer
5648 * :[N]sbmodified [N] to next mod. buffer
5649 */
5650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005651ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005654 if (eap->do_ecmd_cmd != NULL)
5655 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656}
5657
5658/*
5659 * :[N]bnext [N] to next buffer
5660 * :[N]sbnext [N] split and to next buffer
5661 */
5662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
5665 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005666 if (eap->do_ecmd_cmd != NULL)
5667 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668}
5669
5670/*
5671 * :[N]bNext [N] to previous buffer
5672 * :[N]bprevious [N] to previous buffer
5673 * :[N]sbNext [N] split and to previous buffer
5674 * :[N]sbprevious [N] split and to previous buffer
5675 */
5676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
5679 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005680 if (eap->do_ecmd_cmd != NULL)
5681 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682}
5683
5684/*
5685 * :brewind to first buffer
5686 * :bfirst to first buffer
5687 * :sbrewind split and to first buffer
5688 * :sbfirst split and to first buffer
5689 */
5690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005694 if (eap->do_ecmd_cmd != NULL)
5695 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696}
5697
5698/*
5699 * :blast to last buffer
5700 * :sblast split and to last buffer
5701 */
5702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005703ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
5705 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005706 if (eap->do_ecmd_cmd != NULL)
5707 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
5710 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005711ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 return (c == NUL || c == '|' || c == '"' || c == '\n');
5714}
5715
5716#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5717 || defined(PROTO)
5718/*
5719 * Return the next command, after the first '|' or '\n'.
5720 * Return NULL if not found.
5721 */
5722 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005723find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 while (*p != '|' && *p != '\n')
5726 {
5727 if (*p == NUL)
5728 return NULL;
5729 ++p;
5730 }
5731 return (p + 1);
5732}
5733#endif
5734
5735/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005736 * Check if *p is a separator between Ex commands, skipping over white space.
5737 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 */
5739 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005742 char_u *s = skipwhite(p);
5743
5744 if (*s == '|' || *s == '\n')
5745 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 else
5747 return NULL;
5748}
5749
5750/*
5751 * - if there are more files to edit
5752 * - and this is the last window
5753 * - and forceit not used
5754 * - and not repeated twice on a row
5755 * return FAIL and give error message if 'message' TRUE
5756 * return OK otherwise
5757 */
5758 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005759check_more(
5760 int message, /* when FALSE check only, no messages */
5761 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
5763 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5764
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005765 if (!forceit && only_one_window()
5766 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
5768 if (message)
5769 {
5770#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5771 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5772 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005773 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005775 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5776 NGETTEXT("%d more file to edit. Quit anyway?",
5777 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5779 return OK;
5780 return FAIL;
5781 }
5782#endif
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005783 EMSGN(NGETTEXT("E173: %ld more file to edit",
5784 "E173: %ld more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 quitmore = 2; /* next try to quit is allowed */
5786 }
5787 return FAIL;
5788 }
5789 return OK;
5790}
5791
5792#ifdef FEAT_CMDL_COMPL
5793/*
5794 * Function given to ExpandGeneric() to obtain the list of command names.
5795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005797get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 if (idx >= (int)CMD_SIZE)
5800# ifdef FEAT_USR_CMDS
5801 return get_user_command_name(idx);
5802# else
5803 return NULL;
5804# endif
5805 return cmdnames[idx].cmd_name;
5806}
5807#endif
5808
5809#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005810static 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);
5811static void uc_list(char_u *name, size_t name_len);
5812static 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);
5813static char_u *uc_split_args(char_u *arg, size_t *lenp);
5814static 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 +00005815
5816 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005817uc_add_command(
5818 char_u *name,
5819 size_t name_len,
5820 char_u *rep,
5821 long argt,
5822 long def,
5823 int flags,
5824 int compl,
5825 char_u *compl_arg,
5826 int addr_type,
5827 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
5829 ucmd_T *cmd = NULL;
5830 char_u *p;
5831 int i;
5832 int cmp = 1;
5833 char_u *rep_buf = NULL;
5834 garray_T *gap;
5835
Bram Moolenaar9c102382006-05-03 21:26:49 +00005836 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 if (rep_buf == NULL)
5838 {
5839 /* Can't replace termcodes - try using the string as is */
5840 rep_buf = vim_strsave(rep);
5841
5842 /* Give up if out of memory */
5843 if (rep_buf == NULL)
5844 return FAIL;
5845 }
5846
5847 /* get address of growarray: global or in curbuf */
5848 if (flags & UC_BUFFER)
5849 {
5850 gap = &curbuf->b_ucmds;
5851 if (gap->ga_itemsize == 0)
5852 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5853 }
5854 else
5855 gap = &ucmds;
5856
5857 /* Search for the command in the already defined commands. */
5858 for (i = 0; i < gap->ga_len; ++i)
5859 {
5860 size_t len;
5861
5862 cmd = USER_CMD_GA(gap, i);
5863 len = STRLEN(cmd->uc_name);
5864 cmp = STRNCMP(name, cmd->uc_name, name_len);
5865 if (cmp == 0)
5866 {
5867 if (name_len < len)
5868 cmp = -1;
5869 else if (name_len > len)
5870 cmp = 1;
5871 }
5872
5873 if (cmp == 0)
5874 {
5875 if (!force)
5876 {
5877 EMSG(_("E174: Command already exists: add ! to replace it"));
5878 goto fail;
5879 }
5880
Bram Moolenaard23a8232018-02-10 18:45:26 +01005881 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005882#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005883 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 break;
5886 }
5887
5888 /* Stop as soon as we pass the name to add */
5889 if (cmp < 0)
5890 break;
5891 }
5892
5893 /* Extend the array unless we're replacing an existing command */
5894 if (cmp != 0)
5895 {
5896 if (ga_grow(gap, 1) != OK)
5897 goto fail;
5898 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5899 goto fail;
5900
5901 cmd = USER_CMD_GA(gap, i);
5902 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5903
5904 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
5906 cmd->uc_name = p;
5907 }
5908
5909 cmd->uc_rep = rep_buf;
5910 cmd->uc_argt = argt;
5911 cmd->uc_def = def;
5912 cmd->uc_compl = compl;
5913#ifdef FEAT_EVAL
5914 cmd->uc_scriptID = current_SID;
5915# ifdef FEAT_CMDL_COMPL
5916 cmd->uc_compl_arg = compl_arg;
5917# endif
5918#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005919 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 return OK;
5922
5923fail:
5924 vim_free(rep_buf);
5925#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5926 vim_free(compl_arg);
5927#endif
5928 return FAIL;
5929}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005932#if defined(FEAT_USR_CMDS)
5933static struct
5934{
5935 int expand;
5936 char *name;
5937} addr_type_complete[] =
5938{
5939 {ADDR_ARGUMENTS, "arguments"},
5940 {ADDR_LINES, "lines"},
5941 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5942 {ADDR_TABS, "tabs"},
5943 {ADDR_BUFFERS, "buffers"},
5944 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005945 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005946 {-1, NULL}
5947};
5948#endif
5949
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005950#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951/*
5952 * List of names for completion for ":command" with the EXPAND_ flag.
5953 * Must be alphabetical for completion.
5954 */
5955static struct
5956{
5957 int expand;
5958 char *name;
5959} command_complete[] =
5960{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005961 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005963 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005965 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005967 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005968#if defined(FEAT_CSCOPE)
5969 {EXPAND_CSCOPE, "cscope"},
5970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5972 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005973 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#endif
5975 {EXPAND_DIRECTORIES, "dir"},
5976 {EXPAND_ENV_VARS, "environment"},
5977 {EXPAND_EVENTS, "event"},
5978 {EXPAND_EXPRESSION, "expression"},
5979 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005980 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005981 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {EXPAND_FUNCTIONS, "function"},
5983 {EXPAND_HELP, "help"},
5984 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005985#if defined(FEAT_CMDHIST)
5986 {EXPAND_HISTORY, "history"},
5987#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005988#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005989 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005990 {EXPAND_LOCALES, "locale"},
5991#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005992 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {EXPAND_MAPPINGS, "mapping"},
5994 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005995 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005996 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005997#if defined(FEAT_PROFILE)
5998 {EXPAND_SYNTIME, "syntime"},
5999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006001 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006002 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006003#if defined(FEAT_SIGNS)
6004 {EXPAND_SIGN, "sign"},
6005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 {EXPAND_TAGS, "tag"},
6007 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006008 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {EXPAND_USER_VARS, "var"},
6010 {0, NULL}
6011};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006014#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006016uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
6018 int i, j;
6019 int found = FALSE;
6020 ucmd_T *cmd;
6021 int len;
6022 long a;
6023 garray_T *gap;
6024
6025 gap = &curbuf->b_ucmds;
6026 for (;;)
6027 {
6028 for (i = 0; i < gap->ga_len; ++i)
6029 {
6030 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006031 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
Bram Moolenaard29459b2016-08-26 22:29:11 +02006033 /* Skip commands which don't match the requested prefix and
6034 * commands filtered out. */
6035 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6036 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 continue;
6038
6039 /* Put out the title first time */
6040 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006041 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 found = TRUE;
6043 msg_putchar('\n');
6044 if (got_int)
6045 break;
6046
6047 /* Special cases */
6048 msg_putchar(a & BANG ? '!' : ' ');
6049 msg_putchar(a & REGSTR ? '"' : ' ');
6050 msg_putchar(gap != &ucmds ? 'b' : ' ');
6051 msg_putchar(' ');
6052
Bram Moolenaar8820b482017-03-16 17:23:31 +01006053 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 len = (int)STRLEN(cmd->uc_name) + 4;
6055
6056 do {
6057 msg_putchar(' ');
6058 ++len;
6059 } while (len < 16);
6060
6061 len = 0;
6062
6063 /* Arguments */
6064 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6065 {
6066 case 0: IObuff[len++] = '0'; break;
6067 case (EXTRA): IObuff[len++] = '*'; break;
6068 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6069 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6070 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6071 }
6072
6073 do {
6074 IObuff[len++] = ' ';
6075 } while (len < 5);
6076
6077 /* Range */
6078 if (a & (RANGE|COUNT))
6079 {
6080 if (a & COUNT)
6081 {
6082 /* -count=N */
6083 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6084 len += (int)STRLEN(IObuff + len);
6085 }
6086 else if (a & DFLALL)
6087 IObuff[len++] = '%';
6088 else if (cmd->uc_def >= 0)
6089 {
6090 /* -range=N */
6091 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6092 len += (int)STRLEN(IObuff + len);
6093 }
6094 else
6095 IObuff[len++] = '.';
6096 }
6097
6098 do {
6099 IObuff[len++] = ' ';
6100 } while (len < 11);
6101
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006102 /* Address Type */
6103 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6104 if (addr_type_complete[j].expand != ADDR_LINES
6105 && addr_type_complete[j].expand == cmd->uc_addr_type)
6106 {
6107 STRCPY(IObuff + len, addr_type_complete[j].name);
6108 len += (int)STRLEN(IObuff + len);
6109 break;
6110 }
6111
6112 do {
6113 IObuff[len++] = ' ';
6114 } while (len < 21);
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 /* Completion */
6117 for (j = 0; command_complete[j].expand != 0; ++j)
6118 if (command_complete[j].expand == cmd->uc_compl)
6119 {
6120 STRCPY(IObuff + len, command_complete[j].name);
6121 len += (int)STRLEN(IObuff + len);
6122 break;
6123 }
6124
6125 do {
6126 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006127 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129 IObuff[len] = '\0';
6130 msg_outtrans(IObuff);
6131
6132 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006133#ifdef FEAT_EVAL
6134 if (p_verbose > 0)
6135 last_set_msg(cmd->uc_scriptID);
6136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 out_flush();
6138 ui_breakcheck();
6139 if (got_int)
6140 break;
6141 }
6142 if (gap == &ucmds || i < gap->ga_len)
6143 break;
6144 gap = &ucmds;
6145 }
6146
6147 if (!found)
6148 MSG(_("No user-defined commands found"));
6149}
6150
6151 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006152uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6155 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6156 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6157 0xb9, 0x7f, 0};
6158 int i;
6159
6160 for (i = 0; fcmd[i]; ++i)
6161 IObuff[i] = fcmd[i] - 0x40;
6162 IObuff[i] = 0;
6163 return IObuff;
6164}
6165
6166 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006167uc_scan_attr(
6168 char_u *attr,
6169 size_t len,
6170 long *argt,
6171 long *def,
6172 int *flags,
6173 int *compl,
6174 char_u **compl_arg,
6175 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 char_u *p;
6178
6179 if (len == 0)
6180 {
6181 EMSG(_("E175: No attribute specified"));
6182 return FAIL;
6183 }
6184
6185 /* First, try the simple attributes (no arguments) */
6186 if (STRNICMP(attr, "bang", len) == 0)
6187 *argt |= BANG;
6188 else if (STRNICMP(attr, "buffer", len) == 0)
6189 *flags |= UC_BUFFER;
6190 else if (STRNICMP(attr, "register", len) == 0)
6191 *argt |= REGSTR;
6192 else if (STRNICMP(attr, "bar", len) == 0)
6193 *argt |= TRLBAR;
6194 else
6195 {
6196 int i;
6197 char_u *val = NULL;
6198 size_t vallen = 0;
6199 size_t attrlen = len;
6200
6201 /* Look for the attribute name - which is the part before any '=' */
6202 for (i = 0; i < (int)len; ++i)
6203 {
6204 if (attr[i] == '=')
6205 {
6206 val = &attr[i + 1];
6207 vallen = len - i - 1;
6208 attrlen = i;
6209 break;
6210 }
6211 }
6212
6213 if (STRNICMP(attr, "nargs", attrlen) == 0)
6214 {
6215 if (vallen == 1)
6216 {
6217 if (*val == '0')
6218 /* Do nothing - this is the default */;
6219 else if (*val == '1')
6220 *argt |= (EXTRA | NOSPC | NEEDARG);
6221 else if (*val == '*')
6222 *argt |= EXTRA;
6223 else if (*val == '?')
6224 *argt |= (EXTRA | NOSPC);
6225 else if (*val == '+')
6226 *argt |= (EXTRA | NEEDARG);
6227 else
6228 goto wrong_nargs;
6229 }
6230 else
6231 {
6232wrong_nargs:
6233 EMSG(_("E176: Invalid number of arguments"));
6234 return FAIL;
6235 }
6236 }
6237 else if (STRNICMP(attr, "range", attrlen) == 0)
6238 {
6239 *argt |= RANGE;
6240 if (vallen == 1 && *val == '%')
6241 *argt |= DFLALL;
6242 else if (val != NULL)
6243 {
6244 p = val;
6245 if (*def >= 0)
6246 {
6247two_count:
6248 EMSG(_("E177: Count cannot be specified twice"));
6249 return FAIL;
6250 }
6251
6252 *def = getdigits(&p);
6253 *argt |= (ZEROR | NOTADR);
6254
6255 if (p != val + vallen || vallen == 0)
6256 {
6257invalid_count:
6258 EMSG(_("E178: Invalid default value for count"));
6259 return FAIL;
6260 }
6261 }
6262 }
6263 else if (STRNICMP(attr, "count", attrlen) == 0)
6264 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006265 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 if (val != NULL)
6268 {
6269 p = val;
6270 if (*def >= 0)
6271 goto two_count;
6272
6273 *def = getdigits(&p);
6274
6275 if (p != val + vallen)
6276 goto invalid_count;
6277 }
6278
6279 if (*def < 0)
6280 *def = 0;
6281 }
6282 else if (STRNICMP(attr, "complete", attrlen) == 0)
6283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (val == NULL)
6285 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006286 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 return FAIL;
6288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006290 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6291 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006294 else if (STRNICMP(attr, "addr", attrlen) == 0)
6295 {
6296 *argt |= RANGE;
6297 if (val == NULL)
6298 {
6299 EMSG(_("E179: argument required for -addr"));
6300 return FAIL;
6301 }
6302 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6303 == FAIL)
6304 return FAIL;
6305 if (addr_type_arg != ADDR_LINES)
6306 *argt |= (ZEROR | NOTADR) ;
6307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 else
6309 {
6310 char_u ch = attr[len];
6311 attr[len] = '\0';
6312 EMSG2(_("E181: Invalid attribute: %s"), attr);
6313 attr[len] = ch;
6314 return FAIL;
6315 }
6316 }
6317
6318 return OK;
6319}
6320
Bram Moolenaara850a712009-01-28 14:42:59 +00006321/*
6322 * ":command ..."
6323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 char_u *name;
6328 char_u *end;
6329 char_u *p;
6330 long argt = 0;
6331 long def = -1;
6332 int flags = 0;
6333 int compl = EXPAND_NOTHING;
6334 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006335 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006337 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 p = eap->arg;
6340
6341 /* Check for attributes */
6342 while (*p == '-')
6343 {
6344 ++p;
6345 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006346 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6347 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 == FAIL)
6349 return;
6350 p = skipwhite(end);
6351 }
6352
6353 /* Get the name (if any) and skip to the following argument */
6354 name = p;
6355 if (ASCII_ISALPHA(*p))
6356 while (ASCII_ISALNUM(*p))
6357 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006358 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {
6360 EMSG(_("E182: Invalid command name"));
6361 return;
6362 }
6363 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006364 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366 /* If there is nothing after the name, and no attributes were specified,
6367 * we are listing commands
6368 */
6369 p = skipwhite(end);
6370 if (!has_attr && ends_excmd(*p))
6371 {
6372 uc_list(name, end - name);
6373 }
6374 else if (!ASCII_ISUPPER(*name))
6375 {
6376 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6377 return;
6378 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006379 else if ((name_len == 1 && *name == 'X')
6380 || (name_len <= 4
6381 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6382 {
6383 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6384 return;
6385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 else
6387 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006388 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389}
6390
6391/*
6392 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * Clear all user commands, global and for current buffer.
6394 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006395 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006396ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 uc_clear(&ucmds);
6399 uc_clear(&curbuf->b_ucmds);
6400}
6401
6402/*
6403 * Clear all user commands for "gap".
6404 */
6405 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006406uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
6408 int i;
6409 ucmd_T *cmd;
6410
6411 for (i = 0; i < gap->ga_len; ++i)
6412 {
6413 cmd = USER_CMD_GA(gap, i);
6414 vim_free(cmd->uc_name);
6415 vim_free(cmd->uc_rep);
6416# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6417 vim_free(cmd->uc_compl_arg);
6418# endif
6419 }
6420 ga_clear(gap);
6421}
6422
6423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006424ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 int i = 0;
6427 ucmd_T *cmd = NULL;
6428 int cmp = -1;
6429 garray_T *gap;
6430
6431 gap = &curbuf->b_ucmds;
6432 for (;;)
6433 {
6434 for (i = 0; i < gap->ga_len; ++i)
6435 {
6436 cmd = USER_CMD_GA(gap, i);
6437 cmp = STRCMP(eap->arg, cmd->uc_name);
6438 if (cmp <= 0)
6439 break;
6440 }
6441 if (gap == &ucmds || cmp == 0)
6442 break;
6443 gap = &ucmds;
6444 }
6445
6446 if (cmp != 0)
6447 {
6448 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6449 return;
6450 }
6451
6452 vim_free(cmd->uc_name);
6453 vim_free(cmd->uc_rep);
6454# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6455 vim_free(cmd->uc_compl_arg);
6456# endif
6457
6458 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
6460 if (i < gap->ga_len)
6461 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6462}
6463
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006464/*
6465 * split and quote args for <f-args>
6466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006468uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 char_u *buf;
6471 char_u *p;
6472 char_u *q;
6473 int len;
6474
6475 /* Precalculate length */
6476 p = arg;
6477 len = 2; /* Initial and final quotes */
6478
6479 while (*p)
6480 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006481 if (p[0] == '\\' && p[1] == '\\')
6482 {
6483 len += 2;
6484 p += 2;
6485 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006486 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {
6488 len += 1;
6489 p += 2;
6490 }
6491 else if (*p == '\\' || *p == '"')
6492 {
6493 len += 2;
6494 p += 1;
6495 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006496 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 {
6498 p = skipwhite(p);
6499 if (*p == NUL)
6500 break;
6501 len += 3; /* "," */
6502 }
6503 else
6504 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006505#ifdef FEAT_MBYTE
6506 int charlen = (*mb_ptr2len)(p);
6507 len += charlen;
6508 p += charlen;
6509#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 ++len;
6511 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 }
6514 }
6515
6516 buf = alloc(len + 1);
6517 if (buf == NULL)
6518 {
6519 *lenp = 0;
6520 return buf;
6521 }
6522
6523 p = arg;
6524 q = buf;
6525 *q++ = '"';
6526 while (*p)
6527 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006528 if (p[0] == '\\' && p[1] == '\\')
6529 {
6530 *q++ = '\\';
6531 *q++ = '\\';
6532 p += 2;
6533 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006534 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 *q++ = p[1];
6537 p += 2;
6538 }
6539 else if (*p == '\\' || *p == '"')
6540 {
6541 *q++ = '\\';
6542 *q++ = *p++;
6543 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006544 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
6546 p = skipwhite(p);
6547 if (*p == NUL)
6548 break;
6549 *q++ = '"';
6550 *q++ = ',';
6551 *q++ = '"';
6552 }
6553 else
6554 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006555 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557 }
6558 *q++ = '"';
6559 *q = 0;
6560
6561 *lenp = len;
6562 return buf;
6563}
6564
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006565 static size_t
6566add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6567{
6568 size_t result;
6569
6570 result = STRLEN(mod_str);
6571 if (*multi_mods)
6572 result += 1;
6573 if (buf != NULL)
6574 {
6575 if (*multi_mods)
6576 STRCAT(buf, " ");
6577 STRCAT(buf, mod_str);
6578 }
6579
6580 *multi_mods = 1;
6581
6582 return result;
6583}
6584
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585/*
6586 * Check for a <> code in a user command.
6587 * "code" points to the '<'. "len" the length of the <> (inclusive).
6588 * "buf" is where the result is to be added.
6589 * "split_buf" points to a buffer used for splitting, caller should free it.
6590 * "split_len" is the length of what "split_buf" contains.
6591 * Returns the length of the replacement, which has been added to "buf".
6592 * Returns -1 if there was no match, and only the "<" has been copied.
6593 */
6594 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006595uc_check_code(
6596 char_u *code,
6597 size_t len,
6598 char_u *buf,
6599 ucmd_T *cmd, /* the user command we're expanding */
6600 exarg_T *eap, /* ex arguments */
6601 char_u **split_buf,
6602 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 size_t result = 0;
6605 char_u *p = code + 1;
6606 size_t l = len - 2;
6607 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006608 enum {
6609 ct_ARGS,
6610 ct_BANG,
6611 ct_COUNT,
6612 ct_LINE1,
6613 ct_LINE2,
6614 ct_RANGE,
6615 ct_MODS,
6616 ct_REGISTER,
6617 ct_LT,
6618 ct_NONE
6619 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6622 {
6623 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6624 p += 2;
6625 l -= 2;
6626 }
6627
Bram Moolenaar371d5402006-03-20 21:47:49 +00006628 ++l;
6629 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006631 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006633 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006635 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006637 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006639 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006641 else if (STRNICMP(p, "range>", l) == 0)
6642 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006643 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006645 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006647 else if (STRNICMP(p, "mods>", l) == 0)
6648 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650 switch (type)
6651 {
6652 case ct_ARGS:
6653 /* Simple case first */
6654 if (*eap->arg == NUL)
6655 {
6656 if (quote == 1)
6657 {
6658 result = 2;
6659 if (buf != NULL)
6660 STRCPY(buf, "''");
6661 }
6662 else
6663 result = 0;
6664 break;
6665 }
6666
6667 /* When specified there is a single argument don't split it.
6668 * Works for ":Cmd %" when % is "a b c". */
6669 if ((eap->argt & NOSPC) && quote == 2)
6670 quote = 1;
6671
6672 switch (quote)
6673 {
6674 case 0: /* No quoting, no splitting */
6675 result = STRLEN(eap->arg);
6676 if (buf != NULL)
6677 STRCPY(buf, eap->arg);
6678 break;
6679 case 1: /* Quote, but don't split */
6680 result = STRLEN(eap->arg) + 2;
6681 for (p = eap->arg; *p; ++p)
6682 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006683#ifdef FEAT_MBYTE
6684 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6685 /* DBCS can contain \ in a trail byte, skip the
6686 * double-byte character. */
6687 ++p;
6688 else
6689#endif
6690 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 ++result;
6692 }
6693
6694 if (buf != NULL)
6695 {
6696 *buf++ = '"';
6697 for (p = eap->arg; *p; ++p)
6698 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006699#ifdef FEAT_MBYTE
6700 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6701 /* DBCS can contain \ in a trail byte, copy the
6702 * double-byte character to avoid escaping. */
6703 *buf++ = *p++;
6704 else
6705#endif
6706 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 *buf++ = '\\';
6708 *buf++ = *p;
6709 }
6710 *buf = '"';
6711 }
6712
6713 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006714 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 /* This is hard, so only do it once, and cache the result */
6716 if (*split_buf == NULL)
6717 *split_buf = uc_split_args(eap->arg, split_len);
6718
6719 result = *split_len;
6720 if (buf != NULL && result != 0)
6721 STRCPY(buf, *split_buf);
6722
6723 break;
6724 }
6725 break;
6726
6727 case ct_BANG:
6728 result = eap->forceit ? 1 : 0;
6729 if (quote)
6730 result += 2;
6731 if (buf != NULL)
6732 {
6733 if (quote)
6734 *buf++ = '"';
6735 if (eap->forceit)
6736 *buf++ = '!';
6737 if (quote)
6738 *buf = '"';
6739 }
6740 break;
6741
6742 case ct_LINE1:
6743 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006744 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 case ct_COUNT:
6746 {
6747 char num_buf[20];
6748 long num = (type == ct_LINE1) ? eap->line1 :
6749 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006750 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6752 size_t num_len;
6753
6754 sprintf(num_buf, "%ld", num);
6755 num_len = STRLEN(num_buf);
6756 result = num_len;
6757
6758 if (quote)
6759 result += 2;
6760
6761 if (buf != NULL)
6762 {
6763 if (quote)
6764 *buf++ = '"';
6765 STRCPY(buf, num_buf);
6766 buf += num_len;
6767 if (quote)
6768 *buf = '"';
6769 }
6770
6771 break;
6772 }
6773
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006774 case ct_MODS:
6775 {
6776 int multi_mods = 0;
6777 typedef struct {
6778 int *varp;
6779 char *name;
6780 } mod_entry_T;
6781 static mod_entry_T mod_entries[] = {
6782#ifdef FEAT_BROWSE_CMD
6783 {&cmdmod.browse, "browse"},
6784#endif
6785#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6786 {&cmdmod.confirm, "confirm"},
6787#endif
6788 {&cmdmod.hide, "hide"},
6789 {&cmdmod.keepalt, "keepalt"},
6790 {&cmdmod.keepjumps, "keepjumps"},
6791 {&cmdmod.keepmarks, "keepmarks"},
6792 {&cmdmod.keeppatterns, "keeppatterns"},
6793 {&cmdmod.lockmarks, "lockmarks"},
6794 {&cmdmod.noswapfile, "noswapfile"},
6795 {NULL, NULL}
6796 };
6797 int i;
6798
6799 result = quote ? 2 : 0;
6800 if (buf != NULL)
6801 {
6802 if (quote)
6803 *buf++ = '"';
6804 *buf = '\0';
6805 }
6806
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006807 /* :aboveleft and :leftabove */
6808 if (cmdmod.split & WSP_ABOVE)
6809 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6810 /* :belowright and :rightbelow */
6811 if (cmdmod.split & WSP_BELOW)
6812 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6813 /* :botright */
6814 if (cmdmod.split & WSP_BOT)
6815 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006816
6817 /* the modifiers that are simple flags */
6818 for (i = 0; mod_entries[i].varp != NULL; ++i)
6819 if (*mod_entries[i].varp)
6820 result += add_cmd_modifier(buf, mod_entries[i].name,
6821 &multi_mods);
6822
6823 /* TODO: How to support :noautocmd? */
6824#ifdef HAVE_SANDBOX
6825 /* TODO: How to support :sandbox?*/
6826#endif
6827 /* :silent */
6828 if (msg_silent > 0)
6829 result += add_cmd_modifier(buf,
6830 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006831 /* :tab */
6832 if (cmdmod.tab > 0)
6833 result += add_cmd_modifier(buf, "tab", &multi_mods);
6834 /* :topleft */
6835 if (cmdmod.split & WSP_TOP)
6836 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006837 /* TODO: How to support :unsilent?*/
6838 /* :verbose */
6839 if (p_verbose > 0)
6840 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006841 /* :vertical */
6842 if (cmdmod.split & WSP_VERT)
6843 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006844 if (quote && buf != NULL)
6845 {
6846 buf += result - 2;
6847 *buf = '"';
6848 }
6849 break;
6850 }
6851
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 case ct_REGISTER:
6853 result = eap->regname ? 1 : 0;
6854 if (quote)
6855 result += 2;
6856 if (buf != NULL)
6857 {
6858 if (quote)
6859 *buf++ = '\'';
6860 if (eap->regname)
6861 *buf++ = eap->regname;
6862 if (quote)
6863 *buf = '\'';
6864 }
6865 break;
6866
6867 case ct_LT:
6868 result = 1;
6869 if (buf != NULL)
6870 *buf = '<';
6871 break;
6872
6873 default:
6874 /* Not recognized: just copy the '<' and return -1. */
6875 result = (size_t)-1;
6876 if (buf != NULL)
6877 *buf = '<';
6878 break;
6879 }
6880
6881 return result;
6882}
6883
6884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006885do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 char_u *buf;
6888 char_u *p;
6889 char_u *q;
6890
6891 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006892 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006893 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 size_t len, totlen;
6895
6896 size_t split_len = 0;
6897 char_u *split_buf = NULL;
6898 ucmd_T *cmd;
6899#ifdef FEAT_EVAL
6900 scid_T save_current_SID = current_SID;
6901#endif
6902
6903 if (eap->cmdidx == CMD_USER)
6904 cmd = USER_CMD(eap->useridx);
6905 else
6906 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6907
6908 /*
6909 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006910 * First round: "buf" is NULL, compute length, allocate "buf".
6911 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 */
6913 buf = NULL;
6914 for (;;)
6915 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006916 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006917 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006919
6920 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006922 start = vim_strchr(p, '<');
6923 if (start != NULL)
6924 end = vim_strchr(start + 1, '>');
6925 if (buf != NULL)
6926 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006927 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6928 ;
6929 if (*ksp == K_SPECIAL
6930 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006931 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6932# ifdef FEAT_GUI
6933 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6934# endif
6935 ))
6936 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006937 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006938 * KS_SPECIAL KE_FILLER, like for mappings, but
6939 * do_cmdline() doesn't handle that, so convert it back.
6940 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6941 len = ksp - p;
6942 if (len > 0)
6943 {
6944 mch_memmove(q, p, len);
6945 q += len;
6946 }
6947 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6948 p = ksp + 3;
6949 continue;
6950 }
6951 }
6952
6953 /* break if there no <item> is found */
6954 if (start == NULL || end == NULL)
6955 break;
6956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 /* Include the '>' */
6958 ++end;
6959
6960 /* Take everything up to the '<' */
6961 len = start - p;
6962 if (buf == NULL)
6963 totlen += len;
6964 else
6965 {
6966 mch_memmove(q, p, len);
6967 q += len;
6968 }
6969
6970 len = uc_check_code(start, end - start, q, cmd, eap,
6971 &split_buf, &split_len);
6972 if (len == (size_t)-1)
6973 {
6974 /* no match, continue after '<' */
6975 p = start + 1;
6976 len = 1;
6977 }
6978 else
6979 p = end;
6980 if (buf == NULL)
6981 totlen += len;
6982 else
6983 q += len;
6984 }
6985 if (buf != NULL) /* second time here, finished */
6986 {
6987 STRCPY(q, p);
6988 break;
6989 }
6990
6991 totlen += STRLEN(p); /* Add on the trailing characters */
6992 buf = alloc((unsigned)(totlen + 1));
6993 if (buf == NULL)
6994 {
6995 vim_free(split_buf);
6996 return;
6997 }
6998 }
6999
7000#ifdef FEAT_EVAL
7001 current_SID = cmd->uc_scriptID;
7002#endif
7003 (void)do_cmdline(buf, eap->getline, eap->cookie,
7004 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7005#ifdef FEAT_EVAL
7006 current_SID = save_current_SID;
7007#endif
7008 vim_free(buf);
7009 vim_free(split_buf);
7010}
7011
7012# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7013 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007014get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7017}
7018
7019/*
7020 * Function given to ExpandGeneric() to obtain the list of user command names.
7021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007023get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 if (idx < curbuf->b_ucmds.ga_len)
7026 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7027 idx -= curbuf->b_ucmds.ga_len;
7028 if (idx < ucmds.ga_len)
7029 return USER_CMD(idx)->uc_name;
7030 return NULL;
7031}
7032
7033/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007034 * Function given to ExpandGeneric() to obtain the list of user address type names.
7035 */
7036 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007037get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007038{
7039 return (char_u *)addr_type_complete[idx].name;
7040}
7041
7042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 * Function given to ExpandGeneric() to obtain the list of user command
7044 * attributes.
7045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007047get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007050 {"addr", "bang", "bar", "buffer", "complete",
7051 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007053 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 return NULL;
7055 return (char_u *)user_cmd_flags[idx];
7056}
7057
7058/*
7059 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007062get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7065
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007066 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 return NULL;
7068 return (char_u *)user_cmd_nargs[idx];
7069}
7070
7071/*
7072 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007075get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 return (char_u *)command_complete[idx].name;
7078}
7079# endif /* FEAT_CMDL_COMPL */
7080
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007081/*
7082 * Parse address type argument
7083 */
7084 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085parse_addr_type_arg(
7086 char_u *value,
7087 int vallen,
7088 long *argt,
7089 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007090{
7091 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007092
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007093 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7094 {
7095 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7096 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7097 if (a && b)
7098 {
7099 *addr_type_arg = addr_type_complete[i].expand;
7100 break;
7101 }
7102 }
7103
7104 if (addr_type_complete[i].expand == -1)
7105 {
7106 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007107
Bram Moolenaar1c465442017-03-12 20:10:05 +01007108 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007109 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007110 err[i] = NUL;
7111 EMSG2(_("E180: Invalid address type value: %s"), err);
7112 return FAIL;
7113 }
7114
7115 if (*addr_type_arg != ADDR_LINES)
7116 *argt |= NOTADR;
7117
7118 return OK;
7119}
7120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121#endif /* FEAT_USR_CMDS */
7122
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007123#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7124/*
7125 * Parse a completion argument "value[vallen]".
7126 * The detected completion goes in "*complp", argument type in "*argt".
7127 * When there is an argument, for function and user defined completion, it's
7128 * copied to allocated memory and stored in "*compl_arg".
7129 * Returns FAIL if something is wrong.
7130 */
7131 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007132parse_compl_arg(
7133 char_u *value,
7134 int vallen,
7135 int *complp,
7136 long *argt,
7137 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007138{
7139 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007140# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007141 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007142# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007143 int i;
7144 int valend = vallen;
7145
7146 /* Look for any argument part - which is the part after any ',' */
7147 for (i = 0; i < vallen; ++i)
7148 {
7149 if (value[i] == ',')
7150 {
7151 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007152# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007153 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007154# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007155 valend = i;
7156 break;
7157 }
7158 }
7159
7160 for (i = 0; command_complete[i].expand != 0; ++i)
7161 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007162 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007163 && STRNCMP(value, command_complete[i].name, valend) == 0)
7164 {
7165 *complp = command_complete[i].expand;
7166 if (command_complete[i].expand == EXPAND_BUFFERS)
7167 *argt |= BUFNAME;
7168 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7169 || command_complete[i].expand == EXPAND_FILES)
7170 *argt |= XFILE;
7171 break;
7172 }
7173 }
7174
7175 if (command_complete[i].expand == 0)
7176 {
7177 EMSG2(_("E180: Invalid complete value: %s"), value);
7178 return FAIL;
7179 }
7180
7181# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7182 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7183 && arg != NULL)
7184# else
7185 if (arg != NULL)
7186# endif
7187 {
7188 EMSG(_("E468: Completion argument only allowed for custom completion"));
7189 return FAIL;
7190 }
7191
7192# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7193 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7194 && arg == NULL)
7195 {
7196 EMSG(_("E467: Custom completion requires a function argument"));
7197 return FAIL;
7198 }
7199
7200 if (arg != NULL)
7201 *compl_arg = vim_strnsave(arg, (int)arglen);
7202# endif
7203 return OK;
7204}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007205
7206 int
7207cmdcomplete_str_to_type(char_u *complete_str)
7208{
7209 int i;
7210
7211 for (i = 0; command_complete[i].expand != 0; ++i)
7212 if (STRCMP(complete_str, command_complete[i].name) == 0)
7213 return command_complete[i].expand;
7214
7215 return EXPAND_NOTHING;
7216}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007217#endif
7218
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007220ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
Bram Moolenaare6850792010-05-14 15:28:44 +02007222 if (*eap->arg == NUL)
7223 {
7224#ifdef FEAT_EVAL
7225 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7226 char_u *p = NULL;
7227
7228 if (expr != NULL)
7229 {
7230 ++emsg_off;
7231 p = eval_to_string(expr, NULL, FALSE);
7232 --emsg_off;
7233 vim_free(expr);
7234 }
7235 if (p != NULL)
7236 {
7237 MSG(p);
7238 vim_free(p);
7239 }
7240 else
7241 MSG("default");
7242#else
7243 MSG(_("unknown"));
7244#endif
7245 }
7246 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007247 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248}
7249
7250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007251ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
7253 if (*eap->arg == NUL && eap->cmd[2] == '!')
7254 MSG(_("Greetings, Vim user!"));
7255 do_highlight(eap->arg, eap->forceit, FALSE);
7256}
7257
7258
7259/*
7260 * Call this function if we thought we were going to exit, but we won't
7261 * (because of an error). May need to restore the terminal mode.
7262 */
7263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007264not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265{
7266 exiting = FALSE;
7267 settmode(TMODE_RAW);
7268}
7269
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007270 static int
7271before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7272{
7273 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7274
7275 /* Bail out when autocommands closed the window.
7276 * Refuse to quit when the buffer in the last window is being closed (can
7277 * only happen in autocommands). */
7278 if (!win_valid(wp)
7279 || curbuf_locked()
7280 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7281 return TRUE;
7282
7283 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7284 {
7285 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7286 /* Refuse to quit when locked or when the buffer in the last window is
7287 * being closed (can only happen in autocommands). */
7288 if (curbuf_locked()
7289 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7290 return TRUE;
7291 }
7292
7293 return FALSE;
7294}
7295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007297 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007298 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 */
7300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007301ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007303 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007304
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305#ifdef FEAT_CMDWIN
7306 if (cmdwin_type != 0)
7307 {
7308 cmdwin_result = Ctrl_C;
7309 return;
7310 }
7311#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007312 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007313 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007314 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007315 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007316 return;
7317 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007318 if (eap->addr_count > 0)
7319 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007320 int wnr = eap->line2;
7321
7322 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7323 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007324 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007325 }
7326 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007327 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007328
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007329 /* Refuse to quit when locked. */
7330 if (curbuf_locked())
7331 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007332
7333 /* Trigger QuitPre and maybe ExitPre */
7334 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007335 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
7337#ifdef FEAT_NETBEANS_INTG
7338 netbeansForcedQuit = eap->forceit;
7339#endif
7340
7341 /*
7342 * If there are more files or windows we won't exit.
7343 */
7344 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7345 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007346 if ((!buf_hide(wp->w_buffer)
7347 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007348 | (eap->forceit ? CCGD_FORCEIT : 0)
7349 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007351 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 {
7353 not_exiting();
7354 }
7355 else
7356 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007357 /* quit last window
7358 * Note: only_one_window() returns true, even so a help window is
7359 * still open. In that case only quit, if no address has been
7360 * specified. Example:
7361 * :h|wincmd w|1q - don't quit
7362 * :h|wincmd w|q - quit
7363 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007364 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007366 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007367#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007371 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
7373}
7374
7375/*
7376 * ":cquit".
7377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007379ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 getout(1); /* this does not always pass on the exit code to the Manx
7382 compiler. why? */
7383}
7384
7385/*
7386 * ":qall": try to quit all windows
7387 */
7388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007389ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391# ifdef FEAT_CMDWIN
7392 if (cmdwin_type != 0)
7393 {
7394 if (eap->forceit)
7395 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7396 else
7397 cmdwin_result = K_XF2;
7398 return;
7399 }
7400# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007401
7402 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007403 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007404 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007405 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007406 return;
7407 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007408
7409 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007410 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007411
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007413 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 getout(0);
7415 not_exiting();
7416}
7417
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418/*
7419 * ":close": close current window, unless it is the last one
7420 */
7421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007422ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007424 win_T *win;
7425 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007426#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007428 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007430#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007431 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007432 {
7433 if (eap->addr_count == 0)
7434 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007435 else
7436 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007437 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007438 {
7439 winnr++;
7440 if (winnr == eap->line2)
7441 break;
7442 }
7443 if (win == NULL)
7444 win = lastwin;
7445 ex_win_close(eap->forceit, win, NULL);
7446 }
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
Bram Moolenaar4033c552017-09-16 20:54:51 +02007450#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007451/*
7452 * ":pclose": Close any preview window.
7453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007455ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007456{
7457 win_T *win;
7458
Bram Moolenaar29323592016-07-24 22:04:11 +02007459 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007460 if (win->w_p_pvw)
7461 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007462 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007463 break;
7464 }
7465}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007466#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007467
Bram Moolenaarf740b292006-02-16 22:11:02 +00007468/*
7469 * Close window "win" and take care of handling closing the last window for a
7470 * modified buffer.
7471 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007473ex_win_close(
7474 int forceit,
7475 win_T *win,
7476 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
7478 int need_hide;
7479 buf_T *buf = win->w_buffer;
7480
7481 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007482 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007484#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if ((p_confirm || cmdmod.confirm) && p_write)
7486 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007487 bufref_T bufref;
7488
7489 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007491 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 return;
7493 need_hide = FALSE;
7494 }
7495 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007498 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 return;
7500 }
7501 }
7502
Bram Moolenaar4033c552017-09-16 20:54:51 +02007503#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007505#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007508 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007509 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007510 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007511 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512}
7513
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007515 * Handle the argument for a tabpage related ex command.
7516 * Returns a tabpage number.
7517 * When an error is encountered then eap->errmsg is set.
7518 */
7519 static int
7520get_tabpage_arg(exarg_T *eap)
7521{
7522 int tab_number;
7523 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7524
7525 if (eap->arg && *eap->arg != NUL)
7526 {
7527 char_u *p = eap->arg;
7528 char_u *p_save;
7529 int relative = 0; /* argument +N/-N means: go to N places to the
7530 * right/left relative to the current position. */
7531
7532 if (*p == '-')
7533 {
7534 relative = -1;
7535 p++;
7536 }
7537 else if (*p == '+')
7538 {
7539 relative = 1;
7540 p++;
7541 }
7542
7543 p_save = p;
7544 tab_number = getdigits(&p);
7545
7546 if (relative == 0)
7547 {
7548 if (STRCMP(p, "$") == 0)
7549 tab_number = LAST_TAB_NR;
7550 else if (p == p_save || *p_save == '-' || *p != NUL
7551 || tab_number > LAST_TAB_NR)
7552 {
7553 /* No numbers as argument. */
7554 eap->errmsg = e_invarg;
7555 goto theend;
7556 }
7557 }
7558 else
7559 {
7560 if (*p_save == NUL)
7561 tab_number = 1;
7562 else if (p == p_save || *p_save == '-' || *p != NUL
7563 || tab_number == 0)
7564 {
7565 /* No numbers as argument. */
7566 eap->errmsg = e_invarg;
7567 goto theend;
7568 }
7569 tab_number = tab_number * relative + tabpage_index(curtab);
7570 if (!unaccept_arg0 && relative == -1)
7571 --tab_number;
7572 }
7573 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7574 eap->errmsg = e_invarg;
7575 }
7576 else if (eap->addr_count > 0)
7577 {
7578 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007579 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007580 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007581 tab_number = 0;
7582 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007583 else
7584 {
7585 tab_number = eap->line2;
7586 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7587 {
7588 --tab_number;
7589 if (tab_number < unaccept_arg0)
7590 eap->errmsg = e_invarg;
7591 }
7592 }
7593 }
7594 else
7595 {
7596 switch (eap->cmdidx)
7597 {
7598 case CMD_tabnext:
7599 tab_number = tabpage_index(curtab) + 1;
7600 if (tab_number > LAST_TAB_NR)
7601 tab_number = 1;
7602 break;
7603 case CMD_tabmove:
7604 tab_number = LAST_TAB_NR;
7605 break;
7606 default:
7607 tab_number = tabpage_index(curtab);
7608 }
7609 }
7610
7611theend:
7612 return tab_number;
7613}
7614
7615/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007616 * ":tabclose": close current tab page, unless it is the last one.
7617 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 */
7619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007620ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007622 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007623 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007624
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007625# ifdef FEAT_CMDWIN
7626 if (cmdwin_type != 0)
7627 cmdwin_result = K_IGNORE;
7628 else
7629# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007630 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007631 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007632 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007634 tab_number = get_tabpage_arg(eap);
7635 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007636 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007637 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007638 if (tp == NULL)
7639 {
7640 beep_flush();
7641 return;
7642 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007643 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007644 {
7645 tabpage_close_other(tp, eap->forceit);
7646 return;
7647 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007648 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007649 tabpage_close(eap->forceit);
7650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007652}
7653
7654/*
7655 * ":tabonly": close all tab pages except the current one
7656 */
7657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007658ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007659{
7660 tabpage_T *tp;
7661 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007662 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007663
7664# ifdef FEAT_CMDWIN
7665 if (cmdwin_type != 0)
7666 cmdwin_result = K_IGNORE;
7667 else
7668# endif
7669 if (first_tabpage->tp_next == NULL)
7670 MSG(_("Already only one tab page"));
7671 else
7672 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007673 tab_number = get_tabpage_arg(eap);
7674 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007675 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007676 goto_tabpage(tab_number);
7677 /* Repeat this up to a 1000 times, because autocommands may
7678 * mess up the lists. */
7679 for (done = 0; done < 1000; ++done)
7680 {
7681 FOR_ALL_TABPAGES(tp)
7682 if (tp->tp_topframe != topframe)
7683 {
7684 tabpage_close_other(tp, eap->forceit);
7685 /* if we failed to close it quit */
7686 if (valid_tabpage(tp))
7687 done = 1000;
7688 /* start over, "tp" is now invalid */
7689 break;
7690 }
7691 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007692 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007693 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007694 }
7695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
7698/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007699 * Close the current tab page.
7700 */
7701 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007702tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007703{
7704 /* First close all the windows but the current one. If that worked then
7705 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007706 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007707 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007708 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007709 ex_win_close(forceit, curwin, NULL);
7710# ifdef FEAT_GUI
7711 need_mouse_correct = TRUE;
7712# endif
7713}
7714
7715/*
7716 * Close tab page "tp", which is not the current tab page.
7717 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007718 * Also takes care of the tab pages line disappearing when closing the
7719 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007720 */
7721 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007722tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007723{
7724 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007725 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007726 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007727
7728 /* Limit to 1000 windows, autocommands may add a window while we close
7729 * one. OK, so I'm paranoid... */
7730 while (++done < 1000)
7731 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007732 wp = tp->tp_firstwin;
7733 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007734
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007735 /* Autocommands may delete the tab page under our fingers and we may
7736 * fail to close a window with a modified buffer. */
7737 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007738 break;
7739 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007740
Bram Moolenaar29323592016-07-24 22:04:11 +02007741 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007742
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007743 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007744 if (h != tabline_height())
7745 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007746}
7747
7748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 * ":only".
7750 */
7751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007754 win_T *wp;
7755 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756# ifdef FEAT_GUI
7757 need_mouse_correct = TRUE;
7758# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007759 if (eap->addr_count > 0)
7760 {
7761 wnr = eap->line2;
7762 for (wp = firstwin; --wnr > 0; )
7763 {
7764 if (wp->w_next == NULL)
7765 break;
7766 else
7767 wp = wp->w_next;
7768 }
7769 win_goto(wp);
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 close_others(TRUE, eap->forceit);
7772}
7773
7774/*
7775 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007776 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007778 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007779ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781 if (eap->addr_count == 0)
7782 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007783 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785
7786 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007787ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007789 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007790 if (!eap->skip)
7791 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007792#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007793 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007794#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007795 if (eap->addr_count == 0)
7796 win_close(curwin, FALSE); /* don't free buffer */
7797 else
7798 {
7799 int winnr = 0;
7800 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007801
Bram Moolenaar2256c992016-11-15 21:17:07 +01007802 FOR_ALL_WINDOWS(win)
7803 {
7804 winnr++;
7805 if (winnr == eap->line2)
7806 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007807 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007808 if (win == NULL)
7809 win = lastwin;
7810 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 }
7813}
7814
7815/*
7816 * ":stop" and ":suspend": Suspend Vim.
7817 */
7818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007819ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 /*
7822 * Disallow suspending for "rvim".
7823 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007824 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
7826 if (!eap->forceit)
7827 autowrite_all();
7828 windgoto((int)Rows - 1, 0);
7829 out_char('\n');
7830 out_flush();
7831 stoptermcap();
7832 out_flush(); /* needed for SUN to restore xterm buffer */
7833#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007834 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835#endif
7836 ui_suspend(); /* call machine specific function */
7837#ifdef FEAT_TITLE
7838 maketitle();
7839 resettitle(); /* force updating the title */
7840#endif
7841 starttermcap();
7842 scroll_start(); /* scroll screen before redrawing */
7843 redraw_later_clear();
7844 shell_resized(); /* may have resized window */
7845 }
7846}
7847
7848/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007849 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 */
7851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007852ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854#ifdef FEAT_CMDWIN
7855 if (cmdwin_type != 0)
7856 {
7857 cmdwin_result = Ctrl_C;
7858 return;
7859 }
7860#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007861 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007862 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007863 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007864 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007865 return;
7866 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007867
7868 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007869 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
7871 /*
7872 * if more files or windows we won't exit
7873 */
7874 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7875 exiting = TRUE;
7876 if ( ((eap->cmdidx == CMD_wq
7877 || curbufIsChanged())
7878 && do_write(eap) == FAIL)
7879 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007880 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
7882 not_exiting();
7883 }
7884 else
7885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007888 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889# ifdef FEAT_GUI
7890 need_mouse_correct = TRUE;
7891# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007892 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007893 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895}
7896
7897/*
7898 * ":print", ":list", ":number".
7899 */
7900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007903 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7904 EMSG(_(e_emptybuf));
7905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007907 for ( ;!got_int; ui_breakcheck())
7908 {
7909 print_line(eap->line1,
7910 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7911 || (eap->flags & EXFLAG_NR)),
7912 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7913 if (++eap->line1 > eap->line2)
7914 break;
7915 out_flush(); /* show one line at a time */
7916 }
7917 setpcmark();
7918 /* put cursor at last line */
7919 curwin->w_cursor.lnum = eap->line2;
7920 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 }
7922
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924}
7925
7926#ifdef FEAT_BYTEOFF
7927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007928ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 goto_byte(eap->line2);
7931}
7932#endif
7933
7934/*
7935 * ":shell".
7936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007938ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 do_shell(NULL, 0);
7941}
7942
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007943#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007945static int drop_busy = FALSE;
7946static int drop_filec;
7947static char_u **drop_filev = NULL;
7948static int drop_split;
7949static void (*drop_callback)(void *);
7950static void *drop_cookie;
7951
7952 static void
7953handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 exarg_T ea;
7956 int save_msg_scroll = msg_scroll;
7957
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007958 // Setting the argument list may cause screen updates and being called
7959 // recursively. Avoid that by setting drop_busy.
7960 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
7962 /* Check whether the current buffer is changed. If so, we will need
7963 * to split the current window or data could be lost.
7964 * We don't need to check if the 'hidden' option is set, as in this
7965 * case the buffer won't be lost.
7966 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007967 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {
7969 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007970 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 --emsg_off;
7972 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007973 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (win_split(0, 0) == FAIL)
7976 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007977 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978
7979 /* When splitting the window, create a new alist. Otherwise the
7980 * existing one is overwritten. */
7981 alist_unlink(curwin->w_alist);
7982 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984
7985 /*
7986 * Set up the new argument list.
7987 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007988 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 /*
7991 * Move to the first file.
7992 */
7993 /* Fake up a minimal "next" command for do_argfile() */
7994 vim_memset(&ea, 0, sizeof(ea));
7995 ea.cmd = (char_u *)"next";
7996 do_argfile(&ea, 0);
7997
7998 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7999 * mode that is not needed here. */
8000 need_start_insertmode = FALSE;
8001
8002 /* Restore msg_scroll, otherwise a following command may cause scrolling
8003 * unexpectedly. The screen will be redrawn by the caller, thus
8004 * msg_scroll being set by displaying a message is irrelevant. */
8005 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008006
8007 if (drop_callback != NULL)
8008 drop_callback(drop_cookie);
8009
8010 drop_filev = NULL;
8011 drop_busy = FALSE;
8012}
8013
8014/*
8015 * Handle a file drop. The code is here because a drop is *nearly* like an
8016 * :args command, but not quite (we have a list of exact filenames, so we
8017 * don't want to (a) parse a command line, or (b) expand wildcards. So the
8018 * code is very similar to :args and hence needs access to a lot of the static
8019 * functions in this file.
8020 *
8021 * The "filev" list must have been allocated using alloc(), as should each item
8022 * in the list. This function takes over responsibility for freeing the "filev"
8023 * list.
8024 */
8025 void
8026handle_drop(
8027 int filec, // the number of files dropped
8028 char_u **filev, // the list of files dropped
8029 int split, // force splitting the window
8030 void (*callback)(void *), // to be called after setting the argument
8031 // list
8032 void *cookie) // argument for "callback" (allocated)
8033{
8034 // Cannot handle recursive drops, finish the pending one.
8035 if (drop_busy)
8036 {
8037 FreeWild(filec, filev);
8038 vim_free(cookie);
8039 return;
8040 }
8041
8042 // When calling handle_drop() more than once in a row we only use the last
8043 // one.
8044 if (drop_filev != NULL)
8045 {
8046 FreeWild(drop_filec, drop_filev);
8047 vim_free(drop_cookie);
8048 }
8049
8050 drop_filec = filec;
8051 drop_filev = filev;
8052 drop_split = split;
8053 drop_callback = callback;
8054 drop_cookie = cookie;
8055
8056 // Postpone this when:
8057 // - editing the command line
8058 // - not possible to change the current buffer
8059 // - updating the screen
8060 // As it may change buffers and window structures that are in use and cause
8061 // freed memory to be used.
8062 if (text_locked() || curbuf_locked() || updating_screen)
8063 return;
8064
8065 handle_drop_internal();
8066}
8067
8068/*
8069 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8070 * reset: Handle a postponed drop.
8071 */
8072 void
8073handle_any_postponed_drop(void)
8074{
8075 if (!drop_busy && drop_filev != NULL
8076 && !text_locked() && !curbuf_locked() && !updating_screen)
8077 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078}
8079#endif
8080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081/*
8082 * Clear an argument list: free all file names and reset it to zero entries.
8083 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008085alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
8087 while (--al->al_ga.ga_len >= 0)
8088 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8089 ga_clear(&al->al_ga);
8090}
8091
8092/*
8093 * Init an argument list.
8094 */
8095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8099}
8100
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101/*
8102 * Remove a reference from an argument list.
8103 * Ignored when the argument list is the global one.
8104 * If the argument list is no longer used by any window, free it.
8105 */
8106 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008107alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 if (al != &global_alist && --al->al_refcount <= 0)
8110 {
8111 alist_clear(al);
8112 vim_free(al);
8113 }
8114}
8115
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116/*
8117 * Create a new argument list and use it for the current window.
8118 */
8119 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008120alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8123 if (curwin->w_alist == NULL)
8124 {
8125 curwin->w_alist = &global_alist;
8126 ++global_alist.al_refcount;
8127 }
8128 else
8129 {
8130 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008131 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 alist_init(curwin->w_alist);
8133 }
8134}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008136#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137/*
8138 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008139 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8140 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 */
8142 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008143alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008146 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 char_u **new_arg_files;
8148 int new_arg_file_count;
8149 char_u *save_p_su = p_su;
8150 int i;
8151
8152 /* Don't use 'suffixes' here. This should work like the shell did the
8153 * expansion. Also, the vimrc file isn't read yet, thus the user
8154 * can't set the options. */
8155 p_su = empty_option;
8156 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8157 if (old_arg_files != NULL)
8158 {
8159 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008160 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8161 old_arg_count = GARGCOUNT;
8162 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008164 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 && new_arg_file_count > 0)
8166 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008167 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8168 TRUE, fnum_list, fnum_len);
8169 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 }
8172 p_su = save_p_su;
8173}
8174#endif
8175
8176/*
8177 * Set the argument list for the current window.
8178 * Takes over the allocated files[] and the allocated fnames in it.
8179 */
8180 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181alist_set(
8182 alist_T *al,
8183 int count,
8184 char_u **files,
8185 int use_curbuf,
8186 int *fnum_list,
8187 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188{
8189 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008190 static int recursive = 0;
8191
8192 if (recursive)
8193 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008194 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008195 return;
8196 }
8197 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198
8199 alist_clear(al);
8200 if (ga_grow(&al->al_ga, count) == OK)
8201 {
8202 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008203 {
8204 if (got_int)
8205 {
8206 /* When adding many buffers this can take a long time. Allow
8207 * interrupting here. */
8208 while (i < count)
8209 vim_free(files[i++]);
8210 break;
8211 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008212
8213 /* May set buffer name of a buffer previously used for the
8214 * argument list, so that it's re-used by alist_add. */
8215 if (fnum_list != NULL && i < fnum_len)
8216 buf_set_name(fnum_list[i], files[i]);
8217
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008219 ui_breakcheck();
8220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 vim_free(files);
8222 }
8223 else
8224 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008227
8228 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229}
8230
8231/*
8232 * Add file "fname" to argument list "al".
8233 * "fname" must have been allocated and "al" must have been checked for room.
8234 */
8235 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008236alist_add(
8237 alist_T *al,
8238 char_u *fname,
8239 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
8241 if (fname == NULL) /* don't add NULL file names */
8242 return;
8243#ifdef BACKSLASH_IN_FILENAME
8244 slash_adjust(fname);
8245#endif
8246 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8247 if (set_fnum > 0)
8248 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8249 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8250 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251}
8252
8253#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8254/*
8255 * Adjust slashes in file names. Called after 'shellslash' was set.
8256 */
8257 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008258alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
8260 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008262 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263
8264 for (i = 0; i < GARGCOUNT; ++i)
8265 if (GARGLIST[i].ae_fname != NULL)
8266 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008267 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (wp->w_alist != &global_alist)
8269 for (i = 0; i < WARGCOUNT(wp); ++i)
8270 if (WARGLIST(wp)[i].ae_fname != NULL)
8271 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272}
8273#endif
8274
8275/*
8276 * ":preserve".
8277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008279ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008281 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 ml_preserve(curbuf, TRUE);
8283}
8284
8285/*
8286 * ":recover".
8287 */
8288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008289ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
8291 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8292 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008293 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8294 | CCGD_MULTWIN
8295 | (eap->forceit ? CCGD_FORCEIT : 0)
8296 | CCGD_EXCMD)
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 && (*eap->arg == NUL
8299 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8300 ml_recover();
8301 recoverymode = FALSE;
8302}
8303
8304/*
8305 * Command modifier used in a wrong way.
8306 */
8307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008308ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309{
8310 eap->errmsg = e_invcmd;
8311}
8312
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313/*
8314 * :sview [+command] file split window with new file, read-only
8315 * :split [[+command] file] split window with current or new file
8316 * :vsplit [[+command] file] split window vertically with current or new file
8317 * :new [[+command] file] split window with no or new file
8318 * :vnew [[+command] file] split vertically window with no or new file
8319 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008320 *
8321 * :tabedit open new Tab page with empty window
8322 * :tabedit [+command] file open new Tab page and edit "file"
8323 * :tabnew [[+command] file] just like :tabedit
8324 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 */
8326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008327ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008329 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008330#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008332#endif
8333#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008335#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008336 int use_tab = eap->cmdidx == CMD_tabedit
8337 || eap->cmdidx == CMD_tabfind
8338 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
Bram Moolenaar4033c552017-09-16 20:54:51 +02008340#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343
Bram Moolenaar4033c552017-09-16 20:54:51 +02008344#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008346 * quickfix windows. But it's OK when doing ":tab split". */
8347 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
8349 if (eap->cmdidx == CMD_split)
8350 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (eap->cmdidx == CMD_vsplit)
8352 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
Bram Moolenaar4033c552017-09-16 20:54:51 +02008356#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008357 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
8359 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8360 FNAME_MESS, TRUE, curbuf->b_ffname);
8361 if (fname == NULL)
8362 goto theend;
8363 eap->arg = fname;
8364 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008365# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008366 else
8367# endif
8368#endif
8369#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 && eap->cmdidx != CMD_new)
8373 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008374 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008375# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008376 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008377# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008378 au_has_group((char_u *)"FileExplorer"))
8379 {
8380 /* No browsing supported but we do have the file explorer:
8381 * Edit the directory. */
8382 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8383 eap->arg = (char_u *)".";
8384 }
8385 else
8386 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008387 fname = do_browse(0, (char_u *)(use_tab
8388 ? _("Edit File in new tab page")
8389 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008391 if (fname == NULL)
8392 goto theend;
8393 eap->arg = fname;
8394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 }
8396 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008399 /*
8400 * Either open new tab page or split the window.
8401 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008402 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008403 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008404 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8405 : eap->addr_count == 0 ? 0
8406 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008407 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008408 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008409
8410 /* set the alternate buffer for the window we came from */
8411 if (curwin != old_curwin
8412 && win_valid(old_curwin)
8413 && old_curwin->w_buffer != curbuf
8414 && !cmdmod.keepalt)
8415 old_curwin->w_alt_fnum = curbuf->b_fnum;
8416 }
8417 }
8418 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 /* Reset 'scrollbind' when editing another file, but keep it when
8422 * doing ":split" without arguments. */
8423 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008424# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008428 {
8429 RESET_BINDING(curwin);
8430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 else
8432 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 do_exedit(eap, old_curwin);
8434 }
8435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008440# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441theend:
8442 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008445
8446/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008447 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008448 */
8449 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008450tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008451{
8452 exarg_T ea;
8453
8454 vim_memset(&ea, 0, sizeof(ea));
8455 ea.cmdidx = CMD_tabnew;
8456 ea.cmd = (char_u *)"tabn";
8457 ea.arg = (char_u *)"";
8458 ex_splitview(&ea);
8459}
8460
8461/*
8462 * :tabnext command
8463 */
8464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008465ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008466{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008467 int tab_number;
8468
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008469 switch (eap->cmdidx)
8470 {
8471 case CMD_tabfirst:
8472 case CMD_tabrewind:
8473 goto_tabpage(1);
8474 break;
8475 case CMD_tablast:
8476 goto_tabpage(9999);
8477 break;
8478 case CMD_tabprevious:
8479 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008480 if (eap->arg && *eap->arg != NUL)
8481 {
8482 char_u *p = eap->arg;
8483 char_u *p_save = p;
8484
8485 tab_number = getdigits(&p);
8486 if (p == p_save || *p_save == '-' || *p != NUL
8487 || tab_number == 0)
8488 {
8489 /* No numbers as argument. */
8490 eap->errmsg = e_invarg;
8491 return;
8492 }
8493 }
8494 else
8495 {
8496 if (eap->addr_count == 0)
8497 tab_number = 1;
8498 else
8499 {
8500 tab_number = eap->line2;
8501 if (tab_number < 1)
8502 {
8503 eap->errmsg = e_invrange;
8504 return;
8505 }
8506 }
8507 }
8508 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008509 break;
8510 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008511 tab_number = get_tabpage_arg(eap);
8512 if (eap->errmsg == NULL)
8513 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008514 break;
8515 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008516}
8517
8518/*
8519 * :tabmove command
8520 */
8521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008523{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008524 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008525
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008526 tab_number = get_tabpage_arg(eap);
8527 if (eap->errmsg == NULL)
8528 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008529}
8530
8531/*
8532 * :tabs command: List tabs and their contents.
8533 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008535ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008536{
8537 tabpage_T *tp;
8538 win_T *wp;
8539 int tabcount = 1;
8540
8541 msg_start();
8542 msg_scroll = TRUE;
8543 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8544 {
8545 msg_putchar('\n');
8546 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008547 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008548 out_flush(); /* output one line at a time */
8549 ui_breakcheck();
8550
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008551 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008552 wp = firstwin;
8553 else
8554 wp = tp->tp_firstwin;
8555 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8556 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008557 msg_putchar('\n');
8558 msg_putchar(wp == curwin ? '>' : ' ');
8559 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008560 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8561 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008562 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008563 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008564 else
8565 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8566 IObuff, IOSIZE, TRUE);
8567 msg_outtrans(IObuff);
8568 out_flush(); /* output one line at a time */
8569 ui_breakcheck();
8570 }
8571 }
8572}
8573
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574/*
8575 * ":mode": Set screen mode.
8576 * If no argument given, just get the screen size and redraw.
8577 */
8578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008579ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 if (*eap->arg == NUL)
8582 shell_resized();
8583 else
8584 mch_screenmode(eap->arg);
8585}
8586
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587/*
8588 * ":resize".
8589 * set, increment or decrement current window height
8590 */
8591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008592ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 int n;
8595 win_T *wp = curwin;
8596
8597 if (eap->addr_count > 0)
8598 {
8599 n = eap->line2;
8600 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8601 ;
8602 }
8603
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008604# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 if (cmdmod.split & WSP_VERT)
8609 {
8610 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008611 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8613 n = 9999;
8614 win_setwidth_win((int)n, wp);
8615 }
8616 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
8618 if (*eap->arg == '-' || *eap->arg == '+')
8619 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008620 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 n = 9999;
8622 win_setheight_win((int)n, wp);
8623 }
8624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
8626/*
8627 * ":find [+command] <file>" command.
8628 */
8629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008630ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
8632#ifdef FEAT_SEARCHPATH
8633 char_u *fname;
8634 int count;
8635
8636 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8637 TRUE, curbuf->b_ffname);
8638 if (eap->addr_count > 0)
8639 {
8640 /* Repeat finding the file "count" times. This matters when it
8641 * appears several times in the path. */
8642 count = eap->line2;
8643 while (fname != NULL && --count > 0)
8644 {
8645 vim_free(fname);
8646 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8647 FALSE, curbuf->b_ffname);
8648 }
8649 }
8650
8651 if (fname != NULL)
8652 {
8653 eap->arg = fname;
8654#endif
8655 do_exedit(eap, NULL);
8656#ifdef FEAT_SEARCHPATH
8657 vim_free(fname);
8658 }
8659#endif
8660}
8661
8662/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008663 * ":open" simulation: for now just work like ":visual".
8664 */
8665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008666ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008667{
8668 regmatch_T regmatch;
8669 char_u *p;
8670
8671 curwin->w_cursor.lnum = eap->line2;
8672 beginline(BL_SOL | BL_FIX);
8673 if (*eap->arg == '/')
8674 {
8675 /* ":open /pattern/": put cursor in column found with pattern */
8676 ++eap->arg;
8677 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8678 *p = NUL;
8679 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8680 if (regmatch.regprog != NULL)
8681 {
8682 regmatch.rm_ic = p_ic;
8683 p = ml_get_curline();
8684 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008685 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008686 else
8687 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008688 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008689 }
8690 /* Move to the NUL, ignore any other arguments. */
8691 eap->arg += STRLEN(eap->arg);
8692 }
8693 check_cursor();
8694
8695 eap->cmdidx = CMD_visual;
8696 do_exedit(eap, NULL);
8697}
8698
8699/*
8700 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 */
8702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008703ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 do_exedit(eap, NULL);
8706}
8707
8708/*
8709 * ":edit <file>" command and alikes.
8710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008712do_exedit(
8713 exarg_T *eap,
8714 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008718 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 /*
8721 * ":vi" command ends Ex mode.
8722 */
8723 if (exmode_active && (eap->cmdidx == CMD_visual
8724 || eap->cmdidx == CMD_view))
8725 {
8726 exmode_active = FALSE;
8727 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008728 {
8729 /* Special case: ":global/pat/visual\NLvi-commands" */
8730 if (global_busy)
8731 {
8732 int rd = RedrawingDisabled;
8733 int nwr = no_wait_return;
8734 int ms = msg_scroll;
8735#ifdef FEAT_GUI
8736 int he = hold_gui_events;
8737#endif
8738
8739 if (eap->nextcmd != NULL)
8740 {
8741 stuffReadbuff(eap->nextcmd);
8742 eap->nextcmd = NULL;
8743 }
8744
8745 if (exmode_was != EXMODE_VIM)
8746 settmode(TMODE_RAW);
8747 RedrawingDisabled = 0;
8748 no_wait_return = 0;
8749 need_wait_return = FALSE;
8750 msg_scroll = 0;
8751#ifdef FEAT_GUI
8752 hold_gui_events = 0;
8753#endif
8754 must_redraw = CLEAR;
8755
8756 main_loop(FALSE, TRUE);
8757
8758 RedrawingDisabled = rd;
8759 no_wait_return = nwr;
8760 msg_scroll = ms;
8761#ifdef FEAT_GUI
8762 hold_gui_events = he;
8763#endif
8764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 }
8768
8769 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008770 || eap->cmdidx == CMD_tabnew
8771 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008772 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008774 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 setpcmark();
8776 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008777 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8778 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008780 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 || *eap->arg != NUL
8782#ifdef FEAT_BROWSE
8783 || cmdmod.browse
8784#endif
8785 )
8786 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008787 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8788 * can bring us here, others are stopped earlier. */
8789 if (*eap->arg != NUL && curbuf_locked())
8790 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 n = readonlymode;
8793 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8794 readonlymode = TRUE;
8795 else if (eap->cmdidx == CMD_enew)
8796 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8797 empty buffer */
8798 setpcmark();
8799 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8800 NULL, eap,
8801 /* ":edit" goes to first line if Vi compatible */
8802 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8803 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8804 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008805 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008807 /* after a split we can use an existing buffer */
8808 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008810 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 {
8812 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 if (old_curwin != NULL)
8814 {
8815 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008816 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008818#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008819 cleanup_T cs;
8820
8821 /* Reset the error/interrupt/exception state here so that
8822 * aborting() returns FALSE when closing a window. */
8823 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008824#endif
8825#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008827#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008828 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008829
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008830#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008831 /* Restore the error/interrupt/exception state if not
8832 * discarded by a new aborting error, interrupt, or
8833 * uncaught exception. */
8834 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 }
8837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 }
8839 else if (readonlymode && curbuf->b_nwindows == 1)
8840 {
8841 /* When editing an already visited buffer, 'readonly' won't be set
8842 * but the previous value is kept. With ":view" and ":sview" we
8843 * want the file to be readonly, except when another window is
8844 * editing the same buffer. */
8845 curbuf->b_p_ro = TRUE;
8846 }
8847 readonlymode = n;
8848 }
8849 else
8850 {
8851 if (eap->do_ecmd_cmd != NULL)
8852 do_cmdline_cmd(eap->do_ecmd_cmd);
8853#ifdef FEAT_TITLE
8854 n = curwin->w_arg_idx_invalid;
8855#endif
8856 check_arg_idx(curwin);
8857#ifdef FEAT_TITLE
8858 if (n != curwin->w_arg_idx_invalid)
8859 maketitle();
8860#endif
8861 }
8862
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 /*
8864 * if ":split file" worked, set alternate file name in old window to new
8865 * file
8866 */
8867 if (old_curwin != NULL
8868 && *eap->arg != NUL
8869 && curwin != old_curwin
8870 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008871 && old_curwin->w_buffer != curbuf
8872 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
8875 ex_no_reprint = TRUE;
8876}
8877
8878#ifndef FEAT_GUI
8879/*
8880 * ":gui" and ":gvim" when there is no GUI.
8881 */
8882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008883ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885 eap->errmsg = e_nogvim;
8886}
8887#endif
8888
8889#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008891ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893 gui_make_tearoff(eap->arg);
8894}
8895#endif
8896
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008897#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8898 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008900ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008902# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8903 if (gui.in_use)
8904 gui_make_popup(eap->arg, eap->forceit);
8905# ifdef FEAT_TERM_POPUP_MENU
8906 else
8907# endif
8908# endif
8909# ifdef FEAT_TERM_POPUP_MENU
8910 pum_make_popup(eap->arg, eap->forceit);
8911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912}
8913#endif
8914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008916ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8919 MSG(_("No swap file"));
8920 else
8921 msg(curbuf->b_ml.ml_mfp->mf_fname);
8922}
8923
8924/*
8925 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8926 * offset.
8927 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008930ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008933 win_T *save_curwin = curwin;
8934 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 long topline;
8936 long y;
8937 linenr_T old_linenr = curwin->w_cursor.lnum;
8938
8939 setpcmark();
8940
8941 /*
8942 * determine max topline
8943 */
8944 if (curwin->w_p_scb)
8945 {
8946 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008947 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
8949 if (wp->w_p_scb && wp->w_buffer)
8950 {
8951 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8952 if (topline > y)
8953 topline = y;
8954 }
8955 }
8956 if (topline < 1)
8957 topline = 1;
8958 }
8959 else
8960 {
8961 topline = 1;
8962 }
8963
8964
8965 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008966 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008968 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 {
8970 if (curwin->w_p_scb)
8971 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008972 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 y = topline - curwin->w_topline;
8974 if (y > 0)
8975 scrollup(y, TRUE);
8976 else
8977 scrolldown(-y, TRUE);
8978 curwin->w_scbind_pos = topline;
8979 redraw_later(VALID);
8980 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 }
8983 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008984 curwin = save_curwin;
8985 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 if (curwin->w_p_scb)
8987 {
8988 did_syncbind = TRUE;
8989 checkpcmark();
8990 if (old_linenr != curwin->w_cursor.lnum)
8991 {
8992 char_u ctrl_o[2];
8993
8994 ctrl_o[0] = Ctrl_O;
8995 ctrl_o[1] = 0;
8996 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8997 }
8998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
9001
9002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009003ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009005 int i;
9006 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9007 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008
9009 if (eap->usefilter) /* :r!cmd */
9010 do_bang(1, eap, FALSE, FALSE, TRUE);
9011 else
9012 {
9013 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9014 return;
9015
9016#ifdef FEAT_BROWSE
9017 if (cmdmod.browse)
9018 {
9019 char_u *browseFile;
9020
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009021 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 NULL, NULL, NULL, curbuf);
9023 if (browseFile != NULL)
9024 {
9025 i = readfile(browseFile, NULL,
9026 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9027 vim_free(browseFile);
9028 }
9029 else
9030 i = OK;
9031 }
9032 else
9033#endif
9034 if (*eap->arg == NUL)
9035 {
9036 if (check_fname() == FAIL) /* check for no file name */
9037 return;
9038 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9039 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9040 }
9041 else
9042 {
9043 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9044 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9045 i = readfile(eap->arg, NULL,
9046 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9047
9048 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009049 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009051#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 if (!aborting())
9053#endif
9054 EMSG2(_(e_notopen), eap->arg);
9055 }
9056 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009057 {
9058 if (empty && exmode_active)
9059 {
9060 /* Delete the empty line that remains. Historically ex does
9061 * this but vi doesn't. */
9062 if (eap->line2 == 0)
9063 lnum = curbuf->b_ml.ml_line_count;
9064 else
9065 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009066 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009067 {
9068 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009069 if (curwin->w_cursor.lnum > 1
9070 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009071 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009072 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009073 }
9074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 }
9078}
9079
Bram Moolenaarea408852005-06-25 22:49:46 +00009080static char_u *prev_dir = NULL;
9081
9082#if defined(EXITFREE) || defined(PROTO)
9083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009084free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009085{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009086 VIM_CLEAR(prev_dir);
9087 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009088}
9089#endif
9090
Bram Moolenaarf4258302013-06-02 18:20:17 +02009091/*
9092 * Deal with the side effects of changing the current directory.
9093 * When "local" is TRUE then this was after an ":lcd" command.
9094 */
9095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009096post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009097{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009098 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009099 if (local)
9100 {
9101 /* If still in global directory, need to remember current
9102 * directory as global directory. */
9103 if (globaldir == NULL && prev_dir != NULL)
9104 globaldir = vim_strsave(prev_dir);
9105 /* Remember this local directory for the window. */
9106 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9107 curwin->w_localdir = vim_strsave(NameBuff);
9108 }
9109 else
9110 {
9111 /* We are now in the global directory, no need to remember its
9112 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009113 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009114 }
9115
9116 shorten_fnames(TRUE);
9117}
9118
Bram Moolenaarea408852005-06-25 22:49:46 +00009119
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120/*
9121 * ":cd", ":lcd", ":chdir" and ":lchdir".
9122 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009123 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009124ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 char_u *new_dir;
9127 char_u *tofree;
9128
9129 new_dir = eap->arg;
9130#if !defined(UNIX) && !defined(VMS)
9131 /* for non-UNIX ":cd" means: print current directory */
9132 if (*new_dir == NUL)
9133 ex_pwd(NULL);
9134 else
9135#endif
9136 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009137 if (allbuf_locked())
9138 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009139 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9140 && !eap->forceit)
9141 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009142 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009143 return;
9144 }
9145
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 /* ":cd -": Change to previous directory */
9147 if (STRCMP(new_dir, "-") == 0)
9148 {
9149 if (prev_dir == NULL)
9150 {
9151 EMSG(_("E186: No previous directory"));
9152 return;
9153 }
9154 new_dir = prev_dir;
9155 }
9156
9157 /* Save current directory for next ":cd -" */
9158 tofree = prev_dir;
9159 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9160 prev_dir = vim_strsave(NameBuff);
9161 else
9162 prev_dir = NULL;
9163
9164#if defined(UNIX) || defined(VMS)
9165 /* for UNIX ":cd" means: go to home directory */
9166 if (*new_dir == NUL)
9167 {
9168 /* use NameBuff for home directory name */
9169# ifdef VMS
9170 char_u *p;
9171
9172 p = mch_getenv((char_u *)"SYS$LOGIN");
9173 if (p == NULL || *p == NUL) /* empty is the same as not set */
9174 NameBuff[0] = NUL;
9175 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009176 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177# else
9178 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9179# endif
9180 new_dir = NameBuff;
9181 }
9182#endif
9183 if (new_dir == NULL || vim_chdir(new_dir))
9184 EMSG(_(e_failed));
9185 else
9186 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009187 int is_local_chdir = eap->cmdidx == CMD_lcd
9188 || eap->cmdidx == CMD_lchdir;
9189
9190 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191
9192 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009193 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009195 apply_autocmds(EVENT_DIRCHANGED,
9196 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9197 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 }
9199 vim_free(tofree);
9200 }
9201}
9202
9203/*
9204 * ":pwd".
9205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009207ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9210 {
9211#ifdef BACKSLASH_IN_FILENAME
9212 slash_adjust(NameBuff);
9213#endif
9214 msg(NameBuff);
9215 }
9216 else
9217 EMSG(_("E187: Unknown"));
9218}
9219
9220/*
9221 * ":=".
9222 */
9223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009224ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
9226 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009227 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228}
9229
9230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009231ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009233 int n;
9234 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235
9236 if (cursor_valid())
9237 {
9238 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9239 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009240 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009242
9243 len = eap->line2;
9244 switch (*eap->arg)
9245 {
9246 case 'm': break;
9247 case NUL: len *= 1000L; break;
9248 default: EMSG2(_(e_invarg2), eap->arg); return;
9249 }
9250 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251}
9252
9253/*
9254 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9255 */
9256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009257do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009260 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
9262 cursor_on();
9263 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009264 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009266 wait_now = msec - done > 1000L ? 1000L : msec - done;
9267#ifdef FEAT_TIMERS
9268 {
9269 long due_time = check_due_timer();
9270
9271 if (due_time > 0 && due_time < wait_now)
9272 wait_now = due_time;
9273 }
9274#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009275#ifdef FEAT_JOB_CHANNEL
9276 if (has_any_channel() && wait_now > 100L)
9277 wait_now = 100L;
9278#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009279 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009280#ifdef FEAT_JOB_CHANNEL
9281 if (has_any_channel())
9282 ui_breakcheck_force(TRUE);
9283 else
9284#endif
9285 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009286#ifdef MESSAGE_QUEUE
9287 /* Process the netbeans and clientserver messages that may have been
9288 * received in the call to ui_breakcheck() when the GUI is in use. This
9289 * may occur when running a test case. */
9290 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009293
9294 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9295 // input buffer, otherwise a following call to input() fails.
9296 if (got_int)
9297 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298}
9299
9300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009301do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302{
9303 int mode;
9304 char_u *cmdp;
9305
9306 cmdp = eap->cmd;
9307 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9308
9309 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9310 eap->arg, mode, isabbrev))
9311 {
9312 case 1: EMSG(_(e_invarg));
9313 break;
9314 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9315 break;
9316 }
9317}
9318
9319/*
9320 * ":winsize" command (obsolete).
9321 */
9322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009323ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 int w, h;
9326 char_u *arg = eap->arg;
9327 char_u *p;
9328
9329 w = getdigits(&arg);
9330 arg = skipwhite(arg);
9331 p = arg;
9332 h = getdigits(&arg);
9333 if (*p != NUL && *arg == NUL)
9334 set_shellsize(w, h, TRUE);
9335 else
9336 EMSG(_("E465: :winsize requires two number arguments"));
9337}
9338
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009340ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
9342 int xchar = NUL;
9343 char_u *p;
9344
9345 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9346 {
9347 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9348 if (eap->arg[1] == NUL)
9349 {
9350 EMSG(_(e_invarg));
9351 return;
9352 }
9353 xchar = eap->arg[1];
9354 p = eap->arg + 2;
9355 }
9356 else
9357 p = eap->arg + 1;
9358
9359 eap->nextcmd = check_nextcmd(p);
9360 p = skipwhite(p);
9361 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9362 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009363 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 {
9365 /* Pass flags on for ":vertical wincmd ]". */
9366 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009367 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9369 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009370 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 }
9372}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
Bram Moolenaar843ee412004-06-30 16:16:41 +00009374#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375/*
9376 * ":winpos".
9377 */
9378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009379ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381 int x, y;
9382 char_u *arg = eap->arg;
9383 char_u *p;
9384
9385 if (*arg == NUL)
9386 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009387# if defined(FEAT_GUI) || defined(MSWIN)
9388# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009390# else
9391 if (mch_get_winpos(&x, &y) != FAIL)
9392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 {
9394 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9395 msg(IObuff);
9396 }
9397 else
9398# endif
9399 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9400 }
9401 else
9402 {
9403 x = getdigits(&arg);
9404 arg = skipwhite(arg);
9405 p = arg;
9406 y = getdigits(&arg);
9407 if (*p == NUL || *arg != NUL)
9408 {
9409 EMSG(_("E466: :winpos requires two number arguments"));
9410 return;
9411 }
9412# ifdef FEAT_GUI
9413 if (gui.in_use)
9414 gui_mch_set_winpos(x, y);
9415 else if (gui.starting)
9416 {
9417 /* Remember the coordinates for when the window is opened. */
9418 gui_win_x = x;
9419 gui_win_y = y;
9420 }
9421# ifdef HAVE_TGETENT
9422 else
9423# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009424# else
9425# ifdef MSWIN
9426 mch_set_winpos(x, y);
9427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428# endif
9429# ifdef HAVE_TGETENT
9430 if (*T_CWP)
9431 term_set_winpos(x, y);
9432# endif
9433 }
9434}
9435#endif
9436
9437/*
9438 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9439 */
9440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009441ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442{
9443 oparg_T oa;
9444
9445 clear_oparg(&oa);
9446 oa.regname = eap->regname;
9447 oa.start.lnum = eap->line1;
9448 oa.end.lnum = eap->line2;
9449 oa.line_count = eap->line2 - eap->line1 + 1;
9450 oa.motion_type = MLINE;
9451#ifdef FEAT_VIRTUALEDIT
9452 virtual_op = FALSE;
9453#endif
9454 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9455 {
9456 setpcmark();
9457 curwin->w_cursor.lnum = eap->line1;
9458 beginline(BL_SOL | BL_FIX);
9459 }
9460
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009461 if (VIsual_active)
9462 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 switch (eap->cmdidx)
9465 {
9466 case CMD_delete:
9467 oa.op_type = OP_DELETE;
9468 op_delete(&oa);
9469 break;
9470
9471 case CMD_yank:
9472 oa.op_type = OP_YANK;
9473 (void)op_yank(&oa, FALSE, TRUE);
9474 break;
9475
9476 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009477 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009479 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9480#else
9481 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009483 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 oa.op_type = OP_RSHIFT;
9485 else
9486 oa.op_type = OP_LSHIFT;
9487 op_shift(&oa, FALSE, eap->amount);
9488 break;
9489 }
9490#ifdef FEAT_VIRTUALEDIT
9491 virtual_op = MAYBE;
9492#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009493 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
9497 * ":put".
9498 */
9499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 /* ":0put" works like ":1put!". */
9503 if (eap->line2 == 0)
9504 {
9505 eap->line2 = 1;
9506 eap->forceit = TRUE;
9507 }
9508 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009509 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9510 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511}
9512
9513/*
9514 * Handle ":copy" and ":move".
9515 */
9516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009517ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 long n;
9520
Bram Moolenaarded27822017-01-02 14:27:34 +01009521 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 if (eap->arg == NULL) /* error detected */
9523 {
9524 eap->nextcmd = NULL;
9525 return;
9526 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009527 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
9529 /*
9530 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9531 */
9532 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9533 {
9534 EMSG(_(e_invaddr));
9535 return;
9536 }
9537
9538 if (eap->cmdidx == CMD_move)
9539 {
9540 if (do_move(eap->line1, eap->line2, n) == FAIL)
9541 return;
9542 }
9543 else
9544 ex_copy(eap->line1, eap->line2, n);
9545 u_clearline();
9546 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009547 ex_may_print(eap);
9548}
9549
9550/*
9551 * Print the current line if flags were given to the Ex command.
9552 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009553 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009554ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009555{
9556 if (eap->flags != 0)
9557 {
9558 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9559 (eap->flags & EXFLAG_LIST));
9560 ex_no_reprint = TRUE;
9561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562}
9563
9564/*
9565 * ":smagic" and ":snomagic".
9566 */
9567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009568ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570 int magic_save = p_magic;
9571
9572 p_magic = (eap->cmdidx == CMD_smagic);
9573 do_sub(eap);
9574 p_magic = magic_save;
9575}
9576
9577/*
9578 * ":join".
9579 */
9580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009581ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 curwin->w_cursor.lnum = eap->line1;
9584 if (eap->line1 == eap->line2)
9585 {
9586 if (eap->addr_count >= 2) /* :2,2join does nothing */
9587 return;
9588 if (eap->line2 == curbuf->b_ml.ml_line_count)
9589 {
9590 beep_flush();
9591 return;
9592 }
9593 ++eap->line2;
9594 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009595 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009597 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598}
9599
9600/*
9601 * ":[addr]@r" or ":[addr]*r": execute register
9602 */
9603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009604ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009607 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608
9609 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009610 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611
9612#ifdef USE_ON_FLY_SCROLL
9613 dont_scroll = TRUE; /* disallow scrolling here */
9614#endif
9615
9616 /* get the register name. No name means to use the previous one */
9617 c = *eap->arg;
9618 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9619 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009620 /* Put the register in the typeahead buffer with the "silent" flag. */
9621 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9622 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 else
9627 {
9628 int save_efr = exec_from_reg;
9629
9630 exec_from_reg = TRUE;
9631
9632 /*
9633 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009634 * Continue until the stuff buffer is empty and all added characters
9635 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009637 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9639
9640 exec_from_reg = save_efr;
9641 }
9642}
9643
9644/*
9645 * ":!".
9646 */
9647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009648ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
9650 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9651}
9652
9653/*
9654 * ":undo".
9655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009657ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009659 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009660 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009661 else
9662 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663}
9664
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009665#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009667ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009668{
9669 char_u hash[UNDO_HASH_SIZE];
9670
9671 u_compute_hash(hash);
9672 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9673}
9674
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009676ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009677{
9678 char_u hash[UNDO_HASH_SIZE];
9679
9680 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009681 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009682}
9683#endif
9684
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685/*
9686 * ":redo".
9687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009689ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690{
9691 u_redo(1);
9692}
9693
9694/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009695 * ":earlier" and ":later".
9696 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009698ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009699{
9700 long count = 0;
9701 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009702 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009703 char_u *p = eap->arg;
9704
9705 if (*p == NUL)
9706 count = 1;
9707 else if (isdigit(*p))
9708 {
9709 count = getdigits(&p);
9710 switch (*p)
9711 {
9712 case 's': ++p; sec = TRUE; break;
9713 case 'm': ++p; sec = TRUE; count *= 60; break;
9714 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009715 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9716 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009717 }
9718 }
9719
9720 if (*p != NUL)
9721 EMSG2(_(e_invarg2), eap->arg);
9722 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009723 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9724 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009725}
9726
9727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 * ":redir": start/stop redirection.
9729 */
9730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009731ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 char *mode;
9734 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009735 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736
Bram Moolenaarba768492016-07-08 15:32:54 +02009737#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009738 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009739 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009740 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009741 return;
9742 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009743#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009744
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (STRICMP(eap->arg, "END") == 0)
9746 close_redir();
9747 else
9748 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009749 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009751 ++arg;
9752 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009754 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 mode = "a";
9756 }
9757 else
9758 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009759 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760
9761 close_redir();
9762
9763 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009764 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 if (fname == NULL)
9766 return;
9767#ifdef FEAT_BROWSE
9768 if (cmdmod.browse)
9769 {
9770 char_u *browseFile;
9771
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009772 browseFile = do_browse(BROWSE_SAVE,
9773 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009774 fname, NULL, NULL,
9775 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 if (browseFile == NULL)
9777 return; /* operation cancelled */
9778 vim_free(fname);
9779 fname = browseFile;
9780 eap->forceit = TRUE; /* since dialog already asked */
9781 }
9782#endif
9783
9784 redir_fd = open_exfile(fname, eap->forceit, mode);
9785 vim_free(fname);
9786 }
9787#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009788 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 {
9790 /* redirect to a register a-z (resp. A-Z for appending) */
9791 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009792 ++arg;
9793 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009795 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009796 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009798 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009800 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009801 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009802 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009805 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009806 if (*arg == '>')
9807 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009808 /* Make register empty when not using @A-@Z and the
9809 * command is valid. */
9810 if (*arg == NUL && !isupper(redir_reg))
9811 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009813 }
9814 if (*arg != NUL)
9815 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009816 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009817 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009818 }
9819 }
9820 else if (*arg == '=' && arg[1] == '>')
9821 {
9822 int append;
9823
9824 /* redirect to a variable */
9825 close_redir();
9826 arg += 2;
9827
9828 if (*arg == '>')
9829 {
9830 ++arg;
9831 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 }
9833 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009834 append = FALSE;
9835
9836 if (var_redir_start(skipwhite(arg), append) == OK)
9837 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 }
9839#endif
9840
9841 /* TODO: redirect to a buffer */
9842
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009844 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009846
9847 /* Make sure redirection is not off. Can happen for cmdline completion
9848 * that indirectly invokes a command to catch its output. */
9849 if (redir_fd != NULL
9850#ifdef FEAT_EVAL
9851 || redir_reg || redir_vname
9852#endif
9853 )
9854 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855}
9856
9857/*
9858 * ":redraw": force redraw
9859 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009861ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 int r = RedrawingDisabled;
9864 int p = p_lz;
9865
9866 RedrawingDisabled = 0;
9867 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009868 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009870 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871#ifdef FEAT_TITLE
9872 if (need_maketitle)
9873 maketitle();
9874#endif
9875 RedrawingDisabled = r;
9876 p_lz = p;
9877
9878 /* Reset msg_didout, so that a message that's there is overwritten. */
9879 msg_didout = FALSE;
9880 msg_col = 0;
9881
Bram Moolenaar56667a52013-07-17 11:54:28 +02009882 /* No need to wait after an intentional redraw. */
9883 need_wait_return = FALSE;
9884
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 out_flush();
9886}
9887
9888/*
9889 * ":redrawstatus": force redraw of status line(s)
9890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009892ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 int r = RedrawingDisabled;
9895 int p = p_lz;
9896
9897 RedrawingDisabled = 0;
9898 p_lz = FALSE;
9899 if (eap->forceit)
9900 status_redraw_all();
9901 else
9902 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009903 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 RedrawingDisabled = r;
9905 p_lz = p;
9906 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907}
9908
9909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009910close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
9912 if (redir_fd != NULL)
9913 {
9914 fclose(redir_fd);
9915 redir_fd = NULL;
9916 }
9917#ifdef FEAT_EVAL
9918 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009919 if (redir_vname)
9920 {
9921 var_redir_stop();
9922 redir_vname = 0;
9923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#endif
9925}
9926
9927#if defined(FEAT_SESSION) && defined(USE_CRNL)
9928# define MKSESSION_NL
9929static int mksession_nl = FALSE; /* use NL only in put_eol() */
9930#endif
9931
9932/*
9933 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9934 */
9935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009936ex_mkrc(
9937 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938{
9939 FILE *fd;
9940 int failed = FALSE;
9941 char_u *fname;
9942#ifdef FEAT_BROWSE
9943 char_u *browseFile = NULL;
9944#endif
9945#ifdef FEAT_SESSION
9946 int view_session = FALSE;
9947 int using_vdir = FALSE; /* using 'viewdir'? */
9948 char_u *viewFile = NULL;
9949 unsigned *flagp;
9950#endif
9951
9952 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9953 {
9954#ifdef FEAT_SESSION
9955 view_session = TRUE;
9956#else
9957 ex_ni(eap);
9958 return;
9959#endif
9960 }
9961
9962#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009963 /* Use the short file name until ":lcd" is used. We also don't use the
9964 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009965 did_lcd = FALSE;
9966
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9968 if (eap->cmdidx == CMD_mkview
9969 && (*eap->arg == NUL
9970 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9971 {
9972 eap->forceit = TRUE;
9973 fname = get_view_file(*eap->arg);
9974 if (fname == NULL)
9975 return;
9976 viewFile = fname;
9977 using_vdir = TRUE;
9978 }
9979 else
9980#endif
9981 if (*eap->arg != NUL)
9982 fname = eap->arg;
9983 else if (eap->cmdidx == CMD_mkvimrc)
9984 fname = (char_u *)VIMRC_FILE;
9985#ifdef FEAT_SESSION
9986 else if (eap->cmdidx == CMD_mksession)
9987 fname = (char_u *)SESSION_FILE;
9988#endif
9989 else
9990 fname = (char_u *)EXRC_FILE;
9991
9992#ifdef FEAT_BROWSE
9993 if (cmdmod.browse)
9994 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009995 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996# ifdef FEAT_SESSION
9997 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9998 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9999# endif
10000 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010001 fname, (char_u *)"vim", NULL,
10002 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 if (browseFile == NULL)
10004 goto theend;
10005 fname = browseFile;
10006 eap->forceit = TRUE; /* since dialog already asked */
10007 }
10008#endif
10009
10010#if defined(FEAT_SESSION) && defined(vim_mkdir)
10011 /* When using 'viewdir' may have to create the directory. */
10012 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010013 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#endif
10015
10016 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10017 if (fd != NULL)
10018 {
10019#ifdef FEAT_SESSION
10020 if (eap->cmdidx == CMD_mkview)
10021 flagp = &vop_flags;
10022 else
10023 flagp = &ssop_flags;
10024#endif
10025
10026#ifdef MKSESSION_NL
10027 /* "unix" in 'sessionoptions': use NL line separator */
10028 if (view_session && (*flagp & SSOP_UNIX))
10029 mksession_nl = TRUE;
10030#endif
10031
10032 /* Write the version command for :mkvimrc */
10033 if (eap->cmdidx == CMD_mkvimrc)
10034 (void)put_line(fd, "version 6.0");
10035
10036#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010037 if (eap->cmdidx == CMD_mksession)
10038 {
10039 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10040 failed = TRUE;
10041 }
10042
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (eap->cmdidx != CMD_mkview)
10044#endif
10045 {
10046 /* Write setting 'compatible' first, because it has side effects.
10047 * For that same reason only do it when needed. */
10048 if (p_cp)
10049 (void)put_line(fd, "if !&cp | set cp | endif");
10050 else
10051 (void)put_line(fd, "if &cp | set nocp | endif");
10052 }
10053
10054#ifdef FEAT_SESSION
10055 if (!view_session
10056 || (eap->cmdidx == CMD_mksession
10057 && (*flagp & SSOP_OPTIONS)))
10058#endif
10059 failed |= (makemap(fd, NULL) == FAIL
10060 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10061
10062#ifdef FEAT_SESSION
10063 if (!failed && view_session)
10064 {
10065 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10066 failed = TRUE;
10067 if (eap->cmdidx == CMD_mksession)
10068 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010069 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaard9462e32011-04-11 21:35:11 +020010071 dirnow = alloc(MAXPATHL);
10072 if (dirnow == NULL)
10073 failed = TRUE;
10074 else
10075 {
10076 /*
10077 * Change to session file's dir.
10078 */
10079 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010081 *dirnow = NUL;
10082 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10083 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010084 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010085 shorten_fnames(TRUE);
10086 }
10087 else if (*dirnow != NUL
10088 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10089 {
10090 if (mch_chdir((char *)globaldir) == 0)
10091 shorten_fnames(TRUE);
10092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
Bram Moolenaard9462e32011-04-11 21:35:11 +020010094 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
Bram Moolenaard9462e32011-04-11 21:35:11 +020010096 /* restore original dir */
10097 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010099 {
10100 if (mch_chdir((char *)dirnow) != 0)
10101 EMSG(_(e_prev_dir));
10102 shorten_fnames(TRUE);
10103 }
10104 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 }
10106 }
10107 else
10108 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010109 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10110 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 }
10112 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10113 == FAIL)
10114 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010115 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10116 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010117 if (eap->cmdidx == CMD_mksession)
10118 {
10119 if (put_line(fd, "unlet SessionLoad") == FAIL)
10120 failed = TRUE;
10121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 }
10123#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010124 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10125 failed = TRUE;
10126
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 failed |= fclose(fd);
10128
10129 if (failed)
10130 EMSG(_(e_write));
10131#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10132 else if (eap->cmdidx == CMD_mksession)
10133 {
10134 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010135 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136
Bram Moolenaard9462e32011-04-11 21:35:11 +020010137 tbuf = alloc(MAXPATHL);
10138 if (tbuf != NULL)
10139 {
10140 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10141 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10142 vim_free(tbuf);
10143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 }
10145#endif
10146#ifdef MKSESSION_NL
10147 mksession_nl = FALSE;
10148#endif
10149 }
10150
10151#ifdef FEAT_BROWSE
10152theend:
10153 vim_free(browseFile);
10154#endif
10155#ifdef FEAT_SESSION
10156 vim_free(viewFile);
10157#endif
10158}
10159
Bram Moolenaardf177f62005-02-22 08:39:57 +000010160#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10161 || defined(PROTO)
10162 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010163vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010164{
10165 if (vim_mkdir(name, prot) != 0)
10166 {
10167 EMSG2(_("E739: Cannot create directory: %s"), name);
10168 return FAIL;
10169 }
10170 return OK;
10171}
10172#endif
10173
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174/*
10175 * Open a file for writing for an Ex command, with some checks.
10176 * Return file descriptor, or NULL on failure.
10177 */
10178 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010179open_exfile(
10180 char_u *fname,
10181 int forceit,
10182 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
10184 FILE *fd;
10185
10186#ifdef UNIX
10187 /* with Unix it is possible to open a directory */
10188 if (mch_isdir(fname))
10189 {
10190 EMSG2(_(e_isadir2), fname);
10191 return NULL;
10192 }
10193#endif
10194 if (!forceit && *mode != 'a' && vim_fexists(fname))
10195 {
10196 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10197 return NULL;
10198 }
10199
10200 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10201 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10202
10203 return fd;
10204}
10205
10206/*
10207 * ":mark" and ":k".
10208 */
10209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010210ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211{
10212 pos_T pos;
10213
10214 if (*eap->arg == NUL) /* No argument? */
10215 EMSG(_(e_argreq));
10216 else if (eap->arg[1] != NUL) /* more than one character? */
10217 EMSG(_(e_trailing));
10218 else
10219 {
10220 pos = curwin->w_cursor; /* save curwin->w_cursor */
10221 curwin->w_cursor.lnum = eap->line2;
10222 beginline(BL_WHITE | BL_FIX);
10223 if (setmark(*eap->arg) == FAIL) /* set mark */
10224 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10225 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10226 }
10227}
10228
10229/*
10230 * Update w_topline, w_leftcol and the cursor position.
10231 */
10232 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010233update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235 check_cursor(); /* put cursor on valid line */
10236 update_topline();
10237 if (!curwin->w_p_wrap)
10238 validate_cursor();
10239 update_curswant();
10240}
10241
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010243 * Save the current State and go to Normal mode.
10244 * Return TRUE if the typeahead could be saved.
10245 */
10246 int
10247save_current_state(save_state_T *sst)
10248{
10249 sst->save_msg_scroll = msg_scroll;
10250 sst->save_restart_edit = restart_edit;
10251 sst->save_msg_didout = msg_didout;
10252 sst->save_State = State;
10253 sst->save_insertmode = p_im;
10254 sst->save_finish_op = finish_op;
10255 sst->save_opcount = opcount;
10256
10257 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10258 restart_edit = 0; /* don't go to Insert mode */
10259 p_im = FALSE; /* don't use 'insertmode' */
10260
10261 /*
10262 * Save the current typeahead. This is required to allow using ":normal"
10263 * from an event handler and makes sure we don't hang when the argument
10264 * ends with half a command.
10265 */
10266 save_typeahead(&sst->tabuf);
10267 return sst->tabuf.typebuf_valid;
10268}
10269
10270 void
10271restore_current_state(save_state_T *sst)
10272{
10273 /* Restore the previous typeahead. */
10274 restore_typeahead(&sst->tabuf);
10275
10276 msg_scroll = sst->save_msg_scroll;
10277 restart_edit = sst->save_restart_edit;
10278 p_im = sst->save_insertmode;
10279 finish_op = sst->save_finish_op;
10280 opcount = sst->save_opcount;
10281 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10282
10283 /* Restore the state (needed when called from a function executed for
10284 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10285 State = sst->save_State;
10286#ifdef CURSOR_SHAPE
10287 ui_cursor_shape(); /* may show different cursor shape */
10288#endif
10289}
10290
10291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 * ":normal[!] {commands}": Execute normal mode commands.
10293 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010295ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010297 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#ifdef FEAT_MBYTE
10299 char_u *arg = NULL;
10300 int l;
10301 char_u *p;
10302#endif
10303
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010304 if (ex_normal_lock > 0)
10305 {
10306 EMSG(_(e_secure));
10307 return;
10308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (ex_normal_busy >= p_mmd)
10310 {
10311 EMSG(_("E192: Recursive use of :normal too deep"));
10312 return;
10313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314
10315#ifdef FEAT_MBYTE
10316 /*
10317 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10318 * this for the K_SPECIAL leading byte, otherwise special keys will not
10319 * work.
10320 */
10321 if (has_mbyte)
10322 {
10323 int len = 0;
10324
10325 /* Count the number of characters to be escaped. */
10326 for (p = eap->arg; *p != NUL; ++p)
10327 {
10328# ifdef FEAT_GUI
10329 if (*p == CSI) /* leadbyte CSI */
10330 len += 2;
10331# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010332 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10334# ifdef FEAT_GUI
10335 || *p == CSI
10336# endif
10337 )
10338 len += 2;
10339 }
10340 if (len > 0)
10341 {
10342 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10343 if (arg != NULL)
10344 {
10345 len = 0;
10346 for (p = eap->arg; *p != NUL; ++p)
10347 {
10348 arg[len++] = *p;
10349# ifdef FEAT_GUI
10350 if (*p == CSI)
10351 {
10352 arg[len++] = KS_EXTRA;
10353 arg[len++] = (int)KE_CSI;
10354 }
10355# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010356 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
10358 arg[len++] = *++p;
10359 if (*p == K_SPECIAL)
10360 {
10361 arg[len++] = KS_SPECIAL;
10362 arg[len++] = KE_FILLER;
10363 }
10364# ifdef FEAT_GUI
10365 else if (*p == CSI)
10366 {
10367 arg[len++] = KS_EXTRA;
10368 arg[len++] = (int)KE_CSI;
10369 }
10370# endif
10371 }
10372 arg[len] = NUL;
10373 }
10374 }
10375 }
10376 }
10377#endif
10378
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010379 ++ex_normal_busy;
10380 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 {
10382 /*
10383 * Repeat the :normal command for each line in the range. When no
10384 * range given, execute it just once, without positioning the cursor
10385 * first.
10386 */
10387 do
10388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 if (eap->addr_count != 0)
10390 {
10391 curwin->w_cursor.lnum = eap->line1++;
10392 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010393 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
10395
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010396 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397#ifdef FEAT_MBYTE
10398 arg != NULL ? arg :
10399#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010400 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 }
10402 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10403 }
10404
10405 /* Might not return to the main loop when in an event handler. */
10406 update_topline_cursor();
10407
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010408 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010410#ifdef FEAT_MOUSE
10411 setmouse();
10412#endif
10413#ifdef CURSOR_SHAPE
10414 ui_cursor_shape(); /* may show different cursor shape */
10415#endif
10416
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417#ifdef FEAT_MBYTE
10418 vim_free(arg);
10419#endif
10420}
10421
10422/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010423 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 */
10425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010426ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010428 if (eap->forceit)
10429 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010430 /* cursor line can be zero on startup */
10431 if (!curwin->w_cursor.lnum)
10432 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010433 coladvance((colnr_T)MAXCOL);
10434 curwin->w_curswant = MAXCOL;
10435 curwin->w_set_curswant = FALSE;
10436 }
10437
Bram Moolenaara40c5002005-01-09 21:16:21 +000010438 /* Ignore the command when already in Insert mode. Inserting an
10439 * expression register that invokes a function can do this. */
10440 if (State & INSERT)
10441 return;
10442
Bram Moolenaar64969662005-12-14 21:59:55 +000010443 if (eap->cmdidx == CMD_startinsert)
10444 restart_edit = 'a';
10445 else if (eap->cmdidx == CMD_startreplace)
10446 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010448 restart_edit = 'V';
10449
10450 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010452 if (eap->cmdidx == CMD_startinsert)
10453 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 curwin->w_curswant = 0; /* avoid MAXCOL */
10455 }
10456}
10457
10458/*
10459 * ":stopinsert"
10460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010462ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
10464 restart_edit = 0;
10465 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010466 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010469/*
10470 * Execute normal mode command "cmd".
10471 * "remap" can be REMAP_NONE or REMAP_YES.
10472 */
10473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010474exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010475{
Bram Moolenaar25281632016-01-21 23:32:32 +010010476 /* Stuff the argument into the typeahead buffer. */
10477 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010478 exec_normal(FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010479}
Bram Moolenaar25281632016-01-21 23:32:32 +010010480
Bram Moolenaar25281632016-01-21 23:32:32 +010010481/*
10482 * Execute normal_cmd() until there is no typeahead left.
10483 */
10484 void
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010485exec_normal(int was_typed, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010486{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010487 oparg_T oa;
10488
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010489 clear_oparg(&oa);
10490 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010491 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10492 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010493 {
10494 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010495#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010496 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010497 && oa.op_type == OP_NOP && oa.regname == NUL
10498 && !VIsual_active)
10499 {
10500 /* If terminal_loop() returns OK we got a key that is handled
10501 * in Normal model. With FAIL we first need to position the
10502 * cursor and the screen needs to be redrawn. */
10503 if (terminal_loop(TRUE) == OK)
10504 normal_cmd(&oa, TRUE);
10505 }
10506 else
10507#endif
10508 /* execute a Normal mode cmd */
10509 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010510 }
10511}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010512
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513#ifdef FEAT_FIND_ID
10514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010515ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10518 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10519 (linenr_T)1, (linenr_T)MAXLNUM);
10520}
10521
Bram Moolenaar4033c552017-09-16 20:54:51 +020010522#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523/*
10524 * ":psearch"
10525 */
10526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010527ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528{
10529 g_do_tagpreview = p_pvh;
10530 ex_findpat(eap);
10531 g_do_tagpreview = 0;
10532}
10533#endif
10534
10535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010536ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537{
10538 int whole = TRUE;
10539 long n;
10540 char_u *p;
10541 int action;
10542
10543 switch (cmdnames[eap->cmdidx].cmd_name[2])
10544 {
10545 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10546 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10547 action = ACTION_GOTO;
10548 else
10549 action = ACTION_SHOW;
10550 break;
10551 case 'i': /* ":ilist" and ":dlist" */
10552 action = ACTION_SHOW_ALL;
10553 break;
10554 case 'u': /* ":ijump" and ":djump" */
10555 action = ACTION_GOTO;
10556 break;
10557 default: /* ":isplit" and ":dsplit" */
10558 action = ACTION_SPLIT;
10559 break;
10560 }
10561
10562 n = 1;
10563 if (vim_isdigit(*eap->arg)) /* get count */
10564 {
10565 n = getdigits(&eap->arg);
10566 eap->arg = skipwhite(eap->arg);
10567 }
10568 if (*eap->arg == '/') /* Match regexp, not just whole words */
10569 {
10570 whole = FALSE;
10571 ++eap->arg;
10572 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10573 if (*p)
10574 {
10575 *p++ = NUL;
10576 p = skipwhite(p);
10577
10578 /* Check for trailing illegal characters */
10579 if (!ends_excmd(*p))
10580 eap->errmsg = e_trailing;
10581 else
10582 eap->nextcmd = check_nextcmd(p);
10583 }
10584 }
10585 if (!eap->skip)
10586 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10587 whole, !eap->forceit,
10588 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10589 n, action, eap->line1, eap->line2);
10590}
10591#endif
10592
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593
Bram Moolenaar4033c552017-09-16 20:54:51 +020010594#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595/*
10596 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10597 */
10598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010599ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010601 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10603}
10604
10605/*
10606 * ":pedit"
10607 */
10608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010609ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610{
10611 win_T *curwin_save = curwin;
10612
10613 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010614 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010615 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 do_exedit(eap, NULL);
10617 keep_help_flag = FALSE;
10618 if (curwin != curwin_save && win_valid(curwin_save))
10619 {
10620 /* Return cursor to where we were */
10621 validate_cursor();
10622 redraw_later(VALID);
10623 win_enter(curwin_save, TRUE);
10624 }
10625 g_do_tagpreview = 0;
10626}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628
10629/*
10630 * ":stag", ":stselect" and ":stjump".
10631 */
10632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010633ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
10635 postponed_split = -1;
10636 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010637 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10639 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010640 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642
10643/*
10644 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10645 */
10646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010647ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648{
10649 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10650}
10651
10652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010653ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654{
10655 int cmd;
10656
10657 switch (name[1])
10658 {
10659 case 'j': cmd = DT_JUMP; /* ":tjump" */
10660 break;
10661 case 's': cmd = DT_SELECT; /* ":tselect" */
10662 break;
10663 case 'p': cmd = DT_PREV; /* ":tprevious" */
10664 break;
10665 case 'N': cmd = DT_PREV; /* ":tNext" */
10666 break;
10667 case 'n': cmd = DT_NEXT; /* ":tnext" */
10668 break;
10669 case 'o': cmd = DT_POP; /* ":pop" */
10670 break;
10671 case 'f': /* ":tfirst" */
10672 case 'r': cmd = DT_FIRST; /* ":trewind" */
10673 break;
10674 case 'l': cmd = DT_LAST; /* ":tlast" */
10675 break;
10676 default: /* ":tag" */
10677#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010678 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010680 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 return;
10682 }
10683#endif
10684 cmd = DT_TAG;
10685 break;
10686 }
10687
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010688 if (name[0] == 'l')
10689 {
10690#ifndef FEAT_QUICKFIX
10691 ex_ni(eap);
10692 return;
10693#else
10694 cmd = DT_LTAG;
10695#endif
10696 }
10697
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10699 eap->forceit, TRUE);
10700}
10701
10702/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010703 * Check "str" for starting with a special cmdline variable.
10704 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10705 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10706 */
10707 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010708find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010709{
10710 int len;
10711 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010712 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010713 "%",
10714#define SPEC_PERC 0
10715 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010716#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010717 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010718#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010719 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010720#define SPEC_CCWORD (SPEC_CWORD + 1)
10721 "<cexpr>", /* expr under cursor */
10722#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010723 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010724#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010725 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010726#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010727 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010728#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010729 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010730#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010731 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010732#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010733 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010734#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010735#ifdef FEAT_CLIENTSERVER
10736 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010737# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010738#endif
10739 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010740
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010741 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010742 {
10743 len = (int)STRLEN(spec_str[i]);
10744 if (STRNCMP(src, spec_str[i], len) == 0)
10745 {
10746 *usedlen = len;
10747 return i;
10748 }
10749 }
10750 return -1;
10751}
10752
10753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 * Evaluate cmdline variables.
10755 *
10756 * change '%' to curbuf->b_ffname
10757 * '#' to curwin->w_altfile
10758 * '<cword>' to word under the cursor
10759 * '<cWORD>' to WORD under the cursor
10760 * '<cfile>' to path name under the cursor
10761 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010762 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 * '<afile>' to file name for autocommand
10764 * '<abuf>' to buffer number for autocommand
10765 * '<amatch>' to matching name for autocommand
10766 *
10767 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10768 * "" for error without a message) and NULL is returned.
10769 * Returns an allocated string if a valid match was found.
10770 * Returns NULL if no match was found. "usedlen" then still contains the
10771 * number of characters to skip.
10772 */
10773 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010774eval_vars(
10775 char_u *src, /* pointer into commandline */
10776 char_u *srcstart, /* beginning of valid memory for src */
10777 int *usedlen, /* characters after src that are used */
10778 linenr_T *lnump, /* line number for :e command, or NULL */
10779 char_u **errormsg, /* pointer to error message */
10780 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010781 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782{
10783 int i;
10784 char_u *s;
10785 char_u *result;
10786 char_u *resultbuf = NULL;
10787 int resultlen;
10788 buf_T *buf;
10789 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10790 int spec_idx;
10791#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010792 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 int skip_mod = FALSE;
10794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796
10797 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010798 if (escaped != NULL)
10799 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
10801 /*
10802 * Check if there is something to do.
10803 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010804 spec_idx = find_cmdline_var(src, usedlen);
10805 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 {
10807 *usedlen = 1;
10808 return NULL;
10809 }
10810
10811 /*
10812 * Skip when preceded with a backslash "\%" and "\#".
10813 * Note: In "\\%" the % is also not recognized!
10814 */
10815 if (src > srcstart && src[-1] == '\\')
10816 {
10817 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010818 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 return NULL;
10820 }
10821
10822 /*
10823 * word or WORD under cursor
10824 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010825 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10826 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010828 resultlen = find_ident_under_cursor(&result,
10829 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10830 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10831 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 if (resultlen == 0)
10833 {
10834 *errormsg = (char_u *)"";
10835 return NULL;
10836 }
10837 }
10838
10839 /*
10840 * '#': Alternate file name
10841 * '%': Current file name
10842 * File name under the cursor
10843 * File name for autocommand
10844 * and following modifiers
10845 */
10846 else
10847 {
10848 switch (spec_idx)
10849 {
10850 case SPEC_PERC: /* '%': current file */
10851 if (curbuf->b_fname == NULL)
10852 {
10853 result = (char_u *)"";
10854 valid = 0; /* Must have ":p:h" to be valid */
10855 }
10856 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010859#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010860 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010861#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 break;
10864
10865 case SPEC_HASH: /* '#' or "#99": alternate file */
10866 if (src[1] == '#') /* "##": the argument list */
10867 {
10868 result = arg_all();
10869 resultbuf = result;
10870 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010871 if (escaped != NULL)
10872 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873#ifdef FEAT_MODIFY_FNAME
10874 skip_mod = TRUE;
10875#endif
10876 break;
10877 }
10878 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010879 if (*s == '<') /* "#<99" uses v:oldfiles */
10880 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010882 if (s == src + 2 && src[1] == '-')
10883 /* just a minus sign, don't skip over it */
10884 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 *usedlen = (int)(s - src); /* length of what we expand */
10886
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010887 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010889 if (*usedlen < 2)
10890 {
10891 /* Should we give an error message for #<text? */
10892 *usedlen = 1;
10893 return NULL;
10894 }
10895#ifdef FEAT_EVAL
10896 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10897 (long)i);
10898 if (result == NULL)
10899 {
10900 *errormsg = (char_u *)"";
10901 return NULL;
10902 }
10903#else
10904 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 }
10908 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010909 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010910 if (i == 0 && src[1] == '<' && *usedlen > 1)
10911 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010912 buf = buflist_findnr(i);
10913 if (buf == NULL)
10914 {
10915 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10916 return NULL;
10917 }
10918 if (lnump != NULL)
10919 *lnump = ECMD_LAST;
10920 if (buf->b_fname == NULL)
10921 {
10922 result = (char_u *)"";
10923 valid = 0; /* Must have ":p:h" to be valid */
10924 }
10925 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010926 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010927 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010928#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010929 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010930#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010931 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 break;
10934
10935#ifdef FEAT_SEARCHPATH
10936 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010937 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 if (result == NULL)
10939 {
10940 *errormsg = (char_u *)"";
10941 return NULL;
10942 }
10943 resultbuf = result; /* remember allocated string */
10944 break;
10945#endif
10946
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 case SPEC_AFILE: /* file name for autocommand */
10948 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010949 if (result != NULL && !autocmd_fname_full)
10950 {
10951 /* Still need to turn the fname into a full path. It is
10952 * postponed to avoid a delay when <afile> is not used. */
10953 autocmd_fname_full = TRUE;
10954 result = FullName_save(autocmd_fname, FALSE);
10955 vim_free(autocmd_fname);
10956 autocmd_fname = result;
10957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 if (result == NULL)
10959 {
10960 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10961 return NULL;
10962 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010963 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 break;
10965
10966 case SPEC_ABUF: /* buffer number for autocommand */
10967 if (autocmd_bufnr <= 0)
10968 {
10969 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10970 return NULL;
10971 }
10972 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10973 result = strbuf;
10974 break;
10975
10976 case SPEC_AMATCH: /* match name for autocommand */
10977 result = autocmd_match;
10978 if (result == NULL)
10979 {
10980 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10981 return NULL;
10982 }
10983 break;
10984
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 case SPEC_SFILE: /* file name for ":so" command */
10986 result = sourcing_name;
10987 if (result == NULL)
10988 {
10989 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10990 return NULL;
10991 }
10992 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010993 case SPEC_SLNUM: /* line in file for ":so" command */
10994 if (sourcing_name == NULL || sourcing_lnum == 0)
10995 {
10996 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10997 return NULL;
10998 }
10999 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11000 result = strbuf;
11001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002#if defined(FEAT_CLIENTSERVER)
11003 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011004 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11005 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 result = strbuf;
11007 break;
11008#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011009 default:
11010 result = (char_u *)""; /* avoid gcc warning */
11011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 }
11013
11014 resultlen = (int)STRLEN(result); /* length of new string */
11015 if (src[*usedlen] == '<') /* remove the file name extension */
11016 {
11017 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 resultlen = (int)(s - result);
11020 }
11021#ifdef FEAT_MODIFY_FNAME
11022 else if (!skip_mod)
11023 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011024 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 &resultlen);
11026 if (result == NULL)
11027 {
11028 *errormsg = (char_u *)"";
11029 return NULL;
11030 }
11031 }
11032#endif
11033 }
11034
11035 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11036 {
11037 if (valid != VALID_HEAD + VALID_PATH)
11038 /* xgettext:no-c-format */
11039 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
11040 else
11041 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
11042 result = NULL;
11043 }
11044 else
11045 result = vim_strnsave(result, resultlen);
11046 vim_free(resultbuf);
11047 return result;
11048}
11049
11050/*
11051 * Concatenate all files in the argument list, separated by spaces, and return
11052 * it in one allocated string.
11053 * Spaces and backslashes in the file names are escaped with a backslash.
11054 * Returns NULL when out of memory.
11055 */
11056 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011057arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058{
11059 int len;
11060 int idx;
11061 char_u *retval = NULL;
11062 char_u *p;
11063
11064 /*
11065 * Do this loop two times:
11066 * first time: compute the total length
11067 * second time: concatenate the names
11068 */
11069 for (;;)
11070 {
11071 len = 0;
11072 for (idx = 0; idx < ARGCOUNT; ++idx)
11073 {
11074 p = alist_name(&ARGLIST[idx]);
11075 if (p != NULL)
11076 {
11077 if (len > 0)
11078 {
11079 /* insert a space in between names */
11080 if (retval != NULL)
11081 retval[len] = ' ';
11082 ++len;
11083 }
11084 for ( ; *p != NUL; ++p)
11085 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011086 if (*p == ' '
11087#ifndef BACKSLASH_IN_FILENAME
11088 || *p == '\\'
11089#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011090 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
11092 /* insert a backslash */
11093 if (retval != NULL)
11094 retval[len] = '\\';
11095 ++len;
11096 }
11097 if (retval != NULL)
11098 retval[len] = *p;
11099 ++len;
11100 }
11101 }
11102 }
11103
11104 /* second time: break here */
11105 if (retval != NULL)
11106 {
11107 retval[len] = NUL;
11108 break;
11109 }
11110
11111 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011112 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 if (retval == NULL)
11114 break;
11115 }
11116
11117 return retval;
11118}
11119
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120/*
11121 * Expand the <sfile> string in "arg".
11122 *
11123 * Returns an allocated string, or NULL for any error.
11124 */
11125 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011126expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127{
11128 char_u *errormsg;
11129 int len;
11130 char_u *result;
11131 char_u *newres;
11132 char_u *repl;
11133 int srclen;
11134 char_u *p;
11135
11136 result = vim_strsave(arg);
11137 if (result == NULL)
11138 return NULL;
11139
11140 for (p = result; *p; )
11141 {
11142 if (STRNCMP(p, "<sfile>", 7) != 0)
11143 ++p;
11144 else
11145 {
11146 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011147 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 if (errormsg != NULL)
11149 {
11150 if (*errormsg)
11151 emsg(errormsg);
11152 vim_free(result);
11153 return NULL;
11154 }
11155 if (repl == NULL) /* no match (cannot happen) */
11156 {
11157 p += srclen;
11158 continue;
11159 }
11160 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11161 newres = alloc(len);
11162 if (newres == NULL)
11163 {
11164 vim_free(repl);
11165 vim_free(result);
11166 return NULL;
11167 }
11168 mch_memmove(newres, result, (size_t)(p - result));
11169 STRCPY(newres + (p - result), repl);
11170 len = (int)STRLEN(newres);
11171 STRCAT(newres, p + srclen);
11172 vim_free(repl);
11173 vim_free(result);
11174 result = newres;
11175 p = newres + len; /* continue after the match */
11176 }
11177 }
11178
11179 return result;
11180}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181
11182#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011183static int ses_winsizes(FILE *fd, int restore_size,
11184 win_T *tab_firstwin);
11185static int ses_win_rec(FILE *fd, frame_T *fr);
11186static frame_T *ses_skipframe(frame_T *fr);
11187static int ses_do_frame(frame_T *fr);
11188static int ses_do_win(win_T *wp);
11189static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11190static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011191static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192
11193/*
11194 * Write openfile commands for the current buffers to an .exrc file.
11195 * Return FAIL on error, OK otherwise.
11196 */
11197 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011198makeopens(
11199 FILE *fd,
11200 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
11202 buf_T *buf;
11203 int only_save_windows = TRUE;
11204 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 int restore_size = TRUE;
11206 win_T *wp;
11207 char_u *sname;
11208 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011209 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011210 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011211 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011212 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011213 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011214 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215
11216 if (ssop_flags & SSOP_BUFFERS)
11217 only_save_windows = FALSE; /* Save ALL buffers */
11218
11219 /*
11220 * Begin by setting the this_session variable, and then other
11221 * sessionable variables.
11222 */
11223#ifdef FEAT_EVAL
11224 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11225 return FAIL;
11226 if (ssop_flags & SSOP_GLOBALS)
11227 if (store_session_globals(fd) == FAIL)
11228 return FAIL;
11229#endif
11230
11231 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011232 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 */
11234 if (put_line(fd, "silent only") == FAIL)
11235 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011236 if ((ssop_flags & SSOP_TABPAGES)
11237 && put_line(fd, "silent tabonly") == FAIL)
11238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239
11240 /*
11241 * Now a :cd command to the session directory or the current directory
11242 */
11243 if (ssop_flags & SSOP_SESDIR)
11244 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011245 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11246 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 return FAIL;
11248 }
11249 else if (ssop_flags & SSOP_CURDIR)
11250 {
11251 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11252 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011253 || fputs("cd ", fd) < 0
11254 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11255 || put_eol(fd) == FAIL)
11256 {
11257 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 vim_free(sname);
11261 }
11262
11263 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011264 * If there is an empty, unnamed buffer we will wipe it out later.
11265 * Remember the buffer number.
11266 */
11267 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11268 return FAIL;
11269 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11270 return FAIL;
11271 if (put_line(fd, "endif") == FAIL)
11272 return FAIL;
11273
11274 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 * Now save the current files, current buffer first.
11276 */
11277 if (put_line(fd, "set shortmess=aoO") == FAIL)
11278 return FAIL;
11279
11280 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011281 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 {
11283 if (!(only_save_windows && buf->b_nwindows == 0)
11284 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011285#ifdef FEAT_TERMINAL
11286 /* skip terminal buffers: finished ones are not useful, others
11287 * will be resurrected and result in a new buffer */
11288 && !bt_terminal(buf)
11289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 && buf->b_fname != NULL
11291 && buf->b_p_bl)
11292 {
11293 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11294 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011295 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 return FAIL;
11297 }
11298 }
11299
11300 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011301 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11303 return FAIL;
11304
11305 if (ssop_flags & SSOP_RESIZE)
11306 {
11307 /* Note: after the restore we still check it worked!*/
11308 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11309 || put_eol(fd) == FAIL)
11310 return FAIL;
11311 }
11312
11313#ifdef FEAT_GUI
11314 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11315 {
11316 int x, y;
11317
11318 if (gui_mch_get_winpos(&x, &y) == OK)
11319 {
11320 /* Note: after the restore we still check it worked!*/
11321 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11322 return FAIL;
11323 }
11324 }
11325#endif
11326
11327 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011328 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11329 * will be displayed when creating the next tab. That resizes the windows
11330 * in the first tab, which may cause problems. Set 'showtabline' to 2
11331 * temporarily to avoid that.
11332 */
11333 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11334 {
11335 if (put_line(fd, "set stal=2") == FAIL)
11336 return FAIL;
11337 restore_stal = TRUE;
11338 }
11339
11340 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011341 * May repeat putting Windows for each tab, when "tabpages" is in
11342 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011343 * Don't use goto_tabpage(), it may change directory and trigger
11344 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011346 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011347 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011348 if ((ssop_flags & SSOP_TABPAGES))
11349 {
11350 int num_tabs;
11351
11352 /*
11353 * Similar to ses_win_rec() below, populate the tab pages first so
11354 * later local options won't be copied to the new tabs.
11355 */
11356 for (tabnr = 1; ; ++tabnr)
11357 {
11358 tabpage_T *tp = find_tabpage(tabnr);
11359
11360 if (tp == NULL) /* done all tab pages */
11361 break;
11362
11363 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11364 return FAIL;
11365 }
11366
11367 num_tabs = tabnr - 1;
11368 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11369 || put_eol(fd) == FAIL))
11370 return FAIL;
11371 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011372 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011374 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011375 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011376
Bram Moolenaar18144c82006-04-12 21:52:12 +000011377 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011379 tabpage_T *tp = find_tabpage(tabnr);
11380
11381 if (tp == NULL)
11382 break; /* done all tab pages */
11383 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011384 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011385 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011386 tab_topframe = topframe;
11387 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011388 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011389 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011390 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011391 tab_topframe = tp->tp_topframe;
11392 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011393 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011394 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
Bram Moolenaar18144c82006-04-12 21:52:12 +000011397 /*
11398 * Before creating the window layout, try loading one file. If this
11399 * is aborted we don't end up with a number of useless windows.
11400 * This may have side effects! (e.g., compressed or network file).
11401 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011402 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011403 {
11404 if (ses_do_win(wp)
11405 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011406 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011407#ifdef FEAT_QUICKFIX
11408 && !bt_nofile(wp->w_buffer)
11409#endif
11410 )
11411 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011412 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11413 return FAIL;
11414 need_tabnext = FALSE;
11415
11416 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011417 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11418 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011419 return FAIL;
11420 if (!wp->w_arg_idx_invalid)
11421 edited_win = wp;
11422 break;
11423 }
11424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011426 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011427 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011428 return FAIL;
11429
Bram Moolenaar18144c82006-04-12 21:52:12 +000011430 /*
11431 * Save current window layout.
11432 */
11433 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011435 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011437 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11438 return FAIL;
11439 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441
Bram Moolenaar18144c82006-04-12 21:52:12 +000011442 /*
11443 * Check if window sizes can be restored (no windows omitted).
11444 * Remember the window number of the current window after restoring.
11445 */
11446 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011447 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011448 {
11449 if (ses_do_win(wp))
11450 ++nr;
11451 else
11452 restore_size = FALSE;
11453 if (curwin == wp)
11454 cnr = nr;
11455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456
Bram Moolenaar18144c82006-04-12 21:52:12 +000011457 /* Go to the first window. */
11458 if (put_line(fd, "wincmd t") == FAIL)
11459 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011460
Bram Moolenaar18144c82006-04-12 21:52:12 +000011461 /*
11462 * If more than one window, see if sizes can be restored.
11463 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11464 * resized when moving between windows.
11465 * Do this before restoring the view, so that the topline and the
11466 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011467 * winminheight and winminwidth need to be set to avoid an error if the
11468 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011469 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011470 if (put_line(fd, "set winminheight=0") == FAIL
11471 || put_line(fd, "set winheight=1") == FAIL
11472 || put_line(fd, "set winminwidth=0") == FAIL
11473 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011474 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011475 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011476 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
Bram Moolenaar18144c82006-04-12 21:52:12 +000011478 /*
11479 * Restore the view of the window (options, file, cursor, etc.).
11480 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011481 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011482 {
11483 if (!ses_do_win(wp))
11484 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011485 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11486 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011487 return FAIL;
11488 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11489 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011490 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011491 }
11492
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011493 /* The argument index in the first tab page is zero, need to set it in
11494 * each window. For further tab pages it's the window where we do
11495 * "tabedit". */
11496 cur_arg_idx = next_arg_idx;
11497
Bram Moolenaar18144c82006-04-12 21:52:12 +000011498 /*
11499 * Restore cursor to the current window if it's not the first one.
11500 */
11501 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11502 || put_eol(fd) == FAIL))
11503 return FAIL;
11504
11505 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011506 * Restore window sizes again after jumping around in windows, because
11507 * the current window has a minimum size while others may not.
11508 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011509 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011510 return FAIL;
11511
Bram Moolenaar18144c82006-04-12 21:52:12 +000011512 /* Don't continue in another tab page when doing only the current one
11513 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011514 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011515 break;
11516 }
11517
11518 if (ssop_flags & SSOP_TABPAGES)
11519 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011520 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11521 || put_eol(fd) == FAIL)
11522 return FAIL;
11523 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011524 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11525 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011526
Bram Moolenaar9c102382006-05-03 21:26:49 +000011527 /*
11528 * Wipe out an empty unnamed buffer we started in.
11529 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011530 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011531 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011532 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011533 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011534 return FAIL;
11535 if (put_line(fd, "endif") == FAIL)
11536 return FAIL;
11537 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11538 return FAIL;
11539
11540 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11541 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11542 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11543 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011544 /* Re-apply 'winminheight' and 'winminwidth'. */
11545 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11546 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11547 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548
11549 /*
11550 * Lastly, execute the x.vim file if it exists.
11551 */
11552 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11553 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011554 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 || put_line(fd, "endif") == FAIL)
11556 return FAIL;
11557
11558 return OK;
11559}
11560
11561 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011562ses_winsizes(
11563 FILE *fd,
11564 int restore_size,
11565 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566{
11567 int n = 0;
11568 win_T *wp;
11569
11570 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11571 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011572 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 {
11574 if (!ses_do_win(wp))
11575 continue;
11576 ++n;
11577
11578 /* restore height when not full height */
11579 if (wp->w_height + wp->w_status_height < topframe->fr_height
11580 && (fprintf(fd,
11581 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11582 n, (long)wp->w_height, Rows / 2, Rows) < 0
11583 || put_eol(fd) == FAIL))
11584 return FAIL;
11585
11586 /* restore width when not full width */
11587 if (wp->w_width < Columns && (fprintf(fd,
11588 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11589 n, (long)wp->w_width, Columns / 2, Columns) < 0
11590 || put_eol(fd) == FAIL))
11591 return FAIL;
11592 }
11593 }
11594 else
11595 {
11596 /* Just equalise window sizes */
11597 if (put_line(fd, "wincmd =") == FAIL)
11598 return FAIL;
11599 }
11600 return OK;
11601}
11602
11603/*
11604 * Write commands to "fd" to recursively create windows for frame "fr",
11605 * horizontally and vertically split.
11606 * After the commands the last window in the frame is the current window.
11607 * Returns FAIL when writing the commands to "fd" fails.
11608 */
11609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011610ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611{
11612 frame_T *frc;
11613 int count = 0;
11614
11615 if (fr->fr_layout != FR_LEAF)
11616 {
11617 /* Find first frame that's not skipped and then create a window for
11618 * each following one (first frame is already there). */
11619 frc = ses_skipframe(fr->fr_child);
11620 if (frc != NULL)
11621 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11622 {
11623 /* Make window as big as possible so that we have lots of room
11624 * to split. */
11625 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11626 || put_line(fd, fr->fr_layout == FR_COL
11627 ? "split" : "vsplit") == FAIL)
11628 return FAIL;
11629 ++count;
11630 }
11631
11632 /* Go back to the first window. */
11633 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11634 ? "%dwincmd k" : "%dwincmd h", count) < 0
11635 || put_eol(fd) == FAIL))
11636 return FAIL;
11637
11638 /* Recursively create frames/windows in each window of this column or
11639 * row. */
11640 frc = ses_skipframe(fr->fr_child);
11641 while (frc != NULL)
11642 {
11643 ses_win_rec(fd, frc);
11644 frc = ses_skipframe(frc->fr_next);
11645 /* Go to next window. */
11646 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11647 return FAIL;
11648 }
11649 }
11650 return OK;
11651}
11652
11653/*
11654 * Skip frames that don't contain windows we want to save in the Session.
11655 * Returns NULL when there none.
11656 */
11657 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011658ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659{
11660 frame_T *frc;
11661
11662 for (frc = fr; frc != NULL; frc = frc->fr_next)
11663 if (ses_do_frame(frc))
11664 break;
11665 return frc;
11666}
11667
11668/*
11669 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11670 * the Session.
11671 */
11672 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011673ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
11675 frame_T *frc;
11676
11677 if (fr->fr_layout == FR_LEAF)
11678 return ses_do_win(fr->fr_win);
11679 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11680 if (ses_do_frame(frc))
11681 return TRUE;
11682 return FALSE;
11683}
11684
11685/*
11686 * Return non-zero if window "wp" is to be stored in the Session.
11687 */
11688 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011689ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011691#ifdef FEAT_TERMINAL
11692 if (bt_terminal(wp->w_buffer))
11693 return !term_is_finished(wp->w_buffer)
11694 && (ssop_flags & SSOP_TERMINAL)
11695 && term_should_restore(wp->w_buffer);
11696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 if (wp->w_buffer->b_fname == NULL
11698#ifdef FEAT_QUICKFIX
11699 /* When 'buftype' is "nofile" can't restore the window contents. */
11700 || bt_nofile(wp->w_buffer)
11701#endif
11702 )
11703 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011704 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 return (ssop_flags & SSOP_HELP);
11706 return TRUE;
11707}
11708
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011709 static int
11710put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11711{
11712 int r;
11713
11714 if (wp->w_curswant == MAXCOL)
11715 r = fprintf(fd, "%snormal! $", spaces);
11716 else
11717 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11718 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11719}
11720
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721/*
11722 * Write commands to "fd" to restore the view of a window.
11723 * Caller must make sure 'scrolloff' is zero.
11724 */
11725 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011726put_view(
11727 FILE *fd,
11728 win_T *wp,
11729 int add_edit, /* add ":edit" command to view */
11730 unsigned *flagp, /* vop_flags or ssop_flags */
11731 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011732 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733{
11734 win_T *save_curwin;
11735 int f;
11736 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011737 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738
11739 /* Always restore cursor position for ":mksession". For ":mkview" only
11740 * when 'viewoptions' contains "cursor". */
11741 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11742
11743 /*
11744 * Local argument list.
11745 */
11746 if (wp->w_alist == &global_alist)
11747 {
11748 if (put_line(fd, "argglobal") == FAIL)
11749 return FAIL;
11750 }
11751 else
11752 {
11753 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11754 flagp == &vop_flags
11755 || !(*flagp & SSOP_CURDIR)
11756 || wp->w_localdir != NULL, flagp) == FAIL)
11757 return FAIL;
11758 }
11759
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011760 /* Only when part of a session: restore the argument index. Some
11761 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011762 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011763 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011765 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 || put_eol(fd) == FAIL)
11767 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011768 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 }
11770
11771 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011772 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011774# ifdef FEAT_TERMINAL
11775 if (bt_terminal(wp->w_buffer))
11776 {
11777 if (term_write_session(fd, wp) == FAIL)
11778 return FAIL;
11779 }
11780 else
11781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 /*
11783 * Load the file.
11784 */
11785 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011786# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 )
11790 {
11791 /*
11792 * Editing a file in this buffer: use ":edit file".
11793 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011794 *
11795 * Note, if a buffer for that file already exists, use :badd to
11796 * edit that buffer, to not lose folding information (:edit resets
11797 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011799 if (fputs("if bufexists('", fd) < 0
11800 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11801 || fputs("') | buffer ", fd) < 0
11802 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11803 || fputs(" | else | edit ", fd) < 0
11804 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11805 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011806 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 return FAIL;
11808 }
11809 else
11810 {
11811 /* No file in this buffer, just make it empty. */
11812 if (put_line(fd, "enew") == FAIL)
11813 return FAIL;
11814#ifdef FEAT_QUICKFIX
11815 if (wp->w_buffer->b_ffname != NULL)
11816 {
11817 /* The buffer does have a name, but it's not a file name. */
11818 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011819 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 return FAIL;
11821 }
11822#endif
11823 do_cursor = FALSE;
11824 }
11825 }
11826
11827 /*
11828 * Local mappings and abbreviations.
11829 */
11830 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11831 && makemap(fd, wp->w_buffer) == FAIL)
11832 return FAIL;
11833
11834 /*
11835 * Local options. Need to go to the window temporarily.
11836 * Store only local values when using ":mkview" and when ":mksession" is
11837 * used and 'sessionoptions' doesn't include "options".
11838 * Some folding options are always stored when "folds" is included,
11839 * otherwise the folds would not be restored correctly.
11840 */
11841 save_curwin = curwin;
11842 curwin = wp;
11843 curbuf = curwin->w_buffer;
11844 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11845 f = makeset(fd, OPT_LOCAL,
11846 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11847#ifdef FEAT_FOLDING
11848 else if (*flagp & SSOP_FOLDS)
11849 f = makefoldset(fd);
11850#endif
11851 else
11852 f = OK;
11853 curwin = save_curwin;
11854 curbuf = curwin->w_buffer;
11855 if (f == FAIL)
11856 return FAIL;
11857
11858#ifdef FEAT_FOLDING
11859 /*
11860 * Save Folds when 'buftype' is empty and for help files.
11861 */
11862 if ((*flagp & SSOP_FOLDS)
11863 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011864 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 {
11866 if (put_folds(fd, wp) == FAIL)
11867 return FAIL;
11868 }
11869#endif
11870
11871 /*
11872 * Set the cursor after creating folds, since that moves the cursor.
11873 */
11874 if (do_cursor)
11875 {
11876
11877 /* Restore the cursor line in the file and relatively in the
11878 * window. Don't use "G", it changes the jumplist. */
11879 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11880 (long)wp->w_cursor.lnum,
11881 (long)(wp->w_cursor.lnum - wp->w_topline),
11882 (long)wp->w_height / 2, (long)wp->w_height) < 0
11883 || put_eol(fd) == FAIL
11884 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11885 || put_line(fd, "exe s:l") == FAIL
11886 || put_line(fd, "normal! zt") == FAIL
11887 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11888 || put_eol(fd) == FAIL)
11889 return FAIL;
11890 /* Restore the cursor column and left offset when not wrapping. */
11891 if (wp->w_cursor.col == 0)
11892 {
11893 if (put_line(fd, "normal! 0") == FAIL)
11894 return FAIL;
11895 }
11896 else
11897 {
11898 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11899 {
11900 if (fprintf(fd,
11901 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011902 (long)wp->w_virtcol + 1,
11903 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 (long)wp->w_width / 2, (long)wp->w_width) < 0
11905 || put_eol(fd) == FAIL
11906 || put_line(fd, "if s:c > 0") == FAIL
11907 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011908 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11909 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 || put_eol(fd) == FAIL
11911 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011912 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 || put_line(fd, "endif") == FAIL)
11914 return FAIL;
11915 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011916 else if (put_view_curpos(fd, wp, "") == FAIL)
11917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 }
11919 }
11920
11921 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011922 * Local directory, if the current flag is not view options or the "curdir"
11923 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011925 if (wp->w_localdir != NULL
11926 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 {
11928 if (fputs("lcd ", fd) < 0
11929 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11930 || put_eol(fd) == FAIL)
11931 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011932 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 }
11934
11935 return OK;
11936}
11937
11938/*
11939 * Write an argument list to the session file.
11940 * Returns FAIL if writing fails.
11941 */
11942 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011943ses_arglist(
11944 FILE *fd,
11945 char *cmd,
11946 garray_T *gap,
11947 int fullname, /* TRUE: use full path name */
11948 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949{
11950 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011951 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 char_u *s;
11953
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011954 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11955 return FAIL;
11956 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 return FAIL;
11958 for (i = 0; i < gap->ga_len; ++i)
11959 {
11960 /* NULL file names are skipped (only happens when out of memory). */
11961 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11962 if (s != NULL)
11963 {
11964 if (fullname)
11965 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011966 buf = alloc(MAXPATHL);
11967 if (buf != NULL)
11968 {
11969 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11970 s = buf;
11971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011973 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011974 || ses_put_fname(fd, s, flagp) == FAIL
11975 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011976 {
11977 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011979 }
11980 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 }
11982 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011983 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984}
11985
11986/*
11987 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011988 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 * Returns FAIL if writing fails.
11990 */
11991 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011992ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993{
11994 char_u *name;
11995
11996 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011997 * the session file will be sourced.
11998 * Don't do this for ":mkview", we don't know the current directory.
11999 * Don't do this after ":lcd", we don't keep track of what the current
12000 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 if (buf->b_sfname != NULL
12002 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012003 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012004#ifdef FEAT_AUTOCHDIR
12005 && !p_acd
12006#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012007 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 name = buf->b_sfname;
12009 else
12010 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012011 if (ses_put_fname(fd, name, flagp) == FAIL
12012 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 return FAIL;
12014 return OK;
12015}
12016
12017/*
12018 * Write a file name to the session file.
12019 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12020 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012021 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 */
12023 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012024ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025{
12026 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012027 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029
12030 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012031 if (sname == NULL)
12032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012034 if (*flagp & SSOP_SLASH)
12035 {
12036 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012037 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012038 if (*p == '\\')
12039 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012041
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012042 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012043 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012045 if (p == NULL)
12046 return FAIL;
12047
12048 /* write the result */
12049 if (fputs((char *)p, fd) < 0)
12050 retval = FAIL;
12051
12052 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 return retval;
12054}
12055
12056/*
12057 * ":loadview [nr]"
12058 */
12059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012060ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061{
12062 char_u *fname;
12063
12064 fname = get_view_file(*eap->arg);
12065 if (fname != NULL)
12066 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012067 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 vim_free(fname);
12069 }
12070}
12071
12072/*
12073 * Get the name of the view file for the current buffer.
12074 */
12075 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012076get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077{
12078 int len = 0;
12079 char_u *p, *s;
12080 char_u *retval;
12081 char_u *sname;
12082
12083 if (curbuf->b_ffname == NULL)
12084 {
12085 EMSG(_(e_noname));
12086 return NULL;
12087 }
12088 sname = home_replace_save(NULL, curbuf->b_ffname);
12089 if (sname == NULL)
12090 return NULL;
12091
12092 /*
12093 * We want a file name without separators, because we're not going to make
12094 * a directory.
12095 * "normal" path separator -> "=+"
12096 * "=" -> "=="
12097 * ":" path separator -> "=-"
12098 */
12099 for (p = sname; *p; ++p)
12100 if (*p == '=' || vim_ispathsep(*p))
12101 ++len;
12102 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12103 if (retval != NULL)
12104 {
12105 STRCPY(retval, p_vdir);
12106 add_pathsep(retval);
12107 s = retval + STRLEN(retval);
12108 for (p = sname; *p; ++p)
12109 {
12110 if (*p == '=')
12111 {
12112 *s++ = '=';
12113 *s++ = '=';
12114 }
12115 else if (vim_ispathsep(*p))
12116 {
12117 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012118#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 if (*p == ':')
12120 *s++ = '-';
12121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012123 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 }
12125 else
12126 *s++ = *p;
12127 }
12128 *s++ = '=';
12129 *s++ = c;
12130 STRCPY(s, ".vim");
12131 }
12132
12133 vim_free(sname);
12134 return retval;
12135}
12136
12137#endif /* FEAT_SESSION */
12138
12139/*
12140 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12141 * Return FAIL for a write error.
12142 */
12143 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012144put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145{
12146 if (
12147#ifdef USE_CRNL
12148 (
12149# ifdef MKSESSION_NL
12150 !mksession_nl &&
12151# endif
12152 (putc('\r', fd) < 0)) ||
12153#endif
12154 (putc('\n', fd) < 0))
12155 return FAIL;
12156 return OK;
12157}
12158
12159/*
12160 * Write a line to "fd".
12161 * Return FAIL for a write error.
12162 */
12163 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012164put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165{
12166 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12167 return FAIL;
12168 return OK;
12169}
12170
12171#ifdef FEAT_VIMINFO
12172/*
12173 * ":rviminfo" and ":wviminfo".
12174 */
12175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012176ex_viminfo(
12177 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178{
12179 char_u *save_viminfo;
12180
12181 save_viminfo = p_viminfo;
12182 if (*p_viminfo == NUL)
12183 p_viminfo = (char_u *)"'100";
12184 if (eap->cmdidx == CMD_rviminfo)
12185 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012186 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12187 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 EMSG(_("E195: Cannot open viminfo file for reading"));
12189 }
12190 else
12191 write_viminfo(eap->arg, eap->forceit);
12192 p_viminfo = save_viminfo;
12193}
12194#endif
12195
12196#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012197/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012198 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012199 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012202dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 if (fname == NULL)
12205 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012206 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207}
12208#endif
12209
12210/*
12211 * ":behave {mswin,xterm}"
12212 */
12213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012214ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215{
12216 if (STRCMP(eap->arg, "mswin") == 0)
12217 {
12218 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12219 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12220 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12221 set_option_value((char_u *)"keymodel", 0L,
12222 (char_u *)"startsel,stopsel", 0);
12223 }
12224 else if (STRCMP(eap->arg, "xterm") == 0)
12225 {
12226 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12227 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12228 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12229 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12230 }
12231 else
12232 EMSG2(_(e_invarg2), eap->arg);
12233}
12234
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012235#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12236/*
12237 * Function given to ExpandGeneric() to obtain the possible arguments of the
12238 * ":behave {mswin,xterm}" command.
12239 */
12240 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012241get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012242{
12243 if (idx == 0)
12244 return (char_u *)"mswin";
12245 if (idx == 1)
12246 return (char_u *)"xterm";
12247 return NULL;
12248}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012249
12250/*
12251 * Function given to ExpandGeneric() to obtain the possible arguments of the
12252 * ":messages {clear}" command.
12253 */
12254 char_u *
12255get_messages_arg(expand_T *xp UNUSED, int idx)
12256{
12257 if (idx == 0)
12258 return (char_u *)"clear";
12259 return NULL;
12260}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012261#endif
12262
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012263 char_u *
12264get_mapclear_arg(expand_T *xp UNUSED, int idx)
12265{
12266 if (idx == 0)
12267 return (char_u *)"<buffer>";
12268 return NULL;
12269}
12270
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271static int filetype_detect = FALSE;
12272static int filetype_plugin = FALSE;
12273static int filetype_indent = FALSE;
12274
12275/*
12276 * ":filetype [plugin] [indent] {on,off,detect}"
12277 * on: Load the filetype.vim file to install autocommands for file types.
12278 * off: Load the ftoff.vim file to remove all autocommands for file types.
12279 * plugin on: load filetype.vim and ftplugin.vim
12280 * plugin off: load ftplugof.vim
12281 * indent on: load filetype.vim and indent.vim
12282 * indent off: load indoff.vim
12283 */
12284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012285ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286{
12287 char_u *arg = eap->arg;
12288 int plugin = FALSE;
12289 int indent = FALSE;
12290
12291 if (*eap->arg == NUL)
12292 {
12293 /* Print current status. */
12294 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12295 filetype_detect ? "ON" : "OFF",
12296 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12297 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12298 return;
12299 }
12300
12301 /* Accept "plugin" and "indent" in any order. */
12302 for (;;)
12303 {
12304 if (STRNCMP(arg, "plugin", 6) == 0)
12305 {
12306 plugin = TRUE;
12307 arg = skipwhite(arg + 6);
12308 continue;
12309 }
12310 if (STRNCMP(arg, "indent", 6) == 0)
12311 {
12312 indent = TRUE;
12313 arg = skipwhite(arg + 6);
12314 continue;
12315 }
12316 break;
12317 }
12318 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12319 {
12320 if (*arg == 'o' || !filetype_detect)
12321 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012322 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 filetype_detect = TRUE;
12324 if (plugin)
12325 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012326 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 filetype_plugin = TRUE;
12328 }
12329 if (indent)
12330 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012331 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 filetype_indent = TRUE;
12333 }
12334 }
12335 if (*arg == 'd')
12336 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012337 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012338 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339 }
12340 }
12341 else if (STRCMP(arg, "off") == 0)
12342 {
12343 if (plugin || indent)
12344 {
12345 if (plugin)
12346 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012347 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 filetype_plugin = FALSE;
12349 }
12350 if (indent)
12351 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012352 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 filetype_indent = FALSE;
12354 }
12355 }
12356 else
12357 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012358 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 filetype_detect = FALSE;
12360 }
12361 }
12362 else
12363 EMSG2(_(e_invarg2), arg);
12364}
12365
12366/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012367 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 */
12369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012370ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371{
12372 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012373 {
12374 char_u *arg = eap->arg;
12375
12376 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12377 arg += 9;
12378
12379 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12380 if (arg != eap->arg)
12381 did_filetype = FALSE;
12382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012386ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388#ifdef FEAT_DIGRAPHS
12389 if (*eap->arg != NUL)
12390 putdigraph(eap->arg);
12391 else
12392 listdigraphs();
12393#else
12394 EMSG(_("E196: No digraphs in this version"));
12395#endif
12396}
12397
12398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012399ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
12401 int flags = 0;
12402
12403 if (eap->cmdidx == CMD_setlocal)
12404 flags = OPT_LOCAL;
12405 else if (eap->cmdidx == CMD_setglobal)
12406 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012407#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 if (cmdmod.browse && flags == 0)
12409 ex_options(eap);
12410 else
12411#endif
12412 (void)do_set(eap->arg, flags);
12413}
12414
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012415#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12416 void
12417set_no_hlsearch(int flag)
12418{
12419 no_hlsearch = flag;
12420# ifdef FEAT_EVAL
12421 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12422# endif
12423}
12424
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425/*
12426 * ":nohlsearch"
12427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012429ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012431 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012432 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433}
12434
12435/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012436 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 * Sets nextcmd to the start of the next command, if any. Also called when
12438 * skipping commands to find the next command.
12439 */
12440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012441ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
12443 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012444 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 char_u *end;
12446 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012447 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012448
12449 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012450 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012451 else
12452 {
12453 EMSG(e_invcmd);
12454 return;
12455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456
12457 /* First clear any old pattern. */
12458 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012459 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460
12461 if (ends_excmd(*eap->arg))
12462 end = eap->arg;
12463 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012464 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 end = eap->arg + 4;
12466 else
12467 {
12468 p = skiptowhite(eap->arg);
12469 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012470 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 p = skipwhite(p);
12472 if (*p == NUL)
12473 {
12474 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012475 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 EMSG2(_(e_invarg2), eap->arg);
12477 return;
12478 }
12479 end = skip_regexp(p + 1, *p, TRUE, NULL);
12480 if (!eap->skip)
12481 {
12482 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12483 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012484 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 eap->errmsg = e_trailing;
12486 return;
12487 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012488 if (*end != *p)
12489 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012490 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012491 EMSG2(_(e_invarg2), p);
12492 return;
12493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494
12495 c = *end;
12496 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012497 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012498 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012499 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 }
12501 }
12502 eap->nextcmd = find_nextcmd(end);
12503}
12504#endif
12505
12506#ifdef FEAT_CRYPT
12507/*
12508 * ":X": Get crypt key
12509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012511ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012513 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012514 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515}
12516#endif
12517
12518#ifdef FEAT_FOLDING
12519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012520ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521{
12522 if (foldManualAllowed(TRUE))
12523 foldCreate(eap->line1, eap->line2);
12524}
12525
12526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012527ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528{
12529 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12530 eap->forceit, FALSE);
12531}
12532
12533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012534ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535{
12536 linenr_T lnum;
12537
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012538#ifdef FEAT_CLIPBOARD
12539 start_global_changes();
12540#endif
12541
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 /* First set the marks for all lines closed/open. */
12543 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12544 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12545 ml_setmarked(lnum);
12546
12547 /* Execute the command on the marked lines. */
12548 global_exe(eap->arg);
12549 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012550#ifdef FEAT_CLIPBOARD
12551 end_global_changes();
12552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553}
12554#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012555
Bram Moolenaar39665952018-08-15 20:59:48 +020012556#ifdef FEAT_QUICKFIX
12557/*
12558 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12559 * instead of a quickfix command.
12560 */
12561 int
12562is_loclist_cmd(int cmdidx)
12563{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012564 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012565 return FALSE;
12566 return cmdnames[cmdidx].cmd_name[0] == 'l';
12567}
12568#endif
12569
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012570# if defined(FEAT_TIMERS) || defined(PROTO)
12571 int
12572get_pressedreturn(void)
12573{
12574 return ex_pressedreturn;
12575}
12576
12577 void
12578set_pressedreturn(int val)
12579{
12580 ex_pressedreturn = val;
12581}
12582#endif