blob: 3dda3c0952b436c1bb7e1e1f43a4b1b9a81cac31 [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 Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200111#if !defined(FEAT_LISTCMDS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200129#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static 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 +0100141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ex_close(exarg_T *eap);
163static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
164static void ex_only(exarg_T *eap);
165static void ex_resize(exarg_T *eap);
166static void ex_stag(exarg_T *eap);
167static void ex_tabclose(exarg_T *eap);
168static void ex_tabonly(exarg_T *eap);
169static void ex_tabnext(exarg_T *eap);
170static void ex_tabmove(exarg_T *eap);
171static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200172#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100173static void ex_pclose(exarg_T *eap);
174static void ex_ptag(exarg_T *eap);
175static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_hide(exarg_T *eap);
182static void ex_stop(exarg_T *eap);
183static void ex_exit(exarg_T *eap);
184static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#else
188# define ex_goto ex_ni
189#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static void ex_shell(exarg_T *eap);
191static void ex_preserve(exarg_T *eap);
192static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void ex_mode(exarg_T *eap);
200static void ex_wrongmodifier(exarg_T *eap);
201static void ex_find(exarg_T *eap);
202static void ex_open(exarg_T *eap);
203static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
205# define ex_drop ex_ni
206#endif
207#ifndef FEAT_GUI
208# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#endif
211#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100212static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#else
214# define ex_tearoff ex_ni
215#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000216#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#else
219# define ex_popup ex_ni
220#endif
221#ifndef FEAT_GUI_MSWIN
222# define ex_simalt ex_ni
223#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000224#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define gui_mch_find_dialog ex_ni
226# define gui_mch_replace_dialog ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define ex_helpfind ex_ni
230#endif
231#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100232# define ex_cscope ex_ni
233# define ex_scscope ex_ni
234# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
236#ifndef FEAT_SYN_HL
237# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200238# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200240#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200241# define ex_syntime ex_ni
242#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000244# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000245# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000246# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000247# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000248# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000249#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200250#ifndef FEAT_PERSISTENT_UNDO
251# define ex_rundo ex_ni
252# define ex_wundo ex_ni
253#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200254#ifndef FEAT_LUA
255# define ex_lua ex_script_ni
256# define ex_luado ex_ni
257# define ex_luafile ex_ni
258#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000259#ifndef FEAT_MZSCHEME
260# define ex_mzscheme ex_script_ni
261# define ex_mzfile ex_ni
262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_PERL
264# define ex_perl ex_script_ni
265# define ex_perldo ex_ni
266#endif
267#ifndef FEAT_PYTHON
268# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200269# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200274# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100277#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
278# define ex_pyx ex_script_ni
279# define ex_pyxdo ex_ni
280# define ex_pyxfile ex_ni
281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifndef FEAT_TCL
283# define ex_tcl ex_script_ni
284# define ex_tcldo ex_ni
285# define ex_tclfile ex_ni
286#endif
287#ifndef FEAT_RUBY
288# define ex_ruby ex_script_ni
289# define ex_rubydo ex_ni
290# define ex_rubyfile ex_ni
291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifndef FEAT_KEYMAP
293# define ex_loadkeymap ex_ni
294#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100295static void ex_swapname(exarg_T *eap);
296static void ex_syncbind(exarg_T *eap);
297static void ex_read(exarg_T *eap);
298static void ex_pwd(exarg_T *eap);
299static void ex_equal(exarg_T *eap);
300static void ex_sleep(exarg_T *eap);
301static void do_exmap(exarg_T *eap, int isabbrev);
302static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100303static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000304#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#else
307# define ex_winpos ex_ni
308#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_operators(exarg_T *eap);
310static void ex_put(exarg_T *eap);
311static void ex_copymove(exarg_T *eap);
312static void ex_submagic(exarg_T *eap);
313static void ex_join(exarg_T *eap);
314static void ex_at(exarg_T *eap);
315static void ex_bang(exarg_T *eap);
316static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200317#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_wundo(exarg_T *eap);
319static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_redo(exarg_T *eap);
322static void ex_later(exarg_T *eap);
323static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_redrawstatus(exarg_T *eap);
325static void close_redir(void);
326static void ex_mkrc(exarg_T *eap);
327static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static char_u *uc_fun_cmd(void);
330static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_startinsert(exarg_T *eap);
333static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_checkpath(exarg_T *eap);
336static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_findpat ex_ni
339# define ex_checkpath ex_ni
340#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200341#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_psearch ex_ni
345#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_tag(exarg_T *eap);
347static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifndef FEAT_EVAL
349# define ex_scriptnames ex_ni
350# define ex_finish ex_ni
351# define ex_echo ex_ni
352# define ex_echohl ex_ni
353# define ex_execute ex_ni
354# define ex_call ex_ni
355# define ex_if ex_ni
356# define ex_endif ex_ni
357# define ex_else ex_ni
358# define ex_while ex_ni
359# define ex_continue ex_ni
360# define ex_break ex_ni
361# define ex_endwhile ex_ni
362# define ex_throw ex_ni
363# define ex_try ex_ni
364# define ex_catch ex_ni
365# define ex_finally ex_ni
366# define ex_endtry ex_ni
367# define ex_endfunction ex_ni
368# define ex_let ex_ni
369# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000370# define ex_lockvar ex_ni
371# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372# define ex_function ex_ni
373# define ex_delfunction ex_ni
374# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000375# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100377static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100379static int makeopens(FILE *fd, char_u *dirnow);
380static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
381static void ex_loadview(exarg_T *eap);
382static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000383static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#else
385# define ex_loadview ex_ni
386#endif
387#ifndef FEAT_EVAL
388# define ex_compiler ex_ni
389#endif
390#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100391static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#else
393# define ex_viminfo ex_ni
394#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100397static void ex_filetype(exarg_T *eap);
398static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_filetype ex_ni
401# define ex_setfiletype ex_ni
402#endif
403#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000404# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# define ex_diffpatch ex_ni
406# define ex_diffgetput ex_ni
407# define ex_diffsplit ex_ni
408# define ex_diffthis ex_ni
409# define ex_diffupdate ex_ni
410#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static void ex_digraphs(exarg_T *eap);
412static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
414# define ex_options ex_ni
415#endif
416#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100417static void ex_nohlsearch(exarg_T *eap);
418static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#else
420# define ex_nohlsearch ex_ni
421# define ex_match ex_ni
422#endif
423#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100424static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#else
426# define ex_X ex_ni
427#endif
428#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_fold(exarg_T *eap);
430static void ex_foldopen(exarg_T *eap);
431static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#else
433# define ex_fold ex_ni
434# define ex_foldopen ex_ni
435# define ex_folddo ex_ni
436#endif
437#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
438 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
439# define ex_language ex_ni
440#endif
441#ifndef FEAT_SIGNS
442# define ex_sign ex_ni
443#endif
444#ifndef FEAT_SUN_WORKSHOP
445# define ex_wsverb ex_ni
446#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000447#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200448# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000449# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200450# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453#ifndef FEAT_EVAL
454# define ex_debug ex_ni
455# define ex_breakadd ex_ni
456# define ex_debuggreedy ex_ni
457# define ex_breakdel ex_ni
458# define ex_breaklist ex_ni
459#endif
460
461#ifndef FEAT_CMDHIST
462# define ex_history ex_ni
463#endif
464#ifndef FEAT_JUMPLIST
465# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200466# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# define ex_changes ex_ni
468#endif
469
Bram Moolenaar05159a02005-02-26 23:04:13 +0000470#ifndef FEAT_PROFILE
471# define ex_profile ex_ni
472#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200473#ifndef FEAT_TERMINAL
474# define ex_terminal ex_ni
475#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477/*
478 * Declare cmdnames[].
479 */
480#define DO_DECLARE_EXCMD
481#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200482#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484static char_u dollar_command[2] = {'$', 0};
485
486
487#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000488/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489typedef struct
490{
491 char_u *line; /* command line */
492 linenr_T lnum; /* sourcing_lnum of the line */
493} wcmd_T;
494
495/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000496 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000498 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000500struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 garray_T *lines_gap; /* growarray with line info */
503 int current_line; /* last read line from growarray */
504 int repeating; /* TRUE when looping a second time */
505 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100506 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 void *cookie;
508};
509
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100510static char_u *get_loop_line(int c, void *cookie, int indent);
511static int store_loop_line(garray_T *gap, char_u *line);
512static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000513
514/* Struct to save a few things while debugging. Used in do_cmdline() only. */
515struct dbg_stuff
516{
517 int trylevel;
518 int force_abort;
519 except_T *caught_stack;
520 char_u *vv_exception;
521 char_u *vv_throwpoint;
522 int did_emsg;
523 int got_int;
524 int did_throw;
525 int need_rethrow;
526 int check_cstack;
527 except_T *current_exception;
528};
529
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100530static void save_dbg_stuff(struct dbg_stuff *dsp);
531static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000532
533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100534save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000535{
536 dsp->trylevel = trylevel; trylevel = 0;
537 dsp->force_abort = force_abort; force_abort = FALSE;
538 dsp->caught_stack = caught_stack; caught_stack = NULL;
539 dsp->vv_exception = v_exception(NULL);
540 dsp->vv_throwpoint = v_throwpoint(NULL);
541
542 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
543 dsp->did_emsg = did_emsg; did_emsg = FALSE;
544 dsp->got_int = got_int; got_int = FALSE;
545 dsp->did_throw = did_throw; did_throw = FALSE;
546 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
547 dsp->check_cstack = check_cstack; check_cstack = FALSE;
548 dsp->current_exception = current_exception; current_exception = NULL;
549}
550
551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100552restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000553{
554 suppress_errthrow = FALSE;
555 trylevel = dsp->trylevel;
556 force_abort = dsp->force_abort;
557 caught_stack = dsp->caught_stack;
558 (void)v_exception(dsp->vv_exception);
559 (void)v_throwpoint(dsp->vv_throwpoint);
560 did_emsg = dsp->did_emsg;
561 got_int = dsp->got_int;
562 did_throw = dsp->did_throw;
563 need_rethrow = dsp->need_rethrow;
564 check_cstack = dsp->check_cstack;
565 current_exception = dsp->current_exception;
566}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569/*
570 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
571 * command is given.
572 */
573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100574do_exmode(
575 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
577 int save_msg_scroll;
578 int prev_msg_row;
579 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100580 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000581
582 if (improved)
583 exmode_active = EXMODE_VIM;
584 else
585 exmode_active = EXMODE_NORMAL;
586 State = NORMAL;
587
588 /* When using ":global /pat/ visual" and then "Q" we return to continue
589 * the :global command. */
590 if (global_busy)
591 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 save_msg_scroll = msg_scroll;
594 ++RedrawingDisabled; /* don't redisplay the window */
595 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#ifdef FEAT_GUI
597 /* Ignore scrollbar and mouse events in Ex mode */
598 ++hold_gui_events;
599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
602 while (exmode_active)
603 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000604 /* Check for a ":normal" command and no more characters left. */
605 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
606 {
607 exmode_active = FALSE;
608 break;
609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 msg_scroll = TRUE;
611 need_wait_return = FALSE;
612 ex_pressedreturn = FALSE;
613 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100614 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 prev_msg_row = msg_row;
616 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (improved)
618 {
619 cmdline_row = msg_row;
620 do_cmdline(NULL, getexline, NULL, 0);
621 }
622 else
623 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
624 lines_left = Rows - 1;
625
Bram Moolenaardf177f62005-02-22 08:39:57 +0000626 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100627 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 if (curbuf->b_ml.ml_flags & ML_EMPTY)
630 EMSG(_(e_emptybuf));
631 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000633 if (ex_pressedreturn)
634 {
635 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000636 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000637 msg_row = prev_msg_row;
638 if (prev_msg_row == Rows - 1)
639 msg_row--;
640 }
641 msg_col = 0;
642 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
643 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000646 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
647 {
648 if (curbuf->b_ml.ml_flags & ML_EMPTY)
649 EMSG(_(e_emptybuf));
650 else
651 EMSG(_("E501: At end-of-file"));
652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654
655#ifdef FEAT_GUI
656 --hold_gui_events;
657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 --RedrawingDisabled;
659 --no_wait_return;
660 update_screen(CLEAR);
661 need_wait_return = FALSE;
662 msg_scroll = save_msg_scroll;
663}
664
665/*
666 * Execute a simple command line. Used for translated commands like "*".
667 */
668 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100669do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 return do_cmdline(cmd, NULL, NULL,
672 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
673}
674
675/*
676 * do_cmdline(): execute one Ex command line
677 *
678 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100679 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * 2. Split up in parts separated with '|'.
681 *
682 * This function can be called recursively!
683 *
684 * flags:
685 * DOCMD_VERBOSE - The command will be included in the error message.
686 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100687 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * DOCMD_KEYTYPED - Don't reset KeyTyped.
689 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
690 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
691 *
692 * return FAIL if cmdline could not be executed, OK otherwise
693 */
694 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100695do_cmdline(
696 char_u *cmdline,
697 char_u *(*fgetline)(int, void *, int),
698 void *cookie, /* argument for fgetline() */
699 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 char_u *next_cmdline; /* next cmd to execute */
702 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 static int recursive = 0; /* recursive depth */
705 int msg_didout_before_start = 0;
706 int count = 0; /* line number count */
707 int did_inc = FALSE; /* incremented RedrawingDisabled */
708 int retval = OK;
709#ifdef FEAT_EVAL
710 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000711 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 int current_line = 0; /* active line in lines_ga */
713 char_u *fname = NULL; /* function or script name */
714 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
715 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000716 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 int initial_trylevel;
718 struct msglist **saved_msg_list = NULL;
719 struct msglist *private_msg_list;
720
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100722 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000724 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000726 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729# define cmd_cookie cookie
730#endif
731 static int call_depth = 0; /* recursiveness */
732
733#ifdef FEAT_EVAL
734 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
735 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000736 * This ensures that the do_errthrow() call in do_one_cmd() does not
737 * combine the messages stored by an earlier invocation of do_one_cmd()
738 * with the command name of the later one. This would happen when
739 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 saved_msg_list = msg_list;
741 msg_list = &private_msg_list;
742 private_msg_list = NULL;
743#endif
744
745 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100746 * here. The value of 200 allows nested function calls, ":source", etc.
747 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100748 if (call_depth >= 200
749#ifdef FEAT_EVAL
750 && call_depth >= p_mfd
751#endif
752 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 EMSG(_("E169: Command too recursive"));
755#ifdef FEAT_EVAL
756 /* When converting to an exception, we do not include the command name
757 * since this is not an error of the specific command. */
758 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
759 msg_list = saved_msg_list;
760#endif
761 return FAIL;
762 }
763 ++call_depth;
764
765#ifdef FEAT_EVAL
766 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 cstack.cs_trylevel = 0;
769 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000770 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
772
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100776 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 ++ex_nesting_level;
779
780 /* Get the function or script name and the address where the next breakpoint
781 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
784 fname = func_name(real_cookie);
785 breakpoint = func_breakpoint(real_cookie);
786 dbg_tick = func_dbg_tick(real_cookie);
787 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
790 fname = sourcing_name;
791 breakpoint = source_breakpoint(real_cookie);
792 dbg_tick = source_dbg_tick(real_cookie);
793 }
794
795 /*
796 * Initialize "force_abort" and "suppress_errthrow" at the top level.
797 */
798 if (!recursive)
799 {
800 force_abort = FALSE;
801 suppress_errthrow = FALSE;
802 }
803
804 /*
805 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000806 * exception handling (used when debugging). Otherwise clear it to avoid
807 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000809 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000810 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000811 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200812 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 initial_trylevel = trylevel;
815
816 /*
817 * "did_throw" will be set to TRUE when an exception is being thrown.
818 */
819 did_throw = FALSE;
820#endif
821 /*
822 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * If force_abort is set, we cancel everything.
825 */
826 did_emsg = FALSE;
827
828 /*
829 * KeyTyped is only set when calling vgetc(). Reset it here when not
830 * calling vgetc() (sourced command lines).
831 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (!(flags & DOCMD_KEYTYPED)
833 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 KeyTyped = FALSE;
835
836 /*
837 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * - for multiple commands on one line, separated with '|'
840 * - when repeating until there are no more lines (for ":source")
841 */
842 next_cmdline = cmdline;
843 do
844 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847#endif
848
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000849 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (next_cmdline == NULL
851#ifdef FEAT_EVAL
852 && !force_abort
853 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
856 )
857 did_emsg = FALSE;
858
859 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * 3. If a line is given: Make a copy, so we can mess with it.
863 */
864
865#ifdef FEAT_EVAL
866 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
869 /* Each '|' separated command is stored separately in lines_ga, to
870 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100871 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 /* Check if a function has returned or, unless it has an unclosed
874 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000875 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000878 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000879 func_line_end(real_cookie);
880# endif
881 if (func_has_ended(real_cookie))
882 {
883 retval = FAIL;
884 break;
885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000888 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100889 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890 script_line_end();
891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100894 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
896 retval = FAIL;
897 break;
898 }
899
900 /* If breakpoints have been added/deleted need to check for it. */
901 if (breakpoint != NULL && dbg_tick != NULL
902 && *dbg_tick != debug_tick)
903 {
904 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100905 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 fname, sourcing_lnum);
907 *dbg_tick = debug_tick;
908 }
909
910 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
911 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
912
913 /* Did we encounter a breakpoint? */
914 if (breakpoint != NULL && *breakpoint != 0
915 && *breakpoint <= sourcing_lnum)
916 {
917 dbg_breakpoint(fname, sourcing_lnum);
918 /* Find next breakpoint. */
919 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100920 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 fname, sourcing_lnum);
922 *dbg_tick = debug_tick;
923 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000924# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000925 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000926 {
927 if (getline_is_func)
928 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930 script_line_start();
931 }
932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100938 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * below, so that it stores lines in or reads them from
940 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000941 * while/for loop. */
942 cmd_getline = get_loop_line;
943 cmd_cookie = (void *)&cmd_loop_cookie;
944 cmd_loop_cookie.lines_gap = &lines_ga;
945 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000947 cmd_loop_cookie.cookie = cookie;
948 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950 else
951 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100952 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 cmd_cookie = cookie;
954 }
955#endif
956
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (next_cmdline == NULL)
959 {
960 /*
961 * Need to set msg_didout for the first line after an ":if",
962 * otherwise the ":if" will be overwritten.
963 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100964 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100966 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_EVAL
968 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
969#else
970 0
971#endif
972 )) == NULL)
973 {
974 /* Don't call wait_return for aborted command line. The NULL
975 * returned for the end of a sourced file or executed function
976 * doesn't do this. */
977 if (KeyTyped && !(flags & DOCMD_REPEAT))
978 need_wait_return = FALSE;
979 retval = FAIL;
980 break;
981 }
982 used_getline = TRUE;
983
984 /*
985 * Keep the first typed line. Clear it when more lines are typed.
986 */
987 if (flags & DOCMD_KEEPLINE)
988 {
989 vim_free(repeat_cmdline);
990 if (count == 0)
991 repeat_cmdline = vim_strsave(next_cmdline);
992 else
993 repeat_cmdline = NULL;
994 }
995 }
996
997 /* 3. Make a copy of the command so we can mess with it. */
998 else if (cmdline_copy == NULL)
999 {
1000 next_cmdline = vim_strsave(next_cmdline);
1001 if (next_cmdline == NULL)
1002 {
1003 EMSG(_(e_outofmem));
1004 retval = FAIL;
1005 break;
1006 }
1007 }
1008 cmdline_copy = next_cmdline;
1009
1010#ifdef FEAT_EVAL
1011 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001012 * Save the current line when inside a ":while" or ":for", and when
1013 * the command looks like a ":while" or ":for", because we may need it
1014 * later. When there is a '|' and another command, it is stored
1015 * separately, because we need to be able to jump back to it from an
1016 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001018 if (current_line == lines_ga.ga_len
1019 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001021 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 retval = FAIL;
1024 break;
1025 }
1026 }
1027 did_endif = FALSE;
1028#endif
1029
1030 if (count++ == 0)
1031 {
1032 /*
1033 * All output from the commands is put below each other, without
1034 * waiting for a return. Don't do this when executing commands
1035 * from a script or when being called recursive (e.g. for ":e
1036 * +command file").
1037 */
1038 if (!(flags & DOCMD_NOWAIT) && !recursive)
1039 {
1040 msg_didout_before_start = msg_didout;
1041 msg_didany = FALSE; /* no output yet */
1042 msg_start();
1043 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001044 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 ++RedrawingDisabled;
1046 did_inc = TRUE;
1047 }
1048 }
1049
1050 if (p_verbose >= 15 && sourcing_name != NULL)
1051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001053 verbose_enter_scroll();
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 smsg((char_u *)_("line %ld: %s"),
1056 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001057 if (msg_silent == 0)
1058 msg_puts((char_u *)"\n"); /* don't overwrite this */
1059
1060 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 --no_wait_return;
1062 }
1063
1064 /*
1065 * 2. Execute one '|' separated command.
1066 * do_one_cmd() will return NULL if there is no trailing '|'.
1067 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1068 */
1069 ++recursive;
1070 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1071#ifdef FEAT_EVAL
1072 &cstack,
1073#endif
1074 cmd_getline, cmd_cookie);
1075 --recursive;
1076
1077#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 if (cmd_cookie == (void *)&cmd_loop_cookie)
1079 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082#endif
1083
1084 if (next_cmdline == NULL)
1085 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001086 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef FEAT_CMDHIST
1088 /*
1089 * If the command was typed, remember it for the ':' register.
1090 * Do this AFTER executing the command to make :@: work.
1091 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001092 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 && new_last_cmdline != NULL)
1094 {
1095 vim_free(last_cmdline);
1096 last_cmdline = new_last_cmdline;
1097 new_last_cmdline = NULL;
1098 }
1099#endif
1100 }
1101 else
1102 {
1103 /* need to copy the command after the '|' to cmdline_copy, for the
1104 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001105 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 next_cmdline = cmdline_copy;
1107 }
1108
1109
1110#ifdef FEAT_EVAL
1111 /* reset did_emsg for a function that is not aborted by an error */
1112 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001113 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 && !func_has_abort(real_cookie))
1115 did_emsg = FALSE;
1116
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 ++current_line;
1120
1121 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 * An ":endwhile", ":endfor" and ":continue" is handled here.
1123 * If we were executing commands, jump back to the ":while" or
1124 * ":for".
1125 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 /* Jump back to the matching ":while" or ":for". Be careful
1132 * not to use a cs_line[] from an entry that isn't a ":while"
1133 * or ":for": It would make "current_line" invalid and can
1134 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 if (!did_emsg && !got_int && !did_throw
1136 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 && (cstack.cs_flags[cstack.cs_idx]
1138 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 && cstack.cs_line[cstack.cs_idx] >= 0
1140 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1141 {
1142 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 /* remember we jumped there */
1144 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 line_breakcheck(); /* check if CTRL-C typed */
1146
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 /* Check for the next breakpoint at or after the ":while"
1148 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (breakpoint != NULL)
1150 {
1151 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001152 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 fname,
1154 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1155 *dbg_tick = debug_tick;
1156 }
1157 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001162 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1163 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 }
1166
1167 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1174 }
1175 }
1176
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001177 /* Check for the next breakpoint after a watchexpression */
1178 if (breakpoint != NULL && has_watchexpr())
1179 {
1180 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1181 *dbg_tick = debug_tick;
1182 }
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 /*
1185 * When not inside any ":while" loop, clear remembered lines.
1186 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
1189 if (lines_ga.ga_len > 0)
1190 {
1191 sourcing_lnum =
1192 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1193 free_cmdlines(&lines_ga);
1194 }
1195 current_line = 0;
1196 }
1197
1198 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1200 * being restored at the ":endtry". Reset them here and set the
1201 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1202 * This includes the case where a missing ":endif", ":endwhile" or
1203 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 cstack.cs_lflags &= ~CSL_HAD_FINA;
1208 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1209 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 did_throw ? (void *)current_exception : NULL);
1211 did_emsg = got_int = did_throw = FALSE;
1212 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1213 }
1214
1215 /* Update global "trylevel" for recursive calls to do_cmdline() from
1216 * within this loop. */
1217 trylevel = initial_trylevel + cstack.cs_trylevel;
1218
1219 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001220 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 * files) is aborted because of an error, an interrupt, or an uncaught
1222 * exception, cancel everything. If it is left normally, reset
1223 * force_abort to get the non-EH compatible abortion behavior for
1224 * the rest of the script.
1225 */
1226 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1227 force_abort = FALSE;
1228
1229 /* Convert an interrupt to an exception if appropriate. */
1230 (void)do_intthrow(&cstack);
1231#endif /* FEAT_EVAL */
1232
1233 }
1234 /*
1235 * Continue executing command lines when:
1236 * - no CTRL-C typed, no aborting error, no exception thrown or try
1237 * conditionals need to be checked for executing finally clauses or
1238 * catching an interrupt exception
1239 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 * looping for ":source" command or function call.
1242 */
1243 while (!((got_int
1244#ifdef FEAT_EVAL
1245 || (did_emsg && force_abort) || did_throw
1246#endif
1247 )
1248#ifdef FEAT_EVAL
1249 && cstack.cs_trylevel == 0
1250#endif
1251 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001252 && !(did_emsg
1253#ifdef FEAT_EVAL
1254 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001255 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001256 * the :endtry to be missed. */
1257 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1258#endif
1259 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001260 && (getline_equal(fgetline, cookie, getexmodeline)
1261 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 && (next_cmdline != NULL
1263#ifdef FEAT_EVAL
1264 || cstack.cs_idx >= 0
1265#endif
1266 || (flags & DOCMD_REPEAT)));
1267
1268 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001269 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270#ifdef FEAT_EVAL
1271 free_cmdlines(&lines_ga);
1272 ga_clear(&lines_ga);
1273
1274 if (cstack.cs_idx >= 0)
1275 {
1276 /*
1277 * If a sourced file or executed function ran to its end, report the
1278 * unclosed conditional.
1279 */
1280 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001281 && ((getline_equal(fgetline, cookie, getsourceline)
1282 && !source_finished(fgetline, cookie))
1283 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 && !func_has_ended(real_cookie))))
1285 {
1286 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1287 EMSG(_(e_endtry));
1288 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1289 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001290 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1291 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 else
1293 EMSG(_(e_endif));
1294 }
1295
1296 /*
1297 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1298 * ":endtry" in a sourced file or executed function. If the try
1299 * conditional is in its finally clause, ignore anything pending.
1300 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001301 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 */
1303 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001304 {
1305 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1306
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001307 if (idx >= 0)
1308 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001309 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1310 &cstack.cs_looplevel);
1311 }
1312 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 trylevel = initial_trylevel;
1314 }
1315
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001316 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1317 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001319 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 ? (char_u *)"endfunction" : (char_u *)NULL);
1321
1322 if (trylevel == 0)
1323 {
1324 /*
1325 * When an exception is being thrown out of the outermost try
1326 * conditional, discard the uncaught exception, disable the conversion
1327 * of interrupts or errors to exceptions, and ensure that no more
1328 * commands are executed.
1329 */
1330 if (did_throw)
1331 {
1332 void *p = NULL;
1333 char_u *saved_sourcing_name;
1334 int saved_sourcing_lnum;
1335 struct msglist *messages = NULL, *next;
1336
1337 /*
1338 * If the uncaught exception is a user exception, report it as an
1339 * error. If it is an error exception, display the saved error
1340 * message now. For an interrupt exception, do nothing; the
1341 * interrupt message is given elsewhere.
1342 */
1343 switch (current_exception->type)
1344 {
1345 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001346 vim_snprintf((char *)IObuff, IOSIZE,
1347 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 current_exception->value);
1349 p = vim_strsave(IObuff);
1350 break;
1351 case ET_ERROR:
1352 messages = current_exception->messages;
1353 current_exception->messages = NULL;
1354 break;
1355 case ET_INTERRUPT:
1356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358
1359 saved_sourcing_name = sourcing_name;
1360 saved_sourcing_lnum = sourcing_lnum;
1361 sourcing_name = current_exception->throw_name;
1362 sourcing_lnum = current_exception->throw_lnum;
1363 current_exception->throw_name = NULL;
1364
1365 discard_current_exception(); /* uses IObuff if 'verbose' */
1366 suppress_errthrow = TRUE;
1367 force_abort = TRUE;
1368
1369 if (messages != NULL)
1370 {
1371 do
1372 {
1373 next = messages->next;
1374 emsg(messages->msg);
1375 vim_free(messages->msg);
1376 vim_free(messages);
1377 messages = next;
1378 }
1379 while (messages != NULL);
1380 }
1381 else if (p != NULL)
1382 {
1383 emsg(p);
1384 vim_free(p);
1385 }
1386 vim_free(sourcing_name);
1387 sourcing_name = saved_sourcing_name;
1388 sourcing_lnum = saved_sourcing_lnum;
1389 }
1390
1391 /*
1392 * On an interrupt or an aborting error not converted to an exception,
1393 * disable the conversion of errors to exceptions. (Interrupts are not
1394 * converted any more, here.) This enables also the interrupt message
1395 * when force_abort is set and did_emsg unset in case of an interrupt
1396 * from a finally clause after an error.
1397 */
1398 else if (got_int || (did_emsg && force_abort))
1399 suppress_errthrow = TRUE;
1400 }
1401
1402 /*
1403 * The current cstack will be freed when do_cmdline() returns. An uncaught
1404 * exception will have to be rethrown in the previous cstack. If a function
1405 * has just returned or a script file was just finished and the previous
1406 * cstack belongs to the same function or, respectively, script file, it
1407 * will have to be checked for finally clauses to be executed due to the
1408 * ":return" or ":finish". This is done in do_one_cmd().
1409 */
1410 if (did_throw)
1411 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001412 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001414 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 && ex_nesting_level > func_level(real_cookie) + 1))
1416 {
1417 if (!did_throw)
1418 check_cstack = TRUE;
1419 }
1420 else
1421 {
1422 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001423 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 --ex_nesting_level;
1425 /*
1426 * Go to debug mode when returning from a function in which we are
1427 * single-stepping.
1428 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001429 if ((getline_equal(fgetline, cookie, getsourceline)
1430 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001432 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 ? (char_u *)_("End of sourced file")
1434 : (char_u *)_("End of function"));
1435 }
1436
1437 /*
1438 * Restore the exception environment (done after returning from the
1439 * debugger).
1440 */
1441 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001442 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 msg_list = saved_msg_list;
1445#endif /* FEAT_EVAL */
1446
1447 /*
1448 * If there was too much output to fit on the command line, ask the user to
1449 * hit return before redrawing the screen. With the ":global" command we do
1450 * this only once after the command is finished.
1451 */
1452 if (did_inc)
1453 {
1454 --RedrawingDisabled;
1455 --no_wait_return;
1456 msg_scroll = FALSE;
1457
1458 /*
1459 * When just finished an ":if"-":else" which was typed, no need to
1460 * wait for hit-return. Also for an error situation.
1461 */
1462 if (retval == FAIL
1463#ifdef FEAT_EVAL
1464 || (did_endif && KeyTyped && !did_emsg)
1465#endif
1466 )
1467 {
1468 need_wait_return = FALSE;
1469 msg_didany = FALSE; /* don't wait when restarting edit */
1470 }
1471 else if (need_wait_return)
1472 {
1473 /*
1474 * The msg_start() above clears msg_didout. The wait_return we do
1475 * here should not overwrite the command that may be shown before
1476 * doing that.
1477 */
1478 msg_didout |= msg_didout_before_start;
1479 wait_return(FALSE);
1480 }
1481 }
1482
Bram Moolenaar2a942252012-11-28 23:03:07 +01001483#ifdef FEAT_EVAL
1484 did_endif = FALSE; /* in case do_cmdline used recursively */
1485#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 /*
1487 * Reset if_level, in case a sourced script file contains more ":if" than
1488 * ":endif" (could be ":if x | foo | endif").
1489 */
1490 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001491#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 --call_depth;
1494 return retval;
1495}
1496
1497#ifdef FEAT_EVAL
1498/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
1501 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001502get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 wcmd_T *wp;
1506 char_u *line;
1507
1508 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1509 {
1510 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001511 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001513 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 if (cp->getline == NULL)
1515 line = getcmdline(c, 0L, indent);
1516 else
1517 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001518 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 ++cp->current_line;
1520
1521 return line;
1522 }
1523
1524 KeyTyped = FALSE;
1525 ++cp->current_line;
1526 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1527 sourcing_lnum = wp->lnum;
1528 return vim_strsave(wp->line);
1529}
1530
1531/*
1532 * Store a line in "gap" so that a ":while" loop can execute it again.
1533 */
1534 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001535store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 if (ga_grow(gap, 1) == FAIL)
1538 return FAIL;
1539 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1540 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1541 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return OK;
1543}
1544
1545/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001549free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
1551 while (gap->ga_len > 0)
1552 {
1553 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1554 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556}
1557#endif
1558
1559/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001560 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1561 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001564getline_equal(
1565 char_u *(*fgetline)(int, void *, int),
1566 void *cookie UNUSED, /* argument for fgetline() */
1567 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001570 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001571 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaar89d40322006-08-29 15:30:07 +00001573 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001574 * function that's originally used to obtain the lines. This may be
1575 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001576 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577 cp = (struct loop_cookie *)cookie;
1578 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 gp = cp->getline;
1581 cp = cp->cookie;
1582 }
1583 return gp == func;
1584#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001585 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586#endif
1587}
1588
1589#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1590/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001591 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * getline function. Otherwise return "cookie".
1593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001595getline_cookie(
1596 char_u *(*fgetline)(int, void *, int) UNUSED,
1597 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001600 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001601 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaar89d40322006-08-29 15:30:07 +00001603 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001604 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001606 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001607 cp = (struct loop_cookie *)cookie;
1608 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 gp = cp->getline;
1611 cp = cp->cookie;
1612 }
1613 return cp;
1614# else
1615 return cookie;
1616# endif
1617}
1618#endif
1619
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001620
1621/*
1622 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1623 * ":bwipeout", etc.
1624 * Returns the buffer number.
1625 */
1626 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001627compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628{
1629 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001630 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 int count = offset;
1632
1633 buf = firstbuf;
1634 while (buf->b_next != NULL && buf->b_fnum < lnum)
1635 buf = buf->b_next;
1636 while (count != 0)
1637 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001638 count += (offset < 0) ? 1 : -1;
1639 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1640 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001642 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001643 if (addr_type == ADDR_LOADED_BUFFERS)
1644 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001645 while (buf->b_ml.ml_mfp == NULL)
1646 {
1647 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1648 if (nextbuf == NULL)
1649 break;
1650 buf = nextbuf;
1651 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001652 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001653 /* we might have gone too far, last buffer is not loadedd */
1654 if (addr_type == ADDR_LOADED_BUFFERS)
1655 while (buf->b_ml.ml_mfp == NULL)
1656 {
1657 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1658 if (nextbuf == NULL)
1659 break;
1660 buf = nextbuf;
1661 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662 return buf->b_fnum;
1663}
1664
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001665static int current_win_nr(win_T *win);
1666static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001667
1668 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001669current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670{
1671 win_T *wp;
1672 int nr = 0;
1673
Bram Moolenaar29323592016-07-24 22:04:11 +02001674 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001675 {
1676 ++nr;
1677 if (wp == win)
1678 break;
1679 }
1680 return nr;
1681}
1682
1683 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001684current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001685{
1686 tabpage_T *tp;
1687 int nr = 0;
1688
Bram Moolenaar29323592016-07-24 22:04:11 +02001689 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001690 {
1691 ++nr;
1692 if (tp == tab)
1693 break;
1694 }
1695 return nr;
1696}
1697
1698# define CURRENT_WIN_NR current_win_nr(curwin)
1699# define LAST_WIN_NR current_win_nr(NULL)
1700# define CURRENT_TAB_NR current_tab_nr(curtab)
1701# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703/*
1704 * Execute one Ex command.
1705 *
1706 * If 'sourcing' is TRUE, the command will be included in the error message.
1707 *
1708 * 1. skip comment lines and leading space
1709 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001710 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001712 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001713 * 6. parse arguments
1714 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 *
1718 * This function may be called recursively!
1719 */
1720#if (_MSC_VER == 1200)
1721/*
Bram Moolenaared203462004-06-16 11:19:22 +00001722 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001724 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
1726 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001727do_one_cmd(
1728 char_u **cmdlinep,
1729 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001731 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001733 char_u *(*fgetline)(int, void *, int),
1734 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
1736 char_u *p;
1737 linenr_T lnum;
1738 long n;
1739 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001740 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 exarg_T ea; /* Ex command arguments */
1742 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001743 int save_msg_scroll = msg_scroll;
1744 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001746#ifdef HAVE_SANDBOX
1747 int did_sandbox = FALSE;
1748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 cmdmod_T save_cmdmod;
1750 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001751 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001752 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 vim_memset(&ea, 0, sizeof(ea));
1755 ea.line1 = 1;
1756 ea.line2 = 1;
1757#ifdef FEAT_EVAL
1758 ++ex_nesting_level;
1759#endif
1760
Bram Moolenaar21691f82012-12-05 19:13:18 +01001761 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (quitmore
1763#ifdef FEAT_EVAL
1764 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001765 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001766#endif
1767#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001768 /* avoid that an autocommand, e.g. QuitPre, does this */
1769 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#endif
1771 )
1772 --quitmore;
1773
1774 /*
1775 * Reset browse, confirm, etc.. They are restored when returning, for
1776 * recursive calls.
1777 */
1778 save_cmdmod = cmdmod;
1779 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1780
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001781 /* "#!anything" is handled like a comment. */
1782 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1783 goto doend;
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /*
1786 * Repeat until no more command modifiers are found.
1787 */
1788 ea.cmd = *cmdlinep;
1789 for (;;)
1790 {
1791/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001792 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 */
1794 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1795 ++ea.cmd;
1796
1797 /* in ex mode, an empty line works like :+ */
1798 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001799 && (getline_equal(fgetline, cookie, getexmodeline)
1800 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001801 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 ea.cmd = (char_u *)"+";
1804 ex_pressedreturn = TRUE;
1805 }
1806
1807 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001808 if (*ea.cmd == '"')
1809 goto doend;
1810 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001811 {
1812 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
1816/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001817 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001819 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 switch (*p)
1821 {
1822 /* When adding an entry, also modify cmd_exists(). */
1823 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 continue;
1827
1828 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 continue;
1832 }
1833 if (checkforcmd(&ea.cmd, "browse", 3))
1834 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001835#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 cmdmod.browse = TRUE;
1837#endif
1838 continue;
1839 }
1840 if (!checkforcmd(&ea.cmd, "botright", 2))
1841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 continue;
1844
1845 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1846 break;
1847#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1848 cmdmod.confirm = TRUE;
1849#endif
1850 continue;
1851
1852 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1853 {
1854 cmdmod.keepmarks = TRUE;
1855 continue;
1856 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001857 if (checkforcmd(&ea.cmd, "keepalt", 5))
1858 {
1859 cmdmod.keepalt = TRUE;
1860 continue;
1861 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001862 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1863 {
1864 cmdmod.keeppatterns = TRUE;
1865 continue;
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1868 break;
1869 cmdmod.keepjumps = TRUE;
1870 continue;
1871
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001872 case 'f': /* only accept ":filter {pat} cmd" */
1873 {
1874 char_u *reg_pat;
1875
1876 if (!checkforcmd(&p, "filter", 4)
1877 || *p == NUL || ends_excmd(*p))
1878 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001879 if (*p == '!')
1880 {
1881 cmdmod.filter_force = TRUE;
1882 p = skipwhite(p + 1);
1883 if (*p == NUL || ends_excmd(*p))
1884 break;
1885 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001886 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1887 if (p == NULL || *p == NUL)
1888 break;
1889 cmdmod.filter_regmatch.regprog =
1890 vim_regcomp(reg_pat, RE_MAGIC);
1891 if (cmdmod.filter_regmatch.regprog == NULL)
1892 break;
1893 ea.cmd = p;
1894 continue;
1895 }
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 /* ":hide" and ":hide | cmd" are not modifiers */
1898 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1899 || *p == NUL || ends_excmd(*p))
1900 break;
1901 ea.cmd = p;
1902 cmdmod.hide = TRUE;
1903 continue;
1904
1905 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1906 {
1907 cmdmod.lockmarks = TRUE;
1908 continue;
1909 }
1910
1911 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1912 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 continue;
1915
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001916 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001917 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001918#ifdef FEAT_AUTOCMD
1919 if (cmdmod.save_ei == NULL)
1920 {
1921 /* Set 'eventignore' to "all". Restore the
1922 * existing option value later. */
1923 cmdmod.save_ei = vim_strsave(p_ei);
1924 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001925 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001926 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001927#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001928 continue;
1929 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001930 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001931 break;
1932 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001933 continue;
1934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 continue;
1939
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001940 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1941 {
1942#ifdef HAVE_SANDBOX
1943 if (!did_sandbox)
1944 ++sandbox;
1945 did_sandbox = TRUE;
1946#endif
1947 continue;
1948 }
1949 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001951 if (save_msg_silent == -1)
1952 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001954 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 /* ":silent!", but not "silent !cmd" */
1957 ea.cmd = skipwhite(ea.cmd + 1);
1958 ++emsg_silent;
1959 ++did_esilent;
1960 }
1961 continue;
1962
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001963 case 't': if (checkforcmd(&p, "tab", 3))
1964 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001965 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001966 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001967 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001968 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001969 else
1970 {
1971 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1972 {
1973 errormsg = (char_u *)_(e_invrange);
1974 goto doend;
1975 }
1976 cmdmod.tab = tabnr + 1;
1977 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001978 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001979 continue;
1980 }
1981 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 continue;
1985
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001986 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1987 break;
1988 if (save_msg_silent == -1)
1989 save_msg_silent = msg_silent;
1990 msg_silent = 0;
1991 continue;
1992
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 continue;
1997 }
1998 if (!checkforcmd(&p, "verbose", 4))
1999 break;
2000 if (verbose_save < 0)
2001 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002002 if (vim_isdigit(*ea.cmd))
2003 p_verbose = atoi((char *)ea.cmd);
2004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 p_verbose = 1;
2006 ea.cmd = p;
2007 continue;
2008 }
2009 break;
2010 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002011 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013#ifdef FEAT_EVAL
2014 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2015 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2016#else
2017 ea.skip = (if_level > 0);
2018#endif
2019
2020#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021# ifdef FEAT_PROFILE
2022 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002023 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002024 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002025 if (getline_equal(fgetline, cookie, get_func_line))
2026 func_line_exec(getline_cookie(fgetline, cookie));
2027 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002028 script_line_exec();
2029 }
2030#endif
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 /* May go to debug mode. If this happens and the ">quit" debug command is
2033 * used, throw an interrupt exception and skip the next command. */
2034 dbg_check_breakpoint(&ea);
2035 if (!ea.skip && got_int)
2036 {
2037 ea.skip = TRUE;
2038 (void)do_intthrow(cstack);
2039 }
2040#endif
2041
2042/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002043 * 3. Skip over the range to find the command. Let "p" point to after it.
2044 *
2045 * We need the command to know what kind of range it uses.
2046 */
2047 cmd = ea.cmd;
2048 ea.cmd = skip_range(ea.cmd, NULL);
2049 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2050 ea.cmd = skipwhite(ea.cmd + 1);
2051 p = find_command(&ea, NULL);
2052
2053/*
2054 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 *
2056 * where 'addr' is:
2057 *
2058 * % (entire file)
2059 * $ [+-NUM]
2060 * 'x [+-NUM] (where x denotes a currently defined mark)
2061 * . [+-NUM]
2062 * [+-NUM]..
2063 * NUM
2064 *
2065 * The ea.cmd pointer is updated to point to the first character following the
2066 * range spec. If an initial address is found, but no second, the upper bound
2067 * is equal to the lower.
2068 */
2069
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002070 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002071 if (!IS_USER_CMDIDX(ea.cmdidx))
2072 {
2073 if (ea.cmdidx != CMD_SIZE)
2074 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2075 else
2076 ea.addr_type = ADDR_LINES;
2077
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002078 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002079 if (ea.cmdidx == CMD_wincmd && p != NULL)
2080 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002081 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002084 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 for (;;)
2086 {
2087 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002088 switch (ea.addr_type)
2089 {
2090 case ADDR_LINES:
2091 /* default is current line number */
2092 ea.line2 = curwin->w_cursor.lnum;
2093 break;
2094 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002095 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002096 break;
2097 case ADDR_ARGUMENTS:
2098 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002099 if (ea.line2 > ARGCOUNT)
2100 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002101 break;
2102 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002103 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002104 ea.line2 = curbuf->b_fnum;
2105 break;
2106 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002107 ea.line2 = CURRENT_TAB_NR;
2108 break;
2109 case ADDR_TABS_RELATIVE:
2110 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002111 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002112#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002113 case ADDR_QUICKFIX:
2114 ea.line2 = qf_get_cur_valid_idx(&ea);
2115 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002116#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002119 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002120 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (ea.cmd == NULL) /* error detected */
2122 goto doend;
2123 if (lnum == MAXLNUM)
2124 {
2125 if (*ea.cmd == '%') /* '%' - all lines */
2126 {
2127 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002128 switch (ea.addr_type)
2129 {
2130 case ADDR_LINES:
2131 ea.line1 = 1;
2132 ea.line2 = curbuf->b_ml.ml_line_count;
2133 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002134 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002135 {
2136 buf_T *buf = firstbuf;
2137
2138 while (buf->b_next != NULL
2139 && buf->b_ml.ml_mfp == NULL)
2140 buf = buf->b_next;
2141 ea.line1 = buf->b_fnum;
2142 buf = lastbuf;
2143 while (buf->b_prev != NULL
2144 && buf->b_ml.ml_mfp == NULL)
2145 buf = buf->b_prev;
2146 ea.line2 = buf->b_fnum;
2147 break;
2148 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002149 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002150 ea.line1 = firstbuf->b_fnum;
2151 ea.line2 = lastbuf->b_fnum;
2152 break;
2153 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002154 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002155 if (IS_USER_CMDIDX(ea.cmdidx))
2156 {
2157 ea.line1 = 1;
2158 ea.line2 = ea.addr_type == ADDR_WINDOWS
2159 ? LAST_WIN_NR : LAST_TAB_NR;
2160 }
2161 else
2162 {
2163 /* there is no Vim command which uses '%' and
2164 * ADDR_WINDOWS or ADDR_TABS */
2165 errormsg = (char_u *)_(e_invrange);
2166 goto doend;
2167 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002168 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002169 case ADDR_TABS_RELATIVE:
2170 errormsg = (char_u *)_(e_invrange);
2171 goto doend;
2172 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002173 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002174 if (ARGCOUNT == 0)
2175 ea.line1 = ea.line2 = 0;
2176 else
2177 {
2178 ea.line1 = 1;
2179 ea.line2 = ARGCOUNT;
2180 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002181 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002182#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002183 case ADDR_QUICKFIX:
2184 ea.line1 = 1;
2185 ea.line2 = qf_get_size(&ea);
2186 if (ea.line2 == 0)
2187 ea.line2 = 1;
2188 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002189#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 ++ea.addr_count;
2192 }
2193 /* '*' - visual area */
2194 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2195 {
2196 pos_T *fp;
2197
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002198 if (ea.addr_type != ADDR_LINES)
2199 {
2200 errormsg = (char_u *)_(e_invrange);
2201 goto doend;
2202 }
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 ++ea.cmd;
2205 if (!ea.skip)
2206 {
2207 fp = getmark('<', FALSE);
2208 if (check_mark(fp) == FAIL)
2209 goto doend;
2210 ea.line1 = fp->lnum;
2211 fp = getmark('>', FALSE);
2212 if (check_mark(fp) == FAIL)
2213 goto doend;
2214 ea.line2 = fp->lnum;
2215 ++ea.addr_count;
2216 }
2217 }
2218 }
2219 else
2220 ea.line2 = lnum;
2221 ea.addr_count++;
2222
2223 if (*ea.cmd == ';')
2224 {
2225 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002228 /* don't leave the cursor on an illegal line or column */
2229 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232 else if (*ea.cmd != ',')
2233 break;
2234 ++ea.cmd;
2235 }
2236
2237 /* One address given: set start and end lines */
2238 if (ea.addr_count == 1)
2239 {
2240 ea.line1 = ea.line2;
2241 /* ... but only implicit: really no address given */
2242 if (lnum == MAXLNUM)
2243 ea.addr_count = 0;
2244 }
2245
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002247 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 */
2249
2250 /*
2251 * Skip ':' and any white space
2252 */
2253 ea.cmd = skipwhite(ea.cmd);
2254 while (*ea.cmd == ':')
2255 ea.cmd = skipwhite(ea.cmd + 1);
2256
2257 /*
2258 * If we got a line, but no command, then go to the line.
2259 * If we find a '|' or '\n' we set ea.nextcmd.
2260 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002261 if (*ea.cmd == NUL || *ea.cmd == '"'
2262 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 /*
2265 * strange vi behaviour:
2266 * ":3" jumps to line 3
2267 * ":3|..." prints line 3
2268 * ":|" prints current line
2269 */
2270 if (ea.skip) /* skip this if inside :if */
2271 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002272 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 ea.cmdidx = CMD_print;
2275 ea.argt = RANGE+COUNT+TRLBAR;
2276 if ((errormsg = invalid_range(&ea)) == NULL)
2277 {
2278 correct_range(&ea);
2279 ex_print(&ea);
2280 }
2281 }
2282 else if (ea.addr_count != 0)
2283 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002284 if (ea.line2 > curbuf->b_ml.ml_line_count)
2285 {
2286 /* With '-' in 'cpoptions' a line number past the file is an
2287 * error, otherwise put it at the end of the file. */
2288 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2289 ea.line2 = -1;
2290 else
2291 ea.line2 = curbuf->b_ml.ml_line_count;
2292 }
2293
2294 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002295 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 else
2297 {
2298 if (ea.line2 == 0)
2299 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 else
2301 curwin->w_cursor.lnum = ea.line2;
2302 beginline(BL_SOL | BL_FIX);
2303 }
2304 }
2305 goto doend;
2306 }
2307
Bram Moolenaard5005162014-08-22 23:05:54 +02002308#ifdef FEAT_AUTOCMD
2309 /* If this looks like an undefined user command and there are CmdUndefined
2310 * autocommands defined, trigger the matching autocommands. */
2311 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2312 && ASCII_ISUPPER(*ea.cmd)
2313 && has_cmdundefined())
2314 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002315 int ret;
2316
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002317 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002318 while (ASCII_ISALNUM(*p))
2319 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002320 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002321 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2322 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002323 /* If the autocommands did something and didn't cause an error, try
2324 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002325 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002326 }
2327#endif
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifdef FEAT_USR_CMDS
2330 if (p == NULL)
2331 {
2332 if (!ea.skip)
2333 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2334 goto doend;
2335 }
2336 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002337 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2338 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 errormsg = uc_fun_cmd();
2341 goto doend;
2342 }
2343#endif
2344 if (ea.cmdidx == CMD_SIZE)
2345 {
2346 if (!ea.skip)
2347 {
2348 STRCPY(IObuff, _("E492: Not an editor command"));
2349 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002350 {
2351 /* If the modifier was parsed OK the error must be in the
2352 * following command */
2353 if (after_modifier != NULL)
2354 append_command(after_modifier);
2355 else
2356 append_command(*cmdlinep);
2357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002359 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361 goto doend;
2362 }
2363
Bram Moolenaar958636c2014-10-21 20:01:58 +02002364 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2365 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002366#ifdef HAVE_EX_SCRIPT_NI
2367 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2368#endif
2369 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371#ifndef FEAT_EVAL
2372 /*
2373 * When the expression evaluation is disabled, recognize the ":if" and
2374 * ":endif" commands and ignore everything in between it.
2375 */
2376 if (ea.cmdidx == CMD_if)
2377 ++if_level;
2378 if (if_level)
2379 {
2380 if (ea.cmdidx == CMD_endif)
2381 --if_level;
2382 goto doend;
2383 }
2384
2385#endif
2386
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002387 /* forced commands */
2388 if (*p == '!' && ea.cmdidx != CMD_substitute
2389 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
2391 ++p;
2392 ea.forceit = TRUE;
2393 }
2394 else
2395 ea.forceit = FALSE;
2396
2397/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002398 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002400 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002401 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 if (!ea.skip)
2404 {
2405#ifdef HAVE_SANDBOX
2406 if (sandbox != 0 && !(ea.argt & SBOXOK))
2407 {
2408 /* Command not allowed in sandbox. */
2409 errormsg = (char_u *)_(e_sandbox);
2410 goto doend;
2411 }
2412#endif
2413 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2414 {
2415 /* Command not allowed in non-'modifiable' buffer */
2416 errormsg = (char_u *)_(e_modifiable);
2417 goto doend;
2418 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002419
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002420 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002421 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002423 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002424 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 goto doend;
2426 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002427#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002428 /* Disallow editing another buffer when "curbuf_lock" is set.
2429 * Do allow ":edit" (check for argument later).
2430 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002431 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002432 && ea.cmdidx != CMD_edit
2433 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002434 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002435 && curbuf_locked())
2436 goto doend;
2437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2440 {
2441 /* no range allowed */
2442 errormsg = (char_u *)_(e_norange);
2443 goto doend;
2444 }
2445 }
2446
2447 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2448 {
2449 errormsg = (char_u *)_(e_nobang);
2450 goto doend;
2451 }
2452
2453 /*
2454 * Don't complain about the range if it is not used
2455 * (could happen if line_count is accidentally set to 0).
2456 */
2457 if (!ea.skip && !ni)
2458 {
2459 /*
2460 * If the range is backwards, ask for confirmation and, if given, swap
2461 * ea.line1 & ea.line2 so it's forwards again.
2462 * When global command is busy, don't ask, will fail below.
2463 */
2464 if (!global_busy && ea.line1 > ea.line2)
2465 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002466 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002468 if (sourcing || exmode_active)
2469 {
2470 errormsg = (char_u *)_("E493: Backwards range given");
2471 goto doend;
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (ask_yesno((char_u *)
2474 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002475 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477 lnum = ea.line1;
2478 ea.line1 = ea.line2;
2479 ea.line2 = lnum;
2480 }
2481 if ((errormsg = invalid_range(&ea)) != NULL)
2482 goto doend;
2483 }
2484
2485 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2486 ea.line2 = 1;
2487
2488 correct_range(&ea);
2489
2490#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002491 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2492 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 /* Put the first line at the start of a closed fold, put the last line
2495 * at the end of a closed fold. */
2496 (void)hasFolding(ea.line1, &ea.line1, NULL);
2497 (void)hasFolding(ea.line2, NULL, &ea.line2);
2498 }
2499#endif
2500
2501#ifdef FEAT_QUICKFIX
2502 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002503 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * option here, so things like % get expanded.
2505 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002506 p = replace_makeprg(&ea, p, cmdlinep);
2507 if (p == NULL)
2508 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509#endif
2510
2511 /*
2512 * Skip to start of argument.
2513 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2514 */
2515 if (ea.cmdidx == CMD_bang)
2516 ea.arg = p;
2517 else
2518 ea.arg = skipwhite(p);
2519
2520 /*
2521 * Check for "++opt=val" argument.
2522 * Must be first, allow ":w ++enc=utf8 !cmd"
2523 */
2524 if (ea.argt & ARGOPT)
2525 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2526 if (getargopt(&ea) == FAIL && !ni)
2527 {
2528 errormsg = (char_u *)_(e_invarg);
2529 goto doend;
2530 }
2531
2532 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2533 {
2534 if (*ea.arg == '>') /* append */
2535 {
2536 if (*++ea.arg != '>') /* typed wrong */
2537 {
2538 errormsg = (char_u *)_("E494: Use w or w>>");
2539 goto doend;
2540 }
2541 ea.arg = skipwhite(ea.arg + 1);
2542 ea.append = TRUE;
2543 }
2544 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2545 {
2546 ++ea.arg;
2547 ea.usefilter = TRUE;
2548 }
2549 }
2550
2551 if (ea.cmdidx == CMD_read)
2552 {
2553 if (ea.forceit)
2554 {
2555 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2556 ea.forceit = FALSE;
2557 }
2558 else if (*ea.arg == '!') /* :r !filter */
2559 {
2560 ++ea.arg;
2561 ea.usefilter = TRUE;
2562 }
2563 }
2564
2565 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2566 {
2567 ea.amount = 1;
2568 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2569 {
2570 ++ea.arg;
2571 ++ea.amount;
2572 }
2573 ea.arg = skipwhite(ea.arg);
2574 }
2575
2576 /*
2577 * Check for "+command" argument, before checking for next command.
2578 * Don't do this for ":read !cmd" and ":write !cmd".
2579 */
2580 if ((ea.argt & EDITCMD) && !ea.usefilter)
2581 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2582
2583 /*
2584 * Check for '|' to separate commands and '"' to start comments.
2585 * Don't do this for ":read !cmd" and ":write !cmd".
2586 */
2587 if ((ea.argt & TRLBAR) && !ea.usefilter)
2588 separate_nextcmd(&ea);
2589
2590 /*
2591 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002592 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2593 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002595 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002596 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002597 || ea.cmdidx == CMD_global
2598 || ea.cmdidx == CMD_vglobal
2599 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 for (p = ea.arg; *p; ++p)
2602 {
2603 /* Remove one backslash before a newline, so that it's possible to
2604 * pass a newline to the shell and also a newline that is preceded
2605 * with a backslash. This makes it impossible to end a shell
2606 * command in a backslash, but that doesn't appear useful.
2607 * Halving the number of backslashes is incompatible with previous
2608 * versions. */
2609 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002610 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 else if (*p == '\n')
2612 {
2613 ea.nextcmd = p + 1;
2614 *p = NUL;
2615 break;
2616 }
2617 }
2618 }
2619
2620 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2621 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002622 buf_T *buf;
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002625 switch (ea.addr_type)
2626 {
2627 case ADDR_LINES:
2628 ea.line2 = curbuf->b_ml.ml_line_count;
2629 break;
2630 case ADDR_LOADED_BUFFERS:
2631 buf = firstbuf;
2632 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2633 buf = buf->b_next;
2634 ea.line1 = buf->b_fnum;
2635 buf = lastbuf;
2636 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2637 buf = buf->b_prev;
2638 ea.line2 = buf->b_fnum;
2639 break;
2640 case ADDR_BUFFERS:
2641 ea.line1 = firstbuf->b_fnum;
2642 ea.line2 = lastbuf->b_fnum;
2643 break;
2644 case ADDR_WINDOWS:
2645 ea.line2 = LAST_WIN_NR;
2646 break;
2647 case ADDR_TABS:
2648 ea.line2 = LAST_TAB_NR;
2649 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002650 case ADDR_TABS_RELATIVE:
2651 ea.line2 = 1;
2652 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002653 case ADDR_ARGUMENTS:
2654 if (ARGCOUNT == 0)
2655 ea.line1 = ea.line2 = 0;
2656 else
2657 ea.line2 = ARGCOUNT;
2658 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002659#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002660 case ADDR_QUICKFIX:
2661 ea.line2 = qf_get_size(&ea);
2662 if (ea.line2 == 0)
2663 ea.line2 = 1;
2664 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002665#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668
2669 /* accept numbered register only when no count allowed (:put) */
2670 if ( (ea.argt & REGSTR)
2671 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002672 /* Do not allow register = for user commands */
2673 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2675 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002676#ifndef FEAT_CLIPBOARD
2677 /* check these explicitly for a more specific error message */
2678 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002680 errormsg = (char_u *)_(e_invalidreg);
2681 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
2683#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002684 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2685 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002686 {
2687 ea.regname = *ea.arg++;
2688#ifdef FEAT_EVAL
2689 /* for '=' register: accept the rest of the line as an expression */
2690 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2691 {
2692 set_expr_line(vim_strsave(ea.arg));
2693 ea.arg += STRLEN(ea.arg);
2694 }
2695#endif
2696 ea.arg = skipwhite(ea.arg);
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699
2700 /*
2701 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2702 * count, it's a buffer name.
2703 */
2704 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2705 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002706 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
2708 n = getdigits(&ea.arg);
2709 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002710 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
2712 errormsg = (char_u *)_(e_zerocount);
2713 goto doend;
2714 }
2715 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2716 {
2717 ea.line2 = n;
2718 if (ea.addr_count == 0)
2719 ea.addr_count = 1;
2720 }
2721 else
2722 {
2723 ea.line1 = ea.line2;
2724 ea.line2 += n - 1;
2725 ++ea.addr_count;
2726 /*
2727 * Be vi compatible: no error message for out of range.
2728 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002729 if (ea.addr_type == ADDR_LINES
2730 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 ea.line2 = curbuf->b_ml.ml_line_count;
2732 }
2733 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002734
2735 /*
2736 * Check for flags: 'l', 'p' and '#'.
2737 */
2738 if (ea.argt & EXFLAGS)
2739 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002741 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002742 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
2744 errormsg = (char_u *)_(e_trailing);
2745 goto doend;
2746 }
2747
2748 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2749 {
2750 errormsg = (char_u *)_(e_argreq);
2751 goto doend;
2752 }
2753
2754#ifdef FEAT_EVAL
2755 /*
2756 * Skip the command when it's not going to be executed.
2757 * The commands like :if, :endif, etc. always need to be executed.
2758 * Also make an exception for commands that handle a trailing command
2759 * themselves.
2760 */
2761 if (ea.skip)
2762 {
2763 switch (ea.cmdidx)
2764 {
2765 /* commands that need evaluation */
2766 case CMD_while:
2767 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002768 case CMD_for:
2769 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 case CMD_if:
2771 case CMD_elseif:
2772 case CMD_else:
2773 case CMD_endif:
2774 case CMD_try:
2775 case CMD_catch:
2776 case CMD_finally:
2777 case CMD_endtry:
2778 case CMD_function:
2779 break;
2780
2781 /* Commands that handle '|' themselves. Check: A command should
2782 * either have the TRLBAR flag, appear in this list or appear in
2783 * the list at ":help :bar". */
2784 case CMD_aboveleft:
2785 case CMD_and:
2786 case CMD_belowright:
2787 case CMD_botright:
2788 case CMD_browse:
2789 case CMD_call:
2790 case CMD_confirm:
2791 case CMD_delfunction:
2792 case CMD_djump:
2793 case CMD_dlist:
2794 case CMD_dsearch:
2795 case CMD_dsplit:
2796 case CMD_echo:
2797 case CMD_echoerr:
2798 case CMD_echomsg:
2799 case CMD_echon:
2800 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002801 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case CMD_help:
2803 case CMD_hide:
2804 case CMD_ijump:
2805 case CMD_ilist:
2806 case CMD_isearch:
2807 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002808 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 case CMD_keepjumps:
2810 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002811 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 case CMD_leftabove:
2813 case CMD_let:
2814 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002815 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002817 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002818 case CMD_noautocmd:
2819 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case CMD_perl:
2821 case CMD_psearch:
2822 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002823 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002824 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 case CMD_return:
2826 case CMD_rightbelow:
2827 case CMD_ruby:
2828 case CMD_silent:
2829 case CMD_smagic:
2830 case CMD_snomagic:
2831 case CMD_substitute:
2832 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002833 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case CMD_tcl:
2835 case CMD_throw:
2836 case CMD_tilde:
2837 case CMD_topleft:
2838 case CMD_unlet:
2839 case CMD_verbose:
2840 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002841 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 break;
2843
2844 default: goto doend;
2845 }
2846 }
2847#endif
2848
2849 if (ea.argt & XFILE)
2850 {
2851 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2852 goto doend;
2853 }
2854
2855#ifdef FEAT_LISTCMDS
2856 /*
2857 * Accept buffer name. Cannot be used at the same time with a buffer
2858 * number. Don't do this for a user command.
2859 */
2860 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002861 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
2863 /*
2864 * :bdelete, :bwipeout and :bunload take several arguments, separated
2865 * by spaces: find next space (skipping over escaped characters).
2866 * The others take one argument: ignore trailing spaces.
2867 */
2868 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2869 || ea.cmdidx == CMD_bunload)
2870 p = skiptowhite_esc(ea.arg);
2871 else
2872 {
2873 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002874 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 --p;
2876 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002877 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2878 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (ea.line2 < 0) /* failed */
2880 goto doend;
2881 ea.addr_count = 1;
2882 ea.arg = skipwhite(p);
2883 }
2884#endif
2885
2886/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002887 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 *
2889 * The "ea" structure holds the arguments that can be used.
2890 */
2891 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002892 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 ea.cookie = cookie;
2894#ifdef FEAT_EVAL
2895 ea.cstack = cstack;
2896#endif
2897
2898#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002899 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 /*
2902 * Execute a user-defined command.
2903 */
2904 do_ucmd(&ea);
2905 }
2906 else
2907#endif
2908 {
2909 /*
2910 * Call the function to execute the command.
2911 */
2912 ea.errmsg = NULL;
2913 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2914 if (ea.errmsg != NULL)
2915 errormsg = (char_u *)_(ea.errmsg);
2916 }
2917
2918#ifdef FEAT_EVAL
2919 /*
2920 * If the command just executed called do_cmdline(), any throw or ":return"
2921 * or ":finish" encountered there must also check the cstack of the still
2922 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2923 * exception, or reanimate a returned function or finished script file and
2924 * return or finish it again.
2925 */
2926 if (need_rethrow)
2927 do_throw(cstack);
2928 else if (check_cstack)
2929 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002930 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002932 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 && current_func_returned())
2934 do_return(&ea, TRUE, FALSE, NULL);
2935 }
2936 need_rethrow = check_cstack = FALSE;
2937#endif
2938
2939doend:
2940 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002943 curwin->w_cursor.col = 0;
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2947 {
2948 if (sourcing)
2949 {
2950 if (errormsg != IObuff)
2951 {
2952 STRCPY(IObuff, errormsg);
2953 errormsg = IObuff;
2954 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002955 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
2957 emsg(errormsg);
2958 }
2959#ifdef FEAT_EVAL
2960 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002961 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2962 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#endif
2964
2965 if (verbose_save >= 0)
2966 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002967#ifdef FEAT_AUTOCMD
2968 if (cmdmod.save_ei != NULL)
2969 {
2970 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002971 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2972 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002973 free_string_option(cmdmod.save_ei);
2974 }
2975#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002976 if (cmdmod.filter_regmatch.regprog != NULL)
2977 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 cmdmod = save_cmdmod;
2980
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002981 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
2983 /* messages could be enabled for a serious error, need to check if the
2984 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002985 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002986 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 emsg_silent -= did_esilent;
2988 if (emsg_silent < 0)
2989 emsg_silent = 0;
2990 /* Restore msg_scroll, it's set by file I/O commands, even when no
2991 * message is actually displayed. */
2992 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002993
2994 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2995 * somewhere in the line. Put it back in the first column. */
2996 if (redirecting())
2997 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
2999
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003000#ifdef HAVE_SANDBOX
3001 if (did_sandbox)
3002 --sandbox;
3003#endif
3004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3006 ea.nextcmd = NULL;
3007
3008#ifdef FEAT_EVAL
3009 --ex_nesting_level;
3010#endif
3011
3012 return ea.nextcmd;
3013}
3014#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003015 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016#endif
3017
3018/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003019 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 * If there is a match advance "pp" to the argument and return TRUE.
3021 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003023checkforcmd(
3024 char_u **pp, /* start of command */
3025 char *cmd, /* name of command */
3026 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 int i;
3029
3030 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003031 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 break;
3033 if (i >= len && !isalpha((*pp)[i]))
3034 {
3035 *pp = skipwhite(*pp + i);
3036 return TRUE;
3037 }
3038 return FALSE;
3039}
3040
3041/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003042 * Append "cmd" to the error message in IObuff.
3043 * Takes care of limiting the length and handling 0xa0, which would be
3044 * invisible otherwise.
3045 */
3046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003047append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003048{
3049 char_u *s = cmd;
3050 char_u *d;
3051
3052 STRCAT(IObuff, ": ");
3053 d = IObuff + STRLEN(IObuff);
3054 while (*s != NUL && d - IObuff < IOSIZE - 7)
3055 {
3056 if (
3057#ifdef FEAT_MBYTE
3058 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3059#endif
3060 *s == 0xa0)
3061 {
3062 s +=
3063#ifdef FEAT_MBYTE
3064 enc_utf8 ? 2 :
3065#endif
3066 1;
3067 STRCPY(d, "<a0>");
3068 d += 4;
3069 }
3070 else
3071 MB_COPY_CHAR(s, d);
3072 }
3073 *d = NUL;
3074}
3075
3076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003078 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003080 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 * Returns NULL for an ambiguous user command.
3082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003084find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 int len;
3087 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003088 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 /*
3091 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003092 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 * - the 'k' command can directly be followed by any character.
3094 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003095 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003097 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 */
3099 p = eap->cmd;
3100 if (*p == 'k')
3101 {
3102 eap->cmdidx = CMD_k;
3103 ++p;
3104 }
3105 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003106 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3107 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 || p[1] == 'g'
3109 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3110 || p[1] == 'I'
3111 || (p[1] == 'r' && p[2] != 'e')))
3112 {
3113 eap->cmdidx = CMD_substitute;
3114 ++p;
3115 }
3116 else
3117 {
3118 while (ASCII_ISALPHA(*p))
3119 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003120 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003121 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003122 while (ASCII_ISALNUM(*p))
3123 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003124
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 /* check for non-alpha command */
3126 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3127 ++p;
3128 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003129 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3130 {
3131 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3132 * :delete with the 'l' flag. Same for 'p'. */
3133 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003134 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003135 break;
3136 if (i == len - 1)
3137 {
3138 --len;
3139 if (p[-1] == 'l')
3140 eap->flags |= EXFLAG_LIST;
3141 else
3142 eap->flags |= EXFLAG_PRINT;
3143 }
3144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003146 if (ASCII_ISLOWER(eap->cmd[0]))
3147 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003148 int c1 = eap->cmd[0];
3149 int c2 = eap->cmd[1];
3150
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003151 if (command_count != (int)CMD_SIZE)
3152 {
3153 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3154 getout(1);
3155 }
3156
3157 /* Use a precomputed index for fast look-up in cmdnames[]
3158 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003159 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3160 if (ASCII_ISLOWER(c2))
3161 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003164 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
3166 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3167 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3168 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3169 (size_t)len) == 0)
3170 {
3171#ifdef FEAT_EVAL
3172 if (full != NULL
3173 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3174 *full = TRUE;
3175#endif
3176 break;
3177 }
3178
3179#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003180 /* Look for a user defined command as a last resort. Let ":Print" be
3181 * overruled by a user defined command. */
3182 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3183 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003185 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 while (ASCII_ISALNUM(*p))
3187 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
3190#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 eap->cmdidx = CMD_SIZE;
3193 }
3194
3195 return p;
3196}
3197
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198#ifdef FEAT_USR_CMDS
3199/*
3200 * Search for a user command that matches "eap->cmd".
3201 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3202 * Return a pointer to just after the command.
3203 * Return NULL if there is no matching command.
3204 */
3205 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003206find_ucmd(
3207 exarg_T *eap,
3208 char_u *p, /* end of the command (possibly including count) */
3209 int *full, /* set to TRUE for a full match */
3210 expand_T *xp, /* used for completion, NULL otherwise */
3211 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003212{
3213 int len = (int)(p - eap->cmd);
3214 int j, k, matchlen = 0;
3215 ucmd_T *uc;
3216 int found = FALSE;
3217 int possible = FALSE;
3218 char_u *cp, *np; /* Point into typed cmd and test name */
3219 garray_T *gap;
3220 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3221 only full match global is accepted. */
3222
3223 /*
3224 * Look for buffer-local user commands first, then global ones.
3225 */
3226 gap = &curbuf->b_ucmds;
3227 for (;;)
3228 {
3229 for (j = 0; j < gap->ga_len; ++j)
3230 {
3231 uc = USER_CMD_GA(gap, j);
3232 cp = eap->cmd;
3233 np = uc->uc_name;
3234 k = 0;
3235 while (k < len && *np != NUL && *cp++ == *np++)
3236 k++;
3237 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3238 {
3239 /* If finding a second match, the command is ambiguous. But
3240 * not if a buffer-local command wasn't a full match and a
3241 * global command is a full match. */
3242 if (k == len && found && *np != NUL)
3243 {
3244 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246 amb_local = TRUE;
3247 }
3248
3249 if (!found || (k == len && *np == NUL))
3250 {
3251 /* If we matched up to a digit, then there could
3252 * be another command including the digit that we
3253 * should use instead.
3254 */
3255 if (k == len)
3256 found = TRUE;
3257 else
3258 possible = TRUE;
3259
3260 if (gap == &ucmds)
3261 eap->cmdidx = CMD_USER;
3262 else
3263 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003264 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003266 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003267
3268# ifdef FEAT_CMDL_COMPL
3269 if (compl != NULL)
3270 *compl = uc->uc_compl;
3271# ifdef FEAT_EVAL
3272 if (xp != NULL)
3273 {
3274 xp->xp_arg = uc->uc_compl_arg;
3275 xp->xp_scriptID = uc->uc_scriptID;
3276 }
3277# endif
3278# endif
3279 /* Do not search for further abbreviations
3280 * if this is an exact match. */
3281 matchlen = k;
3282 if (k == len && *np == NUL)
3283 {
3284 if (full != NULL)
3285 *full = TRUE;
3286 amb_local = FALSE;
3287 break;
3288 }
3289 }
3290 }
3291 }
3292
3293 /* Stop if we found a full match or searched all. */
3294 if (j < gap->ga_len || gap == &ucmds)
3295 break;
3296 gap = &ucmds;
3297 }
3298
3299 /* Only found ambiguous matches. */
3300 if (amb_local)
3301 {
3302 if (xp != NULL)
3303 xp->xp_context = EXPAND_UNSUCCESSFUL;
3304 return NULL;
3305 }
3306
3307 /* The match we found may be followed immediately by a number. Move "p"
3308 * back to point to it. */
3309 if (found || possible)
3310 return p + (matchlen - len);
3311 return p;
3312}
3313#endif
3314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003316static struct cmdmod
3317{
3318 char *name;
3319 int minlen;
3320 int has_count; /* :123verbose :3tab */
3321} cmdmods[] = {
3322 {"aboveleft", 3, FALSE},
3323 {"belowright", 3, FALSE},
3324 {"botright", 2, FALSE},
3325 {"browse", 3, FALSE},
3326 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003327 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003328 {"hide", 3, FALSE},
3329 {"keepalt", 5, FALSE},
3330 {"keepjumps", 5, FALSE},
3331 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003332 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333 {"leftabove", 5, FALSE},
3334 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003335 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003336 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003337 {"rightbelow", 6, FALSE},
3338 {"sandbox", 3, FALSE},
3339 {"silent", 3, FALSE},
3340 {"tab", 3, TRUE},
3341 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003342 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003343 {"verbose", 4, TRUE},
3344 {"vertical", 4, FALSE},
3345};
3346
3347/*
3348 * Return length of a command modifier (including optional count).
3349 * Return zero when it's not a modifier.
3350 */
3351 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003352modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003353{
3354 int i, j;
3355 char_u *p = cmd;
3356
3357 if (VIM_ISDIGIT(*cmd))
3358 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003359 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360 {
3361 for (j = 0; p[j] != NUL; ++j)
3362 if (p[j] != cmdmods[i].name[j])
3363 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003364 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003365 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003366 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003367 }
3368 return 0;
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371/*
3372 * Return > 0 if an Ex command "name" exists.
3373 * Return 2 if there is an exact match.
3374 * Return 3 if there is an ambiguous match.
3375 */
3376 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003377cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 exarg_T ea;
3380 int full = FALSE;
3381 int i;
3382 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003383 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003386 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 for (j = 0; name[j] != NUL; ++j)
3389 if (name[j] != cmdmods[i].name[j])
3390 break;
3391 if (name[j] == NUL && j >= cmdmods[i].minlen)
3392 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3393 }
3394
Bram Moolenaara9587612006-05-04 21:47:50 +00003395 /* Check built-in commands and user defined commands.
3396 * For ":2match" and ":3match" we need to skip the number. */
3397 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003399 p = find_command(&ea, &full);
3400 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003402 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3403 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003404 if (*skipwhite(p) != NUL)
3405 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3407}
3408#endif
3409
3410/*
3411 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3412 * we don't need/want deleted. Maybe this could be done better if we didn't
3413 * repeat all this stuff. The only problem is that they may not stay
3414 * perfectly compatible with each other, but then the command line syntax
3415 * probably won't change that much -- webb.
3416 */
3417 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003418set_one_cmd_context(
3419 expand_T *xp,
3420 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
3422 char_u *p;
3423 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003424 int len = 0;
3425 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3427 int compl = EXPAND_NOTHING;
3428#endif
3429#ifdef FEAT_CMDL_COMPL
3430 int delim;
3431#endif
3432 int forceit = FALSE;
3433 int usefilter = FALSE; /* filter instead of file name */
3434
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003435 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 xp->xp_pattern = buff;
3437 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003438 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003441 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
3443 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3444 ;
3445 xp->xp_pattern = cmd;
3446
3447 if (*cmd == NUL)
3448 return NULL;
3449 if (*cmd == '"') /* ignore comment lines */
3450 {
3451 xp->xp_context = EXPAND_NOTHING;
3452 return NULL;
3453 }
3454
3455/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003456 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 */
3458 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 xp->xp_pattern = cmd;
3460 if (*cmd == NUL)
3461 return NULL;
3462 if (*cmd == '"')
3463 {
3464 xp->xp_context = EXPAND_NOTHING;
3465 return NULL;
3466 }
3467
3468 if (*cmd == '|' || *cmd == '\n')
3469 return cmd + 1; /* There's another command */
3470
3471 /*
3472 * Isolate the command and search for it in the command table.
3473 * Exceptions:
3474 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003475 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3477 */
3478 if (*cmd == 'k' && cmd[1] != 'e')
3479 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003480 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 p = cmd + 1;
3482 }
3483 else
3484 {
3485 p = cmd;
3486 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3487 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003488 /* a user command may contain digits */
3489 if (ASCII_ISUPPER(cmd[0]))
3490 while (ASCII_ISALNUM(*p) || *p == '*')
3491 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003492 /* for python 3.x: ":py3*" commands completion */
3493 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003494 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003495 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003496 while (ASCII_ISALPHA(*p) || *p == '*')
3497 ++p;
3498 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003499 /* check for non-alpha command */
3500 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3501 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003502 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 xp->xp_context = EXPAND_UNSUCCESSFUL;
3507 return NULL;
3508 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003510 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3511 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3512 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 break;
3514
3515#ifdef FEAT_USR_CMDS
3516 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3518 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#endif
3520 }
3521
3522 /*
3523 * If the cursor is touching the command, and it ends in an alpha-numeric
3524 * character, complete the command name.
3525 */
3526 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3527 return NULL;
3528
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003529 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3532 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003533 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 p = cmd + 1;
3535 }
3536#ifdef FEAT_USR_CMDS
3537 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3538 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 ea.cmd = cmd;
3540 p = find_ucmd(&ea, p, NULL, xp,
3541# if defined(FEAT_CMDL_COMPL)
3542 &compl
3543# else
3544 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003547 if (p == NULL)
3548 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550#endif
3551 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003552 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
3554 /* Not still touching the command and it was an illegal one */
3555 xp->xp_context = EXPAND_UNSUCCESSFUL;
3556 return NULL;
3557 }
3558
3559 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3560
3561 if (*p == '!') /* forced commands */
3562 {
3563 forceit = TRUE;
3564 ++p;
3565 }
3566
3567/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003568 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003570 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003571 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
3573 arg = skipwhite(p);
3574
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
3577 if (*arg == '>') /* append */
3578 {
3579 if (*++arg == '>')
3580 ++arg;
3581 arg = skipwhite(arg);
3582 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 ++arg;
3586 usefilter = TRUE;
3587 }
3588 }
3589
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 usefilter = forceit; /* :r! filter if forced */
3593 if (*arg == '!') /* :r !filter */
3594 {
3595 ++arg;
3596 usefilter = TRUE;
3597 }
3598 }
3599
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003600 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 while (*arg == *cmd) /* allow any number of '>' or '<' */
3603 ++arg;
3604 arg = skipwhite(arg);
3605 }
3606
3607 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003608 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
3610 /* Check if we're in the +command */
3611 p = arg + 1;
3612 arg = skip_cmd_arg(arg, FALSE);
3613
3614 /* Still touching the command after '+'? */
3615 if (*arg == NUL)
3616 return p;
3617
3618 /* Skip space(s) after +command to get to the real argument */
3619 arg = skipwhite(arg);
3620 }
3621
3622 /*
3623 * Check for '|' to separate commands and '"' to start comments.
3624 * Don't do this for ":read !cmd" and ":write !cmd".
3625 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003626 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 p = arg;
3629 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003630 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 p += 2;
3632 while (*p)
3633 {
3634 if (*p == Ctrl_V)
3635 {
3636 if (p[1] != NUL)
3637 ++p;
3638 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 || *p == '|' || *p == '\n')
3641 {
3642 if (*(p - 1) != '\\')
3643 {
3644 if (*p == '|' || *p == '\n')
3645 return p + 1;
3646 return NULL; /* It's a comment */
3647 }
3648 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003649 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651 }
3652
3653 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003654 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 vim_strchr((char_u *)"|\"", *arg) == NULL)
3656 return NULL;
3657
3658 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003659 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003661 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003662 while (*p && p < buff + len)
3663 {
3664 if (*p == ' ' || *p == TAB)
3665 {
3666 /* argument starts after a space */
3667 xp->xp_pattern = ++p;
3668 }
3669 else
3670 {
3671 if (*p == '\\' && *(p + 1) != NUL)
3672 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003673 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003674 }
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003677 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003679 int c;
3680 int in_quote = FALSE;
3681 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
3683 /*
3684 * Allow spaces within back-quotes to count as part of the argument
3685 * being expanded.
3686 */
3687 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003688 p = xp->xp_pattern;
3689 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003691#ifdef FEAT_MBYTE
3692 if (has_mbyte)
3693 c = mb_ptr2char(p);
3694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003696 c = *p;
3697 if (c == '\\' && p[1] != NUL)
3698 ++p;
3699 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
3701 if (!in_quote)
3702 {
3703 xp->xp_pattern = p;
3704 bow = p + 1;
3705 }
3706 in_quote = !in_quote;
3707 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003708 /* An argument can contain just about everything, except
3709 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003710 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003711#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003712 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003713#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003714 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003715 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003716 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003717 while (*p != NUL)
3718 {
3719#ifdef FEAT_MBYTE
3720 if (has_mbyte)
3721 c = mb_ptr2char(p);
3722 else
3723#endif
3724 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003725 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003726 break;
3727#ifdef FEAT_MBYTE
3728 if (has_mbyte)
3729 len = (*mb_ptr2len)(p);
3730 else
3731#endif
3732 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003733 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003734 }
3735 if (in_quote)
3736 bow = p;
3737 else
3738 xp->xp_pattern = p;
3739 p -= len;
3740 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003741 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743
3744 /*
3745 * If we are still inside the quotes, and we passed a space, just
3746 * expand from there.
3747 */
3748 if (bow != NULL && in_quote)
3749 xp->xp_pattern = bow;
3750 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003752 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003753 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003754 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003755#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003756 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003757#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003758 /* When still after the command name expand executables. */
3759 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003760 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /* Check for environment variable */
3764 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003765#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 || *xp->xp_pattern == '%'
3767#endif
3768 )
3769 {
3770 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3771 if (!vim_isIDc(*p))
3772 break;
3773 if (*p == NUL)
3774 {
3775 xp->xp_context = EXPAND_ENV_VARS;
3776 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003777#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3778 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003779 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003780 compl = EXPAND_ENV_VARS;
3781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003784#if defined(FEAT_CMDL_COMPL)
3785 /* Check for user names */
3786 if (*xp->xp_pattern == '~')
3787 {
3788 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3789 ;
3790 /* Complete ~user only if it partially matches a user name.
3791 * A full match ~user<Tab> will be replaced by user's home
3792 * directory i.e. something like ~user<Tab> -> /home/user/ */
3793 if (*p == NUL && p > xp->xp_pattern + 1
3794 && match_user(xp->xp_pattern + 1) == 1)
3795 {
3796 xp->xp_context = EXPAND_USER;
3797 ++xp->xp_pattern;
3798 }
3799 }
3800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802
3803/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003804 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003806 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003808 case CMD_find:
3809 case CMD_sfind:
3810 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003811 if (xp->xp_context == EXPAND_FILES)
3812 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 case CMD_cd:
3815 case CMD_chdir:
3816 case CMD_lcd:
3817 case CMD_lchdir:
3818 if (xp->xp_context == EXPAND_FILES)
3819 xp->xp_context = EXPAND_DIRECTORIES;
3820 break;
3821 case CMD_help:
3822 xp->xp_context = EXPAND_HELP;
3823 xp->xp_pattern = arg;
3824 break;
3825
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003826 /* Command modifiers: return the argument.
3827 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003829 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 case CMD_belowright:
3831 case CMD_botright:
3832 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003833 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003834 case CMD_cdo:
3835 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003837 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 case CMD_folddoclosed:
3839 case CMD_folddoopen:
3840 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003841 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 case CMD_keepjumps:
3843 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003844 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003845 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003847 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003849 case CMD_noautocmd:
3850 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003852 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003854 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003855 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 case CMD_topleft:
3857 case CMD_verbose:
3858 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003859 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 return arg;
3861
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003862 case CMD_filter:
3863 if (*arg != NUL)
3864 arg = skip_vimgrep_pat(arg, NULL, NULL);
3865 if (arg == NULL || *arg == NUL)
3866 {
3867 xp->xp_context = EXPAND_NOTHING;
3868 return NULL;
3869 }
3870 return skipwhite(arg);
3871
Bram Moolenaar4f688582007-07-24 12:34:30 +00003872#ifdef FEAT_CMDL_COMPL
3873# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 case CMD_match:
3875 if (*arg == NUL || !ends_excmd(*arg))
3876 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003877 /* also complete "None" */
3878 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 arg = skipwhite(skiptowhite(arg));
3880 if (*arg != NUL)
3881 {
3882 xp->xp_context = EXPAND_NOTHING;
3883 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3884 }
3885 }
3886 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889/*
3890 * All completion for the +cmdline_compl feature goes here.
3891 */
3892
3893# ifdef FEAT_USR_CMDS
3894 case CMD_command:
3895 /* Check for attributes */
3896 while (*arg == '-')
3897 {
3898 arg++; /* Skip "-" */
3899 p = skiptowhite(arg);
3900 if (*p == NUL)
3901 {
3902 /* Cursor is still in the attribute */
3903 p = vim_strchr(arg, '=');
3904 if (p == NULL)
3905 {
3906 /* No "=", so complete attribute names */
3907 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3908 xp->xp_pattern = arg;
3909 return NULL;
3910 }
3911
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003912 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 * their arguments as well.
3914 */
3915 if (STRNICMP(arg, "complete", p - arg) == 0)
3916 {
3917 xp->xp_context = EXPAND_USER_COMPLETE;
3918 xp->xp_pattern = p + 1;
3919 return NULL;
3920 }
3921 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3922 {
3923 xp->xp_context = EXPAND_USER_NARGS;
3924 xp->xp_pattern = p + 1;
3925 return NULL;
3926 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003927 else if (STRNICMP(arg, "addr", p - arg) == 0)
3928 {
3929 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3930 xp->xp_pattern = p + 1;
3931 return NULL;
3932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 return NULL;
3934 }
3935 arg = skipwhite(p);
3936 }
3937
3938 /* After the attributes comes the new command name */
3939 p = skiptowhite(arg);
3940 if (*p == NUL)
3941 {
3942 xp->xp_context = EXPAND_USER_COMMANDS;
3943 xp->xp_pattern = arg;
3944 break;
3945 }
3946
3947 /* And finally comes a normal command */
3948 return skipwhite(p);
3949
3950 case CMD_delcommand:
3951 xp->xp_context = EXPAND_USER_COMMANDS;
3952 xp->xp_pattern = arg;
3953 break;
3954# endif
3955
3956 case CMD_global:
3957 case CMD_vglobal:
3958 delim = *arg; /* get the delimiter */
3959 if (delim)
3960 ++arg; /* skip delimiter if there is one */
3961
3962 while (arg[0] != NUL && arg[0] != delim)
3963 {
3964 if (arg[0] == '\\' && arg[1] != NUL)
3965 ++arg;
3966 ++arg;
3967 }
3968 if (arg[0] != NUL)
3969 return arg + 1;
3970 break;
3971 case CMD_and:
3972 case CMD_substitute:
3973 delim = *arg;
3974 if (delim)
3975 {
3976 /* skip "from" part */
3977 ++arg;
3978 arg = skip_regexp(arg, delim, p_magic, NULL);
3979 }
3980 /* skip "to" part */
3981 while (arg[0] != NUL && arg[0] != delim)
3982 {
3983 if (arg[0] == '\\' && arg[1] != NUL)
3984 ++arg;
3985 ++arg;
3986 }
3987 if (arg[0] != NUL) /* skip delimiter */
3988 ++arg;
3989 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3990 ++arg;
3991 if (arg[0] != NUL)
3992 return arg;
3993 break;
3994 case CMD_isearch:
3995 case CMD_dsearch:
3996 case CMD_ilist:
3997 case CMD_dlist:
3998 case CMD_ijump:
3999 case CMD_psearch:
4000 case CMD_djump:
4001 case CMD_isplit:
4002 case CMD_dsplit:
4003 arg = skipwhite(skipdigits(arg)); /* skip count */
4004 if (*arg == '/') /* Match regexp, not just whole words */
4005 {
4006 for (++arg; *arg && *arg != '/'; arg++)
4007 if (*arg == '\\' && arg[1] != NUL)
4008 arg++;
4009 if (*arg)
4010 {
4011 arg = skipwhite(arg + 1);
4012
4013 /* Check for trailing illegal characters */
4014 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4015 xp->xp_context = EXPAND_NOTHING;
4016 else
4017 return arg;
4018 }
4019 }
4020 break;
4021#ifdef FEAT_AUTOCMD
4022 case CMD_autocmd:
4023 return set_context_in_autocmd(xp, arg, FALSE);
4024
4025 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004026 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return set_context_in_autocmd(xp, arg, TRUE);
4028#endif
4029 case CMD_set:
4030 set_context_in_set_cmd(xp, arg, 0);
4031 break;
4032 case CMD_setglobal:
4033 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4034 break;
4035 case CMD_setlocal:
4036 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4037 break;
4038 case CMD_tag:
4039 case CMD_stag:
4040 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004041 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 case CMD_tselect:
4043 case CMD_stselect:
4044 case CMD_ptselect:
4045 case CMD_tjump:
4046 case CMD_stjump:
4047 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004048 if (*p_wop != NUL)
4049 xp->xp_context = EXPAND_TAGS_LISTFILES;
4050 else
4051 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 xp->xp_pattern = arg;
4053 break;
4054 case CMD_augroup:
4055 xp->xp_context = EXPAND_AUGROUP;
4056 xp->xp_pattern = arg;
4057 break;
4058#ifdef FEAT_SYN_HL
4059 case CMD_syntax:
4060 set_context_in_syntax_cmd(xp, arg);
4061 break;
4062#endif
4063#ifdef FEAT_EVAL
4064 case CMD_let:
4065 case CMD_if:
4066 case CMD_elseif:
4067 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004068 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 case CMD_echo:
4070 case CMD_echon:
4071 case CMD_execute:
4072 case CMD_echomsg:
4073 case CMD_echoerr:
4074 case CMD_call:
4075 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004076 case CMD_cexpr:
4077 case CMD_caddexpr:
4078 case CMD_cgetexpr:
4079 case CMD_lexpr:
4080 case CMD_laddexpr:
4081 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004082 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 break;
4084
4085 case CMD_unlet:
4086 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4087 arg = xp->xp_pattern + 1;
4088 xp->xp_context = EXPAND_USER_VARS;
4089 xp->xp_pattern = arg;
4090 break;
4091
4092 case CMD_function:
4093 case CMD_delfunction:
4094 xp->xp_context = EXPAND_USER_FUNC;
4095 xp->xp_pattern = arg;
4096 break;
4097
4098 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004099 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 break;
4101#endif
4102 case CMD_highlight:
4103 set_context_in_highlight_cmd(xp, arg);
4104 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004105#ifdef FEAT_CSCOPE
4106 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004107 case CMD_lcscope:
4108 case CMD_scscope:
4109 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004110 break;
4111#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004112#ifdef FEAT_SIGNS
4113 case CMD_sign:
4114 set_context_in_sign_cmd(xp, arg);
4115 break;
4116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#ifdef FEAT_LISTCMDS
4118 case CMD_bdelete:
4119 case CMD_bwipeout:
4120 case CMD_bunload:
4121 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4122 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004123 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 case CMD_buffer:
4125 case CMD_sbuffer:
4126 case CMD_checktime:
4127 xp->xp_context = EXPAND_BUFFERS;
4128 xp->xp_pattern = arg;
4129 break;
4130#endif
4131#ifdef FEAT_USR_CMDS
4132 case CMD_USER:
4133 case CMD_USER_BUF:
4134 if (compl != EXPAND_NOTHING)
4135 {
4136 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004137 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 {
4139# ifdef FEAT_MENU
4140 if (compl == EXPAND_MENUS)
4141 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4142# endif
4143 if (compl == EXPAND_COMMANDS)
4144 return arg;
4145 if (compl == EXPAND_MAPPINGS)
4146 return set_context_in_map_cmd(xp, (char_u *)"map",
4147 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004148 /* Find start of last argument. */
4149 p = arg;
4150 while (*p)
4151 {
4152 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004153 /* argument starts after a space */
4154 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004155 else if (*p == '\\' && *(p + 1) != NUL)
4156 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004157 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 xp->xp_pattern = arg;
4160 }
4161 xp->xp_context = compl;
4162 }
4163 break;
4164#endif
4165 case CMD_map: case CMD_noremap:
4166 case CMD_nmap: case CMD_nnoremap:
4167 case CMD_vmap: case CMD_vnoremap:
4168 case CMD_omap: case CMD_onoremap:
4169 case CMD_imap: case CMD_inoremap:
4170 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004171 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004172 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004173 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004174 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004176 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 case CMD_unmap:
4178 case CMD_nunmap:
4179 case CMD_vunmap:
4180 case CMD_ounmap:
4181 case CMD_iunmap:
4182 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004183 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004184 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004185 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004186 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004188 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004189 case CMD_mapclear:
4190 case CMD_nmapclear:
4191 case CMD_vmapclear:
4192 case CMD_omapclear:
4193 case CMD_imapclear:
4194 case CMD_cmapclear:
4195 case CMD_lmapclear:
4196 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004197 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004198 case CMD_xmapclear:
4199 xp->xp_context = EXPAND_MAPCLEAR;
4200 xp->xp_pattern = arg;
4201 break;
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 case CMD_abbreviate: case CMD_noreabbrev:
4204 case CMD_cabbrev: case CMD_cnoreabbrev:
4205 case CMD_iabbrev: case CMD_inoreabbrev:
4206 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004207 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 case CMD_unabbreviate:
4209 case CMD_cunabbrev:
4210 case CMD_iunabbrev:
4211 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004212 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213#ifdef FEAT_MENU
4214 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4215 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4216 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4217 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4218 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4219 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4220 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4221 case CMD_tmenu: case CMD_tunmenu:
4222 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4223 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4224#endif
4225
4226 case CMD_colorscheme:
4227 xp->xp_context = EXPAND_COLORS;
4228 xp->xp_pattern = arg;
4229 break;
4230
4231 case CMD_compiler:
4232 xp->xp_context = EXPAND_COMPILER;
4233 xp->xp_pattern = arg;
4234 break;
4235
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004236 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004237 xp->xp_context = EXPAND_OWNSYNTAX;
4238 xp->xp_pattern = arg;
4239 break;
4240
4241 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004242 xp->xp_context = EXPAND_FILETYPE;
4243 xp->xp_pattern = arg;
4244 break;
4245
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004246 case CMD_packadd:
4247 xp->xp_context = EXPAND_PACKADD;
4248 xp->xp_pattern = arg;
4249 break;
4250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4252 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4253 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004254 p = skiptowhite(arg);
4255 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 xp->xp_context = EXPAND_LANGUAGE;
4258 xp->xp_pattern = arg;
4259 }
4260 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004261 {
4262 if ( STRNCMP(arg, "messages", p - arg) == 0
4263 || STRNCMP(arg, "ctype", p - arg) == 0
4264 || STRNCMP(arg, "time", p - arg) == 0)
4265 {
4266 xp->xp_context = EXPAND_LOCALES;
4267 xp->xp_pattern = skipwhite(p);
4268 }
4269 else
4270 xp->xp_context = EXPAND_NOTHING;
4271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 break;
4273#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004274#if defined(FEAT_PROFILE)
4275 case CMD_profile:
4276 set_context_in_profile_cmd(xp, arg);
4277 break;
4278#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004279 case CMD_behave:
4280 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004281 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004284 case CMD_messages:
4285 xp->xp_context = EXPAND_MESSAGES;
4286 xp->xp_pattern = arg;
4287 break;
4288
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004289#if defined(FEAT_CMDHIST)
4290 case CMD_history:
4291 xp->xp_context = EXPAND_HISTORY;
4292 xp->xp_pattern = arg;
4293 break;
4294#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004295#if defined(FEAT_PROFILE)
4296 case CMD_syntime:
4297 xp->xp_context = EXPAND_SYNTIME;
4298 xp->xp_pattern = arg;
4299 break;
4300#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif /* FEAT_CMDL_COMPL */
4303
4304 default:
4305 break;
4306 }
4307 return NULL;
4308}
4309
4310/*
4311 * skip a range specifier of the form: addr [,addr] [;addr] ..
4312 *
4313 * Backslashed delimiters after / or ? will be skipped, and commands will
4314 * not be expanded between /'s and ?'s or after "'".
4315 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004316 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 * Returns the "cmd" pointer advanced to beyond the range.
4318 */
4319 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004320skip_range(
4321 char_u *cmd,
4322 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004324 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004326 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004328 if (*cmd == '\\')
4329 {
4330 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4331 ++cmd;
4332 else
4333 break;
4334 }
4335 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 if (*++cmd == NUL && ctx != NULL)
4338 *ctx = EXPAND_NOTHING;
4339 }
4340 else if (*cmd == '/' || *cmd == '?')
4341 {
4342 delim = *cmd++;
4343 while (*cmd != NUL && *cmd != delim)
4344 if (*cmd++ == '\\' && *cmd != NUL)
4345 ++cmd;
4346 if (*cmd == NUL && ctx != NULL)
4347 *ctx = EXPAND_NOTHING;
4348 }
4349 if (*cmd != NUL)
4350 ++cmd;
4351 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004352
4353 /* Skip ":" and white space. */
4354 while (*cmd == ':')
4355 cmd = skipwhite(cmd + 1);
4356
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 return cmd;
4358}
4359
4360/*
4361 * get a single EX address
4362 *
4363 * Set ptr to the next character after the part that was interpreted.
4364 * Set ptr to NULL when an error is encountered.
4365 *
4366 * Return MAXLNUM when no Ex address was found.
4367 */
4368 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004369get_address(
4370 exarg_T *eap UNUSED,
4371 char_u **ptr,
4372 int addr_type, /* flag: one of ADDR_LINES, ... */
4373 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004374 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004375 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 int c;
4378 int i;
4379 long n;
4380 char_u *cmd;
4381 pos_T pos;
4382 pos_T *fp;
4383 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004384 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 cmd = skipwhite(*ptr);
4387 lnum = MAXLNUM;
4388 do
4389 {
4390 switch (*cmd)
4391 {
4392 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 ++cmd;
4394 switch (addr_type)
4395 {
4396 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 lnum = curwin->w_cursor.lnum;
4398 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004400 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 break;
4402 case ADDR_ARGUMENTS:
4403 lnum = curwin->w_arg_idx + 1;
4404 break;
4405 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004406 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 lnum = curbuf->b_fnum;
4408 break;
4409 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004410 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004412 case ADDR_TABS_RELATIVE:
4413 EMSG(_(e_invrange));
4414 cmd = NULL;
4415 goto error;
4416 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004417#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004418 case ADDR_QUICKFIX:
4419 lnum = qf_get_cur_valid_idx(eap);
4420 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004421#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 }
4423 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004426 ++cmd;
4427 switch (addr_type)
4428 {
4429 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 lnum = curbuf->b_ml.ml_line_count;
4431 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004433 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 break;
4435 case ADDR_ARGUMENTS:
4436 lnum = ARGCOUNT;
4437 break;
4438 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004439 buf = lastbuf;
4440 while (buf->b_ml.ml_mfp == NULL)
4441 {
4442 if (buf->b_prev == NULL)
4443 break;
4444 buf = buf->b_prev;
4445 }
4446 lnum = buf->b_fnum;
4447 break;
4448 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 lnum = lastbuf->b_fnum;
4450 break;
4451 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004452 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004454 case ADDR_TABS_RELATIVE:
4455 EMSG(_(e_invrange));
4456 cmd = NULL;
4457 goto error;
4458 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004459#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004460 case ADDR_QUICKFIX:
4461 lnum = qf_get_size(eap);
4462 if (lnum == 0)
4463 lnum = 1;
4464 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004465#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 }
4467 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 if (*++cmd == NUL)
4471 {
4472 cmd = NULL;
4473 goto error;
4474 }
4475 if (addr_type != ADDR_LINES)
4476 {
4477 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004478 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 goto error;
4480 }
4481 if (skip)
4482 ++cmd;
4483 else
4484 {
4485 /* Only accept a mark in another file when it is
4486 * used by itself: ":'M". */
4487 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4488 ++cmd;
4489 if (fp == (pos_T *)-1)
4490 /* Jumped to another file. */
4491 lnum = curwin->w_cursor.lnum;
4492 else
4493 {
4494 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
4496 cmd = NULL;
4497 goto error;
4498 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 lnum = fp->lnum;
4500 }
4501 }
4502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 case '/':
4505 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 c = *cmd++;
4507 if (addr_type != ADDR_LINES)
4508 {
4509 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004510 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 goto error;
4512 }
4513 if (skip) /* skip "/pat/" */
4514 {
4515 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4516 if (*cmd == c)
4517 ++cmd;
4518 }
4519 else
4520 {
4521 pos = curwin->w_cursor; /* save curwin->w_cursor */
4522 /*
4523 * When '/' or '?' follows another address, start
4524 * from there.
4525 */
4526 if (lnum != MAXLNUM)
4527 curwin->w_cursor.lnum = lnum;
4528 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004529 * Start a forward search at the end of the line (unless
4530 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 * Start a backward search at the start of the line.
4532 * This makes sure we never match in the current
4533 * line, and can match anywhere in the
4534 * next/previous line.
4535 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004536 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 curwin->w_cursor.col = MAXCOL;
4538 else
4539 curwin->w_cursor.col = 0;
4540 searchcmdlen = 0;
4541 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004542 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004543 {
4544 curwin->w_cursor = pos;
4545 cmd = NULL;
4546 goto error;
4547 }
4548 lnum = curwin->w_cursor.lnum;
4549 curwin->w_cursor = pos;
4550 /* adjust command string pointer */
4551 cmd += searchcmdlen;
4552 }
4553 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004556 ++cmd;
4557 if (addr_type != ADDR_LINES)
4558 {
4559 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004560 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004561 goto error;
4562 }
4563 if (*cmd == '&')
4564 i = RE_SUBST;
4565 else if (*cmd == '?' || *cmd == '/')
4566 i = RE_SEARCH;
4567 else
4568 {
4569 EMSG(_(e_backslash));
4570 cmd = NULL;
4571 goto error;
4572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004574 if (!skip)
4575 {
4576 /*
4577 * When search follows another address, start from
4578 * there.
4579 */
4580 if (lnum != MAXLNUM)
4581 pos.lnum = lnum;
4582 else
4583 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004585 /*
4586 * Start the search just like for the above
4587 * do_search().
4588 */
4589 if (*cmd != '?')
4590 pos.col = MAXCOL;
4591 else
4592 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004593#ifdef FEAT_VIRTUALEDIT
4594 pos.coladd = 0;
4595#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004596 if (searchit(curwin, curbuf, &pos,
4597 *cmd == '?' ? BACKWARD : FORWARD,
4598 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004599 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004600 lnum = pos.lnum;
4601 else
4602 {
4603 cmd = NULL;
4604 goto error;
4605 }
4606 }
4607 ++cmd;
4608 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
4610 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4612 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614
4615 for (;;)
4616 {
4617 cmd = skipwhite(cmd);
4618 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4619 break;
4620
4621 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 {
4623 switch (addr_type)
4624 {
4625 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004626 /* "+1" is same as ".+1" */
4627 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004628 break;
4629 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004630 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004631 break;
4632 case ADDR_ARGUMENTS:
4633 lnum = curwin->w_arg_idx + 1;
4634 break;
4635 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004636 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 lnum = curbuf->b_fnum;
4638 break;
4639 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004640 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004641 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004642 case ADDR_TABS_RELATIVE:
4643 lnum = 1;
4644 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004645#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004646 case ADDR_QUICKFIX:
4647 lnum = qf_get_cur_valid_idx(eap);
4648 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004649#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004650 }
4651 }
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (VIM_ISDIGIT(*cmd))
4654 i = '+'; /* "number" is same as "+number" */
4655 else
4656 i = *cmd++;
4657 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4658 n = 1;
4659 else
4660 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004661
4662 if (addr_type == ADDR_TABS_RELATIVE)
4663 {
4664 EMSG(_(e_invrange));
4665 cmd = NULL;
4666 goto error;
4667 }
4668 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004669 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004670 lnum = compute_buffer_local_count(
4671 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004673 {
4674#ifdef FEAT_FOLDING
4675 /* Relative line addressing, need to adjust for folded lines
4676 * now, but only do it after the first address. */
4677 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4678 && address_count >= 2)
4679 (void)hasFolding(lnum, NULL, &lnum);
4680#endif
4681 if (i == '-')
4682 lnum -= n;
4683 else
4684 lnum += n;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687 } while (*cmd == '/' || *cmd == '?');
4688
4689error:
4690 *ptr = cmd;
4691 return lnum;
4692}
4693
4694/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004695 * Get flags from an Ex command argument.
4696 */
4697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004698get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004699{
4700 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4701 {
4702 if (*eap->arg == 'l')
4703 eap->flags |= EXFLAG_LIST;
4704 else if (*eap->arg == 'p')
4705 eap->flags |= EXFLAG_PRINT;
4706 else
4707 eap->flags |= EXFLAG_NR;
4708 eap->arg = skipwhite(eap->arg + 1);
4709 }
4710}
4711
4712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 * Function called for command which is Not Implemented. NI!
4714 */
4715 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004716ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718 if (!eap->skip)
4719 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4720}
4721
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004722#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723/*
4724 * Function called for script command which is Not Implemented. NI!
4725 * Skips over ":perl <<EOF" constructs.
4726 */
4727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004728ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 if (!eap->skip)
4731 ex_ni(eap);
4732 else
4733 vim_free(script_get(eap, eap->arg));
4734}
4735#endif
4736
4737/*
4738 * Check range in Ex command for validity.
4739 * Return NULL when valid, error message when invalid.
4740 */
4741 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004742invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004744 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if ( eap->line1 < 0
4746 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004747 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004749
4750 if (eap->argt & RANGE)
4751 {
4752 switch(eap->addr_type)
4753 {
4754 case ADDR_LINES:
4755 if (!(eap->argt & NOTADR)
4756 && eap->line2 > curbuf->b_ml.ml_line_count
4757#ifdef FEAT_DIFF
4758 + (eap->cmdidx == CMD_diffget)
4759#endif
4760 )
4761 return (char_u *)_(e_invrange);
4762 break;
4763 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004764 /* add 1 if ARGCOUNT is 0 */
4765 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004766 return (char_u *)_(e_invrange);
4767 break;
4768 case ADDR_BUFFERS:
4769 if (eap->line1 < firstbuf->b_fnum
4770 || eap->line2 > lastbuf->b_fnum)
4771 return (char_u *)_(e_invrange);
4772 break;
4773 case ADDR_LOADED_BUFFERS:
4774 buf = firstbuf;
4775 while (buf->b_ml.ml_mfp == NULL)
4776 {
4777 if (buf->b_next == NULL)
4778 return (char_u *)_(e_invrange);
4779 buf = buf->b_next;
4780 }
4781 if (eap->line1 < buf->b_fnum)
4782 return (char_u *)_(e_invrange);
4783 buf = lastbuf;
4784 while (buf->b_ml.ml_mfp == NULL)
4785 {
4786 if (buf->b_prev == NULL)
4787 return (char_u *)_(e_invrange);
4788 buf = buf->b_prev;
4789 }
4790 if (eap->line2 > buf->b_fnum)
4791 return (char_u *)_(e_invrange);
4792 break;
4793 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004794 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004795 return (char_u *)_(e_invrange);
4796 break;
4797 case ADDR_TABS:
4798 if (eap->line2 > LAST_TAB_NR)
4799 return (char_u *)_(e_invrange);
4800 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004801 case ADDR_TABS_RELATIVE:
4802 /* Do nothing */
4803 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004804#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004805 case ADDR_QUICKFIX:
4806 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4807 return (char_u *)_(e_invrange);
4808 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004809#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004810 }
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 return NULL;
4813}
4814
4815/*
4816 * Correct the range for zero line number, if required.
4817 */
4818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004819correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
4821 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4822 {
4823 if (eap->line1 == 0)
4824 eap->line1 = 1;
4825 if (eap->line2 == 0)
4826 eap->line2 = 1;
4827 }
4828}
4829
Bram Moolenaar748bf032005-02-02 23:04:36 +00004830#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004831static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004832
4833/*
4834 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4835 * pattern. Otherwise return eap->arg.
4836 */
4837 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004839{
4840 char_u *p = eap->arg;
4841
Bram Moolenaara37420f2006-02-04 22:37:47 +00004842 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4843 || eap->cmdidx == CMD_vimgrepadd
4844 || eap->cmdidx == CMD_lvimgrepadd
4845 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004846 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004847 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848 if (p == NULL)
4849 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004850 }
4851 return p;
4852}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004853
4854/*
4855 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4856 * in the command line, so that things like % get expanded.
4857 */
4858 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004859replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004860{
4861 char_u *new_cmdline;
4862 char_u *program;
4863 char_u *pos;
4864 char_u *ptr;
4865 int len;
4866 int i;
4867
4868 /*
4869 * Don't do it when ":vimgrep" is used for ":grep".
4870 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004871 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4872 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4873 || eap->cmdidx == CMD_grepadd
4874 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004875 && !grep_internal(eap->cmdidx))
4876 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004877 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4878 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004879 {
4880 if (*curbuf->b_p_gp == NUL)
4881 program = p_gp;
4882 else
4883 program = curbuf->b_p_gp;
4884 }
4885 else
4886 {
4887 if (*curbuf->b_p_mp == NUL)
4888 program = p_mp;
4889 else
4890 program = curbuf->b_p_mp;
4891 }
4892
4893 p = skipwhite(p);
4894
4895 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4896 {
4897 /* replace $* by given arguments */
4898 i = 1;
4899 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4900 ++i;
4901 len = (int)STRLEN(p);
4902 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4903 if (new_cmdline == NULL)
4904 return NULL; /* out of memory */
4905 ptr = new_cmdline;
4906 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4907 {
4908 i = (int)(pos - program);
4909 STRNCPY(ptr, program, i);
4910 STRCPY(ptr += i, p);
4911 ptr += len;
4912 program = pos + 2;
4913 }
4914 STRCPY(ptr, program);
4915 }
4916 else
4917 {
4918 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4919 if (new_cmdline == NULL)
4920 return NULL; /* out of memory */
4921 STRCPY(new_cmdline, program);
4922 STRCAT(new_cmdline, " ");
4923 STRCAT(new_cmdline, p);
4924 }
4925 msg_make(p);
4926
4927 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4928 vim_free(*cmdlinep);
4929 *cmdlinep = new_cmdline;
4930 p = new_cmdline;
4931 }
4932 return p;
4933}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004934#endif
4935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936/*
4937 * Expand file name in Ex command argument.
4938 * Return FAIL for failure, OK otherwise.
4939 */
4940 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004941expand_filename(
4942 exarg_T *eap,
4943 char_u **cmdlinep,
4944 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 int has_wildcards; /* need to expand wildcards */
4947 char_u *repl;
4948 int srclen;
4949 char_u *p;
4950 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004951 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
Bram Moolenaar748bf032005-02-02 23:04:36 +00004953#ifdef FEAT_QUICKFIX
4954 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4955 p = skip_grep_pat(eap);
4956#else
4957 p = eap->arg;
4958#endif
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 /*
4961 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4962 * the file name contains a wildcard it should not cause expanding.
4963 * (it will be expanded anyway if there is a wildcard before replacing).
4964 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004965 has_wildcards = mch_has_wildcard(p);
4966 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004968#ifdef FEAT_EVAL
4969 /* Skip over `=expr`, wildcards in it are not expanded. */
4970 if (p[0] == '`' && p[1] == '=')
4971 {
4972 p += 2;
4973 (void)skip_expr(&p);
4974 if (*p == '`')
4975 ++p;
4976 continue;
4977 }
4978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 /*
4980 * Quick check if this cannot be the start of a special string.
4981 * Also removes backslash before '%', '#' and '<'.
4982 */
4983 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4984 {
4985 ++p;
4986 continue;
4987 }
4988
4989 /*
4990 * Try to find a match at this position.
4991 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004992 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4993 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 if (*errormsgp != NULL) /* error detected */
4995 return FAIL;
4996 if (repl == NULL) /* no match found */
4997 {
4998 p += srclen;
4999 continue;
5000 }
5001
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005002 /* Wildcards won't be expanded below, the replacement is taken
5003 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5004 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5005 {
5006 char_u *l = repl;
5007
5008 repl = expand_env_save(repl);
5009 vim_free(l);
5010 }
5011
Bram Moolenaar63b92542007-03-27 14:57:09 +00005012 /* Need to escape white space et al. with a backslash.
5013 * Don't do this for:
5014 * - replacement that already has been escaped: "##"
5015 * - shell commands (may have to use quotes instead).
5016 * - non-unix systems when there is a single argument (spaces don't
5017 * separate arguments then).
5018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005020 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 && eap->cmdidx != CMD_grep
5023 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005024 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005025 && eap->cmdidx != CMD_lgrep
5026 && eap->cmdidx != CMD_lgrepadd
5027 && eap->cmdidx != CMD_lmake
5028 && eap->cmdidx != CMD_make
5029 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#ifndef UNIX
5031 && !(eap->argt & NOSPC)
5032#endif
5033 )
5034 {
5035 char_u *l;
5036#ifdef BACKSLASH_IN_FILENAME
5037 /* Don't escape a backslash here, because rem_backslash() doesn't
5038 * remove it later. */
5039 static char_u *nobslash = (char_u *)" \t\"|";
5040# define ESCAPE_CHARS nobslash
5041#else
5042# define ESCAPE_CHARS escape_chars
5043#endif
5044
5045 for (l = repl; *l; ++l)
5046 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5047 {
5048 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5049 if (l != NULL)
5050 {
5051 vim_free(repl);
5052 repl = l;
5053 }
5054 break;
5055 }
5056 }
5057
5058 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005059 if ((eap->usefilter || eap->cmdidx == CMD_bang
5060 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005061 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 char_u *l;
5064
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005065 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (l != NULL)
5067 {
5068 vim_free(repl);
5069 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 }
5072
5073 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5074 vim_free(repl);
5075 if (p == NULL)
5076 return FAIL;
5077 }
5078
5079 /*
5080 * One file argument: Expand wildcards.
5081 * Don't do this with ":r !command" or ":w !command".
5082 */
5083 if ((eap->argt & NOSPC) && !eap->usefilter)
5084 {
5085 /*
5086 * May do this twice:
5087 * 1. Replace environment variables.
5088 * 2. Replace any other wildcards, remove backslashes.
5089 */
5090 for (n = 1; n <= 2; ++n)
5091 {
5092 if (n == 2)
5093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 /*
5095 * Halve the number of backslashes (this is Vi compatible).
5096 * For Unix and OS/2, when wildcards are expanded, this is
5097 * done by ExpandOne() below.
5098 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005099#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (!has_wildcards)
5101#endif
5102 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104
5105 if (has_wildcards)
5106 {
5107 if (n == 1)
5108 {
5109 /*
5110 * First loop: May expand environment variables. This
5111 * can be done much faster with expand_env() than with
5112 * something else (e.g., calling a shell).
5113 * After expanding environment variables, check again
5114 * if there are still wildcards present.
5115 */
5116 if (vim_strchr(eap->arg, '$') != NULL
5117 || vim_strchr(eap->arg, '~') != NULL)
5118 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005119 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005120 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 has_wildcards = mch_has_wildcard(NameBuff);
5122 p = NameBuff;
5123 }
5124 else
5125 p = NULL;
5126 }
5127 else /* n == 2 */
5128 {
5129 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005130 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 ExpandInit(&xpc);
5133 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005134 if (p_wic)
5135 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005137 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (p == NULL)
5139 return FAIL;
5140 }
5141 if (p != NULL)
5142 {
5143 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5144 p, cmdlinep);
5145 if (n == 2) /* p came from ExpandOne() */
5146 vim_free(p);
5147 }
5148 }
5149 }
5150 }
5151 return OK;
5152}
5153
5154/*
5155 * Replace part of the command line, keeping eap->cmd, eap->arg and
5156 * eap->nextcmd correct.
5157 * "src" points to the part that is to be replaced, of length "srclen".
5158 * "repl" is the replacement string.
5159 * Returns a pointer to the character after the replaced string.
5160 * Returns NULL for failure.
5161 */
5162 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005163repl_cmdline(
5164 exarg_T *eap,
5165 char_u *src,
5166 int srclen,
5167 char_u *repl,
5168 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 int len;
5171 int i;
5172 char_u *new_cmdline;
5173
5174 /*
5175 * The new command line is build in new_cmdline[].
5176 * First allocate it.
5177 * Careful: a "+cmd" argument may have been NUL terminated.
5178 */
5179 len = (int)STRLEN(repl);
5180 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005181 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5183 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5184 return NULL; /* out of memory! */
5185
5186 /*
5187 * Copy the stuff before the expanded part.
5188 * Copy the expanded stuff.
5189 * Copy what came after the expanded part.
5190 * Copy the next commands, if there are any.
5191 */
5192 i = (int)(src - *cmdlinep); /* length of part before match */
5193 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 mch_memmove(new_cmdline + i, repl, (size_t)len);
5196 i += len; /* remember the end of the string */
5197 STRCPY(new_cmdline + i, src + srclen);
5198 src = new_cmdline + i; /* remember where to continue */
5199
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005200 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
5202 i = (int)STRLEN(new_cmdline) + 1;
5203 STRCPY(new_cmdline + i, eap->nextcmd);
5204 eap->nextcmd = new_cmdline + i;
5205 }
5206 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5207 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5208 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5209 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5210 vim_free(*cmdlinep);
5211 *cmdlinep = new_cmdline;
5212
5213 return src;
5214}
5215
5216/*
5217 * Check for '|' to separate commands and '"' to start comments.
5218 */
5219 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005220separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 char_u *p;
5223
Bram Moolenaar86b68352004-12-27 21:59:20 +00005224#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005225 p = skip_grep_pat(eap);
5226#else
5227 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005228#endif
5229
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005230 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 if (*p == Ctrl_V)
5233 {
5234 if (eap->argt & (USECTRLV | XFILE))
5235 ++p; /* skip CTRL-V and next char */
5236 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005237 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005238 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (*p == NUL) /* stop at NUL after CTRL-V */
5240 break;
5241 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005242
5243#ifdef FEAT_EVAL
5244 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005245 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005246 {
5247 p += 2;
5248 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005249 }
5250#endif
5251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 /* Check for '"': start of comment or '|': next command */
5253 /* :@" and :*" do not start a comment!
5254 * :redir @" doesn't either. */
5255 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5256 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5257 || p != eap->arg)
5258 && (eap->cmdidx != CMD_redir
5259 || p != eap->arg + 1 || p[-1] != '@'))
5260 || *p == '|' || *p == '\n')
5261 {
5262 /*
5263 * We remove the '\' before the '|', unless USECTRLV is used
5264 * AND 'b' is present in 'cpoptions'.
5265 */
5266 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5267 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5268 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005269 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 --p;
5271 }
5272 else
5273 {
5274 eap->nextcmd = check_nextcmd(p);
5275 *p = NUL;
5276 break;
5277 }
5278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5282 del_trailing_spaces(eap->arg);
5283}
5284
5285/*
5286 * get + command from ex argument
5287 */
5288 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005289getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
5291 char_u *arg = *argp;
5292 char_u *command = NULL;
5293
5294 if (*arg == '+') /* +[command] */
5295 {
5296 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005297 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 command = dollar_command;
5299 else
5300 {
5301 command = arg;
5302 arg = skip_cmd_arg(command, TRUE);
5303 if (*arg != NUL)
5304 *arg++ = NUL; /* terminate command with NUL */
5305 }
5306
5307 arg = skipwhite(arg); /* skip over spaces */
5308 *argp = arg;
5309 }
5310 return command;
5311}
5312
5313/*
5314 * Find end of "+command" argument. Skip over "\ " and "\\".
5315 */
5316 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005317skip_cmd_arg(
5318 char_u *p,
5319 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320{
5321 while (*p && !vim_isspace(*p))
5322 {
5323 if (*p == '\\' && p[1] != NUL)
5324 {
5325 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005326 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 else
5328 ++p;
5329 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005330 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332 return p;
5333}
5334
5335/*
5336 * Get "++opt=arg" argument.
5337 * Return FAIL or OK.
5338 */
5339 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 char_u *arg = eap->arg + 2;
5343 int *pp = NULL;
5344#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005345 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 char_u *p;
5347#endif
5348
5349 /* ":edit ++[no]bin[ary] file" */
5350 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5351 {
5352 if (*arg == 'n')
5353 {
5354 arg += 2;
5355 eap->force_bin = FORCE_NOBIN;
5356 }
5357 else
5358 eap->force_bin = FORCE_BIN;
5359 if (!checkforcmd(&arg, "binary", 3))
5360 return FAIL;
5361 eap->arg = skipwhite(arg);
5362 return OK;
5363 }
5364
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 /* ":read ++edit file" */
5366 if (STRNCMP(arg, "edit", 4) == 0)
5367 {
5368 eap->read_edit = TRUE;
5369 eap->arg = skipwhite(arg + 4);
5370 return OK;
5371 }
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 if (STRNCMP(arg, "ff", 2) == 0)
5374 {
5375 arg += 2;
5376 pp = &eap->force_ff;
5377 }
5378 else if (STRNCMP(arg, "fileformat", 10) == 0)
5379 {
5380 arg += 10;
5381 pp = &eap->force_ff;
5382 }
5383#ifdef FEAT_MBYTE
5384 else if (STRNCMP(arg, "enc", 3) == 0)
5385 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005386 if (STRNCMP(arg, "encoding", 8) == 0)
5387 arg += 8;
5388 else
5389 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 pp = &eap->force_enc;
5391 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005392 else if (STRNCMP(arg, "bad", 3) == 0)
5393 {
5394 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005395 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif
5398
5399 if (pp == NULL || *arg != '=')
5400 return FAIL;
5401
5402 ++arg;
5403 *pp = (int)(arg - eap->cmd);
5404 arg = skip_cmd_arg(arg, FALSE);
5405 eap->arg = skipwhite(arg);
5406 *arg = NUL;
5407
5408#ifdef FEAT_MBYTE
5409 if (pp == &eap->force_ff)
5410 {
5411#endif
5412 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5413 return FAIL;
5414#ifdef FEAT_MBYTE
5415 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005416 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 /* Make 'fileencoding' lower case. */
5419 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5420 *p = TOLOWER_ASC(*p);
5421 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005422 else
5423 {
5424 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5425 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005426 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005427 if (STRICMP(p, "keep") == 0)
5428 eap->bad_char = BAD_KEEP;
5429 else if (STRICMP(p, "drop") == 0)
5430 eap->bad_char = BAD_DROP;
5431 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5432 eap->bad_char = *p;
5433 else
5434 return FAIL;
5435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#endif
5437
5438 return OK;
5439}
5440
5441/*
5442 * ":abbreviate" and friends.
5443 */
5444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005445ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
5447 do_exmap(eap, TRUE); /* almost the same as mapping */
5448}
5449
5450/*
5451 * ":map" and friends.
5452 */
5453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 /*
5457 * If we are sourcing .exrc or .vimrc in current directory we
5458 * print the mappings for security reasons.
5459 */
5460 if (secure)
5461 {
5462 secure = 2;
5463 msg_outtrans(eap->cmd);
5464 msg_putchar('\n');
5465 }
5466 do_exmap(eap, FALSE);
5467}
5468
5469/*
5470 * ":unmap" and friends.
5471 */
5472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 do_exmap(eap, FALSE);
5476}
5477
5478/*
5479 * ":mapclear" and friends.
5480 */
5481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5485}
5486
5487/*
5488 * ":abclear" and friends.
5489 */
5490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5494}
5495
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005496#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 /*
5501 * Disallow auto commands from .exrc and .vimrc in current
5502 * directory for security reasons.
5503 */
5504 if (secure)
5505 {
5506 secure = 2;
5507 eap->errmsg = e_curdir;
5508 }
5509 else if (eap->cmdidx == CMD_autocmd)
5510 do_autocmd(eap->arg, eap->forceit);
5511 else
5512 do_augroup(eap->arg, eap->forceit);
5513}
5514
5515/*
5516 * ":doautocmd": Apply the automatic commands to the current buffer.
5517 */
5518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005521 char_u *arg = eap->arg;
5522 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005523 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005524
Bram Moolenaar1610d052016-06-09 22:53:01 +02005525 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5526 /* Only when there is no <nomodeline>. */
5527 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005528 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529}
5530#endif
5531
5532#ifdef FEAT_LISTCMDS
5533/*
5534 * :[N]bunload[!] [N] [bufname] unload buffer
5535 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5536 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5537 */
5538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005539ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 eap->errmsg = do_bufdel(
5542 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5543 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5544 : DOBUF_UNLOAD, eap->arg,
5545 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5546}
5547
5548/*
5549 * :[N]buffer [N] to buffer N
5550 * :[N]sbuffer [N] to buffer N
5551 */
5552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 if (*eap->arg)
5556 eap->errmsg = e_trailing;
5557 else
5558 {
5559 if (eap->addr_count == 0) /* default is current buffer */
5560 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5561 else
5562 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005563 if (eap->do_ecmd_cmd != NULL)
5564 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566}
5567
5568/*
5569 * :[N]bmodified [N] to next mod. buffer
5570 * :[N]sbmodified [N] to next mod. buffer
5571 */
5572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005573ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005576 if (eap->do_ecmd_cmd != NULL)
5577 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578}
5579
5580/*
5581 * :[N]bnext [N] to next buffer
5582 * :[N]sbnext [N] split and to next buffer
5583 */
5584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005588 if (eap->do_ecmd_cmd != NULL)
5589 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590}
5591
5592/*
5593 * :[N]bNext [N] to previous buffer
5594 * :[N]bprevious [N] to previous buffer
5595 * :[N]sbNext [N] split and to previous buffer
5596 * :[N]sbprevious [N] split and to previous buffer
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005602 if (eap->do_ecmd_cmd != NULL)
5603 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606/*
5607 * :brewind to first buffer
5608 * :bfirst to first buffer
5609 * :sbrewind split and to first buffer
5610 * :sbfirst split and to first buffer
5611 */
5612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005616 if (eap->do_ecmd_cmd != NULL)
5617 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619
5620/*
5621 * :blast to last buffer
5622 * :sblast split and to last buffer
5623 */
5624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005628 if (eap->do_ecmd_cmd != NULL)
5629 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630}
5631#endif
5632
5633 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 return (c == NUL || c == '|' || c == '"' || c == '\n');
5637}
5638
5639#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5640 || defined(PROTO)
5641/*
5642 * Return the next command, after the first '|' or '\n'.
5643 * Return NULL if not found.
5644 */
5645 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 while (*p != '|' && *p != '\n')
5649 {
5650 if (*p == NUL)
5651 return NULL;
5652 ++p;
5653 }
5654 return (p + 1);
5655}
5656#endif
5657
5658/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005659 * Check if *p is a separator between Ex commands, skipping over white space.
5660 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 */
5662 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005665 char_u *s = skipwhite(p);
5666
5667 if (*s == '|' || *s == '\n')
5668 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 else
5670 return NULL;
5671}
5672
5673/*
5674 * - if there are more files to edit
5675 * - and this is the last window
5676 * - and forceit not used
5677 * - and not repeated twice on a row
5678 * return FAIL and give error message if 'message' TRUE
5679 * return OK otherwise
5680 */
5681 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005682check_more(
5683 int message, /* when FALSE check only, no messages */
5684 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5687
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005688 if (!forceit && only_one_window()
5689 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
5691 if (message)
5692 {
5693#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5694 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5695 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005696 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005699 vim_strncpy(buff,
5700 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005701 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005703 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 _("%d more files to edit. Quit anyway?"), n);
5705 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5706 return OK;
5707 return FAIL;
5708 }
5709#endif
5710 if (n == 1)
5711 EMSG(_("E173: 1 more file to edit"));
5712 else
5713 EMSGN(_("E173: %ld more files to edit"), n);
5714 quitmore = 2; /* next try to quit is allowed */
5715 }
5716 return FAIL;
5717 }
5718 return OK;
5719}
5720
5721#ifdef FEAT_CMDL_COMPL
5722/*
5723 * Function given to ExpandGeneric() to obtain the list of command names.
5724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005726get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 if (idx >= (int)CMD_SIZE)
5729# ifdef FEAT_USR_CMDS
5730 return get_user_command_name(idx);
5731# else
5732 return NULL;
5733# endif
5734 return cmdnames[idx].cmd_name;
5735}
5736#endif
5737
5738#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005739static 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);
5740static void uc_list(char_u *name, size_t name_len);
5741static 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);
5742static char_u *uc_split_args(char_u *arg, size_t *lenp);
5743static 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 +00005744
5745 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746uc_add_command(
5747 char_u *name,
5748 size_t name_len,
5749 char_u *rep,
5750 long argt,
5751 long def,
5752 int flags,
5753 int compl,
5754 char_u *compl_arg,
5755 int addr_type,
5756 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758 ucmd_T *cmd = NULL;
5759 char_u *p;
5760 int i;
5761 int cmp = 1;
5762 char_u *rep_buf = NULL;
5763 garray_T *gap;
5764
Bram Moolenaar9c102382006-05-03 21:26:49 +00005765 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (rep_buf == NULL)
5767 {
5768 /* Can't replace termcodes - try using the string as is */
5769 rep_buf = vim_strsave(rep);
5770
5771 /* Give up if out of memory */
5772 if (rep_buf == NULL)
5773 return FAIL;
5774 }
5775
5776 /* get address of growarray: global or in curbuf */
5777 if (flags & UC_BUFFER)
5778 {
5779 gap = &curbuf->b_ucmds;
5780 if (gap->ga_itemsize == 0)
5781 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5782 }
5783 else
5784 gap = &ucmds;
5785
5786 /* Search for the command in the already defined commands. */
5787 for (i = 0; i < gap->ga_len; ++i)
5788 {
5789 size_t len;
5790
5791 cmd = USER_CMD_GA(gap, i);
5792 len = STRLEN(cmd->uc_name);
5793 cmp = STRNCMP(name, cmd->uc_name, name_len);
5794 if (cmp == 0)
5795 {
5796 if (name_len < len)
5797 cmp = -1;
5798 else if (name_len > len)
5799 cmp = 1;
5800 }
5801
5802 if (cmp == 0)
5803 {
5804 if (!force)
5805 {
5806 EMSG(_("E174: Command already exists: add ! to replace it"));
5807 goto fail;
5808 }
5809
Bram Moolenaard23a8232018-02-10 18:45:26 +01005810 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005811#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005812 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815 }
5816
5817 /* Stop as soon as we pass the name to add */
5818 if (cmp < 0)
5819 break;
5820 }
5821
5822 /* Extend the array unless we're replacing an existing command */
5823 if (cmp != 0)
5824 {
5825 if (ga_grow(gap, 1) != OK)
5826 goto fail;
5827 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5828 goto fail;
5829
5830 cmd = USER_CMD_GA(gap, i);
5831 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5832
5833 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835 cmd->uc_name = p;
5836 }
5837
5838 cmd->uc_rep = rep_buf;
5839 cmd->uc_argt = argt;
5840 cmd->uc_def = def;
5841 cmd->uc_compl = compl;
5842#ifdef FEAT_EVAL
5843 cmd->uc_scriptID = current_SID;
5844# ifdef FEAT_CMDL_COMPL
5845 cmd->uc_compl_arg = compl_arg;
5846# endif
5847#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005848 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850 return OK;
5851
5852fail:
5853 vim_free(rep_buf);
5854#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5855 vim_free(compl_arg);
5856#endif
5857 return FAIL;
5858}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005861#if defined(FEAT_USR_CMDS)
5862static struct
5863{
5864 int expand;
5865 char *name;
5866} addr_type_complete[] =
5867{
5868 {ADDR_ARGUMENTS, "arguments"},
5869 {ADDR_LINES, "lines"},
5870 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5871 {ADDR_TABS, "tabs"},
5872 {ADDR_BUFFERS, "buffers"},
5873 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005874 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005875 {-1, NULL}
5876};
5877#endif
5878
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005879#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880/*
5881 * List of names for completion for ":command" with the EXPAND_ flag.
5882 * Must be alphabetical for completion.
5883 */
5884static struct
5885{
5886 int expand;
5887 char *name;
5888} command_complete[] =
5889{
5890 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005891 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005893 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005895 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005896#if defined(FEAT_CSCOPE)
5897 {EXPAND_CSCOPE, "cscope"},
5898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5900 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005901 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#endif
5903 {EXPAND_DIRECTORIES, "dir"},
5904 {EXPAND_ENV_VARS, "environment"},
5905 {EXPAND_EVENTS, "event"},
5906 {EXPAND_EXPRESSION, "expression"},
5907 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005908 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005909 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {EXPAND_FUNCTIONS, "function"},
5911 {EXPAND_HELP, "help"},
5912 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005913#if defined(FEAT_CMDHIST)
5914 {EXPAND_HISTORY, "history"},
5915#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005916#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005917 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005918 {EXPAND_LOCALES, "locale"},
5919#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005920 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {EXPAND_MAPPINGS, "mapping"},
5922 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005923 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005924 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005925#if defined(FEAT_PROFILE)
5926 {EXPAND_SYNTIME, "syntime"},
5927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005929 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005930 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005931#if defined(FEAT_SIGNS)
5932 {EXPAND_SIGN, "sign"},
5933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {EXPAND_TAGS, "tag"},
5935 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005936 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {EXPAND_USER_VARS, "var"},
5938 {0, NULL}
5939};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005942#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005944uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
5946 int i, j;
5947 int found = FALSE;
5948 ucmd_T *cmd;
5949 int len;
5950 long a;
5951 garray_T *gap;
5952
5953 gap = &curbuf->b_ucmds;
5954 for (;;)
5955 {
5956 for (i = 0; i < gap->ga_len; ++i)
5957 {
5958 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005959 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaard29459b2016-08-26 22:29:11 +02005961 /* Skip commands which don't match the requested prefix and
5962 * commands filtered out. */
5963 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5964 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 continue;
5966
5967 /* Put out the title first time */
5968 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005969 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 found = TRUE;
5971 msg_putchar('\n');
5972 if (got_int)
5973 break;
5974
5975 /* Special cases */
5976 msg_putchar(a & BANG ? '!' : ' ');
5977 msg_putchar(a & REGSTR ? '"' : ' ');
5978 msg_putchar(gap != &ucmds ? 'b' : ' ');
5979 msg_putchar(' ');
5980
Bram Moolenaar8820b482017-03-16 17:23:31 +01005981 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 len = (int)STRLEN(cmd->uc_name) + 4;
5983
5984 do {
5985 msg_putchar(' ');
5986 ++len;
5987 } while (len < 16);
5988
5989 len = 0;
5990
5991 /* Arguments */
5992 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5993 {
5994 case 0: IObuff[len++] = '0'; break;
5995 case (EXTRA): IObuff[len++] = '*'; break;
5996 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5997 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5998 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5999 }
6000
6001 do {
6002 IObuff[len++] = ' ';
6003 } while (len < 5);
6004
6005 /* Range */
6006 if (a & (RANGE|COUNT))
6007 {
6008 if (a & COUNT)
6009 {
6010 /* -count=N */
6011 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6012 len += (int)STRLEN(IObuff + len);
6013 }
6014 else if (a & DFLALL)
6015 IObuff[len++] = '%';
6016 else if (cmd->uc_def >= 0)
6017 {
6018 /* -range=N */
6019 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6020 len += (int)STRLEN(IObuff + len);
6021 }
6022 else
6023 IObuff[len++] = '.';
6024 }
6025
6026 do {
6027 IObuff[len++] = ' ';
6028 } while (len < 11);
6029
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006030 /* Address Type */
6031 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6032 if (addr_type_complete[j].expand != ADDR_LINES
6033 && addr_type_complete[j].expand == cmd->uc_addr_type)
6034 {
6035 STRCPY(IObuff + len, addr_type_complete[j].name);
6036 len += (int)STRLEN(IObuff + len);
6037 break;
6038 }
6039
6040 do {
6041 IObuff[len++] = ' ';
6042 } while (len < 21);
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 /* Completion */
6045 for (j = 0; command_complete[j].expand != 0; ++j)
6046 if (command_complete[j].expand == cmd->uc_compl)
6047 {
6048 STRCPY(IObuff + len, command_complete[j].name);
6049 len += (int)STRLEN(IObuff + len);
6050 break;
6051 }
6052
6053 do {
6054 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006055 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 IObuff[len] = '\0';
6058 msg_outtrans(IObuff);
6059
6060 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006061#ifdef FEAT_EVAL
6062 if (p_verbose > 0)
6063 last_set_msg(cmd->uc_scriptID);
6064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 out_flush();
6066 ui_breakcheck();
6067 if (got_int)
6068 break;
6069 }
6070 if (gap == &ucmds || i < gap->ga_len)
6071 break;
6072 gap = &ucmds;
6073 }
6074
6075 if (!found)
6076 MSG(_("No user-defined commands found"));
6077}
6078
6079 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006080uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6083 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6084 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6085 0xb9, 0x7f, 0};
6086 int i;
6087
6088 for (i = 0; fcmd[i]; ++i)
6089 IObuff[i] = fcmd[i] - 0x40;
6090 IObuff[i] = 0;
6091 return IObuff;
6092}
6093
6094 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095uc_scan_attr(
6096 char_u *attr,
6097 size_t len,
6098 long *argt,
6099 long *def,
6100 int *flags,
6101 int *compl,
6102 char_u **compl_arg,
6103 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 char_u *p;
6106
6107 if (len == 0)
6108 {
6109 EMSG(_("E175: No attribute specified"));
6110 return FAIL;
6111 }
6112
6113 /* First, try the simple attributes (no arguments) */
6114 if (STRNICMP(attr, "bang", len) == 0)
6115 *argt |= BANG;
6116 else if (STRNICMP(attr, "buffer", len) == 0)
6117 *flags |= UC_BUFFER;
6118 else if (STRNICMP(attr, "register", len) == 0)
6119 *argt |= REGSTR;
6120 else if (STRNICMP(attr, "bar", len) == 0)
6121 *argt |= TRLBAR;
6122 else
6123 {
6124 int i;
6125 char_u *val = NULL;
6126 size_t vallen = 0;
6127 size_t attrlen = len;
6128
6129 /* Look for the attribute name - which is the part before any '=' */
6130 for (i = 0; i < (int)len; ++i)
6131 {
6132 if (attr[i] == '=')
6133 {
6134 val = &attr[i + 1];
6135 vallen = len - i - 1;
6136 attrlen = i;
6137 break;
6138 }
6139 }
6140
6141 if (STRNICMP(attr, "nargs", attrlen) == 0)
6142 {
6143 if (vallen == 1)
6144 {
6145 if (*val == '0')
6146 /* Do nothing - this is the default */;
6147 else if (*val == '1')
6148 *argt |= (EXTRA | NOSPC | NEEDARG);
6149 else if (*val == '*')
6150 *argt |= EXTRA;
6151 else if (*val == '?')
6152 *argt |= (EXTRA | NOSPC);
6153 else if (*val == '+')
6154 *argt |= (EXTRA | NEEDARG);
6155 else
6156 goto wrong_nargs;
6157 }
6158 else
6159 {
6160wrong_nargs:
6161 EMSG(_("E176: Invalid number of arguments"));
6162 return FAIL;
6163 }
6164 }
6165 else if (STRNICMP(attr, "range", attrlen) == 0)
6166 {
6167 *argt |= RANGE;
6168 if (vallen == 1 && *val == '%')
6169 *argt |= DFLALL;
6170 else if (val != NULL)
6171 {
6172 p = val;
6173 if (*def >= 0)
6174 {
6175two_count:
6176 EMSG(_("E177: Count cannot be specified twice"));
6177 return FAIL;
6178 }
6179
6180 *def = getdigits(&p);
6181 *argt |= (ZEROR | NOTADR);
6182
6183 if (p != val + vallen || vallen == 0)
6184 {
6185invalid_count:
6186 EMSG(_("E178: Invalid default value for count"));
6187 return FAIL;
6188 }
6189 }
6190 }
6191 else if (STRNICMP(attr, "count", attrlen) == 0)
6192 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006193 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
6195 if (val != NULL)
6196 {
6197 p = val;
6198 if (*def >= 0)
6199 goto two_count;
6200
6201 *def = getdigits(&p);
6202
6203 if (p != val + vallen)
6204 goto invalid_count;
6205 }
6206
6207 if (*def < 0)
6208 *def = 0;
6209 }
6210 else if (STRNICMP(attr, "complete", attrlen) == 0)
6211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 if (val == NULL)
6213 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006214 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 return FAIL;
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006218 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6219 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006222 else if (STRNICMP(attr, "addr", attrlen) == 0)
6223 {
6224 *argt |= RANGE;
6225 if (val == NULL)
6226 {
6227 EMSG(_("E179: argument required for -addr"));
6228 return FAIL;
6229 }
6230 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6231 == FAIL)
6232 return FAIL;
6233 if (addr_type_arg != ADDR_LINES)
6234 *argt |= (ZEROR | NOTADR) ;
6235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 else
6237 {
6238 char_u ch = attr[len];
6239 attr[len] = '\0';
6240 EMSG2(_("E181: Invalid attribute: %s"), attr);
6241 attr[len] = ch;
6242 return FAIL;
6243 }
6244 }
6245
6246 return OK;
6247}
6248
Bram Moolenaara850a712009-01-28 14:42:59 +00006249/*
6250 * ":command ..."
6251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006253ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 char_u *name;
6256 char_u *end;
6257 char_u *p;
6258 long argt = 0;
6259 long def = -1;
6260 int flags = 0;
6261 int compl = EXPAND_NOTHING;
6262 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006263 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006265 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 p = eap->arg;
6268
6269 /* Check for attributes */
6270 while (*p == '-')
6271 {
6272 ++p;
6273 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006274 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6275 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 == FAIL)
6277 return;
6278 p = skipwhite(end);
6279 }
6280
6281 /* Get the name (if any) and skip to the following argument */
6282 name = p;
6283 if (ASCII_ISALPHA(*p))
6284 while (ASCII_ISALNUM(*p))
6285 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006286 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
6288 EMSG(_("E182: Invalid command name"));
6289 return;
6290 }
6291 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006292 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
6294 /* If there is nothing after the name, and no attributes were specified,
6295 * we are listing commands
6296 */
6297 p = skipwhite(end);
6298 if (!has_attr && ends_excmd(*p))
6299 {
6300 uc_list(name, end - name);
6301 }
6302 else if (!ASCII_ISUPPER(*name))
6303 {
6304 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6305 return;
6306 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006307 else if ((name_len == 1 && *name == 'X')
6308 || (name_len <= 4
6309 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6310 {
6311 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6312 return;
6313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 else
6315 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006316 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317}
6318
6319/*
6320 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * Clear all user commands, global and for current buffer.
6322 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 uc_clear(&ucmds);
6327 uc_clear(&curbuf->b_ucmds);
6328}
6329
6330/*
6331 * Clear all user commands for "gap".
6332 */
6333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006334uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
6336 int i;
6337 ucmd_T *cmd;
6338
6339 for (i = 0; i < gap->ga_len; ++i)
6340 {
6341 cmd = USER_CMD_GA(gap, i);
6342 vim_free(cmd->uc_name);
6343 vim_free(cmd->uc_rep);
6344# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6345 vim_free(cmd->uc_compl_arg);
6346# endif
6347 }
6348 ga_clear(gap);
6349}
6350
6351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006352ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 int i = 0;
6355 ucmd_T *cmd = NULL;
6356 int cmp = -1;
6357 garray_T *gap;
6358
6359 gap = &curbuf->b_ucmds;
6360 for (;;)
6361 {
6362 for (i = 0; i < gap->ga_len; ++i)
6363 {
6364 cmd = USER_CMD_GA(gap, i);
6365 cmp = STRCMP(eap->arg, cmd->uc_name);
6366 if (cmp <= 0)
6367 break;
6368 }
6369 if (gap == &ucmds || cmp == 0)
6370 break;
6371 gap = &ucmds;
6372 }
6373
6374 if (cmp != 0)
6375 {
6376 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6377 return;
6378 }
6379
6380 vim_free(cmd->uc_name);
6381 vim_free(cmd->uc_rep);
6382# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6383 vim_free(cmd->uc_compl_arg);
6384# endif
6385
6386 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 if (i < gap->ga_len)
6389 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6390}
6391
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006392/*
6393 * split and quote args for <f-args>
6394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006396uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 char_u *buf;
6399 char_u *p;
6400 char_u *q;
6401 int len;
6402
6403 /* Precalculate length */
6404 p = arg;
6405 len = 2; /* Initial and final quotes */
6406
6407 while (*p)
6408 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006409 if (p[0] == '\\' && p[1] == '\\')
6410 {
6411 len += 2;
6412 p += 2;
6413 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006414 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
6416 len += 1;
6417 p += 2;
6418 }
6419 else if (*p == '\\' || *p == '"')
6420 {
6421 len += 2;
6422 p += 1;
6423 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006424 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 {
6426 p = skipwhite(p);
6427 if (*p == NUL)
6428 break;
6429 len += 3; /* "," */
6430 }
6431 else
6432 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006433#ifdef FEAT_MBYTE
6434 int charlen = (*mb_ptr2len)(p);
6435 len += charlen;
6436 p += charlen;
6437#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 ++len;
6439 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 }
6442 }
6443
6444 buf = alloc(len + 1);
6445 if (buf == NULL)
6446 {
6447 *lenp = 0;
6448 return buf;
6449 }
6450
6451 p = arg;
6452 q = buf;
6453 *q++ = '"';
6454 while (*p)
6455 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006456 if (p[0] == '\\' && p[1] == '\\')
6457 {
6458 *q++ = '\\';
6459 *q++ = '\\';
6460 p += 2;
6461 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006462 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
6464 *q++ = p[1];
6465 p += 2;
6466 }
6467 else if (*p == '\\' || *p == '"')
6468 {
6469 *q++ = '\\';
6470 *q++ = *p++;
6471 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006472 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 {
6474 p = skipwhite(p);
6475 if (*p == NUL)
6476 break;
6477 *q++ = '"';
6478 *q++ = ',';
6479 *q++ = '"';
6480 }
6481 else
6482 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006483 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485 }
6486 *q++ = '"';
6487 *q = 0;
6488
6489 *lenp = len;
6490 return buf;
6491}
6492
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006493 static size_t
6494add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6495{
6496 size_t result;
6497
6498 result = STRLEN(mod_str);
6499 if (*multi_mods)
6500 result += 1;
6501 if (buf != NULL)
6502 {
6503 if (*multi_mods)
6504 STRCAT(buf, " ");
6505 STRCAT(buf, mod_str);
6506 }
6507
6508 *multi_mods = 1;
6509
6510 return result;
6511}
6512
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513/*
6514 * Check for a <> code in a user command.
6515 * "code" points to the '<'. "len" the length of the <> (inclusive).
6516 * "buf" is where the result is to be added.
6517 * "split_buf" points to a buffer used for splitting, caller should free it.
6518 * "split_len" is the length of what "split_buf" contains.
6519 * Returns the length of the replacement, which has been added to "buf".
6520 * Returns -1 if there was no match, and only the "<" has been copied.
6521 */
6522 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006523uc_check_code(
6524 char_u *code,
6525 size_t len,
6526 char_u *buf,
6527 ucmd_T *cmd, /* the user command we're expanding */
6528 exarg_T *eap, /* ex arguments */
6529 char_u **split_buf,
6530 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 size_t result = 0;
6533 char_u *p = code + 1;
6534 size_t l = len - 2;
6535 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006536 enum {
6537 ct_ARGS,
6538 ct_BANG,
6539 ct_COUNT,
6540 ct_LINE1,
6541 ct_LINE2,
6542 ct_RANGE,
6543 ct_MODS,
6544 ct_REGISTER,
6545 ct_LT,
6546 ct_NONE
6547 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6550 {
6551 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6552 p += 2;
6553 l -= 2;
6554 }
6555
Bram Moolenaar371d5402006-03-20 21:47:49 +00006556 ++l;
6557 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006559 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006561 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006563 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006565 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006567 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006569 else if (STRNICMP(p, "range>", l) == 0)
6570 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006571 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006573 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006575 else if (STRNICMP(p, "mods>", l) == 0)
6576 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 switch (type)
6579 {
6580 case ct_ARGS:
6581 /* Simple case first */
6582 if (*eap->arg == NUL)
6583 {
6584 if (quote == 1)
6585 {
6586 result = 2;
6587 if (buf != NULL)
6588 STRCPY(buf, "''");
6589 }
6590 else
6591 result = 0;
6592 break;
6593 }
6594
6595 /* When specified there is a single argument don't split it.
6596 * Works for ":Cmd %" when % is "a b c". */
6597 if ((eap->argt & NOSPC) && quote == 2)
6598 quote = 1;
6599
6600 switch (quote)
6601 {
6602 case 0: /* No quoting, no splitting */
6603 result = STRLEN(eap->arg);
6604 if (buf != NULL)
6605 STRCPY(buf, eap->arg);
6606 break;
6607 case 1: /* Quote, but don't split */
6608 result = STRLEN(eap->arg) + 2;
6609 for (p = eap->arg; *p; ++p)
6610 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006611#ifdef FEAT_MBYTE
6612 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6613 /* DBCS can contain \ in a trail byte, skip the
6614 * double-byte character. */
6615 ++p;
6616 else
6617#endif
6618 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 ++result;
6620 }
6621
6622 if (buf != NULL)
6623 {
6624 *buf++ = '"';
6625 for (p = eap->arg; *p; ++p)
6626 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006627#ifdef FEAT_MBYTE
6628 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6629 /* DBCS can contain \ in a trail byte, copy the
6630 * double-byte character to avoid escaping. */
6631 *buf++ = *p++;
6632 else
6633#endif
6634 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 *buf++ = '\\';
6636 *buf++ = *p;
6637 }
6638 *buf = '"';
6639 }
6640
6641 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006642 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 /* This is hard, so only do it once, and cache the result */
6644 if (*split_buf == NULL)
6645 *split_buf = uc_split_args(eap->arg, split_len);
6646
6647 result = *split_len;
6648 if (buf != NULL && result != 0)
6649 STRCPY(buf, *split_buf);
6650
6651 break;
6652 }
6653 break;
6654
6655 case ct_BANG:
6656 result = eap->forceit ? 1 : 0;
6657 if (quote)
6658 result += 2;
6659 if (buf != NULL)
6660 {
6661 if (quote)
6662 *buf++ = '"';
6663 if (eap->forceit)
6664 *buf++ = '!';
6665 if (quote)
6666 *buf = '"';
6667 }
6668 break;
6669
6670 case ct_LINE1:
6671 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006672 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 case ct_COUNT:
6674 {
6675 char num_buf[20];
6676 long num = (type == ct_LINE1) ? eap->line1 :
6677 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006678 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6680 size_t num_len;
6681
6682 sprintf(num_buf, "%ld", num);
6683 num_len = STRLEN(num_buf);
6684 result = num_len;
6685
6686 if (quote)
6687 result += 2;
6688
6689 if (buf != NULL)
6690 {
6691 if (quote)
6692 *buf++ = '"';
6693 STRCPY(buf, num_buf);
6694 buf += num_len;
6695 if (quote)
6696 *buf = '"';
6697 }
6698
6699 break;
6700 }
6701
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006702 case ct_MODS:
6703 {
6704 int multi_mods = 0;
6705 typedef struct {
6706 int *varp;
6707 char *name;
6708 } mod_entry_T;
6709 static mod_entry_T mod_entries[] = {
6710#ifdef FEAT_BROWSE_CMD
6711 {&cmdmod.browse, "browse"},
6712#endif
6713#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6714 {&cmdmod.confirm, "confirm"},
6715#endif
6716 {&cmdmod.hide, "hide"},
6717 {&cmdmod.keepalt, "keepalt"},
6718 {&cmdmod.keepjumps, "keepjumps"},
6719 {&cmdmod.keepmarks, "keepmarks"},
6720 {&cmdmod.keeppatterns, "keeppatterns"},
6721 {&cmdmod.lockmarks, "lockmarks"},
6722 {&cmdmod.noswapfile, "noswapfile"},
6723 {NULL, NULL}
6724 };
6725 int i;
6726
6727 result = quote ? 2 : 0;
6728 if (buf != NULL)
6729 {
6730 if (quote)
6731 *buf++ = '"';
6732 *buf = '\0';
6733 }
6734
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006735 /* :aboveleft and :leftabove */
6736 if (cmdmod.split & WSP_ABOVE)
6737 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6738 /* :belowright and :rightbelow */
6739 if (cmdmod.split & WSP_BELOW)
6740 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6741 /* :botright */
6742 if (cmdmod.split & WSP_BOT)
6743 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006744
6745 /* the modifiers that are simple flags */
6746 for (i = 0; mod_entries[i].varp != NULL; ++i)
6747 if (*mod_entries[i].varp)
6748 result += add_cmd_modifier(buf, mod_entries[i].name,
6749 &multi_mods);
6750
6751 /* TODO: How to support :noautocmd? */
6752#ifdef HAVE_SANDBOX
6753 /* TODO: How to support :sandbox?*/
6754#endif
6755 /* :silent */
6756 if (msg_silent > 0)
6757 result += add_cmd_modifier(buf,
6758 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006759 /* :tab */
6760 if (cmdmod.tab > 0)
6761 result += add_cmd_modifier(buf, "tab", &multi_mods);
6762 /* :topleft */
6763 if (cmdmod.split & WSP_TOP)
6764 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006765 /* TODO: How to support :unsilent?*/
6766 /* :verbose */
6767 if (p_verbose > 0)
6768 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006769 /* :vertical */
6770 if (cmdmod.split & WSP_VERT)
6771 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006772 if (quote && buf != NULL)
6773 {
6774 buf += result - 2;
6775 *buf = '"';
6776 }
6777 break;
6778 }
6779
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 case ct_REGISTER:
6781 result = eap->regname ? 1 : 0;
6782 if (quote)
6783 result += 2;
6784 if (buf != NULL)
6785 {
6786 if (quote)
6787 *buf++ = '\'';
6788 if (eap->regname)
6789 *buf++ = eap->regname;
6790 if (quote)
6791 *buf = '\'';
6792 }
6793 break;
6794
6795 case ct_LT:
6796 result = 1;
6797 if (buf != NULL)
6798 *buf = '<';
6799 break;
6800
6801 default:
6802 /* Not recognized: just copy the '<' and return -1. */
6803 result = (size_t)-1;
6804 if (buf != NULL)
6805 *buf = '<';
6806 break;
6807 }
6808
6809 return result;
6810}
6811
6812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006813do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 char_u *buf;
6816 char_u *p;
6817 char_u *q;
6818
6819 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006820 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006821 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 size_t len, totlen;
6823
6824 size_t split_len = 0;
6825 char_u *split_buf = NULL;
6826 ucmd_T *cmd;
6827#ifdef FEAT_EVAL
6828 scid_T save_current_SID = current_SID;
6829#endif
6830
6831 if (eap->cmdidx == CMD_USER)
6832 cmd = USER_CMD(eap->useridx);
6833 else
6834 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6835
6836 /*
6837 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006838 * First round: "buf" is NULL, compute length, allocate "buf".
6839 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
6841 buf = NULL;
6842 for (;;)
6843 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006844 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006845 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006847
6848 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006850 start = vim_strchr(p, '<');
6851 if (start != NULL)
6852 end = vim_strchr(start + 1, '>');
6853 if (buf != NULL)
6854 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006855 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6856 ;
6857 if (*ksp == K_SPECIAL
6858 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006859 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6860# ifdef FEAT_GUI
6861 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6862# endif
6863 ))
6864 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006865 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006866 * KS_SPECIAL KE_FILLER, like for mappings, but
6867 * do_cmdline() doesn't handle that, so convert it back.
6868 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6869 len = ksp - p;
6870 if (len > 0)
6871 {
6872 mch_memmove(q, p, len);
6873 q += len;
6874 }
6875 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6876 p = ksp + 3;
6877 continue;
6878 }
6879 }
6880
6881 /* break if there no <item> is found */
6882 if (start == NULL || end == NULL)
6883 break;
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 /* Include the '>' */
6886 ++end;
6887
6888 /* Take everything up to the '<' */
6889 len = start - p;
6890 if (buf == NULL)
6891 totlen += len;
6892 else
6893 {
6894 mch_memmove(q, p, len);
6895 q += len;
6896 }
6897
6898 len = uc_check_code(start, end - start, q, cmd, eap,
6899 &split_buf, &split_len);
6900 if (len == (size_t)-1)
6901 {
6902 /* no match, continue after '<' */
6903 p = start + 1;
6904 len = 1;
6905 }
6906 else
6907 p = end;
6908 if (buf == NULL)
6909 totlen += len;
6910 else
6911 q += len;
6912 }
6913 if (buf != NULL) /* second time here, finished */
6914 {
6915 STRCPY(q, p);
6916 break;
6917 }
6918
6919 totlen += STRLEN(p); /* Add on the trailing characters */
6920 buf = alloc((unsigned)(totlen + 1));
6921 if (buf == NULL)
6922 {
6923 vim_free(split_buf);
6924 return;
6925 }
6926 }
6927
6928#ifdef FEAT_EVAL
6929 current_SID = cmd->uc_scriptID;
6930#endif
6931 (void)do_cmdline(buf, eap->getline, eap->cookie,
6932 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6933#ifdef FEAT_EVAL
6934 current_SID = save_current_SID;
6935#endif
6936 vim_free(buf);
6937 vim_free(split_buf);
6938}
6939
6940# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6941 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006942get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6945}
6946
6947/*
6948 * Function given to ExpandGeneric() to obtain the list of user command names.
6949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006951get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
6953 if (idx < curbuf->b_ucmds.ga_len)
6954 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6955 idx -= curbuf->b_ucmds.ga_len;
6956 if (idx < ucmds.ga_len)
6957 return USER_CMD(idx)->uc_name;
6958 return NULL;
6959}
6960
6961/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006962 * Function given to ExpandGeneric() to obtain the list of user address type names.
6963 */
6964 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006965get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006966{
6967 return (char_u *)addr_type_complete[idx].name;
6968}
6969
6970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * Function given to ExpandGeneric() to obtain the list of user command
6972 * attributes.
6973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006978 {"addr", "bang", "bar", "buffer", "complete",
6979 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006981 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 return NULL;
6983 return (char_u *)user_cmd_flags[idx];
6984}
6985
6986/*
6987 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006990get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6993
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006994 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 return NULL;
6996 return (char_u *)user_cmd_nargs[idx];
6997}
6998
6999/*
7000 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007003get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 return (char_u *)command_complete[idx].name;
7006}
7007# endif /* FEAT_CMDL_COMPL */
7008
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007009/*
7010 * Parse address type argument
7011 */
7012 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013parse_addr_type_arg(
7014 char_u *value,
7015 int vallen,
7016 long *argt,
7017 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007018{
7019 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007020
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007021 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7022 {
7023 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7024 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7025 if (a && b)
7026 {
7027 *addr_type_arg = addr_type_complete[i].expand;
7028 break;
7029 }
7030 }
7031
7032 if (addr_type_complete[i].expand == -1)
7033 {
7034 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007035
Bram Moolenaar1c465442017-03-12 20:10:05 +01007036 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007037 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007038 err[i] = NUL;
7039 EMSG2(_("E180: Invalid address type value: %s"), err);
7040 return FAIL;
7041 }
7042
7043 if (*addr_type_arg != ADDR_LINES)
7044 *argt |= NOTADR;
7045
7046 return OK;
7047}
7048
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049#endif /* FEAT_USR_CMDS */
7050
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007051#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7052/*
7053 * Parse a completion argument "value[vallen]".
7054 * The detected completion goes in "*complp", argument type in "*argt".
7055 * When there is an argument, for function and user defined completion, it's
7056 * copied to allocated memory and stored in "*compl_arg".
7057 * Returns FAIL if something is wrong.
7058 */
7059 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007060parse_compl_arg(
7061 char_u *value,
7062 int vallen,
7063 int *complp,
7064 long *argt,
7065 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007066{
7067 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007068# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007069 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007070# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007071 int i;
7072 int valend = vallen;
7073
7074 /* Look for any argument part - which is the part after any ',' */
7075 for (i = 0; i < vallen; ++i)
7076 {
7077 if (value[i] == ',')
7078 {
7079 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007080# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007081 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007082# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007083 valend = i;
7084 break;
7085 }
7086 }
7087
7088 for (i = 0; command_complete[i].expand != 0; ++i)
7089 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007090 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007091 && STRNCMP(value, command_complete[i].name, valend) == 0)
7092 {
7093 *complp = command_complete[i].expand;
7094 if (command_complete[i].expand == EXPAND_BUFFERS)
7095 *argt |= BUFNAME;
7096 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7097 || command_complete[i].expand == EXPAND_FILES)
7098 *argt |= XFILE;
7099 break;
7100 }
7101 }
7102
7103 if (command_complete[i].expand == 0)
7104 {
7105 EMSG2(_("E180: Invalid complete value: %s"), value);
7106 return FAIL;
7107 }
7108
7109# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7110 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7111 && arg != NULL)
7112# else
7113 if (arg != NULL)
7114# endif
7115 {
7116 EMSG(_("E468: Completion argument only allowed for custom completion"));
7117 return FAIL;
7118 }
7119
7120# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7121 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7122 && arg == NULL)
7123 {
7124 EMSG(_("E467: Custom completion requires a function argument"));
7125 return FAIL;
7126 }
7127
7128 if (arg != NULL)
7129 *compl_arg = vim_strnsave(arg, (int)arglen);
7130# endif
7131 return OK;
7132}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007133
7134 int
7135cmdcomplete_str_to_type(char_u *complete_str)
7136{
7137 int i;
7138
7139 for (i = 0; command_complete[i].expand != 0; ++i)
7140 if (STRCMP(complete_str, command_complete[i].name) == 0)
7141 return command_complete[i].expand;
7142
7143 return EXPAND_NOTHING;
7144}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007145#endif
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
Bram Moolenaare6850792010-05-14 15:28:44 +02007150 if (*eap->arg == NUL)
7151 {
7152#ifdef FEAT_EVAL
7153 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7154 char_u *p = NULL;
7155
7156 if (expr != NULL)
7157 {
7158 ++emsg_off;
7159 p = eval_to_string(expr, NULL, FALSE);
7160 --emsg_off;
7161 vim_free(expr);
7162 }
7163 if (p != NULL)
7164 {
7165 MSG(p);
7166 vim_free(p);
7167 }
7168 else
7169 MSG("default");
7170#else
7171 MSG(_("unknown"));
7172#endif
7173 }
7174 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007175 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176}
7177
7178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007179ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 if (*eap->arg == NUL && eap->cmd[2] == '!')
7182 MSG(_("Greetings, Vim user!"));
7183 do_highlight(eap->arg, eap->forceit, FALSE);
7184}
7185
7186
7187/*
7188 * Call this function if we thought we were going to exit, but we won't
7189 * (because of an error). May need to restore the terminal mode.
7190 */
7191 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007192not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 exiting = FALSE;
7195 settmode(TMODE_RAW);
7196}
7197
7198/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007199 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 */
7201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007202ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007204 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206#ifdef FEAT_CMDWIN
7207 if (cmdwin_type != 0)
7208 {
7209 cmdwin_result = Ctrl_C;
7210 return;
7211 }
7212#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007213 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007214 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007215 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007216 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007217 return;
7218 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007219 if (eap->addr_count > 0)
7220 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007221 int wnr = eap->line2;
7222
7223 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7224 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007225 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007226 }
7227 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007228 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007229
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007230#ifdef FEAT_AUTOCMD
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007231 /* Refuse to quit when locked. */
7232 if (curbuf_locked())
7233 return;
7234 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7235 /* Bail out when autocommands closed the window.
7236 * Refuse to quit when the buffer in the last window is being closed (can
7237 * only happen in autocommands). */
7238 if (!win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007239 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007240 return;
7241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243#ifdef FEAT_NETBEANS_INTG
7244 netbeansForcedQuit = eap->forceit;
7245#endif
7246
7247 /*
7248 * If there are more files or windows we won't exit.
7249 */
7250 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7251 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007252 if ((!buf_hide(wp->w_buffer)
7253 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007254 | (eap->forceit ? CCGD_FORCEIT : 0)
7255 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007257 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 not_exiting();
7260 }
7261 else
7262 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007263 /* quit last window
7264 * Note: only_one_window() returns true, even so a help window is
7265 * still open. In that case only quit, if no address has been
7266 * specified. Example:
7267 * :h|wincmd w|1q - don't quit
7268 * :h|wincmd w|q - quit
7269 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007270 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007272 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007273#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007277 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279}
7280
7281/*
7282 * ":cquit".
7283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007285ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287 getout(1); /* this does not always pass on the exit code to the Manx
7288 compiler. why? */
7289}
7290
7291/*
7292 * ":qall": try to quit all windows
7293 */
7294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007295ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
7297# ifdef FEAT_CMDWIN
7298 if (cmdwin_type != 0)
7299 {
7300 if (eap->forceit)
7301 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7302 else
7303 cmdwin_result = K_XF2;
7304 return;
7305 }
7306# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007307
7308 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007309 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007310 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007311 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007312 return;
7313 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007314#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007315 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7316 /* Refuse to quit when locked or when the buffer in the last window is
7317 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007318 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007319 return;
7320#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007323 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 getout(0);
7325 not_exiting();
7326}
7327
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328/*
7329 * ":close": close current window, unless it is the last one
7330 */
7331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007332ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007334 win_T *win;
7335 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007336#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007338 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007340#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007341 if (!text_locked()
7342#ifdef FEAT_AUTOCMD
7343 && !curbuf_locked()
7344#endif
7345 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007346 {
7347 if (eap->addr_count == 0)
7348 ex_win_close(eap->forceit, curwin, NULL);
7349 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007350 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007351 {
7352 winnr++;
7353 if (winnr == eap->line2)
7354 break;
7355 }
7356 if (win == NULL)
7357 win = lastwin;
7358 ex_win_close(eap->forceit, win, NULL);
7359 }
7360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361}
7362
Bram Moolenaar4033c552017-09-16 20:54:51 +02007363#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007364/*
7365 * ":pclose": Close any preview window.
7366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007368ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007369{
7370 win_T *win;
7371
Bram Moolenaar29323592016-07-24 22:04:11 +02007372 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373 if (win->w_p_pvw)
7374 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007375 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376 break;
7377 }
7378}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007379#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007380
Bram Moolenaarf740b292006-02-16 22:11:02 +00007381/*
7382 * Close window "win" and take care of handling closing the last window for a
7383 * modified buffer.
7384 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007386ex_win_close(
7387 int forceit,
7388 win_T *win,
7389 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 int need_hide;
7392 buf_T *buf = win->w_buffer;
7393
7394 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007395 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007397#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if ((p_confirm || cmdmod.confirm) && p_write)
7399 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007400 bufref_T bufref;
7401
7402 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007404 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 return;
7406 need_hide = FALSE;
7407 }
7408 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007411 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 return;
7413 }
7414 }
7415
Bram Moolenaar4033c552017-09-16 20:54:51 +02007416#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007418#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007421 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007422 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007423 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007424 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425}
7426
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007428 * Handle the argument for a tabpage related ex command.
7429 * Returns a tabpage number.
7430 * When an error is encountered then eap->errmsg is set.
7431 */
7432 static int
7433get_tabpage_arg(exarg_T *eap)
7434{
7435 int tab_number;
7436 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7437
7438 if (eap->arg && *eap->arg != NUL)
7439 {
7440 char_u *p = eap->arg;
7441 char_u *p_save;
7442 int relative = 0; /* argument +N/-N means: go to N places to the
7443 * right/left relative to the current position. */
7444
7445 if (*p == '-')
7446 {
7447 relative = -1;
7448 p++;
7449 }
7450 else if (*p == '+')
7451 {
7452 relative = 1;
7453 p++;
7454 }
7455
7456 p_save = p;
7457 tab_number = getdigits(&p);
7458
7459 if (relative == 0)
7460 {
7461 if (STRCMP(p, "$") == 0)
7462 tab_number = LAST_TAB_NR;
7463 else if (p == p_save || *p_save == '-' || *p != NUL
7464 || tab_number > LAST_TAB_NR)
7465 {
7466 /* No numbers as argument. */
7467 eap->errmsg = e_invarg;
7468 goto theend;
7469 }
7470 }
7471 else
7472 {
7473 if (*p_save == NUL)
7474 tab_number = 1;
7475 else if (p == p_save || *p_save == '-' || *p != NUL
7476 || tab_number == 0)
7477 {
7478 /* No numbers as argument. */
7479 eap->errmsg = e_invarg;
7480 goto theend;
7481 }
7482 tab_number = tab_number * relative + tabpage_index(curtab);
7483 if (!unaccept_arg0 && relative == -1)
7484 --tab_number;
7485 }
7486 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7487 eap->errmsg = e_invarg;
7488 }
7489 else if (eap->addr_count > 0)
7490 {
7491 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007492 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007493 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007494 tab_number = 0;
7495 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007496 else
7497 {
7498 tab_number = eap->line2;
7499 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7500 {
7501 --tab_number;
7502 if (tab_number < unaccept_arg0)
7503 eap->errmsg = e_invarg;
7504 }
7505 }
7506 }
7507 else
7508 {
7509 switch (eap->cmdidx)
7510 {
7511 case CMD_tabnext:
7512 tab_number = tabpage_index(curtab) + 1;
7513 if (tab_number > LAST_TAB_NR)
7514 tab_number = 1;
7515 break;
7516 case CMD_tabmove:
7517 tab_number = LAST_TAB_NR;
7518 break;
7519 default:
7520 tab_number = tabpage_index(curtab);
7521 }
7522 }
7523
7524theend:
7525 return tab_number;
7526}
7527
7528/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007529 * ":tabclose": close current tab page, unless it is the last one.
7530 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 */
7532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007533ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007535 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007536 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007537
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007538# ifdef FEAT_CMDWIN
7539 if (cmdwin_type != 0)
7540 cmdwin_result = K_IGNORE;
7541 else
7542# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007543 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007544 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007545 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007547 tab_number = get_tabpage_arg(eap);
7548 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007549 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007550 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007551 if (tp == NULL)
7552 {
7553 beep_flush();
7554 return;
7555 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007556 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007557 {
7558 tabpage_close_other(tp, eap->forceit);
7559 return;
7560 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007561 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007562#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007563 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007564#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007565 )
7566 tabpage_close(eap->forceit);
7567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007569}
7570
7571/*
7572 * ":tabonly": close all tab pages except the current one
7573 */
7574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007575ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007576{
7577 tabpage_T *tp;
7578 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007579 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007580
7581# ifdef FEAT_CMDWIN
7582 if (cmdwin_type != 0)
7583 cmdwin_result = K_IGNORE;
7584 else
7585# endif
7586 if (first_tabpage->tp_next == NULL)
7587 MSG(_("Already only one tab page"));
7588 else
7589 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007590 tab_number = get_tabpage_arg(eap);
7591 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007592 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007593 goto_tabpage(tab_number);
7594 /* Repeat this up to a 1000 times, because autocommands may
7595 * mess up the lists. */
7596 for (done = 0; done < 1000; ++done)
7597 {
7598 FOR_ALL_TABPAGES(tp)
7599 if (tp->tp_topframe != topframe)
7600 {
7601 tabpage_close_other(tp, eap->forceit);
7602 /* if we failed to close it quit */
7603 if (valid_tabpage(tp))
7604 done = 1000;
7605 /* start over, "tp" is now invalid */
7606 break;
7607 }
7608 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007609 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007610 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007611 }
7612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614
7615/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007616 * Close the current tab page.
7617 */
7618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007619tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007620{
7621 /* First close all the windows but the current one. If that worked then
7622 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007623 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007624 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007625 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007626 ex_win_close(forceit, curwin, NULL);
7627# ifdef FEAT_GUI
7628 need_mouse_correct = TRUE;
7629# endif
7630}
7631
7632/*
7633 * Close tab page "tp", which is not the current tab page.
7634 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007635 * Also takes care of the tab pages line disappearing when closing the
7636 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637 */
7638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007639tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007640{
7641 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007642 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007643 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007644
7645 /* Limit to 1000 windows, autocommands may add a window while we close
7646 * one. OK, so I'm paranoid... */
7647 while (++done < 1000)
7648 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007649 wp = tp->tp_firstwin;
7650 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007651
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007652 /* Autocommands may delete the tab page under our fingers and we may
7653 * fail to close a window with a modified buffer. */
7654 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007655 break;
7656 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007657
Bram Moolenaar29323592016-07-24 22:04:11 +02007658#ifdef FEAT_AUTOCMD
7659 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7660#endif
7661
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007663 if (h != tabline_height())
7664 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007665}
7666
7667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 * ":only".
7669 */
7670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007671ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007673 win_T *wp;
7674 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675# ifdef FEAT_GUI
7676 need_mouse_correct = TRUE;
7677# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007678 if (eap->addr_count > 0)
7679 {
7680 wnr = eap->line2;
7681 for (wp = firstwin; --wnr > 0; )
7682 {
7683 if (wp->w_next == NULL)
7684 break;
7685 else
7686 wp = wp->w_next;
7687 }
7688 win_goto(wp);
7689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 close_others(TRUE, eap->forceit);
7691}
7692
7693/*
7694 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007695 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 if (eap->addr_count == 0)
7701 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007702 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704
7705 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007706ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007708 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007709 if (!eap->skip)
7710 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007711#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007712 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007713#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007714 if (eap->addr_count == 0)
7715 win_close(curwin, FALSE); /* don't free buffer */
7716 else
7717 {
7718 int winnr = 0;
7719 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007720
Bram Moolenaar2256c992016-11-15 21:17:07 +01007721 FOR_ALL_WINDOWS(win)
7722 {
7723 winnr++;
7724 if (winnr == eap->line2)
7725 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007726 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007727 if (win == NULL)
7728 win = lastwin;
7729 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732}
7733
7734/*
7735 * ":stop" and ":suspend": Suspend Vim.
7736 */
7737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007738ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 /*
7741 * Disallow suspending for "rvim".
7742 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007743 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {
7745 if (!eap->forceit)
7746 autowrite_all();
7747 windgoto((int)Rows - 1, 0);
7748 out_char('\n');
7749 out_flush();
7750 stoptermcap();
7751 out_flush(); /* needed for SUN to restore xterm buffer */
7752#ifdef FEAT_TITLE
7753 mch_restore_title(3); /* restore window titles */
7754#endif
7755 ui_suspend(); /* call machine specific function */
7756#ifdef FEAT_TITLE
7757 maketitle();
7758 resettitle(); /* force updating the title */
7759#endif
7760 starttermcap();
7761 scroll_start(); /* scroll screen before redrawing */
7762 redraw_later_clear();
7763 shell_resized(); /* may have resized window */
7764 }
7765}
7766
7767/*
7768 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7769 */
7770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007771ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773#ifdef FEAT_CMDWIN
7774 if (cmdwin_type != 0)
7775 {
7776 cmdwin_result = Ctrl_C;
7777 return;
7778 }
7779#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007780 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007781 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007782 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007783 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007784 return;
7785 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007786#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007787 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7788 /* Refuse to quit when locked or when the buffer in the last window is
7789 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007790 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007791 return;
7792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 /*
7795 * if more files or windows we won't exit
7796 */
7797 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7798 exiting = TRUE;
7799 if ( ((eap->cmdidx == CMD_wq
7800 || curbufIsChanged())
7801 && do_write(eap) == FAIL)
7802 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007803 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {
7805 not_exiting();
7806 }
7807 else
7808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007811 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812# ifdef FEAT_GUI
7813 need_mouse_correct = TRUE;
7814# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007815 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007816 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818}
7819
7820/*
7821 * ":print", ":list", ":number".
7822 */
7823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007824ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007826 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7827 EMSG(_(e_emptybuf));
7828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007830 for ( ;!got_int; ui_breakcheck())
7831 {
7832 print_line(eap->line1,
7833 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7834 || (eap->flags & EXFLAG_NR)),
7835 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7836 if (++eap->line1 > eap->line2)
7837 break;
7838 out_flush(); /* show one line at a time */
7839 }
7840 setpcmark();
7841 /* put cursor at last line */
7842 curwin->w_cursor.lnum = eap->line2;
7843 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847}
7848
7849#ifdef FEAT_BYTEOFF
7850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007851ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 goto_byte(eap->line2);
7854}
7855#endif
7856
7857/*
7858 * ":shell".
7859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007861ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 do_shell(NULL, 0);
7864}
7865
Bram Moolenaar4033c552017-09-16 20:54:51 +02007866#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007868 || defined(FEAT_GUI_MSWIN) \
7869 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 || defined(PROTO)
7871
7872/*
7873 * Handle a file drop. The code is here because a drop is *nearly* like an
7874 * :args command, but not quite (we have a list of exact filenames, so we
7875 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7876 * code is very similar to :args and hence needs access to a lot of the static
7877 * functions in this file.
7878 *
7879 * The list should be allocated using alloc(), as should each item in the
7880 * list. This function takes over responsibility for freeing the list.
7881 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007882 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007883 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 */
7885 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007886handle_drop(
7887 int filec, /* the number of files dropped */
7888 char_u **filev, /* the list of files dropped */
7889 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 exarg_T ea;
7892 int save_msg_scroll = msg_scroll;
7893
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007894 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007895 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007897#ifdef FEAT_AUTOCMD
7898 if (curbuf_locked())
7899 return;
7900#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007901 /* When the screen is being updated we should not change buffers and
7902 * windows structures, it may cause freed memory to be used. */
7903 if (updating_screen)
7904 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
7906 /* Check whether the current buffer is changed. If so, we will need
7907 * to split the current window or data could be lost.
7908 * We don't need to check if the 'hidden' option is set, as in this
7909 * case the buffer won't be lost.
7910 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007911 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
7913 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007914 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 --emsg_off;
7916 }
7917 if (split)
7918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 if (win_split(0, 0) == FAIL)
7920 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007921 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923 /* When splitting the window, create a new alist. Otherwise the
7924 * existing one is overwritten. */
7925 alist_unlink(curwin->w_alist);
7926 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928
7929 /*
7930 * Set up the new argument list.
7931 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007932 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
7934 /*
7935 * Move to the first file.
7936 */
7937 /* Fake up a minimal "next" command for do_argfile() */
7938 vim_memset(&ea, 0, sizeof(ea));
7939 ea.cmd = (char_u *)"next";
7940 do_argfile(&ea, 0);
7941
7942 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7943 * mode that is not needed here. */
7944 need_start_insertmode = FALSE;
7945
7946 /* Restore msg_scroll, otherwise a following command may cause scrolling
7947 * unexpectedly. The screen will be redrawn by the caller, thus
7948 * msg_scroll being set by displaying a message is irrelevant. */
7949 msg_scroll = save_msg_scroll;
7950}
7951#endif
7952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953/*
7954 * Clear an argument list: free all file names and reset it to zero entries.
7955 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007956 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007957alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959 while (--al->al_ga.ga_len >= 0)
7960 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7961 ga_clear(&al->al_ga);
7962}
7963
7964/*
7965 * Init an argument list.
7966 */
7967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7971}
7972
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973/*
7974 * Remove a reference from an argument list.
7975 * Ignored when the argument list is the global one.
7976 * If the argument list is no longer used by any window, free it.
7977 */
7978 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007979alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 if (al != &global_alist && --al->al_refcount <= 0)
7982 {
7983 alist_clear(al);
7984 vim_free(al);
7985 }
7986}
7987
Bram Moolenaar4033c552017-09-16 20:54:51 +02007988#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989/*
7990 * Create a new argument list and use it for the current window.
7991 */
7992 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007993alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7996 if (curwin->w_alist == NULL)
7997 {
7998 curwin->w_alist = &global_alist;
7999 ++global_alist.al_refcount;
8000 }
8001 else
8002 {
8003 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008004 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 alist_init(curwin->w_alist);
8006 }
8007}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008#endif
8009
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008010#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011/*
8012 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008013 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8014 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 */
8016 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008017alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008020 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 char_u **new_arg_files;
8022 int new_arg_file_count;
8023 char_u *save_p_su = p_su;
8024 int i;
8025
8026 /* Don't use 'suffixes' here. This should work like the shell did the
8027 * expansion. Also, the vimrc file isn't read yet, thus the user
8028 * can't set the options. */
8029 p_su = empty_option;
8030 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8031 if (old_arg_files != NULL)
8032 {
8033 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008034 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8035 old_arg_count = GARGCOUNT;
8036 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008038 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 && new_arg_file_count > 0)
8040 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008041 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8042 TRUE, fnum_list, fnum_len);
8043 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 }
8046 p_su = save_p_su;
8047}
8048#endif
8049
8050/*
8051 * Set the argument list for the current window.
8052 * Takes over the allocated files[] and the allocated fnames in it.
8053 */
8054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055alist_set(
8056 alist_T *al,
8057 int count,
8058 char_u **files,
8059 int use_curbuf,
8060 int *fnum_list,
8061 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008064 static int recursive = 0;
8065
8066 if (recursive)
8067 {
8068#ifdef FEAT_AUTOCMD
8069 EMSG(_(e_au_recursive));
8070#endif
8071 return;
8072 }
8073 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074
8075 alist_clear(al);
8076 if (ga_grow(&al->al_ga, count) == OK)
8077 {
8078 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008079 {
8080 if (got_int)
8081 {
8082 /* When adding many buffers this can take a long time. Allow
8083 * interrupting here. */
8084 while (i < count)
8085 vim_free(files[i++]);
8086 break;
8087 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008088
8089 /* May set buffer name of a buffer previously used for the
8090 * argument list, so that it's re-used by alist_add. */
8091 if (fnum_list != NULL && i < fnum_len)
8092 buf_set_name(fnum_list[i], files[i]);
8093
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008095 ui_breakcheck();
8096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 vim_free(files);
8098 }
8099 else
8100 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008103
8104 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105}
8106
8107/*
8108 * Add file "fname" to argument list "al".
8109 * "fname" must have been allocated and "al" must have been checked for room.
8110 */
8111 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008112alist_add(
8113 alist_T *al,
8114 char_u *fname,
8115 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 if (fname == NULL) /* don't add NULL file names */
8118 return;
8119#ifdef BACKSLASH_IN_FILENAME
8120 slash_adjust(fname);
8121#endif
8122 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8123 if (set_fnum > 0)
8124 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8125 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8126 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127}
8128
8129#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8130/*
8131 * Adjust slashes in file names. Called after 'shellslash' was set.
8132 */
8133 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008134alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008138 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139
8140 for (i = 0; i < GARGCOUNT; ++i)
8141 if (GARGLIST[i].ae_fname != NULL)
8142 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008143 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 if (wp->w_alist != &global_alist)
8145 for (i = 0; i < WARGCOUNT(wp); ++i)
8146 if (WARGLIST(wp)[i].ae_fname != NULL)
8147 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148}
8149#endif
8150
8151/*
8152 * ":preserve".
8153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008155ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008157 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 ml_preserve(curbuf, TRUE);
8159}
8160
8161/*
8162 * ":recover".
8163 */
8164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008165ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8168 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008169 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8170 | CCGD_MULTWIN
8171 | (eap->forceit ? CCGD_FORCEIT : 0)
8172 | CCGD_EXCMD)
8173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 && (*eap->arg == NUL
8175 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8176 ml_recover();
8177 recoverymode = FALSE;
8178}
8179
8180/*
8181 * Command modifier used in a wrong way.
8182 */
8183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008184ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 eap->errmsg = e_invcmd;
8187}
8188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189/*
8190 * :sview [+command] file split window with new file, read-only
8191 * :split [[+command] file] split window with current or new file
8192 * :vsplit [[+command] file] split window vertically with current or new file
8193 * :new [[+command] file] split window with no or new file
8194 * :vnew [[+command] file] split vertically window with no or new file
8195 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008196 *
8197 * :tabedit open new Tab page with empty window
8198 * :tabedit [+command] file open new Tab page and edit "file"
8199 * :tabnew [[+command] file] just like :tabedit
8200 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 */
8202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008203ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008205 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008206#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008208#endif
8209#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
Bram Moolenaar4033c552017-09-16 20:54:51 +02008213#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
Bram Moolenaar4033c552017-09-16 20:54:51 +02008217#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008219 * quickfix windows. But it's OK when doing ":tab split". */
8220 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {
8222 if (eap->cmdidx == CMD_split)
8223 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 if (eap->cmdidx == CMD_vsplit)
8225 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228
Bram Moolenaar4033c552017-09-16 20:54:51 +02008229#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008230 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
8232 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8233 FNAME_MESS, TRUE, curbuf->b_ffname);
8234 if (fname == NULL)
8235 goto theend;
8236 eap->arg = fname;
8237 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008238# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008239 else
8240# endif
8241#endif
8242#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 && eap->cmdidx != CMD_new)
8246 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008247# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008248 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008249# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008250 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008251# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008252 au_has_group((char_u *)"FileExplorer"))
8253 {
8254 /* No browsing supported but we do have the file explorer:
8255 * Edit the directory. */
8256 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8257 eap->arg = (char_u *)".";
8258 }
8259 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008260# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008261 {
8262 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008264 if (fname == NULL)
8265 goto theend;
8266 eap->arg = fname;
8267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 }
8269 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008272 /*
8273 * Either open new tab page or split the window.
8274 */
8275 if (eap->cmdidx == CMD_tabedit
8276 || eap->cmdidx == CMD_tabfind
8277 || eap->cmdidx == CMD_tabnew)
8278 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008279 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8280 : eap->addr_count == 0 ? 0
8281 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008282 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008283 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008284
8285 /* set the alternate buffer for the window we came from */
8286 if (curwin != old_curwin
8287 && win_valid(old_curwin)
8288 && old_curwin->w_buffer != curbuf
8289 && !cmdmod.keepalt)
8290 old_curwin->w_alt_fnum = curbuf->b_fnum;
8291 }
8292 }
8293 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8295 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008296# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 /* Reset 'scrollbind' when editing another file, but keep it when
8298 * doing ":split" without arguments. */
8299 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008300# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008304 {
8305 RESET_BINDING(curwin);
8306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 else
8308 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 do_exedit(eap, old_curwin);
8311 }
8312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008313# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008317# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318theend:
8319 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322
8323/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008324 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008325 */
8326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008327tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008328{
8329 exarg_T ea;
8330
8331 vim_memset(&ea, 0, sizeof(ea));
8332 ea.cmdidx = CMD_tabnew;
8333 ea.cmd = (char_u *)"tabn";
8334 ea.arg = (char_u *)"";
8335 ex_splitview(&ea);
8336}
8337
8338/*
8339 * :tabnext command
8340 */
8341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008342ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008343{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008344 int tab_number;
8345
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008346 switch (eap->cmdidx)
8347 {
8348 case CMD_tabfirst:
8349 case CMD_tabrewind:
8350 goto_tabpage(1);
8351 break;
8352 case CMD_tablast:
8353 goto_tabpage(9999);
8354 break;
8355 case CMD_tabprevious:
8356 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008357 if (eap->arg && *eap->arg != NUL)
8358 {
8359 char_u *p = eap->arg;
8360 char_u *p_save = p;
8361
8362 tab_number = getdigits(&p);
8363 if (p == p_save || *p_save == '-' || *p != NUL
8364 || tab_number == 0)
8365 {
8366 /* No numbers as argument. */
8367 eap->errmsg = e_invarg;
8368 return;
8369 }
8370 }
8371 else
8372 {
8373 if (eap->addr_count == 0)
8374 tab_number = 1;
8375 else
8376 {
8377 tab_number = eap->line2;
8378 if (tab_number < 1)
8379 {
8380 eap->errmsg = e_invrange;
8381 return;
8382 }
8383 }
8384 }
8385 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008386 break;
8387 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008388 tab_number = get_tabpage_arg(eap);
8389 if (eap->errmsg == NULL)
8390 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008391 break;
8392 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008393}
8394
8395/*
8396 * :tabmove command
8397 */
8398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008399ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008400{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008401 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008402
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008403 tab_number = get_tabpage_arg(eap);
8404 if (eap->errmsg == NULL)
8405 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008406}
8407
8408/*
8409 * :tabs command: List tabs and their contents.
8410 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008412ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413{
8414 tabpage_T *tp;
8415 win_T *wp;
8416 int tabcount = 1;
8417
8418 msg_start();
8419 msg_scroll = TRUE;
8420 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8421 {
8422 msg_putchar('\n');
8423 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008424 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008425 out_flush(); /* output one line at a time */
8426 ui_breakcheck();
8427
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008428 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008429 wp = firstwin;
8430 else
8431 wp = tp->tp_firstwin;
8432 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8433 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008434 msg_putchar('\n');
8435 msg_putchar(wp == curwin ? '>' : ' ');
8436 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008437 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8438 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008439 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008440 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008441 else
8442 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8443 IObuff, IOSIZE, TRUE);
8444 msg_outtrans(IObuff);
8445 out_flush(); /* output one line at a time */
8446 ui_breakcheck();
8447 }
8448 }
8449}
8450
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451/*
8452 * ":mode": Set screen mode.
8453 * If no argument given, just get the screen size and redraw.
8454 */
8455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008456ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
8458 if (*eap->arg == NUL)
8459 shell_resized();
8460 else
8461 mch_screenmode(eap->arg);
8462}
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464/*
8465 * ":resize".
8466 * set, increment or decrement current window height
8467 */
8468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008469ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 int n;
8472 win_T *wp = curwin;
8473
8474 if (eap->addr_count > 0)
8475 {
8476 n = eap->line2;
8477 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8478 ;
8479 }
8480
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008481# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (cmdmod.split & WSP_VERT)
8486 {
8487 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008488 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8490 n = 9999;
8491 win_setwidth_win((int)n, wp);
8492 }
8493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {
8495 if (*eap->arg == '-' || *eap->arg == '+')
8496 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008497 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 n = 9999;
8499 win_setheight_win((int)n, wp);
8500 }
8501}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502
8503/*
8504 * ":find [+command] <file>" command.
8505 */
8506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008507ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509#ifdef FEAT_SEARCHPATH
8510 char_u *fname;
8511 int count;
8512
8513 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8514 TRUE, curbuf->b_ffname);
8515 if (eap->addr_count > 0)
8516 {
8517 /* Repeat finding the file "count" times. This matters when it
8518 * appears several times in the path. */
8519 count = eap->line2;
8520 while (fname != NULL && --count > 0)
8521 {
8522 vim_free(fname);
8523 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8524 FALSE, curbuf->b_ffname);
8525 }
8526 }
8527
8528 if (fname != NULL)
8529 {
8530 eap->arg = fname;
8531#endif
8532 do_exedit(eap, NULL);
8533#ifdef FEAT_SEARCHPATH
8534 vim_free(fname);
8535 }
8536#endif
8537}
8538
8539/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008540 * ":open" simulation: for now just work like ":visual".
8541 */
8542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008543ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008544{
8545 regmatch_T regmatch;
8546 char_u *p;
8547
8548 curwin->w_cursor.lnum = eap->line2;
8549 beginline(BL_SOL | BL_FIX);
8550 if (*eap->arg == '/')
8551 {
8552 /* ":open /pattern/": put cursor in column found with pattern */
8553 ++eap->arg;
8554 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8555 *p = NUL;
8556 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8557 if (regmatch.regprog != NULL)
8558 {
8559 regmatch.rm_ic = p_ic;
8560 p = ml_get_curline();
8561 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008562 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008563 else
8564 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008565 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008566 }
8567 /* Move to the NUL, ignore any other arguments. */
8568 eap->arg += STRLEN(eap->arg);
8569 }
8570 check_cursor();
8571
8572 eap->cmdidx = CMD_visual;
8573 do_exedit(eap, NULL);
8574}
8575
8576/*
8577 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 */
8579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008580ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 do_exedit(eap, NULL);
8583}
8584
8585/*
8586 * ":edit <file>" command and alikes.
8587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008589do_exedit(
8590 exarg_T *eap,
8591 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008595 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596
8597 /*
8598 * ":vi" command ends Ex mode.
8599 */
8600 if (exmode_active && (eap->cmdidx == CMD_visual
8601 || eap->cmdidx == CMD_view))
8602 {
8603 exmode_active = FALSE;
8604 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008605 {
8606 /* Special case: ":global/pat/visual\NLvi-commands" */
8607 if (global_busy)
8608 {
8609 int rd = RedrawingDisabled;
8610 int nwr = no_wait_return;
8611 int ms = msg_scroll;
8612#ifdef FEAT_GUI
8613 int he = hold_gui_events;
8614#endif
8615
8616 if (eap->nextcmd != NULL)
8617 {
8618 stuffReadbuff(eap->nextcmd);
8619 eap->nextcmd = NULL;
8620 }
8621
8622 if (exmode_was != EXMODE_VIM)
8623 settmode(TMODE_RAW);
8624 RedrawingDisabled = 0;
8625 no_wait_return = 0;
8626 need_wait_return = FALSE;
8627 msg_scroll = 0;
8628#ifdef FEAT_GUI
8629 hold_gui_events = 0;
8630#endif
8631 must_redraw = CLEAR;
8632
8633 main_loop(FALSE, TRUE);
8634
8635 RedrawingDisabled = rd;
8636 no_wait_return = nwr;
8637 msg_scroll = ms;
8638#ifdef FEAT_GUI
8639 hold_gui_events = he;
8640#endif
8641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645
8646 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008647 || eap->cmdidx == CMD_tabnew
8648 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008649 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008651 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 setpcmark();
8653 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008654 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8655 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008657 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 || *eap->arg != NUL
8659#ifdef FEAT_BROWSE
8660 || cmdmod.browse
8661#endif
8662 )
8663 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008664#ifdef FEAT_AUTOCMD
8665 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8666 * can bring us here, others are stopped earlier. */
8667 if (*eap->arg != NUL && curbuf_locked())
8668 return;
8669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 n = readonlymode;
8671 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8672 readonlymode = TRUE;
8673 else if (eap->cmdidx == CMD_enew)
8674 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8675 empty buffer */
8676 setpcmark();
8677 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8678 NULL, eap,
8679 /* ":edit" goes to first line if Vi compatible */
8680 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8681 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8682 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008683 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008685 /* after a split we can use an existing buffer */
8686 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687#ifdef FEAT_LISTCMDS
8688 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8689#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008690 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
8692 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 if (old_curwin != NULL)
8694 {
8695 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008696 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02008698#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008699 cleanup_T cs;
8700
8701 /* Reset the error/interrupt/exception state here so that
8702 * aborting() returns FALSE when closing a window. */
8703 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008704#endif
8705#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008707#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008708 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008709
Bram Moolenaar4033c552017-09-16 20:54:51 +02008710#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008711 /* Restore the error/interrupt/exception state if not
8712 * discarded by a new aborting error, interrupt, or
8713 * uncaught exception. */
8714 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 }
8717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
8719 else if (readonlymode && curbuf->b_nwindows == 1)
8720 {
8721 /* When editing an already visited buffer, 'readonly' won't be set
8722 * but the previous value is kept. With ":view" and ":sview" we
8723 * want the file to be readonly, except when another window is
8724 * editing the same buffer. */
8725 curbuf->b_p_ro = TRUE;
8726 }
8727 readonlymode = n;
8728 }
8729 else
8730 {
8731 if (eap->do_ecmd_cmd != NULL)
8732 do_cmdline_cmd(eap->do_ecmd_cmd);
8733#ifdef FEAT_TITLE
8734 n = curwin->w_arg_idx_invalid;
8735#endif
8736 check_arg_idx(curwin);
8737#ifdef FEAT_TITLE
8738 if (n != curwin->w_arg_idx_invalid)
8739 maketitle();
8740#endif
8741 }
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 /*
8744 * if ":split file" worked, set alternate file name in old window to new
8745 * file
8746 */
8747 if (old_curwin != NULL
8748 && *eap->arg != NUL
8749 && curwin != old_curwin
8750 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008751 && old_curwin->w_buffer != curbuf
8752 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754
8755 ex_no_reprint = TRUE;
8756}
8757
8758#ifndef FEAT_GUI
8759/*
8760 * ":gui" and ":gvim" when there is no GUI.
8761 */
8762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008763ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 eap->errmsg = e_nogvim;
8766}
8767#endif
8768
8769#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008771ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 gui_make_tearoff(eap->arg);
8774}
8775#endif
8776
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008777#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008779ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008781 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782}
8783#endif
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008786ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8789 MSG(_("No swap file"));
8790 else
8791 msg(curbuf->b_ml.ml_mfp->mf_fname);
8792}
8793
8794/*
8795 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8796 * offset.
8797 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008800ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802#ifdef FEAT_SCROLLBIND
8803 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008804 win_T *save_curwin = curwin;
8805 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 long topline;
8807 long y;
8808 linenr_T old_linenr = curwin->w_cursor.lnum;
8809
8810 setpcmark();
8811
8812 /*
8813 * determine max topline
8814 */
8815 if (curwin->w_p_scb)
8816 {
8817 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008818 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
8820 if (wp->w_p_scb && wp->w_buffer)
8821 {
8822 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8823 if (topline > y)
8824 topline = y;
8825 }
8826 }
8827 if (topline < 1)
8828 topline = 1;
8829 }
8830 else
8831 {
8832 topline = 1;
8833 }
8834
8835
8836 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008837 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008839 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
8841 if (curwin->w_p_scb)
8842 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008843 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 y = topline - curwin->w_topline;
8845 if (y > 0)
8846 scrollup(y, TRUE);
8847 else
8848 scrolldown(-y, TRUE);
8849 curwin->w_scbind_pos = topline;
8850 redraw_later(VALID);
8851 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 }
8854 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008855 curwin = save_curwin;
8856 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 if (curwin->w_p_scb)
8858 {
8859 did_syncbind = TRUE;
8860 checkpcmark();
8861 if (old_linenr != curwin->w_cursor.lnum)
8862 {
8863 char_u ctrl_o[2];
8864
8865 ctrl_o[0] = Ctrl_O;
8866 ctrl_o[1] = 0;
8867 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8868 }
8869 }
8870#endif
8871}
8872
8873
8874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008875ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008877 int i;
8878 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8879 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 if (eap->usefilter) /* :r!cmd */
8882 do_bang(1, eap, FALSE, FALSE, TRUE);
8883 else
8884 {
8885 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8886 return;
8887
8888#ifdef FEAT_BROWSE
8889 if (cmdmod.browse)
8890 {
8891 char_u *browseFile;
8892
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008893 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 NULL, NULL, NULL, curbuf);
8895 if (browseFile != NULL)
8896 {
8897 i = readfile(browseFile, NULL,
8898 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8899 vim_free(browseFile);
8900 }
8901 else
8902 i = OK;
8903 }
8904 else
8905#endif
8906 if (*eap->arg == NUL)
8907 {
8908 if (check_fname() == FAIL) /* check for no file name */
8909 return;
8910 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8911 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8912 }
8913 else
8914 {
8915 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8916 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8917 i = readfile(eap->arg, NULL,
8918 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8919
8920 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008921 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
8923#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8924 if (!aborting())
8925#endif
8926 EMSG2(_(e_notopen), eap->arg);
8927 }
8928 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008929 {
8930 if (empty && exmode_active)
8931 {
8932 /* Delete the empty line that remains. Historically ex does
8933 * this but vi doesn't. */
8934 if (eap->line2 == 0)
8935 lnum = curbuf->b_ml.ml_line_count;
8936 else
8937 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008938 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008939 {
8940 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008941 if (curwin->w_cursor.lnum > 1
8942 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008943 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008944 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008945 }
8946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 }
8950}
8951
Bram Moolenaarea408852005-06-25 22:49:46 +00008952static char_u *prev_dir = NULL;
8953
8954#if defined(EXITFREE) || defined(PROTO)
8955 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008956free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008957{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008958 VIM_CLEAR(prev_dir);
8959 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008960}
8961#endif
8962
Bram Moolenaarf4258302013-06-02 18:20:17 +02008963/*
8964 * Deal with the side effects of changing the current directory.
8965 * When "local" is TRUE then this was after an ":lcd" command.
8966 */
8967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008968post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008969{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008970 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008971 if (local)
8972 {
8973 /* If still in global directory, need to remember current
8974 * directory as global directory. */
8975 if (globaldir == NULL && prev_dir != NULL)
8976 globaldir = vim_strsave(prev_dir);
8977 /* Remember this local directory for the window. */
8978 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8979 curwin->w_localdir = vim_strsave(NameBuff);
8980 }
8981 else
8982 {
8983 /* We are now in the global directory, no need to remember its
8984 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008985 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008986 }
8987
8988 shorten_fnames(TRUE);
8989}
8990
Bram Moolenaarea408852005-06-25 22:49:46 +00008991
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992/*
8993 * ":cd", ":lcd", ":chdir" and ":lchdir".
8994 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008996ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 char_u *new_dir;
8999 char_u *tofree;
9000
9001 new_dir = eap->arg;
9002#if !defined(UNIX) && !defined(VMS)
9003 /* for non-UNIX ":cd" means: print current directory */
9004 if (*new_dir == NUL)
9005 ex_pwd(NULL);
9006 else
9007#endif
9008 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009009#ifdef FEAT_AUTOCMD
9010 if (allbuf_locked())
9011 return;
9012#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009013 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9014 && !eap->forceit)
9015 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009016 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009017 return;
9018 }
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 /* ":cd -": Change to previous directory */
9021 if (STRCMP(new_dir, "-") == 0)
9022 {
9023 if (prev_dir == NULL)
9024 {
9025 EMSG(_("E186: No previous directory"));
9026 return;
9027 }
9028 new_dir = prev_dir;
9029 }
9030
9031 /* Save current directory for next ":cd -" */
9032 tofree = prev_dir;
9033 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9034 prev_dir = vim_strsave(NameBuff);
9035 else
9036 prev_dir = NULL;
9037
9038#if defined(UNIX) || defined(VMS)
9039 /* for UNIX ":cd" means: go to home directory */
9040 if (*new_dir == NUL)
9041 {
9042 /* use NameBuff for home directory name */
9043# ifdef VMS
9044 char_u *p;
9045
9046 p = mch_getenv((char_u *)"SYS$LOGIN");
9047 if (p == NULL || *p == NUL) /* empty is the same as not set */
9048 NameBuff[0] = NUL;
9049 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009050 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051# else
9052 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9053# endif
9054 new_dir = NameBuff;
9055 }
9056#endif
9057 if (new_dir == NULL || vim_chdir(new_dir))
9058 EMSG(_(e_failed));
9059 else
9060 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009061 int is_local_chdir = eap->cmdidx == CMD_lcd
9062 || eap->cmdidx == CMD_lchdir;
9063
9064 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
9066 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009067 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009069#ifdef FEAT_AUTOCMD
9070 apply_autocmds(EVENT_DIRCHANGED,
9071 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9072 new_dir, FALSE, curbuf);
9073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 }
9075 vim_free(tofree);
9076 }
9077}
9078
9079/*
9080 * ":pwd".
9081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009083ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
9085 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9086 {
9087#ifdef BACKSLASH_IN_FILENAME
9088 slash_adjust(NameBuff);
9089#endif
9090 msg(NameBuff);
9091 }
9092 else
9093 EMSG(_("E187: Unknown"));
9094}
9095
9096/*
9097 * ":=".
9098 */
9099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009100ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009103 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
9105
9106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009109 int n;
9110 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111
9112 if (cursor_valid())
9113 {
9114 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9115 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009116 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009118
9119 len = eap->line2;
9120 switch (*eap->arg)
9121 {
9122 case 'm': break;
9123 case NUL: len *= 1000L; break;
9124 default: EMSG2(_(e_invarg2), eap->arg); return;
9125 }
9126 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127}
9128
9129/*
9130 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9131 */
9132 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009133do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009136 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
9138 cursor_on();
9139 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009140 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009142 wait_now = msec - done > 1000L ? 1000L : msec - done;
9143#ifdef FEAT_TIMERS
9144 {
9145 long due_time = check_due_timer();
9146
9147 if (due_time > 0 && due_time < wait_now)
9148 wait_now = due_time;
9149 }
9150#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009151#ifdef FEAT_JOB_CHANNEL
9152 if (has_any_channel() && wait_now > 100L)
9153 wait_now = 100L;
9154#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009155 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009156#ifdef FEAT_JOB_CHANNEL
9157 if (has_any_channel())
9158 ui_breakcheck_force(TRUE);
9159 else
9160#endif
9161 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009162#ifdef MESSAGE_QUEUE
9163 /* Process the netbeans and clientserver messages that may have been
9164 * received in the call to ui_breakcheck() when the GUI is in use. This
9165 * may occur when running a test case. */
9166 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169}
9170
9171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009172do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
9174 int mode;
9175 char_u *cmdp;
9176
9177 cmdp = eap->cmd;
9178 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9179
9180 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9181 eap->arg, mode, isabbrev))
9182 {
9183 case 1: EMSG(_(e_invarg));
9184 break;
9185 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9186 break;
9187 }
9188}
9189
9190/*
9191 * ":winsize" command (obsolete).
9192 */
9193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009194ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 int w, h;
9197 char_u *arg = eap->arg;
9198 char_u *p;
9199
9200 w = getdigits(&arg);
9201 arg = skipwhite(arg);
9202 p = arg;
9203 h = getdigits(&arg);
9204 if (*p != NUL && *arg == NUL)
9205 set_shellsize(w, h, TRUE);
9206 else
9207 EMSG(_("E465: :winsize requires two number arguments"));
9208}
9209
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009211ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213 int xchar = NUL;
9214 char_u *p;
9215
9216 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9217 {
9218 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9219 if (eap->arg[1] == NUL)
9220 {
9221 EMSG(_(e_invarg));
9222 return;
9223 }
9224 xchar = eap->arg[1];
9225 p = eap->arg + 2;
9226 }
9227 else
9228 p = eap->arg + 1;
9229
9230 eap->nextcmd = check_nextcmd(p);
9231 p = skipwhite(p);
9232 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9233 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009234 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 {
9236 /* Pass flags on for ":vertical wincmd ]". */
9237 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009238 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9240 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009241 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 }
9243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
Bram Moolenaar843ee412004-06-30 16:16:41 +00009245#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246/*
9247 * ":winpos".
9248 */
9249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009250ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251{
9252 int x, y;
9253 char_u *arg = eap->arg;
9254 char_u *p;
9255
9256 if (*arg == NUL)
9257 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009258# if defined(FEAT_GUI) || defined(MSWIN)
9259# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009261# else
9262 if (mch_get_winpos(&x, &y) != FAIL)
9263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 {
9265 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9266 msg(IObuff);
9267 }
9268 else
9269# endif
9270 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9271 }
9272 else
9273 {
9274 x = getdigits(&arg);
9275 arg = skipwhite(arg);
9276 p = arg;
9277 y = getdigits(&arg);
9278 if (*p == NUL || *arg != NUL)
9279 {
9280 EMSG(_("E466: :winpos requires two number arguments"));
9281 return;
9282 }
9283# ifdef FEAT_GUI
9284 if (gui.in_use)
9285 gui_mch_set_winpos(x, y);
9286 else if (gui.starting)
9287 {
9288 /* Remember the coordinates for when the window is opened. */
9289 gui_win_x = x;
9290 gui_win_y = y;
9291 }
9292# ifdef HAVE_TGETENT
9293 else
9294# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009295# else
9296# ifdef MSWIN
9297 mch_set_winpos(x, y);
9298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299# endif
9300# ifdef HAVE_TGETENT
9301 if (*T_CWP)
9302 term_set_winpos(x, y);
9303# endif
9304 }
9305}
9306#endif
9307
9308/*
9309 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9310 */
9311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009312ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 oparg_T oa;
9315
9316 clear_oparg(&oa);
9317 oa.regname = eap->regname;
9318 oa.start.lnum = eap->line1;
9319 oa.end.lnum = eap->line2;
9320 oa.line_count = eap->line2 - eap->line1 + 1;
9321 oa.motion_type = MLINE;
9322#ifdef FEAT_VIRTUALEDIT
9323 virtual_op = FALSE;
9324#endif
9325 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9326 {
9327 setpcmark();
9328 curwin->w_cursor.lnum = eap->line1;
9329 beginline(BL_SOL | BL_FIX);
9330 }
9331
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009332 if (VIsual_active)
9333 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009334
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 switch (eap->cmdidx)
9336 {
9337 case CMD_delete:
9338 oa.op_type = OP_DELETE;
9339 op_delete(&oa);
9340 break;
9341
9342 case CMD_yank:
9343 oa.op_type = OP_YANK;
9344 (void)op_yank(&oa, FALSE, TRUE);
9345 break;
9346
9347 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009348 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009350 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9351#else
9352 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009354 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 oa.op_type = OP_RSHIFT;
9356 else
9357 oa.op_type = OP_LSHIFT;
9358 op_shift(&oa, FALSE, eap->amount);
9359 break;
9360 }
9361#ifdef FEAT_VIRTUALEDIT
9362 virtual_op = MAYBE;
9363#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009364 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365}
9366
9367/*
9368 * ":put".
9369 */
9370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009371ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372{
9373 /* ":0put" works like ":1put!". */
9374 if (eap->line2 == 0)
9375 {
9376 eap->line2 = 1;
9377 eap->forceit = TRUE;
9378 }
9379 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009380 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9381 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382}
9383
9384/*
9385 * Handle ":copy" and ":move".
9386 */
9387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009388ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 long n;
9391
Bram Moolenaarded27822017-01-02 14:27:34 +01009392 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (eap->arg == NULL) /* error detected */
9394 {
9395 eap->nextcmd = NULL;
9396 return;
9397 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009398 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
9400 /*
9401 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9402 */
9403 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9404 {
9405 EMSG(_(e_invaddr));
9406 return;
9407 }
9408
9409 if (eap->cmdidx == CMD_move)
9410 {
9411 if (do_move(eap->line1, eap->line2, n) == FAIL)
9412 return;
9413 }
9414 else
9415 ex_copy(eap->line1, eap->line2, n);
9416 u_clearline();
9417 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009418 ex_may_print(eap);
9419}
9420
9421/*
9422 * Print the current line if flags were given to the Ex command.
9423 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009424 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009425ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009426{
9427 if (eap->flags != 0)
9428 {
9429 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9430 (eap->flags & EXFLAG_LIST));
9431 ex_no_reprint = TRUE;
9432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433}
9434
9435/*
9436 * ":smagic" and ":snomagic".
9437 */
9438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009439ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
9441 int magic_save = p_magic;
9442
9443 p_magic = (eap->cmdidx == CMD_smagic);
9444 do_sub(eap);
9445 p_magic = magic_save;
9446}
9447
9448/*
9449 * ":join".
9450 */
9451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009452ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
9454 curwin->w_cursor.lnum = eap->line1;
9455 if (eap->line1 == eap->line2)
9456 {
9457 if (eap->addr_count >= 2) /* :2,2join does nothing */
9458 return;
9459 if (eap->line2 == curbuf->b_ml.ml_line_count)
9460 {
9461 beep_flush();
9462 return;
9463 }
9464 ++eap->line2;
9465 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009466 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009468 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471/*
9472 * ":[addr]@r" or ":[addr]*r": execute register
9473 */
9474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009475ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009478 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009481 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
9483#ifdef USE_ON_FLY_SCROLL
9484 dont_scroll = TRUE; /* disallow scrolling here */
9485#endif
9486
9487 /* get the register name. No name means to use the previous one */
9488 c = *eap->arg;
9489 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9490 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009491 /* Put the register in the typeahead buffer with the "silent" flag. */
9492 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9493 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 else
9498 {
9499 int save_efr = exec_from_reg;
9500
9501 exec_from_reg = TRUE;
9502
9503 /*
9504 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009505 * Continue until the stuff buffer is empty and all added characters
9506 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009508 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9510
9511 exec_from_reg = save_efr;
9512 }
9513}
9514
9515/*
9516 * ":!".
9517 */
9518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9522}
9523
9524/*
9525 * ":undo".
9526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009528ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009530 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009531 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009532 else
9533 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534}
9535
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009536#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009538ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009539{
9540 char_u hash[UNDO_HASH_SIZE];
9541
9542 u_compute_hash(hash);
9543 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9544}
9545
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009547ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009548{
9549 char_u hash[UNDO_HASH_SIZE];
9550
9551 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009552 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009553}
9554#endif
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556/*
9557 * ":redo".
9558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009560ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 u_redo(1);
9563}
9564
9565/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009566 * ":earlier" and ":later".
9567 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009569ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009570{
9571 long count = 0;
9572 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009573 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009574 char_u *p = eap->arg;
9575
9576 if (*p == NUL)
9577 count = 1;
9578 else if (isdigit(*p))
9579 {
9580 count = getdigits(&p);
9581 switch (*p)
9582 {
9583 case 's': ++p; sec = TRUE; break;
9584 case 'm': ++p; sec = TRUE; count *= 60; break;
9585 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009586 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9587 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009588 }
9589 }
9590
9591 if (*p != NUL)
9592 EMSG2(_(e_invarg2), eap->arg);
9593 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009594 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9595 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009596}
9597
9598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 * ":redir": start/stop redirection.
9600 */
9601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009602ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603{
9604 char *mode;
9605 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009606 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607
Bram Moolenaarba768492016-07-08 15:32:54 +02009608#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009609 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009610 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009611 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009612 return;
9613 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009614#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009615
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 if (STRICMP(eap->arg, "END") == 0)
9617 close_redir();
9618 else
9619 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009620 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009622 ++arg;
9623 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009625 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 mode = "a";
9627 }
9628 else
9629 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009630 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631
9632 close_redir();
9633
9634 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009635 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (fname == NULL)
9637 return;
9638#ifdef FEAT_BROWSE
9639 if (cmdmod.browse)
9640 {
9641 char_u *browseFile;
9642
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009643 browseFile = do_browse(BROWSE_SAVE,
9644 (char_u *)_("Save Redirection"),
9645 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (browseFile == NULL)
9647 return; /* operation cancelled */
9648 vim_free(fname);
9649 fname = browseFile;
9650 eap->forceit = TRUE; /* since dialog already asked */
9651 }
9652#endif
9653
9654 redir_fd = open_exfile(fname, eap->forceit, mode);
9655 vim_free(fname);
9656 }
9657#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009658 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 {
9660 /* redirect to a register a-z (resp. A-Z for appending) */
9661 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009662 ++arg;
9663 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009665 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009666 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009668 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009670 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009671 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009672 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009675 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009676 if (*arg == '>')
9677 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009678 /* Make register empty when not using @A-@Z and the
9679 * command is valid. */
9680 if (*arg == NUL && !isupper(redir_reg))
9681 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009683 }
9684 if (*arg != NUL)
9685 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009686 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009687 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009688 }
9689 }
9690 else if (*arg == '=' && arg[1] == '>')
9691 {
9692 int append;
9693
9694 /* redirect to a variable */
9695 close_redir();
9696 arg += 2;
9697
9698 if (*arg == '>')
9699 {
9700 ++arg;
9701 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 }
9703 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009704 append = FALSE;
9705
9706 if (var_redir_start(skipwhite(arg), append) == OK)
9707 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
9709#endif
9710
9711 /* TODO: redirect to a buffer */
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009714 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009716
9717 /* Make sure redirection is not off. Can happen for cmdline completion
9718 * that indirectly invokes a command to catch its output. */
9719 if (redir_fd != NULL
9720#ifdef FEAT_EVAL
9721 || redir_reg || redir_vname
9722#endif
9723 )
9724 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727/*
9728 * ":redraw": force redraw
9729 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009730 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009731ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 int r = RedrawingDisabled;
9734 int p = p_lz;
9735
9736 RedrawingDisabled = 0;
9737 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009738 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009740 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741#ifdef FEAT_TITLE
9742 if (need_maketitle)
9743 maketitle();
9744#endif
9745 RedrawingDisabled = r;
9746 p_lz = p;
9747
9748 /* Reset msg_didout, so that a message that's there is overwritten. */
9749 msg_didout = FALSE;
9750 msg_col = 0;
9751
Bram Moolenaar56667a52013-07-17 11:54:28 +02009752 /* No need to wait after an intentional redraw. */
9753 need_wait_return = FALSE;
9754
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 out_flush();
9756}
9757
9758/*
9759 * ":redrawstatus": force redraw of status line(s)
9760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009762ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 int r = RedrawingDisabled;
9765 int p = p_lz;
9766
9767 RedrawingDisabled = 0;
9768 p_lz = FALSE;
9769 if (eap->forceit)
9770 status_redraw_all();
9771 else
9772 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009773 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 RedrawingDisabled = r;
9775 p_lz = p;
9776 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
9779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009780close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782 if (redir_fd != NULL)
9783 {
9784 fclose(redir_fd);
9785 redir_fd = NULL;
9786 }
9787#ifdef FEAT_EVAL
9788 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009789 if (redir_vname)
9790 {
9791 var_redir_stop();
9792 redir_vname = 0;
9793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794#endif
9795}
9796
9797#if defined(FEAT_SESSION) && defined(USE_CRNL)
9798# define MKSESSION_NL
9799static int mksession_nl = FALSE; /* use NL only in put_eol() */
9800#endif
9801
9802/*
9803 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9804 */
9805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009806ex_mkrc(
9807 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809 FILE *fd;
9810 int failed = FALSE;
9811 char_u *fname;
9812#ifdef FEAT_BROWSE
9813 char_u *browseFile = NULL;
9814#endif
9815#ifdef FEAT_SESSION
9816 int view_session = FALSE;
9817 int using_vdir = FALSE; /* using 'viewdir'? */
9818 char_u *viewFile = NULL;
9819 unsigned *flagp;
9820#endif
9821
9822 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9823 {
9824#ifdef FEAT_SESSION
9825 view_session = TRUE;
9826#else
9827 ex_ni(eap);
9828 return;
9829#endif
9830 }
9831
9832#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009833 /* Use the short file name until ":lcd" is used. We also don't use the
9834 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009835 did_lcd = FALSE;
9836
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9838 if (eap->cmdidx == CMD_mkview
9839 && (*eap->arg == NUL
9840 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9841 {
9842 eap->forceit = TRUE;
9843 fname = get_view_file(*eap->arg);
9844 if (fname == NULL)
9845 return;
9846 viewFile = fname;
9847 using_vdir = TRUE;
9848 }
9849 else
9850#endif
9851 if (*eap->arg != NUL)
9852 fname = eap->arg;
9853 else if (eap->cmdidx == CMD_mkvimrc)
9854 fname = (char_u *)VIMRC_FILE;
9855#ifdef FEAT_SESSION
9856 else if (eap->cmdidx == CMD_mksession)
9857 fname = (char_u *)SESSION_FILE;
9858#endif
9859 else
9860 fname = (char_u *)EXRC_FILE;
9861
9862#ifdef FEAT_BROWSE
9863 if (cmdmod.browse)
9864 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009865 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866# ifdef FEAT_SESSION
9867 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9868 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9869# endif
9870 (char_u *)_("Save Setup"),
9871 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9872 if (browseFile == NULL)
9873 goto theend;
9874 fname = browseFile;
9875 eap->forceit = TRUE; /* since dialog already asked */
9876 }
9877#endif
9878
9879#if defined(FEAT_SESSION) && defined(vim_mkdir)
9880 /* When using 'viewdir' may have to create the directory. */
9881 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009882 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883#endif
9884
9885 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9886 if (fd != NULL)
9887 {
9888#ifdef FEAT_SESSION
9889 if (eap->cmdidx == CMD_mkview)
9890 flagp = &vop_flags;
9891 else
9892 flagp = &ssop_flags;
9893#endif
9894
9895#ifdef MKSESSION_NL
9896 /* "unix" in 'sessionoptions': use NL line separator */
9897 if (view_session && (*flagp & SSOP_UNIX))
9898 mksession_nl = TRUE;
9899#endif
9900
9901 /* Write the version command for :mkvimrc */
9902 if (eap->cmdidx == CMD_mkvimrc)
9903 (void)put_line(fd, "version 6.0");
9904
9905#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009906 if (eap->cmdidx == CMD_mksession)
9907 {
9908 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9909 failed = TRUE;
9910 }
9911
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 if (eap->cmdidx != CMD_mkview)
9913#endif
9914 {
9915 /* Write setting 'compatible' first, because it has side effects.
9916 * For that same reason only do it when needed. */
9917 if (p_cp)
9918 (void)put_line(fd, "if !&cp | set cp | endif");
9919 else
9920 (void)put_line(fd, "if &cp | set nocp | endif");
9921 }
9922
9923#ifdef FEAT_SESSION
9924 if (!view_session
9925 || (eap->cmdidx == CMD_mksession
9926 && (*flagp & SSOP_OPTIONS)))
9927#endif
9928 failed |= (makemap(fd, NULL) == FAIL
9929 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9930
9931#ifdef FEAT_SESSION
9932 if (!failed && view_session)
9933 {
9934 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9935 failed = TRUE;
9936 if (eap->cmdidx == CMD_mksession)
9937 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009938 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
Bram Moolenaard9462e32011-04-11 21:35:11 +02009940 dirnow = alloc(MAXPATHL);
9941 if (dirnow == NULL)
9942 failed = TRUE;
9943 else
9944 {
9945 /*
9946 * Change to session file's dir.
9947 */
9948 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009950 *dirnow = NUL;
9951 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9952 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009953 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009954 shorten_fnames(TRUE);
9955 }
9956 else if (*dirnow != NUL
9957 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9958 {
9959 if (mch_chdir((char *)globaldir) == 0)
9960 shorten_fnames(TRUE);
9961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962
Bram Moolenaard9462e32011-04-11 21:35:11 +02009963 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
Bram Moolenaard9462e32011-04-11 21:35:11 +02009965 /* restore original dir */
9966 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009968 {
9969 if (mch_chdir((char *)dirnow) != 0)
9970 EMSG(_(e_prev_dir));
9971 shorten_fnames(TRUE);
9972 }
9973 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 }
9975 }
9976 else
9977 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009978 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9979 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 }
9981 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9982 == FAIL)
9983 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009984 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9985 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009986 if (eap->cmdidx == CMD_mksession)
9987 {
9988 if (put_line(fd, "unlet SessionLoad") == FAIL)
9989 failed = TRUE;
9990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
9992#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009993 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9994 failed = TRUE;
9995
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 failed |= fclose(fd);
9997
9998 if (failed)
9999 EMSG(_(e_write));
10000#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10001 else if (eap->cmdidx == CMD_mksession)
10002 {
10003 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010004 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005
Bram Moolenaard9462e32011-04-11 21:35:11 +020010006 tbuf = alloc(MAXPATHL);
10007 if (tbuf != NULL)
10008 {
10009 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10010 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10011 vim_free(tbuf);
10012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 }
10014#endif
10015#ifdef MKSESSION_NL
10016 mksession_nl = FALSE;
10017#endif
10018 }
10019
10020#ifdef FEAT_BROWSE
10021theend:
10022 vim_free(browseFile);
10023#endif
10024#ifdef FEAT_SESSION
10025 vim_free(viewFile);
10026#endif
10027}
10028
Bram Moolenaardf177f62005-02-22 08:39:57 +000010029#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10030 || defined(PROTO)
10031 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010032vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010033{
10034 if (vim_mkdir(name, prot) != 0)
10035 {
10036 EMSG2(_("E739: Cannot create directory: %s"), name);
10037 return FAIL;
10038 }
10039 return OK;
10040}
10041#endif
10042
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043/*
10044 * Open a file for writing for an Ex command, with some checks.
10045 * Return file descriptor, or NULL on failure.
10046 */
10047 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010048open_exfile(
10049 char_u *fname,
10050 int forceit,
10051 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
10053 FILE *fd;
10054
10055#ifdef UNIX
10056 /* with Unix it is possible to open a directory */
10057 if (mch_isdir(fname))
10058 {
10059 EMSG2(_(e_isadir2), fname);
10060 return NULL;
10061 }
10062#endif
10063 if (!forceit && *mode != 'a' && vim_fexists(fname))
10064 {
10065 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10066 return NULL;
10067 }
10068
10069 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10070 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10071
10072 return fd;
10073}
10074
10075/*
10076 * ":mark" and ":k".
10077 */
10078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010079ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080{
10081 pos_T pos;
10082
10083 if (*eap->arg == NUL) /* No argument? */
10084 EMSG(_(e_argreq));
10085 else if (eap->arg[1] != NUL) /* more than one character? */
10086 EMSG(_(e_trailing));
10087 else
10088 {
10089 pos = curwin->w_cursor; /* save curwin->w_cursor */
10090 curwin->w_cursor.lnum = eap->line2;
10091 beginline(BL_WHITE | BL_FIX);
10092 if (setmark(*eap->arg) == FAIL) /* set mark */
10093 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10094 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10095 }
10096}
10097
10098/*
10099 * Update w_topline, w_leftcol and the cursor position.
10100 */
10101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010102update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104 check_cursor(); /* put cursor on valid line */
10105 update_topline();
10106 if (!curwin->w_p_wrap)
10107 validate_cursor();
10108 update_curswant();
10109}
10110
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010112 * Save the current State and go to Normal mode.
10113 * Return TRUE if the typeahead could be saved.
10114 */
10115 int
10116save_current_state(save_state_T *sst)
10117{
10118 sst->save_msg_scroll = msg_scroll;
10119 sst->save_restart_edit = restart_edit;
10120 sst->save_msg_didout = msg_didout;
10121 sst->save_State = State;
10122 sst->save_insertmode = p_im;
10123 sst->save_finish_op = finish_op;
10124 sst->save_opcount = opcount;
10125
10126 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10127 restart_edit = 0; /* don't go to Insert mode */
10128 p_im = FALSE; /* don't use 'insertmode' */
10129
10130 /*
10131 * Save the current typeahead. This is required to allow using ":normal"
10132 * from an event handler and makes sure we don't hang when the argument
10133 * ends with half a command.
10134 */
10135 save_typeahead(&sst->tabuf);
10136 return sst->tabuf.typebuf_valid;
10137}
10138
10139 void
10140restore_current_state(save_state_T *sst)
10141{
10142 /* Restore the previous typeahead. */
10143 restore_typeahead(&sst->tabuf);
10144
10145 msg_scroll = sst->save_msg_scroll;
10146 restart_edit = sst->save_restart_edit;
10147 p_im = sst->save_insertmode;
10148 finish_op = sst->save_finish_op;
10149 opcount = sst->save_opcount;
10150 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10151
10152 /* Restore the state (needed when called from a function executed for
10153 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10154 State = sst->save_State;
10155#ifdef CURSOR_SHAPE
10156 ui_cursor_shape(); /* may show different cursor shape */
10157#endif
10158}
10159
10160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 * ":normal[!] {commands}": Execute normal mode commands.
10162 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010163 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010164ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010166 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167#ifdef FEAT_MBYTE
10168 char_u *arg = NULL;
10169 int l;
10170 char_u *p;
10171#endif
10172
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010173 if (ex_normal_lock > 0)
10174 {
10175 EMSG(_(e_secure));
10176 return;
10177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 if (ex_normal_busy >= p_mmd)
10179 {
10180 EMSG(_("E192: Recursive use of :normal too deep"));
10181 return;
10182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183
10184#ifdef FEAT_MBYTE
10185 /*
10186 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10187 * this for the K_SPECIAL leading byte, otherwise special keys will not
10188 * work.
10189 */
10190 if (has_mbyte)
10191 {
10192 int len = 0;
10193
10194 /* Count the number of characters to be escaped. */
10195 for (p = eap->arg; *p != NUL; ++p)
10196 {
10197# ifdef FEAT_GUI
10198 if (*p == CSI) /* leadbyte CSI */
10199 len += 2;
10200# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010201 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10203# ifdef FEAT_GUI
10204 || *p == CSI
10205# endif
10206 )
10207 len += 2;
10208 }
10209 if (len > 0)
10210 {
10211 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10212 if (arg != NULL)
10213 {
10214 len = 0;
10215 for (p = eap->arg; *p != NUL; ++p)
10216 {
10217 arg[len++] = *p;
10218# ifdef FEAT_GUI
10219 if (*p == CSI)
10220 {
10221 arg[len++] = KS_EXTRA;
10222 arg[len++] = (int)KE_CSI;
10223 }
10224# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010225 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 {
10227 arg[len++] = *++p;
10228 if (*p == K_SPECIAL)
10229 {
10230 arg[len++] = KS_SPECIAL;
10231 arg[len++] = KE_FILLER;
10232 }
10233# ifdef FEAT_GUI
10234 else if (*p == CSI)
10235 {
10236 arg[len++] = KS_EXTRA;
10237 arg[len++] = (int)KE_CSI;
10238 }
10239# endif
10240 }
10241 arg[len] = NUL;
10242 }
10243 }
10244 }
10245 }
10246#endif
10247
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010248 ++ex_normal_busy;
10249 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 {
10251 /*
10252 * Repeat the :normal command for each line in the range. When no
10253 * range given, execute it just once, without positioning the cursor
10254 * first.
10255 */
10256 do
10257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 if (eap->addr_count != 0)
10259 {
10260 curwin->w_cursor.lnum = eap->line1++;
10261 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010262 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 }
10264
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010265 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266#ifdef FEAT_MBYTE
10267 arg != NULL ? arg :
10268#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010269 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 }
10271 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10272 }
10273
10274 /* Might not return to the main loop when in an event handler. */
10275 update_topline_cursor();
10276
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010277 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010279#ifdef FEAT_MOUSE
10280 setmouse();
10281#endif
10282#ifdef CURSOR_SHAPE
10283 ui_cursor_shape(); /* may show different cursor shape */
10284#endif
10285
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286#ifdef FEAT_MBYTE
10287 vim_free(arg);
10288#endif
10289}
10290
10291/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010292 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 */
10294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010295ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010297 if (eap->forceit)
10298 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010299 /* cursor line can be zero on startup */
10300 if (!curwin->w_cursor.lnum)
10301 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010302 coladvance((colnr_T)MAXCOL);
10303 curwin->w_curswant = MAXCOL;
10304 curwin->w_set_curswant = FALSE;
10305 }
10306
Bram Moolenaara40c5002005-01-09 21:16:21 +000010307 /* Ignore the command when already in Insert mode. Inserting an
10308 * expression register that invokes a function can do this. */
10309 if (State & INSERT)
10310 return;
10311
Bram Moolenaar64969662005-12-14 21:59:55 +000010312 if (eap->cmdidx == CMD_startinsert)
10313 restart_edit = 'a';
10314 else if (eap->cmdidx == CMD_startreplace)
10315 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010317 restart_edit = 'V';
10318
10319 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010321 if (eap->cmdidx == CMD_startinsert)
10322 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 curwin->w_curswant = 0; /* avoid MAXCOL */
10324 }
10325}
10326
10327/*
10328 * ":stopinsert"
10329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010331ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
10333 restart_edit = 0;
10334 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010335 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010338/*
10339 * Execute normal mode command "cmd".
10340 * "remap" can be REMAP_NONE or REMAP_YES.
10341 */
10342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010343exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010344{
Bram Moolenaar25281632016-01-21 23:32:32 +010010345 /* Stuff the argument into the typeahead buffer. */
10346 ins_typebuf(cmd, remap, 0, TRUE, silent);
10347 exec_normal(FALSE);
10348}
Bram Moolenaar25281632016-01-21 23:32:32 +010010349
Bram Moolenaar25281632016-01-21 23:32:32 +010010350/*
10351 * Execute normal_cmd() until there is no typeahead left.
10352 */
10353 void
10354exec_normal(int was_typed)
10355{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010356 oparg_T oa;
10357
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010358 clear_oparg(&oa);
10359 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010360 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10361 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010362 {
10363 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010364 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010365 }
10366}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010367
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368#ifdef FEAT_FIND_ID
10369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010370ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371{
10372 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10373 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10374 (linenr_T)1, (linenr_T)MAXLNUM);
10375}
10376
Bram Moolenaar4033c552017-09-16 20:54:51 +020010377#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378/*
10379 * ":psearch"
10380 */
10381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010382ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383{
10384 g_do_tagpreview = p_pvh;
10385 ex_findpat(eap);
10386 g_do_tagpreview = 0;
10387}
10388#endif
10389
10390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010391ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 int whole = TRUE;
10394 long n;
10395 char_u *p;
10396 int action;
10397
10398 switch (cmdnames[eap->cmdidx].cmd_name[2])
10399 {
10400 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10401 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10402 action = ACTION_GOTO;
10403 else
10404 action = ACTION_SHOW;
10405 break;
10406 case 'i': /* ":ilist" and ":dlist" */
10407 action = ACTION_SHOW_ALL;
10408 break;
10409 case 'u': /* ":ijump" and ":djump" */
10410 action = ACTION_GOTO;
10411 break;
10412 default: /* ":isplit" and ":dsplit" */
10413 action = ACTION_SPLIT;
10414 break;
10415 }
10416
10417 n = 1;
10418 if (vim_isdigit(*eap->arg)) /* get count */
10419 {
10420 n = getdigits(&eap->arg);
10421 eap->arg = skipwhite(eap->arg);
10422 }
10423 if (*eap->arg == '/') /* Match regexp, not just whole words */
10424 {
10425 whole = FALSE;
10426 ++eap->arg;
10427 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10428 if (*p)
10429 {
10430 *p++ = NUL;
10431 p = skipwhite(p);
10432
10433 /* Check for trailing illegal characters */
10434 if (!ends_excmd(*p))
10435 eap->errmsg = e_trailing;
10436 else
10437 eap->nextcmd = check_nextcmd(p);
10438 }
10439 }
10440 if (!eap->skip)
10441 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10442 whole, !eap->forceit,
10443 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10444 n, action, eap->line1, eap->line2);
10445}
10446#endif
10447
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448
Bram Moolenaar4033c552017-09-16 20:54:51 +020010449#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450/*
10451 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10452 */
10453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010454ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010456 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10458}
10459
10460/*
10461 * ":pedit"
10462 */
10463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010464ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466 win_T *curwin_save = curwin;
10467
10468 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010469 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010470 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 do_exedit(eap, NULL);
10472 keep_help_flag = FALSE;
10473 if (curwin != curwin_save && win_valid(curwin_save))
10474 {
10475 /* Return cursor to where we were */
10476 validate_cursor();
10477 redraw_later(VALID);
10478 win_enter(curwin_save, TRUE);
10479 }
10480 g_do_tagpreview = 0;
10481}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
10484/*
10485 * ":stag", ":stselect" and ":stjump".
10486 */
10487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010488ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489{
10490 postponed_split = -1;
10491 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010492 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10494 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010495 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497
10498/*
10499 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10500 */
10501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010502ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10505}
10506
10507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010508ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 int cmd;
10511
10512 switch (name[1])
10513 {
10514 case 'j': cmd = DT_JUMP; /* ":tjump" */
10515 break;
10516 case 's': cmd = DT_SELECT; /* ":tselect" */
10517 break;
10518 case 'p': cmd = DT_PREV; /* ":tprevious" */
10519 break;
10520 case 'N': cmd = DT_PREV; /* ":tNext" */
10521 break;
10522 case 'n': cmd = DT_NEXT; /* ":tnext" */
10523 break;
10524 case 'o': cmd = DT_POP; /* ":pop" */
10525 break;
10526 case 'f': /* ":tfirst" */
10527 case 'r': cmd = DT_FIRST; /* ":trewind" */
10528 break;
10529 case 'l': cmd = DT_LAST; /* ":tlast" */
10530 break;
10531 default: /* ":tag" */
10532#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010533 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010535 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 return;
10537 }
10538#endif
10539 cmd = DT_TAG;
10540 break;
10541 }
10542
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010543 if (name[0] == 'l')
10544 {
10545#ifndef FEAT_QUICKFIX
10546 ex_ni(eap);
10547 return;
10548#else
10549 cmd = DT_LTAG;
10550#endif
10551 }
10552
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10554 eap->forceit, TRUE);
10555}
10556
10557/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010558 * Check "str" for starting with a special cmdline variable.
10559 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10560 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10561 */
10562 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010563find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010564{
10565 int len;
10566 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010567 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010568 "%",
10569#define SPEC_PERC 0
10570 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010571#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010572 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010573#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010574 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010575#define SPEC_CCWORD (SPEC_CWORD + 1)
10576 "<cexpr>", /* expr under cursor */
10577#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010578 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010579#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010580 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010581#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010582 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010583#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010584#ifdef FEAT_AUTOCMD
10585 "<afile>", /* autocommand file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010586# define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010587 "<abuf>", /* autocommand buffer number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010588# define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010589 "<amatch>", /* autocommand match name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010590# define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010591#endif
10592#ifdef FEAT_CLIENTSERVER
10593 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010594# ifdef FEAT_AUTOCMD
Bram Moolenaar65f08472017-09-10 18:16:20 +020010595# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010596# else
Bram Moolenaar65f08472017-09-10 18:16:20 +020010597# define SPEC_CLIENT (SPEC_SLNUM + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010598# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010599#endif
10600 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010601
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010602 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010603 {
10604 len = (int)STRLEN(spec_str[i]);
10605 if (STRNCMP(src, spec_str[i], len) == 0)
10606 {
10607 *usedlen = len;
10608 return i;
10609 }
10610 }
10611 return -1;
10612}
10613
10614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 * Evaluate cmdline variables.
10616 *
10617 * change '%' to curbuf->b_ffname
10618 * '#' to curwin->w_altfile
10619 * '<cword>' to word under the cursor
10620 * '<cWORD>' to WORD under the cursor
10621 * '<cfile>' to path name under the cursor
10622 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010623 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 * '<afile>' to file name for autocommand
10625 * '<abuf>' to buffer number for autocommand
10626 * '<amatch>' to matching name for autocommand
10627 *
10628 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10629 * "" for error without a message) and NULL is returned.
10630 * Returns an allocated string if a valid match was found.
10631 * Returns NULL if no match was found. "usedlen" then still contains the
10632 * number of characters to skip.
10633 */
10634 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010635eval_vars(
10636 char_u *src, /* pointer into commandline */
10637 char_u *srcstart, /* beginning of valid memory for src */
10638 int *usedlen, /* characters after src that are used */
10639 linenr_T *lnump, /* line number for :e command, or NULL */
10640 char_u **errormsg, /* pointer to error message */
10641 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010642 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643{
10644 int i;
10645 char_u *s;
10646 char_u *result;
10647 char_u *resultbuf = NULL;
10648 int resultlen;
10649 buf_T *buf;
10650 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10651 int spec_idx;
10652#ifdef FEAT_MODIFY_FNAME
10653 int skip_mod = FALSE;
10654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656
10657 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010658 if (escaped != NULL)
10659 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660
10661 /*
10662 * Check if there is something to do.
10663 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010664 spec_idx = find_cmdline_var(src, usedlen);
10665 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 {
10667 *usedlen = 1;
10668 return NULL;
10669 }
10670
10671 /*
10672 * Skip when preceded with a backslash "\%" and "\#".
10673 * Note: In "\\%" the % is also not recognized!
10674 */
10675 if (src > srcstart && src[-1] == '\\')
10676 {
10677 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010678 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 return NULL;
10680 }
10681
10682 /*
10683 * word or WORD under cursor
10684 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010685 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10686 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010688 resultlen = find_ident_under_cursor(&result,
10689 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10690 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10691 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 if (resultlen == 0)
10693 {
10694 *errormsg = (char_u *)"";
10695 return NULL;
10696 }
10697 }
10698
10699 /*
10700 * '#': Alternate file name
10701 * '%': Current file name
10702 * File name under the cursor
10703 * File name for autocommand
10704 * and following modifiers
10705 */
10706 else
10707 {
10708 switch (spec_idx)
10709 {
10710 case SPEC_PERC: /* '%': current file */
10711 if (curbuf->b_fname == NULL)
10712 {
10713 result = (char_u *)"";
10714 valid = 0; /* Must have ":p:h" to be valid */
10715 }
10716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 break;
10719
10720 case SPEC_HASH: /* '#' or "#99": alternate file */
10721 if (src[1] == '#') /* "##": the argument list */
10722 {
10723 result = arg_all();
10724 resultbuf = result;
10725 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010726 if (escaped != NULL)
10727 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728#ifdef FEAT_MODIFY_FNAME
10729 skip_mod = TRUE;
10730#endif
10731 break;
10732 }
10733 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010734 if (*s == '<') /* "#<99" uses v:oldfiles */
10735 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010737 if (s == src + 2 && src[1] == '-')
10738 /* just a minus sign, don't skip over it */
10739 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 *usedlen = (int)(s - src); /* length of what we expand */
10741
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010742 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010744 if (*usedlen < 2)
10745 {
10746 /* Should we give an error message for #<text? */
10747 *usedlen = 1;
10748 return NULL;
10749 }
10750#ifdef FEAT_EVAL
10751 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10752 (long)i);
10753 if (result == NULL)
10754 {
10755 *errormsg = (char_u *)"";
10756 return NULL;
10757 }
10758#else
10759 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 }
10763 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010764 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010765 if (i == 0 && src[1] == '<' && *usedlen > 1)
10766 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010767 buf = buflist_findnr(i);
10768 if (buf == NULL)
10769 {
10770 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10771 return NULL;
10772 }
10773 if (lnump != NULL)
10774 *lnump = ECMD_LAST;
10775 if (buf->b_fname == NULL)
10776 {
10777 result = (char_u *)"";
10778 valid = 0; /* Must have ":p:h" to be valid */
10779 }
10780 else
10781 result = buf->b_fname;
10782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 break;
10784
10785#ifdef FEAT_SEARCHPATH
10786 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010787 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 if (result == NULL)
10789 {
10790 *errormsg = (char_u *)"";
10791 return NULL;
10792 }
10793 resultbuf = result; /* remember allocated string */
10794 break;
10795#endif
10796
10797#ifdef FEAT_AUTOCMD
10798 case SPEC_AFILE: /* file name for autocommand */
10799 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010800 if (result != NULL && !autocmd_fname_full)
10801 {
10802 /* Still need to turn the fname into a full path. It is
10803 * postponed to avoid a delay when <afile> is not used. */
10804 autocmd_fname_full = TRUE;
10805 result = FullName_save(autocmd_fname, FALSE);
10806 vim_free(autocmd_fname);
10807 autocmd_fname = result;
10808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 if (result == NULL)
10810 {
10811 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10812 return NULL;
10813 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010814 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 break;
10816
10817 case SPEC_ABUF: /* buffer number for autocommand */
10818 if (autocmd_bufnr <= 0)
10819 {
10820 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10821 return NULL;
10822 }
10823 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10824 result = strbuf;
10825 break;
10826
10827 case SPEC_AMATCH: /* match name for autocommand */
10828 result = autocmd_match;
10829 if (result == NULL)
10830 {
10831 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10832 return NULL;
10833 }
10834 break;
10835
10836#endif
10837 case SPEC_SFILE: /* file name for ":so" command */
10838 result = sourcing_name;
10839 if (result == NULL)
10840 {
10841 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10842 return NULL;
10843 }
10844 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010845 case SPEC_SLNUM: /* line in file for ":so" command */
10846 if (sourcing_name == NULL || sourcing_lnum == 0)
10847 {
10848 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10849 return NULL;
10850 }
10851 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10852 result = strbuf;
10853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854#if defined(FEAT_CLIENTSERVER)
10855 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010856 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10857 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 result = strbuf;
10859 break;
10860#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010861 default:
10862 result = (char_u *)""; /* avoid gcc warning */
10863 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 }
10865
10866 resultlen = (int)STRLEN(result); /* length of new string */
10867 if (src[*usedlen] == '<') /* remove the file name extension */
10868 {
10869 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 resultlen = (int)(s - result);
10872 }
10873#ifdef FEAT_MODIFY_FNAME
10874 else if (!skip_mod)
10875 {
10876 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10877 &resultlen);
10878 if (result == NULL)
10879 {
10880 *errormsg = (char_u *)"";
10881 return NULL;
10882 }
10883 }
10884#endif
10885 }
10886
10887 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10888 {
10889 if (valid != VALID_HEAD + VALID_PATH)
10890 /* xgettext:no-c-format */
10891 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10892 else
10893 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10894 result = NULL;
10895 }
10896 else
10897 result = vim_strnsave(result, resultlen);
10898 vim_free(resultbuf);
10899 return result;
10900}
10901
10902/*
10903 * Concatenate all files in the argument list, separated by spaces, and return
10904 * it in one allocated string.
10905 * Spaces and backslashes in the file names are escaped with a backslash.
10906 * Returns NULL when out of memory.
10907 */
10908 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010909arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910{
10911 int len;
10912 int idx;
10913 char_u *retval = NULL;
10914 char_u *p;
10915
10916 /*
10917 * Do this loop two times:
10918 * first time: compute the total length
10919 * second time: concatenate the names
10920 */
10921 for (;;)
10922 {
10923 len = 0;
10924 for (idx = 0; idx < ARGCOUNT; ++idx)
10925 {
10926 p = alist_name(&ARGLIST[idx]);
10927 if (p != NULL)
10928 {
10929 if (len > 0)
10930 {
10931 /* insert a space in between names */
10932 if (retval != NULL)
10933 retval[len] = ' ';
10934 ++len;
10935 }
10936 for ( ; *p != NUL; ++p)
10937 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010938 if (*p == ' '
10939#ifndef BACKSLASH_IN_FILENAME
10940 || *p == '\\'
10941#endif
10942 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 {
10944 /* insert a backslash */
10945 if (retval != NULL)
10946 retval[len] = '\\';
10947 ++len;
10948 }
10949 if (retval != NULL)
10950 retval[len] = *p;
10951 ++len;
10952 }
10953 }
10954 }
10955
10956 /* second time: break here */
10957 if (retval != NULL)
10958 {
10959 retval[len] = NUL;
10960 break;
10961 }
10962
10963 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010964 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 if (retval == NULL)
10966 break;
10967 }
10968
10969 return retval;
10970}
10971
10972#if defined(FEAT_AUTOCMD) || defined(PROTO)
10973/*
10974 * Expand the <sfile> string in "arg".
10975 *
10976 * Returns an allocated string, or NULL for any error.
10977 */
10978 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010979expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980{
10981 char_u *errormsg;
10982 int len;
10983 char_u *result;
10984 char_u *newres;
10985 char_u *repl;
10986 int srclen;
10987 char_u *p;
10988
10989 result = vim_strsave(arg);
10990 if (result == NULL)
10991 return NULL;
10992
10993 for (p = result; *p; )
10994 {
10995 if (STRNCMP(p, "<sfile>", 7) != 0)
10996 ++p;
10997 else
10998 {
10999 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011000 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 if (errormsg != NULL)
11002 {
11003 if (*errormsg)
11004 emsg(errormsg);
11005 vim_free(result);
11006 return NULL;
11007 }
11008 if (repl == NULL) /* no match (cannot happen) */
11009 {
11010 p += srclen;
11011 continue;
11012 }
11013 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11014 newres = alloc(len);
11015 if (newres == NULL)
11016 {
11017 vim_free(repl);
11018 vim_free(result);
11019 return NULL;
11020 }
11021 mch_memmove(newres, result, (size_t)(p - result));
11022 STRCPY(newres + (p - result), repl);
11023 len = (int)STRLEN(newres);
11024 STRCAT(newres, p + srclen);
11025 vim_free(repl);
11026 vim_free(result);
11027 result = newres;
11028 p = newres + len; /* continue after the match */
11029 }
11030 }
11031
11032 return result;
11033}
11034#endif
11035
11036#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011037static int ses_winsizes(FILE *fd, int restore_size,
11038 win_T *tab_firstwin);
11039static int ses_win_rec(FILE *fd, frame_T *fr);
11040static frame_T *ses_skipframe(frame_T *fr);
11041static int ses_do_frame(frame_T *fr);
11042static int ses_do_win(win_T *wp);
11043static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11044static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011045static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046
11047/*
11048 * Write openfile commands for the current buffers to an .exrc file.
11049 * Return FAIL on error, OK otherwise.
11050 */
11051 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011052makeopens(
11053 FILE *fd,
11054 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055{
11056 buf_T *buf;
11057 int only_save_windows = TRUE;
11058 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 int restore_size = TRUE;
11060 win_T *wp;
11061 char_u *sname;
11062 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011063 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011064 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011065 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011066 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011067 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011068 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069
11070 if (ssop_flags & SSOP_BUFFERS)
11071 only_save_windows = FALSE; /* Save ALL buffers */
11072
11073 /*
11074 * Begin by setting the this_session variable, and then other
11075 * sessionable variables.
11076 */
11077#ifdef FEAT_EVAL
11078 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11079 return FAIL;
11080 if (ssop_flags & SSOP_GLOBALS)
11081 if (store_session_globals(fd) == FAIL)
11082 return FAIL;
11083#endif
11084
11085 /*
11086 * Close all windows but one.
11087 */
11088 if (put_line(fd, "silent only") == FAIL)
11089 return FAIL;
11090
11091 /*
11092 * Now a :cd command to the session directory or the current directory
11093 */
11094 if (ssop_flags & SSOP_SESDIR)
11095 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011096 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11097 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 return FAIL;
11099 }
11100 else if (ssop_flags & SSOP_CURDIR)
11101 {
11102 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11103 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011104 || fputs("cd ", fd) < 0
11105 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11106 || put_eol(fd) == FAIL)
11107 {
11108 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 vim_free(sname);
11112 }
11113
11114 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011115 * If there is an empty, unnamed buffer we will wipe it out later.
11116 * Remember the buffer number.
11117 */
11118 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11119 return FAIL;
11120 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11121 return FAIL;
11122 if (put_line(fd, "endif") == FAIL)
11123 return FAIL;
11124
11125 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 * Now save the current files, current buffer first.
11127 */
11128 if (put_line(fd, "set shortmess=aoO") == FAIL)
11129 return FAIL;
11130
11131 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011132 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 {
11134 if (!(only_save_windows && buf->b_nwindows == 0)
11135 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11136 && buf->b_fname != NULL
11137 && buf->b_p_bl)
11138 {
11139 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11140 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011141 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 return FAIL;
11143 }
11144 }
11145
11146 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011147 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11149 return FAIL;
11150
11151 if (ssop_flags & SSOP_RESIZE)
11152 {
11153 /* Note: after the restore we still check it worked!*/
11154 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11155 || put_eol(fd) == FAIL)
11156 return FAIL;
11157 }
11158
11159#ifdef FEAT_GUI
11160 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11161 {
11162 int x, y;
11163
11164 if (gui_mch_get_winpos(&x, &y) == OK)
11165 {
11166 /* Note: after the restore we still check it worked!*/
11167 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11168 return FAIL;
11169 }
11170 }
11171#endif
11172
11173 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011174 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11175 * will be displayed when creating the next tab. That resizes the windows
11176 * in the first tab, which may cause problems. Set 'showtabline' to 2
11177 * temporarily to avoid that.
11178 */
11179 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11180 {
11181 if (put_line(fd, "set stal=2") == FAIL)
11182 return FAIL;
11183 restore_stal = TRUE;
11184 }
11185
11186 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011187 * May repeat putting Windows for each tab, when "tabpages" is in
11188 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011189 * Don't use goto_tabpage(), it may change directory and trigger
11190 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011192 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011193 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011194 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011196 int need_tabnew = FALSE;
11197 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011198
Bram Moolenaar18144c82006-04-12 21:52:12 +000011199 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011201 tabpage_T *tp = find_tabpage(tabnr);
11202
11203 if (tp == NULL)
11204 break; /* done all tab pages */
11205 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011206 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011207 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011208 tab_topframe = topframe;
11209 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011210 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011211 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011212 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011213 tab_topframe = tp->tp_topframe;
11214 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011215 if (tabnr > 1)
11216 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218
Bram Moolenaar18144c82006-04-12 21:52:12 +000011219 /*
11220 * Before creating the window layout, try loading one file. If this
11221 * is aborted we don't end up with a number of useless windows.
11222 * This may have side effects! (e.g., compressed or network file).
11223 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011224 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011225 {
11226 if (ses_do_win(wp)
11227 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011228 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011229#ifdef FEAT_QUICKFIX
11230 && !bt_nofile(wp->w_buffer)
11231#endif
11232 )
11233 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011234 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011235 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11236 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011237 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011238 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011239 if (!wp->w_arg_idx_invalid)
11240 edited_win = wp;
11241 break;
11242 }
11243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011245 /* If no file got edited create an empty tab page. */
11246 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11247 return FAIL;
11248
Bram Moolenaar18144c82006-04-12 21:52:12 +000011249 /*
11250 * Save current window layout.
11251 */
11252 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011254 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011256 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11257 return FAIL;
11258 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260
Bram Moolenaar18144c82006-04-12 21:52:12 +000011261 /*
11262 * Check if window sizes can be restored (no windows omitted).
11263 * Remember the window number of the current window after restoring.
11264 */
11265 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011266 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011267 {
11268 if (ses_do_win(wp))
11269 ++nr;
11270 else
11271 restore_size = FALSE;
11272 if (curwin == wp)
11273 cnr = nr;
11274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275
Bram Moolenaar18144c82006-04-12 21:52:12 +000011276 /* Go to the first window. */
11277 if (put_line(fd, "wincmd t") == FAIL)
11278 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011279
Bram Moolenaar18144c82006-04-12 21:52:12 +000011280 /*
11281 * If more than one window, see if sizes can be restored.
11282 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11283 * resized when moving between windows.
11284 * Do this before restoring the view, so that the topline and the
11285 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011286 * winminheight and winminwidth need to be set to avoid an error if the
11287 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011288 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011289 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011290 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011291 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
Bram Moolenaar18144c82006-04-12 21:52:12 +000011294 /*
11295 * Restore the view of the window (options, file, cursor, etc.).
11296 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011297 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011298 {
11299 if (!ses_do_win(wp))
11300 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011301 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11302 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011303 return FAIL;
11304 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11305 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011306 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011307 }
11308
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011309 /* The argument index in the first tab page is zero, need to set it in
11310 * each window. For further tab pages it's the window where we do
11311 * "tabedit". */
11312 cur_arg_idx = next_arg_idx;
11313
Bram Moolenaar18144c82006-04-12 21:52:12 +000011314 /*
11315 * Restore cursor to the current window if it's not the first one.
11316 */
11317 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11318 || put_eol(fd) == FAIL))
11319 return FAIL;
11320
11321 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011322 * Restore window sizes again after jumping around in windows, because
11323 * the current window has a minimum size while others may not.
11324 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011325 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011326 return FAIL;
11327
Bram Moolenaar18144c82006-04-12 21:52:12 +000011328 /* Don't continue in another tab page when doing only the current one
11329 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011330 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011331 break;
11332 }
11333
11334 if (ssop_flags & SSOP_TABPAGES)
11335 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011336 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11337 || put_eol(fd) == FAIL)
11338 return FAIL;
11339 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011340 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11341 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011342
Bram Moolenaar9c102382006-05-03 21:26:49 +000011343 /*
11344 * Wipe out an empty unnamed buffer we started in.
11345 */
11346 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11347 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011348 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011349 return FAIL;
11350 if (put_line(fd, "endif") == FAIL)
11351 return FAIL;
11352 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11353 return FAIL;
11354
11355 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11356 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11357 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11358 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011359 /* Re-apply 'winminheight' and 'winminwidth'. */
11360 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11361 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363
11364 /*
11365 * Lastly, execute the x.vim file if it exists.
11366 */
11367 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11368 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011369 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 || put_line(fd, "endif") == FAIL)
11371 return FAIL;
11372
11373 return OK;
11374}
11375
11376 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011377ses_winsizes(
11378 FILE *fd,
11379 int restore_size,
11380 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381{
11382 int n = 0;
11383 win_T *wp;
11384
11385 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11386 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011387 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 {
11389 if (!ses_do_win(wp))
11390 continue;
11391 ++n;
11392
11393 /* restore height when not full height */
11394 if (wp->w_height + wp->w_status_height < topframe->fr_height
11395 && (fprintf(fd,
11396 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11397 n, (long)wp->w_height, Rows / 2, Rows) < 0
11398 || put_eol(fd) == FAIL))
11399 return FAIL;
11400
11401 /* restore width when not full width */
11402 if (wp->w_width < Columns && (fprintf(fd,
11403 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11404 n, (long)wp->w_width, Columns / 2, Columns) < 0
11405 || put_eol(fd) == FAIL))
11406 return FAIL;
11407 }
11408 }
11409 else
11410 {
11411 /* Just equalise window sizes */
11412 if (put_line(fd, "wincmd =") == FAIL)
11413 return FAIL;
11414 }
11415 return OK;
11416}
11417
11418/*
11419 * Write commands to "fd" to recursively create windows for frame "fr",
11420 * horizontally and vertically split.
11421 * After the commands the last window in the frame is the current window.
11422 * Returns FAIL when writing the commands to "fd" fails.
11423 */
11424 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011425ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426{
11427 frame_T *frc;
11428 int count = 0;
11429
11430 if (fr->fr_layout != FR_LEAF)
11431 {
11432 /* Find first frame that's not skipped and then create a window for
11433 * each following one (first frame is already there). */
11434 frc = ses_skipframe(fr->fr_child);
11435 if (frc != NULL)
11436 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11437 {
11438 /* Make window as big as possible so that we have lots of room
11439 * to split. */
11440 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11441 || put_line(fd, fr->fr_layout == FR_COL
11442 ? "split" : "vsplit") == FAIL)
11443 return FAIL;
11444 ++count;
11445 }
11446
11447 /* Go back to the first window. */
11448 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11449 ? "%dwincmd k" : "%dwincmd h", count) < 0
11450 || put_eol(fd) == FAIL))
11451 return FAIL;
11452
11453 /* Recursively create frames/windows in each window of this column or
11454 * row. */
11455 frc = ses_skipframe(fr->fr_child);
11456 while (frc != NULL)
11457 {
11458 ses_win_rec(fd, frc);
11459 frc = ses_skipframe(frc->fr_next);
11460 /* Go to next window. */
11461 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11462 return FAIL;
11463 }
11464 }
11465 return OK;
11466}
11467
11468/*
11469 * Skip frames that don't contain windows we want to save in the Session.
11470 * Returns NULL when there none.
11471 */
11472 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011473ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
11475 frame_T *frc;
11476
11477 for (frc = fr; frc != NULL; frc = frc->fr_next)
11478 if (ses_do_frame(frc))
11479 break;
11480 return frc;
11481}
11482
11483/*
11484 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11485 * the Session.
11486 */
11487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011488ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489{
11490 frame_T *frc;
11491
11492 if (fr->fr_layout == FR_LEAF)
11493 return ses_do_win(fr->fr_win);
11494 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11495 if (ses_do_frame(frc))
11496 return TRUE;
11497 return FALSE;
11498}
11499
11500/*
11501 * Return non-zero if window "wp" is to be stored in the Session.
11502 */
11503 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011504ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505{
11506 if (wp->w_buffer->b_fname == NULL
11507#ifdef FEAT_QUICKFIX
11508 /* When 'buftype' is "nofile" can't restore the window contents. */
11509 || bt_nofile(wp->w_buffer)
11510#endif
11511 )
11512 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011513 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 return (ssop_flags & SSOP_HELP);
11515 return TRUE;
11516}
11517
11518/*
11519 * Write commands to "fd" to restore the view of a window.
11520 * Caller must make sure 'scrolloff' is zero.
11521 */
11522 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011523put_view(
11524 FILE *fd,
11525 win_T *wp,
11526 int add_edit, /* add ":edit" command to view */
11527 unsigned *flagp, /* vop_flags or ssop_flags */
11528 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011529 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 win_T *save_curwin;
11532 int f;
11533 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011534 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535
11536 /* Always restore cursor position for ":mksession". For ":mkview" only
11537 * when 'viewoptions' contains "cursor". */
11538 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11539
11540 /*
11541 * Local argument list.
11542 */
11543 if (wp->w_alist == &global_alist)
11544 {
11545 if (put_line(fd, "argglobal") == FAIL)
11546 return FAIL;
11547 }
11548 else
11549 {
11550 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11551 flagp == &vop_flags
11552 || !(*flagp & SSOP_CURDIR)
11553 || wp->w_localdir != NULL, flagp) == FAIL)
11554 return FAIL;
11555 }
11556
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011557 /* Only when part of a session: restore the argument index. Some
11558 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011559 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011560 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011562 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 || put_eol(fd) == FAIL)
11564 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011565 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 }
11567
11568 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011569 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 {
11571 /*
11572 * Load the file.
11573 */
11574 if (wp->w_buffer->b_ffname != NULL
11575#ifdef FEAT_QUICKFIX
11576 && !bt_nofile(wp->w_buffer)
11577#endif
11578 )
11579 {
11580 /*
11581 * Editing a file in this buffer: use ":edit file".
11582 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011583 *
11584 * Note, if a buffer for that file already exists, use :badd to
11585 * edit that buffer, to not lose folding information (:edit resets
11586 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011588 if (fputs("if bufexists('", fd) < 0
11589 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11590 || fputs("') | buffer ", fd) < 0
11591 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11592 || fputs(" | else | edit ", fd) < 0
11593 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11594 || fputs(" | endif", fd) < 0
11595 ||
11596 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 return FAIL;
11598 }
11599 else
11600 {
11601 /* No file in this buffer, just make it empty. */
11602 if (put_line(fd, "enew") == FAIL)
11603 return FAIL;
11604#ifdef FEAT_QUICKFIX
11605 if (wp->w_buffer->b_ffname != NULL)
11606 {
11607 /* The buffer does have a name, but it's not a file name. */
11608 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011609 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 return FAIL;
11611 }
11612#endif
11613 do_cursor = FALSE;
11614 }
11615 }
11616
11617 /*
11618 * Local mappings and abbreviations.
11619 */
11620 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11621 && makemap(fd, wp->w_buffer) == FAIL)
11622 return FAIL;
11623
11624 /*
11625 * Local options. Need to go to the window temporarily.
11626 * Store only local values when using ":mkview" and when ":mksession" is
11627 * used and 'sessionoptions' doesn't include "options".
11628 * Some folding options are always stored when "folds" is included,
11629 * otherwise the folds would not be restored correctly.
11630 */
11631 save_curwin = curwin;
11632 curwin = wp;
11633 curbuf = curwin->w_buffer;
11634 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11635 f = makeset(fd, OPT_LOCAL,
11636 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11637#ifdef FEAT_FOLDING
11638 else if (*flagp & SSOP_FOLDS)
11639 f = makefoldset(fd);
11640#endif
11641 else
11642 f = OK;
11643 curwin = save_curwin;
11644 curbuf = curwin->w_buffer;
11645 if (f == FAIL)
11646 return FAIL;
11647
11648#ifdef FEAT_FOLDING
11649 /*
11650 * Save Folds when 'buftype' is empty and for help files.
11651 */
11652 if ((*flagp & SSOP_FOLDS)
11653 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011654 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 {
11656 if (put_folds(fd, wp) == FAIL)
11657 return FAIL;
11658 }
11659#endif
11660
11661 /*
11662 * Set the cursor after creating folds, since that moves the cursor.
11663 */
11664 if (do_cursor)
11665 {
11666
11667 /* Restore the cursor line in the file and relatively in the
11668 * window. Don't use "G", it changes the jumplist. */
11669 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11670 (long)wp->w_cursor.lnum,
11671 (long)(wp->w_cursor.lnum - wp->w_topline),
11672 (long)wp->w_height / 2, (long)wp->w_height) < 0
11673 || put_eol(fd) == FAIL
11674 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11675 || put_line(fd, "exe s:l") == FAIL
11676 || put_line(fd, "normal! zt") == FAIL
11677 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11678 || put_eol(fd) == FAIL)
11679 return FAIL;
11680 /* Restore the cursor column and left offset when not wrapping. */
11681 if (wp->w_cursor.col == 0)
11682 {
11683 if (put_line(fd, "normal! 0") == FAIL)
11684 return FAIL;
11685 }
11686 else
11687 {
11688 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11689 {
11690 if (fprintf(fd,
11691 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011692 (long)wp->w_virtcol + 1,
11693 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 (long)wp->w_width / 2, (long)wp->w_width) < 0
11695 || put_eol(fd) == FAIL
11696 || put_line(fd, "if s:c > 0") == FAIL
11697 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011698 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11699 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 || put_eol(fd) == FAIL
11701 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011702 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 || put_eol(fd) == FAIL
11704 || put_line(fd, "endif") == FAIL)
11705 return FAIL;
11706 }
11707 else
11708 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011709 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 || put_eol(fd) == FAIL)
11711 return FAIL;
11712 }
11713 }
11714 }
11715
11716 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011717 * Local directory, if the current flag is not view options or the "curdir"
11718 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011720 if (wp->w_localdir != NULL
11721 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 {
11723 if (fputs("lcd ", fd) < 0
11724 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11725 || put_eol(fd) == FAIL)
11726 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011727 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 }
11729
11730 return OK;
11731}
11732
11733/*
11734 * Write an argument list to the session file.
11735 * Returns FAIL if writing fails.
11736 */
11737 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011738ses_arglist(
11739 FILE *fd,
11740 char *cmd,
11741 garray_T *gap,
11742 int fullname, /* TRUE: use full path name */
11743 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744{
11745 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011746 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 char_u *s;
11748
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011749 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11750 return FAIL;
11751 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 return FAIL;
11753 for (i = 0; i < gap->ga_len; ++i)
11754 {
11755 /* NULL file names are skipped (only happens when out of memory). */
11756 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11757 if (s != NULL)
11758 {
11759 if (fullname)
11760 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011761 buf = alloc(MAXPATHL);
11762 if (buf != NULL)
11763 {
11764 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11765 s = buf;
11766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011768 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011769 || ses_put_fname(fd, s, flagp) == FAIL
11770 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011771 {
11772 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011774 }
11775 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 }
11777 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011778 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779}
11780
11781/*
11782 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011783 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 * Returns FAIL if writing fails.
11785 */
11786 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011787ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788{
11789 char_u *name;
11790
11791 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011792 * the session file will be sourced.
11793 * Don't do this for ":mkview", we don't know the current directory.
11794 * Don't do this after ":lcd", we don't keep track of what the current
11795 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 if (buf->b_sfname != NULL
11797 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011798 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011799#ifdef FEAT_AUTOCHDIR
11800 && !p_acd
11801#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011802 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 name = buf->b_sfname;
11804 else
11805 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011806 if (ses_put_fname(fd, name, flagp) == FAIL
11807 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 return FAIL;
11809 return OK;
11810}
11811
11812/*
11813 * Write a file name to the session file.
11814 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11815 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011816 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 */
11818 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011819ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820{
11821 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011822 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824
11825 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011826 if (sname == NULL)
11827 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011829 if (*flagp & SSOP_SLASH)
11830 {
11831 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011832 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011833 if (*p == '\\')
11834 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011836
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011837 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011838 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011840 if (p == NULL)
11841 return FAIL;
11842
11843 /* write the result */
11844 if (fputs((char *)p, fd) < 0)
11845 retval = FAIL;
11846
11847 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 return retval;
11849}
11850
11851/*
11852 * ":loadview [nr]"
11853 */
11854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011855ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
11857 char_u *fname;
11858
11859 fname = get_view_file(*eap->arg);
11860 if (fname != NULL)
11861 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011862 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 vim_free(fname);
11864 }
11865}
11866
11867/*
11868 * Get the name of the view file for the current buffer.
11869 */
11870 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011871get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872{
11873 int len = 0;
11874 char_u *p, *s;
11875 char_u *retval;
11876 char_u *sname;
11877
11878 if (curbuf->b_ffname == NULL)
11879 {
11880 EMSG(_(e_noname));
11881 return NULL;
11882 }
11883 sname = home_replace_save(NULL, curbuf->b_ffname);
11884 if (sname == NULL)
11885 return NULL;
11886
11887 /*
11888 * We want a file name without separators, because we're not going to make
11889 * a directory.
11890 * "normal" path separator -> "=+"
11891 * "=" -> "=="
11892 * ":" path separator -> "=-"
11893 */
11894 for (p = sname; *p; ++p)
11895 if (*p == '=' || vim_ispathsep(*p))
11896 ++len;
11897 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11898 if (retval != NULL)
11899 {
11900 STRCPY(retval, p_vdir);
11901 add_pathsep(retval);
11902 s = retval + STRLEN(retval);
11903 for (p = sname; *p; ++p)
11904 {
11905 if (*p == '=')
11906 {
11907 *s++ = '=';
11908 *s++ = '=';
11909 }
11910 else if (vim_ispathsep(*p))
11911 {
11912 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011913#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 if (*p == ':')
11915 *s++ = '-';
11916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011918 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 }
11920 else
11921 *s++ = *p;
11922 }
11923 *s++ = '=';
11924 *s++ = c;
11925 STRCPY(s, ".vim");
11926 }
11927
11928 vim_free(sname);
11929 return retval;
11930}
11931
11932#endif /* FEAT_SESSION */
11933
11934/*
11935 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11936 * Return FAIL for a write error.
11937 */
11938 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011939put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940{
11941 if (
11942#ifdef USE_CRNL
11943 (
11944# ifdef MKSESSION_NL
11945 !mksession_nl &&
11946# endif
11947 (putc('\r', fd) < 0)) ||
11948#endif
11949 (putc('\n', fd) < 0))
11950 return FAIL;
11951 return OK;
11952}
11953
11954/*
11955 * Write a line to "fd".
11956 * Return FAIL for a write error.
11957 */
11958 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011959put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960{
11961 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11962 return FAIL;
11963 return OK;
11964}
11965
11966#ifdef FEAT_VIMINFO
11967/*
11968 * ":rviminfo" and ":wviminfo".
11969 */
11970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011971ex_viminfo(
11972 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973{
11974 char_u *save_viminfo;
11975
11976 save_viminfo = p_viminfo;
11977 if (*p_viminfo == NUL)
11978 p_viminfo = (char_u *)"'100";
11979 if (eap->cmdidx == CMD_rviminfo)
11980 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011981 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11982 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 EMSG(_("E195: Cannot open viminfo file for reading"));
11984 }
11985 else
11986 write_viminfo(eap->arg, eap->forceit);
11987 p_viminfo = save_viminfo;
11988}
11989#endif
11990
11991#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011992/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011993 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011994 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011997dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 if (fname == NULL)
12000 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012001 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002}
12003#endif
12004
12005/*
12006 * ":behave {mswin,xterm}"
12007 */
12008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012009ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
12011 if (STRCMP(eap->arg, "mswin") == 0)
12012 {
12013 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12014 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12015 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12016 set_option_value((char_u *)"keymodel", 0L,
12017 (char_u *)"startsel,stopsel", 0);
12018 }
12019 else if (STRCMP(eap->arg, "xterm") == 0)
12020 {
12021 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12022 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12023 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12024 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12025 }
12026 else
12027 EMSG2(_(e_invarg2), eap->arg);
12028}
12029
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012030#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12031/*
12032 * Function given to ExpandGeneric() to obtain the possible arguments of the
12033 * ":behave {mswin,xterm}" command.
12034 */
12035 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012036get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012037{
12038 if (idx == 0)
12039 return (char_u *)"mswin";
12040 if (idx == 1)
12041 return (char_u *)"xterm";
12042 return NULL;
12043}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012044
12045/*
12046 * Function given to ExpandGeneric() to obtain the possible arguments of the
12047 * ":messages {clear}" command.
12048 */
12049 char_u *
12050get_messages_arg(expand_T *xp UNUSED, int idx)
12051{
12052 if (idx == 0)
12053 return (char_u *)"clear";
12054 return NULL;
12055}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012056#endif
12057
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012058 char_u *
12059get_mapclear_arg(expand_T *xp UNUSED, int idx)
12060{
12061 if (idx == 0)
12062 return (char_u *)"<buffer>";
12063 return NULL;
12064}
12065
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066#ifdef FEAT_AUTOCMD
12067static int filetype_detect = FALSE;
12068static int filetype_plugin = FALSE;
12069static int filetype_indent = FALSE;
12070
12071/*
12072 * ":filetype [plugin] [indent] {on,off,detect}"
12073 * on: Load the filetype.vim file to install autocommands for file types.
12074 * off: Load the ftoff.vim file to remove all autocommands for file types.
12075 * plugin on: load filetype.vim and ftplugin.vim
12076 * plugin off: load ftplugof.vim
12077 * indent on: load filetype.vim and indent.vim
12078 * indent off: load indoff.vim
12079 */
12080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012081ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082{
12083 char_u *arg = eap->arg;
12084 int plugin = FALSE;
12085 int indent = FALSE;
12086
12087 if (*eap->arg == NUL)
12088 {
12089 /* Print current status. */
12090 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12091 filetype_detect ? "ON" : "OFF",
12092 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12093 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12094 return;
12095 }
12096
12097 /* Accept "plugin" and "indent" in any order. */
12098 for (;;)
12099 {
12100 if (STRNCMP(arg, "plugin", 6) == 0)
12101 {
12102 plugin = TRUE;
12103 arg = skipwhite(arg + 6);
12104 continue;
12105 }
12106 if (STRNCMP(arg, "indent", 6) == 0)
12107 {
12108 indent = TRUE;
12109 arg = skipwhite(arg + 6);
12110 continue;
12111 }
12112 break;
12113 }
12114 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12115 {
12116 if (*arg == 'o' || !filetype_detect)
12117 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012118 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 filetype_detect = TRUE;
12120 if (plugin)
12121 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012122 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 filetype_plugin = TRUE;
12124 }
12125 if (indent)
12126 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012127 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 filetype_indent = TRUE;
12129 }
12130 }
12131 if (*arg == 'd')
12132 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012133 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012134 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 }
12136 }
12137 else if (STRCMP(arg, "off") == 0)
12138 {
12139 if (plugin || indent)
12140 {
12141 if (plugin)
12142 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012143 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 filetype_plugin = FALSE;
12145 }
12146 if (indent)
12147 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012148 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 filetype_indent = FALSE;
12150 }
12151 }
12152 else
12153 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012154 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 filetype_detect = FALSE;
12156 }
12157 }
12158 else
12159 EMSG2(_(e_invarg2), arg);
12160}
12161
12162/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012163 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 */
12165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012166ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167{
12168 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012169 {
12170 char_u *arg = eap->arg;
12171
12172 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12173 arg += 9;
12174
12175 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12176 if (arg != eap->arg)
12177 did_filetype = FALSE;
12178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179}
12180#endif
12181
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012183ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185#ifdef FEAT_DIGRAPHS
12186 if (*eap->arg != NUL)
12187 putdigraph(eap->arg);
12188 else
12189 listdigraphs();
12190#else
12191 EMSG(_("E196: No digraphs in this version"));
12192#endif
12193}
12194
12195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012196ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197{
12198 int flags = 0;
12199
12200 if (eap->cmdidx == CMD_setlocal)
12201 flags = OPT_LOCAL;
12202 else if (eap->cmdidx == CMD_setglobal)
12203 flags = OPT_GLOBAL;
12204#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12205 if (cmdmod.browse && flags == 0)
12206 ex_options(eap);
12207 else
12208#endif
12209 (void)do_set(eap->arg, flags);
12210}
12211
12212#ifdef FEAT_SEARCH_EXTRA
12213/*
12214 * ":nohlsearch"
12215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012217ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012219 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012220 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221}
12222
12223/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012224 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225 * Sets nextcmd to the start of the next command, if any. Also called when
12226 * skipping commands to find the next command.
12227 */
12228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012229ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230{
12231 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012232 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 char_u *end;
12234 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012235 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012236
12237 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012238 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012239 else
12240 {
12241 EMSG(e_invcmd);
12242 return;
12243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244
12245 /* First clear any old pattern. */
12246 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012247 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248
12249 if (ends_excmd(*eap->arg))
12250 end = eap->arg;
12251 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012252 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 end = eap->arg + 4;
12254 else
12255 {
12256 p = skiptowhite(eap->arg);
12257 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012258 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 p = skipwhite(p);
12260 if (*p == NUL)
12261 {
12262 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012263 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 EMSG2(_(e_invarg2), eap->arg);
12265 return;
12266 }
12267 end = skip_regexp(p + 1, *p, TRUE, NULL);
12268 if (!eap->skip)
12269 {
12270 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12271 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012272 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 eap->errmsg = e_trailing;
12274 return;
12275 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012276 if (*end != *p)
12277 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012278 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012279 EMSG2(_(e_invarg2), p);
12280 return;
12281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282
12283 c = *end;
12284 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012285 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012286 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012287 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 }
12289 }
12290 eap->nextcmd = find_nextcmd(end);
12291}
12292#endif
12293
12294#ifdef FEAT_CRYPT
12295/*
12296 * ":X": Get crypt key
12297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012299ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012301 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012302 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303}
12304#endif
12305
12306#ifdef FEAT_FOLDING
12307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012308ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309{
12310 if (foldManualAllowed(TRUE))
12311 foldCreate(eap->line1, eap->line2);
12312}
12313
12314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012315ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316{
12317 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12318 eap->forceit, FALSE);
12319}
12320
12321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012322ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324 linenr_T lnum;
12325
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012326#ifdef FEAT_CLIPBOARD
12327 start_global_changes();
12328#endif
12329
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 /* First set the marks for all lines closed/open. */
12331 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12332 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12333 ml_setmarked(lnum);
12334
12335 /* Execute the command on the marked lines. */
12336 global_exe(eap->arg);
12337 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012338#ifdef FEAT_CLIPBOARD
12339 end_global_changes();
12340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341}
12342#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012343
12344# if defined(FEAT_TIMERS) || defined(PROTO)
12345 int
12346get_pressedreturn(void)
12347{
12348 return ex_pressedreturn;
12349}
12350
12351 void
12352set_pressedreturn(int val)
12353{
12354 ex_pressedreturn = val;
12355}
12356#endif