blob: adf42799e4942062953f9dd49afd0fe79de77e72 [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. */
871 vim_free(cmdline_copy);
872 cmdline_copy = NULL;
873
874 /* Check if a function has returned or, unless it has an unclosed
875 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000879 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000880 func_line_end(real_cookie);
881# endif
882 if (func_has_ended(real_cookie))
883 {
884 retval = FAIL;
885 break;
886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000889 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100890 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891 script_line_end();
892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
897 retval = FAIL;
898 break;
899 }
900
901 /* If breakpoints have been added/deleted need to check for it. */
902 if (breakpoint != NULL && dbg_tick != NULL
903 && *dbg_tick != debug_tick)
904 {
905 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100906 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 fname, sourcing_lnum);
908 *dbg_tick = debug_tick;
909 }
910
911 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
912 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
913
914 /* Did we encounter a breakpoint? */
915 if (breakpoint != NULL && *breakpoint != 0
916 && *breakpoint <= sourcing_lnum)
917 {
918 dbg_breakpoint(fname, sourcing_lnum);
919 /* Find next breakpoint. */
920 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100921 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 fname, sourcing_lnum);
923 *dbg_tick = debug_tick;
924 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000926 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 {
928 if (getline_is_func)
929 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100930 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 script_line_start();
932 }
933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * below, so that it stores lines in or reads them from
941 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000942 * while/for loop. */
943 cmd_getline = get_loop_line;
944 cmd_cookie = (void *)&cmd_loop_cookie;
945 cmd_loop_cookie.lines_gap = &lines_ga;
946 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000948 cmd_loop_cookie.cookie = cookie;
949 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951 else
952 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100953 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 cmd_cookie = cookie;
955 }
956#endif
957
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (next_cmdline == NULL)
960 {
961 /*
962 * Need to set msg_didout for the first line after an ":if",
963 * otherwise the ":if" will be overwritten.
964 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100965 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100967 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_EVAL
969 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
970#else
971 0
972#endif
973 )) == NULL)
974 {
975 /* Don't call wait_return for aborted command line. The NULL
976 * returned for the end of a sourced file or executed function
977 * doesn't do this. */
978 if (KeyTyped && !(flags & DOCMD_REPEAT))
979 need_wait_return = FALSE;
980 retval = FAIL;
981 break;
982 }
983 used_getline = TRUE;
984
985 /*
986 * Keep the first typed line. Clear it when more lines are typed.
987 */
988 if (flags & DOCMD_KEEPLINE)
989 {
990 vim_free(repeat_cmdline);
991 if (count == 0)
992 repeat_cmdline = vim_strsave(next_cmdline);
993 else
994 repeat_cmdline = NULL;
995 }
996 }
997
998 /* 3. Make a copy of the command so we can mess with it. */
999 else if (cmdline_copy == NULL)
1000 {
1001 next_cmdline = vim_strsave(next_cmdline);
1002 if (next_cmdline == NULL)
1003 {
1004 EMSG(_(e_outofmem));
1005 retval = FAIL;
1006 break;
1007 }
1008 }
1009 cmdline_copy = next_cmdline;
1010
1011#ifdef FEAT_EVAL
1012 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001013 * Save the current line when inside a ":while" or ":for", and when
1014 * the command looks like a ":while" or ":for", because we may need it
1015 * later. When there is a '|' and another command, it is stored
1016 * separately, because we need to be able to jump back to it from an
1017 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001019 if (current_line == lines_ga.ga_len
1020 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001022 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
1024 retval = FAIL;
1025 break;
1026 }
1027 }
1028 did_endif = FALSE;
1029#endif
1030
1031 if (count++ == 0)
1032 {
1033 /*
1034 * All output from the commands is put below each other, without
1035 * waiting for a return. Don't do this when executing commands
1036 * from a script or when being called recursive (e.g. for ":e
1037 * +command file").
1038 */
1039 if (!(flags & DOCMD_NOWAIT) && !recursive)
1040 {
1041 msg_didout_before_start = msg_didout;
1042 msg_didany = FALSE; /* no output yet */
1043 msg_start();
1044 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001045 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 ++RedrawingDisabled;
1047 did_inc = TRUE;
1048 }
1049 }
1050
1051 if (p_verbose >= 15 && sourcing_name != NULL)
1052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001054 verbose_enter_scroll();
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 smsg((char_u *)_("line %ld: %s"),
1057 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001058 if (msg_silent == 0)
1059 msg_puts((char_u *)"\n"); /* don't overwrite this */
1060
1061 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 --no_wait_return;
1063 }
1064
1065 /*
1066 * 2. Execute one '|' separated command.
1067 * do_one_cmd() will return NULL if there is no trailing '|'.
1068 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1069 */
1070 ++recursive;
1071 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1072#ifdef FEAT_EVAL
1073 &cstack,
1074#endif
1075 cmd_getline, cmd_cookie);
1076 --recursive;
1077
1078#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 if (cmd_cookie == (void *)&cmd_loop_cookie)
1080 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#endif
1084
1085 if (next_cmdline == NULL)
1086 {
1087 vim_free(cmdline_copy);
1088 cmdline_copy = NULL;
1089#ifdef FEAT_CMDHIST
1090 /*
1091 * If the command was typed, remember it for the ':' register.
1092 * Do this AFTER executing the command to make :@: work.
1093 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001094 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 && new_last_cmdline != NULL)
1096 {
1097 vim_free(last_cmdline);
1098 last_cmdline = new_last_cmdline;
1099 new_last_cmdline = NULL;
1100 }
1101#endif
1102 }
1103 else
1104 {
1105 /* need to copy the command after the '|' to cmdline_copy, for the
1106 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001107 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 next_cmdline = cmdline_copy;
1109 }
1110
1111
1112#ifdef FEAT_EVAL
1113 /* reset did_emsg for a function that is not aborted by an error */
1114 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001115 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 && !func_has_abort(real_cookie))
1117 did_emsg = FALSE;
1118
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 ++current_line;
1122
1123 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 * An ":endwhile", ":endfor" and ":continue" is handled here.
1125 * If we were executing commands, jump back to the ":while" or
1126 * ":for".
1127 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 if (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 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 /* Jump back to the matching ":while" or ":for". Be careful
1134 * not to use a cs_line[] from an entry that isn't a ":while"
1135 * or ":for": It would make "current_line" invalid and can
1136 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (!did_emsg && !got_int && !did_throw
1138 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 && (cstack.cs_flags[cstack.cs_idx]
1140 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 && cstack.cs_line[cstack.cs_idx] >= 0
1142 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1143 {
1144 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001145 /* remember we jumped there */
1146 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 line_breakcheck(); /* check if CTRL-C typed */
1148
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 /* Check for the next breakpoint at or after the ":while"
1150 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (breakpoint != NULL)
1152 {
1153 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001154 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 fname,
1156 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1157 *dbg_tick = debug_tick;
1158 }
1159 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001164 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1165 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 }
1168
1169 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1176 }
1177 }
1178
1179 /*
1180 * When not inside any ":while" loop, clear remembered lines.
1181 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184 if (lines_ga.ga_len > 0)
1185 {
1186 sourcing_lnum =
1187 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1188 free_cmdlines(&lines_ga);
1189 }
1190 current_line = 0;
1191 }
1192
1193 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1195 * being restored at the ":endtry". Reset them here and set the
1196 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1197 * This includes the case where a missing ":endif", ":endwhile" or
1198 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 cstack.cs_lflags &= ~CSL_HAD_FINA;
1203 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1204 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 did_throw ? (void *)current_exception : NULL);
1206 did_emsg = got_int = did_throw = FALSE;
1207 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1208 }
1209
1210 /* Update global "trylevel" for recursive calls to do_cmdline() from
1211 * within this loop. */
1212 trylevel = initial_trylevel + cstack.cs_trylevel;
1213
1214 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001215 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 * files) is aborted because of an error, an interrupt, or an uncaught
1217 * exception, cancel everything. If it is left normally, reset
1218 * force_abort to get the non-EH compatible abortion behavior for
1219 * the rest of the script.
1220 */
1221 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1222 force_abort = FALSE;
1223
1224 /* Convert an interrupt to an exception if appropriate. */
1225 (void)do_intthrow(&cstack);
1226#endif /* FEAT_EVAL */
1227
1228 }
1229 /*
1230 * Continue executing command lines when:
1231 * - no CTRL-C typed, no aborting error, no exception thrown or try
1232 * conditionals need to be checked for executing finally clauses or
1233 * catching an interrupt exception
1234 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001235 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 * looping for ":source" command or function call.
1237 */
1238 while (!((got_int
1239#ifdef FEAT_EVAL
1240 || (did_emsg && force_abort) || did_throw
1241#endif
1242 )
1243#ifdef FEAT_EVAL
1244 && cstack.cs_trylevel == 0
1245#endif
1246 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001247 && !(did_emsg
1248#ifdef FEAT_EVAL
1249 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001250 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001251 * the :endtry to be missed. */
1252 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1253#endif
1254 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001255 && (getline_equal(fgetline, cookie, getexmodeline)
1256 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 && (next_cmdline != NULL
1258#ifdef FEAT_EVAL
1259 || cstack.cs_idx >= 0
1260#endif
1261 || (flags & DOCMD_REPEAT)));
1262
1263 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001264 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265#ifdef FEAT_EVAL
1266 free_cmdlines(&lines_ga);
1267 ga_clear(&lines_ga);
1268
1269 if (cstack.cs_idx >= 0)
1270 {
1271 /*
1272 * If a sourced file or executed function ran to its end, report the
1273 * unclosed conditional.
1274 */
1275 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001276 && ((getline_equal(fgetline, cookie, getsourceline)
1277 && !source_finished(fgetline, cookie))
1278 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 && !func_has_ended(real_cookie))))
1280 {
1281 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1282 EMSG(_(e_endtry));
1283 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1284 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001285 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1286 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else
1288 EMSG(_(e_endif));
1289 }
1290
1291 /*
1292 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1293 * ":endtry" in a sourced file or executed function. If the try
1294 * conditional is in its finally clause, ignore anything pending.
1295 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001296 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001299 {
1300 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1301
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001302 if (idx >= 0)
1303 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001304 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1305 &cstack.cs_looplevel);
1306 }
1307 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 trylevel = initial_trylevel;
1309 }
1310
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001311 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1312 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001314 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 ? (char_u *)"endfunction" : (char_u *)NULL);
1316
1317 if (trylevel == 0)
1318 {
1319 /*
1320 * When an exception is being thrown out of the outermost try
1321 * conditional, discard the uncaught exception, disable the conversion
1322 * of interrupts or errors to exceptions, and ensure that no more
1323 * commands are executed.
1324 */
1325 if (did_throw)
1326 {
1327 void *p = NULL;
1328 char_u *saved_sourcing_name;
1329 int saved_sourcing_lnum;
1330 struct msglist *messages = NULL, *next;
1331
1332 /*
1333 * If the uncaught exception is a user exception, report it as an
1334 * error. If it is an error exception, display the saved error
1335 * message now. For an interrupt exception, do nothing; the
1336 * interrupt message is given elsewhere.
1337 */
1338 switch (current_exception->type)
1339 {
1340 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001341 vim_snprintf((char *)IObuff, IOSIZE,
1342 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 current_exception->value);
1344 p = vim_strsave(IObuff);
1345 break;
1346 case ET_ERROR:
1347 messages = current_exception->messages;
1348 current_exception->messages = NULL;
1349 break;
1350 case ET_INTERRUPT:
1351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353
1354 saved_sourcing_name = sourcing_name;
1355 saved_sourcing_lnum = sourcing_lnum;
1356 sourcing_name = current_exception->throw_name;
1357 sourcing_lnum = current_exception->throw_lnum;
1358 current_exception->throw_name = NULL;
1359
1360 discard_current_exception(); /* uses IObuff if 'verbose' */
1361 suppress_errthrow = TRUE;
1362 force_abort = TRUE;
1363
1364 if (messages != NULL)
1365 {
1366 do
1367 {
1368 next = messages->next;
1369 emsg(messages->msg);
1370 vim_free(messages->msg);
1371 vim_free(messages);
1372 messages = next;
1373 }
1374 while (messages != NULL);
1375 }
1376 else if (p != NULL)
1377 {
1378 emsg(p);
1379 vim_free(p);
1380 }
1381 vim_free(sourcing_name);
1382 sourcing_name = saved_sourcing_name;
1383 sourcing_lnum = saved_sourcing_lnum;
1384 }
1385
1386 /*
1387 * On an interrupt or an aborting error not converted to an exception,
1388 * disable the conversion of errors to exceptions. (Interrupts are not
1389 * converted any more, here.) This enables also the interrupt message
1390 * when force_abort is set and did_emsg unset in case of an interrupt
1391 * from a finally clause after an error.
1392 */
1393 else if (got_int || (did_emsg && force_abort))
1394 suppress_errthrow = TRUE;
1395 }
1396
1397 /*
1398 * The current cstack will be freed when do_cmdline() returns. An uncaught
1399 * exception will have to be rethrown in the previous cstack. If a function
1400 * has just returned or a script file was just finished and the previous
1401 * cstack belongs to the same function or, respectively, script file, it
1402 * will have to be checked for finally clauses to be executed due to the
1403 * ":return" or ":finish". This is done in do_one_cmd().
1404 */
1405 if (did_throw)
1406 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001407 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001409 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 && ex_nesting_level > func_level(real_cookie) + 1))
1411 {
1412 if (!did_throw)
1413 check_cstack = TRUE;
1414 }
1415 else
1416 {
1417 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001418 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 --ex_nesting_level;
1420 /*
1421 * Go to debug mode when returning from a function in which we are
1422 * single-stepping.
1423 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001424 if ((getline_equal(fgetline, cookie, getsourceline)
1425 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001427 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 ? (char_u *)_("End of sourced file")
1429 : (char_u *)_("End of function"));
1430 }
1431
1432 /*
1433 * Restore the exception environment (done after returning from the
1434 * debugger).
1435 */
1436 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001437 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 msg_list = saved_msg_list;
1440#endif /* FEAT_EVAL */
1441
1442 /*
1443 * If there was too much output to fit on the command line, ask the user to
1444 * hit return before redrawing the screen. With the ":global" command we do
1445 * this only once after the command is finished.
1446 */
1447 if (did_inc)
1448 {
1449 --RedrawingDisabled;
1450 --no_wait_return;
1451 msg_scroll = FALSE;
1452
1453 /*
1454 * When just finished an ":if"-":else" which was typed, no need to
1455 * wait for hit-return. Also for an error situation.
1456 */
1457 if (retval == FAIL
1458#ifdef FEAT_EVAL
1459 || (did_endif && KeyTyped && !did_emsg)
1460#endif
1461 )
1462 {
1463 need_wait_return = FALSE;
1464 msg_didany = FALSE; /* don't wait when restarting edit */
1465 }
1466 else if (need_wait_return)
1467 {
1468 /*
1469 * The msg_start() above clears msg_didout. The wait_return we do
1470 * here should not overwrite the command that may be shown before
1471 * doing that.
1472 */
1473 msg_didout |= msg_didout_before_start;
1474 wait_return(FALSE);
1475 }
1476 }
1477
Bram Moolenaar2a942252012-11-28 23:03:07 +01001478#ifdef FEAT_EVAL
1479 did_endif = FALSE; /* in case do_cmdline used recursively */
1480#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 /*
1482 * Reset if_level, in case a sourced script file contains more ":if" than
1483 * ":endif" (could be ":if x | foo | endif").
1484 */
1485 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001486#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 --call_depth;
1489 return retval;
1490}
1491
1492#ifdef FEAT_EVAL
1493/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001497get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 wcmd_T *wp;
1501 char_u *line;
1502
1503 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1504 {
1505 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (cp->getline == NULL)
1510 line = getcmdline(c, 0L, indent);
1511 else
1512 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001513 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 ++cp->current_line;
1515
1516 return line;
1517 }
1518
1519 KeyTyped = FALSE;
1520 ++cp->current_line;
1521 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1522 sourcing_lnum = wp->lnum;
1523 return vim_strsave(wp->line);
1524}
1525
1526/*
1527 * Store a line in "gap" so that a ":while" loop can execute it again.
1528 */
1529 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001530store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 if (ga_grow(gap, 1) == FAIL)
1533 return FAIL;
1534 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1535 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1536 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return OK;
1538}
1539
1540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 */
1543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001544free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 while (gap->ga_len > 0)
1547 {
1548 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1549 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
1551}
1552#endif
1553
1554/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001555 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1556 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001559getline_equal(
1560 char_u *(*fgetline)(int, void *, int),
1561 void *cookie UNUSED, /* argument for fgetline() */
1562 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001565 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaar89d40322006-08-29 15:30:07 +00001568 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001569 * function that's originally used to obtain the lines. This may be
1570 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001571 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572 cp = (struct loop_cookie *)cookie;
1573 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 {
1575 gp = cp->getline;
1576 cp = cp->cookie;
1577 }
1578 return gp == func;
1579#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001580 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581#endif
1582}
1583
1584#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1585/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 * getline function. Otherwise return "cookie".
1588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001590getline_cookie(
1591 char_u *(*fgetline)(int, void *, int) UNUSED,
1592 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
1594# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001595 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001596 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaar89d40322006-08-29 15:30:07 +00001598 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001599 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001601 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 cp = (struct loop_cookie *)cookie;
1603 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 gp = cp->getline;
1606 cp = cp->cookie;
1607 }
1608 return cp;
1609# else
1610 return cookie;
1611# endif
1612}
1613#endif
1614
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615
1616/*
1617 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1618 * ":bwipeout", etc.
1619 * Returns the buffer number.
1620 */
1621 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001622compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623{
1624 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 int count = offset;
1627
1628 buf = firstbuf;
1629 while (buf->b_next != NULL && buf->b_fnum < lnum)
1630 buf = buf->b_next;
1631 while (count != 0)
1632 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001633 count += (offset < 0) ? 1 : -1;
1634 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1635 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001636 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001637 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638 if (addr_type == ADDR_LOADED_BUFFERS)
1639 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001640 while (buf->b_ml.ml_mfp == NULL)
1641 {
1642 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1643 if (nextbuf == NULL)
1644 break;
1645 buf = nextbuf;
1646 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001647 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001648 /* we might have gone too far, last buffer is not loadedd */
1649 if (addr_type == ADDR_LOADED_BUFFERS)
1650 while (buf->b_ml.ml_mfp == NULL)
1651 {
1652 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1653 if (nextbuf == NULL)
1654 break;
1655 buf = nextbuf;
1656 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657 return buf->b_fnum;
1658}
1659
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001660static int current_win_nr(win_T *win);
1661static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662
1663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665{
1666 win_T *wp;
1667 int nr = 0;
1668
Bram Moolenaar29323592016-07-24 22:04:11 +02001669 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670 {
1671 ++nr;
1672 if (wp == win)
1673 break;
1674 }
1675 return nr;
1676}
1677
1678 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001679current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001680{
1681 tabpage_T *tp;
1682 int nr = 0;
1683
Bram Moolenaar29323592016-07-24 22:04:11 +02001684 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001685 {
1686 ++nr;
1687 if (tp == tab)
1688 break;
1689 }
1690 return nr;
1691}
1692
1693# define CURRENT_WIN_NR current_win_nr(curwin)
1694# define LAST_WIN_NR current_win_nr(NULL)
1695# define CURRENT_TAB_NR current_tab_nr(curtab)
1696# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698/*
1699 * Execute one Ex command.
1700 *
1701 * If 'sourcing' is TRUE, the command will be included in the error message.
1702 *
1703 * 1. skip comment lines and leading space
1704 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001705 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001707 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 6. parse arguments
1709 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001711 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
1713 * This function may be called recursively!
1714 */
1715#if (_MSC_VER == 1200)
1716/*
Bram Moolenaared203462004-06-16 11:19:22 +00001717 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001719 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001722do_one_cmd(
1723 char_u **cmdlinep,
1724 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001726 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001728 char_u *(*fgetline)(int, void *, int),
1729 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730{
1731 char_u *p;
1732 linenr_T lnum;
1733 long n;
1734 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001735 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 exarg_T ea; /* Ex command arguments */
1737 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001738 int save_msg_scroll = msg_scroll;
1739 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001741#ifdef HAVE_SANDBOX
1742 int did_sandbox = FALSE;
1743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 cmdmod_T save_cmdmod;
1745 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001746 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001747 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749 vim_memset(&ea, 0, sizeof(ea));
1750 ea.line1 = 1;
1751 ea.line2 = 1;
1752#ifdef FEAT_EVAL
1753 ++ex_nesting_level;
1754#endif
1755
Bram Moolenaar21691f82012-12-05 19:13:18 +01001756 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (quitmore
1758#ifdef FEAT_EVAL
1759 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001761#endif
1762#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001763 /* avoid that an autocommand, e.g. QuitPre, does this */
1764 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
1766 )
1767 --quitmore;
1768
1769 /*
1770 * Reset browse, confirm, etc.. They are restored when returning, for
1771 * recursive calls.
1772 */
1773 save_cmdmod = cmdmod;
1774 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1775
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001776 /* "#!anything" is handled like a comment. */
1777 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1778 goto doend;
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 /*
1781 * Repeat until no more command modifiers are found.
1782 */
1783 ea.cmd = *cmdlinep;
1784 for (;;)
1785 {
1786/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001787 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 */
1789 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1790 ++ea.cmd;
1791
1792 /* in ex mode, an empty line works like :+ */
1793 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001794 && (getline_equal(fgetline, cookie, getexmodeline)
1795 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001796 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798 ea.cmd = (char_u *)"+";
1799 ex_pressedreturn = TRUE;
1800 }
1801
1802 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001803 if (*ea.cmd == '"')
1804 goto doend;
1805 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001806 {
1807 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001812 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001814 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 switch (*p)
1816 {
1817 /* When adding an entry, also modify cmd_exists(). */
1818 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 continue;
1822
1823 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 continue;
1827 }
1828 if (checkforcmd(&ea.cmd, "browse", 3))
1829 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001830#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 cmdmod.browse = TRUE;
1832#endif
1833 continue;
1834 }
1835 if (!checkforcmd(&ea.cmd, "botright", 2))
1836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 continue;
1839
1840 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1841 break;
1842#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1843 cmdmod.confirm = TRUE;
1844#endif
1845 continue;
1846
1847 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1848 {
1849 cmdmod.keepmarks = TRUE;
1850 continue;
1851 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001852 if (checkforcmd(&ea.cmd, "keepalt", 5))
1853 {
1854 cmdmod.keepalt = TRUE;
1855 continue;
1856 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001857 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1858 {
1859 cmdmod.keeppatterns = TRUE;
1860 continue;
1861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1863 break;
1864 cmdmod.keepjumps = TRUE;
1865 continue;
1866
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001867 case 'f': /* only accept ":filter {pat} cmd" */
1868 {
1869 char_u *reg_pat;
1870
1871 if (!checkforcmd(&p, "filter", 4)
1872 || *p == NUL || ends_excmd(*p))
1873 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001874 if (*p == '!')
1875 {
1876 cmdmod.filter_force = TRUE;
1877 p = skipwhite(p + 1);
1878 if (*p == NUL || ends_excmd(*p))
1879 break;
1880 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001881 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1882 if (p == NULL || *p == NUL)
1883 break;
1884 cmdmod.filter_regmatch.regprog =
1885 vim_regcomp(reg_pat, RE_MAGIC);
1886 if (cmdmod.filter_regmatch.regprog == NULL)
1887 break;
1888 ea.cmd = p;
1889 continue;
1890 }
1891
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 /* ":hide" and ":hide | cmd" are not modifiers */
1893 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1894 || *p == NUL || ends_excmd(*p))
1895 break;
1896 ea.cmd = p;
1897 cmdmod.hide = TRUE;
1898 continue;
1899
1900 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1901 {
1902 cmdmod.lockmarks = TRUE;
1903 continue;
1904 }
1905
1906 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 continue;
1910
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001911 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001912 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001913#ifdef FEAT_AUTOCMD
1914 if (cmdmod.save_ei == NULL)
1915 {
1916 /* Set 'eventignore' to "all". Restore the
1917 * existing option value later. */
1918 cmdmod.save_ei = vim_strsave(p_ei);
1919 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001920 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001921 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001922#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001923 continue;
1924 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001925 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001926 break;
1927 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001928 continue;
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 continue;
1934
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001935 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1936 {
1937#ifdef HAVE_SANDBOX
1938 if (!did_sandbox)
1939 ++sandbox;
1940 did_sandbox = TRUE;
1941#endif
1942 continue;
1943 }
1944 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001946 if (save_msg_silent == -1)
1947 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001949 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 /* ":silent!", but not "silent !cmd" */
1952 ea.cmd = skipwhite(ea.cmd + 1);
1953 ++emsg_silent;
1954 ++did_esilent;
1955 }
1956 continue;
1957
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001958 case 't': if (checkforcmd(&p, "tab", 3))
1959 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001960 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001961 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001962 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001963 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001964 else
1965 {
1966 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1967 {
1968 errormsg = (char_u *)_(e_invrange);
1969 goto doend;
1970 }
1971 cmdmod.tab = tabnr + 1;
1972 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001973 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001974 continue;
1975 }
1976 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 continue;
1980
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001981 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1982 break;
1983 if (save_msg_silent == -1)
1984 save_msg_silent = msg_silent;
1985 msg_silent = 0;
1986 continue;
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 continue;
1992 }
1993 if (!checkforcmd(&p, "verbose", 4))
1994 break;
1995 if (verbose_save < 0)
1996 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001997 if (vim_isdigit(*ea.cmd))
1998 p_verbose = atoi((char *)ea.cmd);
1999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 p_verbose = 1;
2001 ea.cmd = p;
2002 continue;
2003 }
2004 break;
2005 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002006 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
2008#ifdef FEAT_EVAL
2009 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2010 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2011#else
2012 ea.skip = (if_level > 0);
2013#endif
2014
2015#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002016# ifdef FEAT_PROFILE
2017 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002018 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002019 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002020 if (getline_equal(fgetline, cookie, get_func_line))
2021 func_line_exec(getline_cookie(fgetline, cookie));
2022 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002023 script_line_exec();
2024 }
2025#endif
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 /* May go to debug mode. If this happens and the ">quit" debug command is
2028 * used, throw an interrupt exception and skip the next command. */
2029 dbg_check_breakpoint(&ea);
2030 if (!ea.skip && got_int)
2031 {
2032 ea.skip = TRUE;
2033 (void)do_intthrow(cstack);
2034 }
2035#endif
2036
2037/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002038 * 3. Skip over the range to find the command. Let "p" point to after it.
2039 *
2040 * We need the command to know what kind of range it uses.
2041 */
2042 cmd = ea.cmd;
2043 ea.cmd = skip_range(ea.cmd, NULL);
2044 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2045 ea.cmd = skipwhite(ea.cmd + 1);
2046 p = find_command(&ea, NULL);
2047
2048/*
2049 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 *
2051 * where 'addr' is:
2052 *
2053 * % (entire file)
2054 * $ [+-NUM]
2055 * 'x [+-NUM] (where x denotes a currently defined mark)
2056 * . [+-NUM]
2057 * [+-NUM]..
2058 * NUM
2059 *
2060 * The ea.cmd pointer is updated to point to the first character following the
2061 * range spec. If an initial address is found, but no second, the upper bound
2062 * is equal to the lower.
2063 */
2064
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002065 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002066 if (!IS_USER_CMDIDX(ea.cmdidx))
2067 {
2068 if (ea.cmdidx != CMD_SIZE)
2069 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2070 else
2071 ea.addr_type = ADDR_LINES;
2072
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002073 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002074 if (ea.cmdidx == CMD_wincmd && p != NULL)
2075 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002076 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002079 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 for (;;)
2081 {
2082 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002083 switch (ea.addr_type)
2084 {
2085 case ADDR_LINES:
2086 /* default is current line number */
2087 ea.line2 = curwin->w_cursor.lnum;
2088 break;
2089 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002090 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002091 break;
2092 case ADDR_ARGUMENTS:
2093 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002094 if (ea.line2 > ARGCOUNT)
2095 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002096 break;
2097 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002098 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002099 ea.line2 = curbuf->b_fnum;
2100 break;
2101 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002102 ea.line2 = CURRENT_TAB_NR;
2103 break;
2104 case ADDR_TABS_RELATIVE:
2105 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002106 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002107#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002108 case ADDR_QUICKFIX:
2109 ea.line2 = qf_get_cur_valid_idx(&ea);
2110 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002111#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002114 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002115 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (ea.cmd == NULL) /* error detected */
2117 goto doend;
2118 if (lnum == MAXLNUM)
2119 {
2120 if (*ea.cmd == '%') /* '%' - all lines */
2121 {
2122 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002123 switch (ea.addr_type)
2124 {
2125 case ADDR_LINES:
2126 ea.line1 = 1;
2127 ea.line2 = curbuf->b_ml.ml_line_count;
2128 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002129 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002130 {
2131 buf_T *buf = firstbuf;
2132
2133 while (buf->b_next != NULL
2134 && buf->b_ml.ml_mfp == NULL)
2135 buf = buf->b_next;
2136 ea.line1 = buf->b_fnum;
2137 buf = lastbuf;
2138 while (buf->b_prev != NULL
2139 && buf->b_ml.ml_mfp == NULL)
2140 buf = buf->b_prev;
2141 ea.line2 = buf->b_fnum;
2142 break;
2143 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002144 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002145 ea.line1 = firstbuf->b_fnum;
2146 ea.line2 = lastbuf->b_fnum;
2147 break;
2148 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002149 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002150 if (IS_USER_CMDIDX(ea.cmdidx))
2151 {
2152 ea.line1 = 1;
2153 ea.line2 = ea.addr_type == ADDR_WINDOWS
2154 ? LAST_WIN_NR : LAST_TAB_NR;
2155 }
2156 else
2157 {
2158 /* there is no Vim command which uses '%' and
2159 * ADDR_WINDOWS or ADDR_TABS */
2160 errormsg = (char_u *)_(e_invrange);
2161 goto doend;
2162 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002163 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002164 case ADDR_TABS_RELATIVE:
2165 errormsg = (char_u *)_(e_invrange);
2166 goto doend;
2167 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002168 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002169 if (ARGCOUNT == 0)
2170 ea.line1 = ea.line2 = 0;
2171 else
2172 {
2173 ea.line1 = 1;
2174 ea.line2 = ARGCOUNT;
2175 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002176 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002177#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002178 case ADDR_QUICKFIX:
2179 ea.line1 = 1;
2180 ea.line2 = qf_get_size(&ea);
2181 if (ea.line2 == 0)
2182 ea.line2 = 1;
2183 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002184#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 ++ea.addr_count;
2187 }
2188 /* '*' - visual area */
2189 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2190 {
2191 pos_T *fp;
2192
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002193 if (ea.addr_type != ADDR_LINES)
2194 {
2195 errormsg = (char_u *)_(e_invrange);
2196 goto doend;
2197 }
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 ++ea.cmd;
2200 if (!ea.skip)
2201 {
2202 fp = getmark('<', FALSE);
2203 if (check_mark(fp) == FAIL)
2204 goto doend;
2205 ea.line1 = fp->lnum;
2206 fp = getmark('>', FALSE);
2207 if (check_mark(fp) == FAIL)
2208 goto doend;
2209 ea.line2 = fp->lnum;
2210 ++ea.addr_count;
2211 }
2212 }
2213 }
2214 else
2215 ea.line2 = lnum;
2216 ea.addr_count++;
2217
2218 if (*ea.cmd == ';')
2219 {
2220 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002223 /* don't leave the cursor on an illegal line or column */
2224 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227 else if (*ea.cmd != ',')
2228 break;
2229 ++ea.cmd;
2230 }
2231
2232 /* One address given: set start and end lines */
2233 if (ea.addr_count == 1)
2234 {
2235 ea.line1 = ea.line2;
2236 /* ... but only implicit: really no address given */
2237 if (lnum == MAXLNUM)
2238 ea.addr_count = 0;
2239 }
2240
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002242 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 */
2244
2245 /*
2246 * Skip ':' and any white space
2247 */
2248 ea.cmd = skipwhite(ea.cmd);
2249 while (*ea.cmd == ':')
2250 ea.cmd = skipwhite(ea.cmd + 1);
2251
2252 /*
2253 * If we got a line, but no command, then go to the line.
2254 * If we find a '|' or '\n' we set ea.nextcmd.
2255 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002256 if (*ea.cmd == NUL || *ea.cmd == '"'
2257 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 /*
2260 * strange vi behaviour:
2261 * ":3" jumps to line 3
2262 * ":3|..." prints line 3
2263 * ":|" prints current line
2264 */
2265 if (ea.skip) /* skip this if inside :if */
2266 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002267 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 ea.cmdidx = CMD_print;
2270 ea.argt = RANGE+COUNT+TRLBAR;
2271 if ((errormsg = invalid_range(&ea)) == NULL)
2272 {
2273 correct_range(&ea);
2274 ex_print(&ea);
2275 }
2276 }
2277 else if (ea.addr_count != 0)
2278 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002279 if (ea.line2 > curbuf->b_ml.ml_line_count)
2280 {
2281 /* With '-' in 'cpoptions' a line number past the file is an
2282 * error, otherwise put it at the end of the file. */
2283 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2284 ea.line2 = -1;
2285 else
2286 ea.line2 = curbuf->b_ml.ml_line_count;
2287 }
2288
2289 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002290 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 else
2292 {
2293 if (ea.line2 == 0)
2294 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 else
2296 curwin->w_cursor.lnum = ea.line2;
2297 beginline(BL_SOL | BL_FIX);
2298 }
2299 }
2300 goto doend;
2301 }
2302
Bram Moolenaard5005162014-08-22 23:05:54 +02002303#ifdef FEAT_AUTOCMD
2304 /* If this looks like an undefined user command and there are CmdUndefined
2305 * autocommands defined, trigger the matching autocommands. */
2306 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2307 && ASCII_ISUPPER(*ea.cmd)
2308 && has_cmdundefined())
2309 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002310 int ret;
2311
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002312 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002313 while (ASCII_ISALNUM(*p))
2314 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002315 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002316 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2317 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002318 /* If the autocommands did something and didn't cause an error, try
2319 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002320 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002321 }
2322#endif
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#ifdef FEAT_USR_CMDS
2325 if (p == NULL)
2326 {
2327 if (!ea.skip)
2328 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2329 goto doend;
2330 }
2331 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002332 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2333 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
2335 errormsg = uc_fun_cmd();
2336 goto doend;
2337 }
2338#endif
2339 if (ea.cmdidx == CMD_SIZE)
2340 {
2341 if (!ea.skip)
2342 {
2343 STRCPY(IObuff, _("E492: Not an editor command"));
2344 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002345 {
2346 /* If the modifier was parsed OK the error must be in the
2347 * following command */
2348 if (after_modifier != NULL)
2349 append_command(after_modifier);
2350 else
2351 append_command(*cmdlinep);
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002354 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356 goto doend;
2357 }
2358
Bram Moolenaar958636c2014-10-21 20:01:58 +02002359 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2360 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002361#ifdef HAVE_EX_SCRIPT_NI
2362 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2363#endif
2364 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366#ifndef FEAT_EVAL
2367 /*
2368 * When the expression evaluation is disabled, recognize the ":if" and
2369 * ":endif" commands and ignore everything in between it.
2370 */
2371 if (ea.cmdidx == CMD_if)
2372 ++if_level;
2373 if (if_level)
2374 {
2375 if (ea.cmdidx == CMD_endif)
2376 --if_level;
2377 goto doend;
2378 }
2379
2380#endif
2381
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002382 /* forced commands */
2383 if (*p == '!' && ea.cmdidx != CMD_substitute
2384 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
2386 ++p;
2387 ea.forceit = TRUE;
2388 }
2389 else
2390 ea.forceit = FALSE;
2391
2392/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002393 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002395 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002396 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 if (!ea.skip)
2399 {
2400#ifdef HAVE_SANDBOX
2401 if (sandbox != 0 && !(ea.argt & SBOXOK))
2402 {
2403 /* Command not allowed in sandbox. */
2404 errormsg = (char_u *)_(e_sandbox);
2405 goto doend;
2406 }
2407#endif
2408 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2409 {
2410 /* Command not allowed in non-'modifiable' buffer */
2411 errormsg = (char_u *)_(e_modifiable);
2412 goto doend;
2413 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002414
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002415 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002416 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002418 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002419 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 goto doend;
2421 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002422#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002423 /* Disallow editing another buffer when "curbuf_lock" is set.
2424 * Do allow ":edit" (check for argument later).
2425 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002426 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002427 && ea.cmdidx != CMD_edit
2428 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002429 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002430 && curbuf_locked())
2431 goto doend;
2432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2435 {
2436 /* no range allowed */
2437 errormsg = (char_u *)_(e_norange);
2438 goto doend;
2439 }
2440 }
2441
2442 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2443 {
2444 errormsg = (char_u *)_(e_nobang);
2445 goto doend;
2446 }
2447
2448 /*
2449 * Don't complain about the range if it is not used
2450 * (could happen if line_count is accidentally set to 0).
2451 */
2452 if (!ea.skip && !ni)
2453 {
2454 /*
2455 * If the range is backwards, ask for confirmation and, if given, swap
2456 * ea.line1 & ea.line2 so it's forwards again.
2457 * When global command is busy, don't ask, will fail below.
2458 */
2459 if (!global_busy && ea.line1 > ea.line2)
2460 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002461 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002463 if (sourcing || exmode_active)
2464 {
2465 errormsg = (char_u *)_("E493: Backwards range given");
2466 goto doend;
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (ask_yesno((char_u *)
2469 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002470 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472 lnum = ea.line1;
2473 ea.line1 = ea.line2;
2474 ea.line2 = lnum;
2475 }
2476 if ((errormsg = invalid_range(&ea)) != NULL)
2477 goto doend;
2478 }
2479
2480 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2481 ea.line2 = 1;
2482
2483 correct_range(&ea);
2484
2485#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002486 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2487 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 /* Put the first line at the start of a closed fold, put the last line
2490 * at the end of a closed fold. */
2491 (void)hasFolding(ea.line1, &ea.line1, NULL);
2492 (void)hasFolding(ea.line2, NULL, &ea.line2);
2493 }
2494#endif
2495
2496#ifdef FEAT_QUICKFIX
2497 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002498 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * option here, so things like % get expanded.
2500 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002501 p = replace_makeprg(&ea, p, cmdlinep);
2502 if (p == NULL)
2503 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504#endif
2505
2506 /*
2507 * Skip to start of argument.
2508 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2509 */
2510 if (ea.cmdidx == CMD_bang)
2511 ea.arg = p;
2512 else
2513 ea.arg = skipwhite(p);
2514
2515 /*
2516 * Check for "++opt=val" argument.
2517 * Must be first, allow ":w ++enc=utf8 !cmd"
2518 */
2519 if (ea.argt & ARGOPT)
2520 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2521 if (getargopt(&ea) == FAIL && !ni)
2522 {
2523 errormsg = (char_u *)_(e_invarg);
2524 goto doend;
2525 }
2526
2527 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2528 {
2529 if (*ea.arg == '>') /* append */
2530 {
2531 if (*++ea.arg != '>') /* typed wrong */
2532 {
2533 errormsg = (char_u *)_("E494: Use w or w>>");
2534 goto doend;
2535 }
2536 ea.arg = skipwhite(ea.arg + 1);
2537 ea.append = TRUE;
2538 }
2539 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2540 {
2541 ++ea.arg;
2542 ea.usefilter = TRUE;
2543 }
2544 }
2545
2546 if (ea.cmdidx == CMD_read)
2547 {
2548 if (ea.forceit)
2549 {
2550 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2551 ea.forceit = FALSE;
2552 }
2553 else if (*ea.arg == '!') /* :r !filter */
2554 {
2555 ++ea.arg;
2556 ea.usefilter = TRUE;
2557 }
2558 }
2559
2560 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2561 {
2562 ea.amount = 1;
2563 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2564 {
2565 ++ea.arg;
2566 ++ea.amount;
2567 }
2568 ea.arg = skipwhite(ea.arg);
2569 }
2570
2571 /*
2572 * Check for "+command" argument, before checking for next command.
2573 * Don't do this for ":read !cmd" and ":write !cmd".
2574 */
2575 if ((ea.argt & EDITCMD) && !ea.usefilter)
2576 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2577
2578 /*
2579 * Check for '|' to separate commands and '"' to start comments.
2580 * Don't do this for ":read !cmd" and ":write !cmd".
2581 */
2582 if ((ea.argt & TRLBAR) && !ea.usefilter)
2583 separate_nextcmd(&ea);
2584
2585 /*
2586 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002587 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2588 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002590 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002591 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002592 || ea.cmdidx == CMD_global
2593 || ea.cmdidx == CMD_vglobal
2594 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
2596 for (p = ea.arg; *p; ++p)
2597 {
2598 /* Remove one backslash before a newline, so that it's possible to
2599 * pass a newline to the shell and also a newline that is preceded
2600 * with a backslash. This makes it impossible to end a shell
2601 * command in a backslash, but that doesn't appear useful.
2602 * Halving the number of backslashes is incompatible with previous
2603 * versions. */
2604 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002605 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else if (*p == '\n')
2607 {
2608 ea.nextcmd = p + 1;
2609 *p = NUL;
2610 break;
2611 }
2612 }
2613 }
2614
2615 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2616 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002617 buf_T *buf;
2618
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002620 switch (ea.addr_type)
2621 {
2622 case ADDR_LINES:
2623 ea.line2 = curbuf->b_ml.ml_line_count;
2624 break;
2625 case ADDR_LOADED_BUFFERS:
2626 buf = firstbuf;
2627 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2628 buf = buf->b_next;
2629 ea.line1 = buf->b_fnum;
2630 buf = lastbuf;
2631 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2632 buf = buf->b_prev;
2633 ea.line2 = buf->b_fnum;
2634 break;
2635 case ADDR_BUFFERS:
2636 ea.line1 = firstbuf->b_fnum;
2637 ea.line2 = lastbuf->b_fnum;
2638 break;
2639 case ADDR_WINDOWS:
2640 ea.line2 = LAST_WIN_NR;
2641 break;
2642 case ADDR_TABS:
2643 ea.line2 = LAST_TAB_NR;
2644 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002645 case ADDR_TABS_RELATIVE:
2646 ea.line2 = 1;
2647 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002648 case ADDR_ARGUMENTS:
2649 if (ARGCOUNT == 0)
2650 ea.line1 = ea.line2 = 0;
2651 else
2652 ea.line2 = ARGCOUNT;
2653 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002654#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002655 case ADDR_QUICKFIX:
2656 ea.line2 = qf_get_size(&ea);
2657 if (ea.line2 == 0)
2658 ea.line2 = 1;
2659 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002660#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663
2664 /* accept numbered register only when no count allowed (:put) */
2665 if ( (ea.argt & REGSTR)
2666 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002667 /* Do not allow register = for user commands */
2668 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2670 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002671#ifndef FEAT_CLIPBOARD
2672 /* check these explicitly for a more specific error message */
2673 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002675 errormsg = (char_u *)_(e_invalidreg);
2676 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002679 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2680 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002681 {
2682 ea.regname = *ea.arg++;
2683#ifdef FEAT_EVAL
2684 /* for '=' register: accept the rest of the line as an expression */
2685 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2686 {
2687 set_expr_line(vim_strsave(ea.arg));
2688 ea.arg += STRLEN(ea.arg);
2689 }
2690#endif
2691 ea.arg = skipwhite(ea.arg);
2692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694
2695 /*
2696 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2697 * count, it's a buffer name.
2698 */
2699 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2700 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002701 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
2703 n = getdigits(&ea.arg);
2704 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002705 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 errormsg = (char_u *)_(e_zerocount);
2708 goto doend;
2709 }
2710 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2711 {
2712 ea.line2 = n;
2713 if (ea.addr_count == 0)
2714 ea.addr_count = 1;
2715 }
2716 else
2717 {
2718 ea.line1 = ea.line2;
2719 ea.line2 += n - 1;
2720 ++ea.addr_count;
2721 /*
2722 * Be vi compatible: no error message for out of range.
2723 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002724 if (ea.addr_type == ADDR_LINES
2725 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 ea.line2 = curbuf->b_ml.ml_line_count;
2727 }
2728 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002729
2730 /*
2731 * Check for flags: 'l', 'p' and '#'.
2732 */
2733 if (ea.argt & EXFLAGS)
2734 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002736 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002737 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
2739 errormsg = (char_u *)_(e_trailing);
2740 goto doend;
2741 }
2742
2743 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2744 {
2745 errormsg = (char_u *)_(e_argreq);
2746 goto doend;
2747 }
2748
2749#ifdef FEAT_EVAL
2750 /*
2751 * Skip the command when it's not going to be executed.
2752 * The commands like :if, :endif, etc. always need to be executed.
2753 * Also make an exception for commands that handle a trailing command
2754 * themselves.
2755 */
2756 if (ea.skip)
2757 {
2758 switch (ea.cmdidx)
2759 {
2760 /* commands that need evaluation */
2761 case CMD_while:
2762 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002763 case CMD_for:
2764 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 case CMD_if:
2766 case CMD_elseif:
2767 case CMD_else:
2768 case CMD_endif:
2769 case CMD_try:
2770 case CMD_catch:
2771 case CMD_finally:
2772 case CMD_endtry:
2773 case CMD_function:
2774 break;
2775
2776 /* Commands that handle '|' themselves. Check: A command should
2777 * either have the TRLBAR flag, appear in this list or appear in
2778 * the list at ":help :bar". */
2779 case CMD_aboveleft:
2780 case CMD_and:
2781 case CMD_belowright:
2782 case CMD_botright:
2783 case CMD_browse:
2784 case CMD_call:
2785 case CMD_confirm:
2786 case CMD_delfunction:
2787 case CMD_djump:
2788 case CMD_dlist:
2789 case CMD_dsearch:
2790 case CMD_dsplit:
2791 case CMD_echo:
2792 case CMD_echoerr:
2793 case CMD_echomsg:
2794 case CMD_echon:
2795 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002796 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 case CMD_help:
2798 case CMD_hide:
2799 case CMD_ijump:
2800 case CMD_ilist:
2801 case CMD_isearch:
2802 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002803 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 case CMD_keepjumps:
2805 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002806 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 case CMD_leftabove:
2808 case CMD_let:
2809 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002810 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002812 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002813 case CMD_noautocmd:
2814 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 case CMD_perl:
2816 case CMD_psearch:
2817 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002818 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002819 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case CMD_return:
2821 case CMD_rightbelow:
2822 case CMD_ruby:
2823 case CMD_silent:
2824 case CMD_smagic:
2825 case CMD_snomagic:
2826 case CMD_substitute:
2827 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002828 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 case CMD_tcl:
2830 case CMD_throw:
2831 case CMD_tilde:
2832 case CMD_topleft:
2833 case CMD_unlet:
2834 case CMD_verbose:
2835 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002836 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 break;
2838
2839 default: goto doend;
2840 }
2841 }
2842#endif
2843
2844 if (ea.argt & XFILE)
2845 {
2846 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2847 goto doend;
2848 }
2849
2850#ifdef FEAT_LISTCMDS
2851 /*
2852 * Accept buffer name. Cannot be used at the same time with a buffer
2853 * number. Don't do this for a user command.
2854 */
2855 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002856 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 /*
2859 * :bdelete, :bwipeout and :bunload take several arguments, separated
2860 * by spaces: find next space (skipping over escaped characters).
2861 * The others take one argument: ignore trailing spaces.
2862 */
2863 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2864 || ea.cmdidx == CMD_bunload)
2865 p = skiptowhite_esc(ea.arg);
2866 else
2867 {
2868 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002869 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 --p;
2871 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002872 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2873 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (ea.line2 < 0) /* failed */
2875 goto doend;
2876 ea.addr_count = 1;
2877 ea.arg = skipwhite(p);
2878 }
2879#endif
2880
2881/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002882 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 *
2884 * The "ea" structure holds the arguments that can be used.
2885 */
2886 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002887 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 ea.cookie = cookie;
2889#ifdef FEAT_EVAL
2890 ea.cstack = cstack;
2891#endif
2892
2893#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002894 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 /*
2897 * Execute a user-defined command.
2898 */
2899 do_ucmd(&ea);
2900 }
2901 else
2902#endif
2903 {
2904 /*
2905 * Call the function to execute the command.
2906 */
2907 ea.errmsg = NULL;
2908 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2909 if (ea.errmsg != NULL)
2910 errormsg = (char_u *)_(ea.errmsg);
2911 }
2912
2913#ifdef FEAT_EVAL
2914 /*
2915 * If the command just executed called do_cmdline(), any throw or ":return"
2916 * or ":finish" encountered there must also check the cstack of the still
2917 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2918 * exception, or reanimate a returned function or finished script file and
2919 * return or finish it again.
2920 */
2921 if (need_rethrow)
2922 do_throw(cstack);
2923 else if (check_cstack)
2924 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002925 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002927 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 && current_func_returned())
2929 do_return(&ea, TRUE, FALSE, NULL);
2930 }
2931 need_rethrow = check_cstack = FALSE;
2932#endif
2933
2934doend:
2935 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002938 curwin->w_cursor.col = 0;
2939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2942 {
2943 if (sourcing)
2944 {
2945 if (errormsg != IObuff)
2946 {
2947 STRCPY(IObuff, errormsg);
2948 errormsg = IObuff;
2949 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002950 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952 emsg(errormsg);
2953 }
2954#ifdef FEAT_EVAL
2955 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002956 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2957 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif
2959
2960 if (verbose_save >= 0)
2961 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002962#ifdef FEAT_AUTOCMD
2963 if (cmdmod.save_ei != NULL)
2964 {
2965 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002966 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2967 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002968 free_string_option(cmdmod.save_ei);
2969 }
2970#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002971 if (cmdmod.filter_regmatch.regprog != NULL)
2972 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 cmdmod = save_cmdmod;
2975
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002976 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
2978 /* messages could be enabled for a serious error, need to check if the
2979 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002980 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002981 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 emsg_silent -= did_esilent;
2983 if (emsg_silent < 0)
2984 emsg_silent = 0;
2985 /* Restore msg_scroll, it's set by file I/O commands, even when no
2986 * message is actually displayed. */
2987 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002988
2989 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2990 * somewhere in the line. Put it back in the first column. */
2991 if (redirecting())
2992 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002995#ifdef HAVE_SANDBOX
2996 if (did_sandbox)
2997 --sandbox;
2998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3001 ea.nextcmd = NULL;
3002
3003#ifdef FEAT_EVAL
3004 --ex_nesting_level;
3005#endif
3006
3007 return ea.nextcmd;
3008}
3009#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003010 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011#endif
3012
3013/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003014 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * If there is a match advance "pp" to the argument and return TRUE.
3016 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003017 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003018checkforcmd(
3019 char_u **pp, /* start of command */
3020 char *cmd, /* name of command */
3021 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 int i;
3024
3025 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003026 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 break;
3028 if (i >= len && !isalpha((*pp)[i]))
3029 {
3030 *pp = skipwhite(*pp + i);
3031 return TRUE;
3032 }
3033 return FALSE;
3034}
3035
3036/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003037 * Append "cmd" to the error message in IObuff.
3038 * Takes care of limiting the length and handling 0xa0, which would be
3039 * invisible otherwise.
3040 */
3041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003043{
3044 char_u *s = cmd;
3045 char_u *d;
3046
3047 STRCAT(IObuff, ": ");
3048 d = IObuff + STRLEN(IObuff);
3049 while (*s != NUL && d - IObuff < IOSIZE - 7)
3050 {
3051 if (
3052#ifdef FEAT_MBYTE
3053 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3054#endif
3055 *s == 0xa0)
3056 {
3057 s +=
3058#ifdef FEAT_MBYTE
3059 enc_utf8 ? 2 :
3060#endif
3061 1;
3062 STRCPY(d, "<a0>");
3063 d += 4;
3064 }
3065 else
3066 MB_COPY_CHAR(s, d);
3067 }
3068 *d = NUL;
3069}
3070
3071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003073 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003075 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * Returns NULL for an ambiguous user command.
3077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 int len;
3082 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003083 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
3085 /*
3086 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003087 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * - the 'k' command can directly be followed by any character.
3089 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003090 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003092 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
3094 p = eap->cmd;
3095 if (*p == 'k')
3096 {
3097 eap->cmdidx = CMD_k;
3098 ++p;
3099 }
3100 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003101 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3102 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 || p[1] == 'g'
3104 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3105 || p[1] == 'I'
3106 || (p[1] == 'r' && p[2] != 'e')))
3107 {
3108 eap->cmdidx = CMD_substitute;
3109 ++p;
3110 }
3111 else
3112 {
3113 while (ASCII_ISALPHA(*p))
3114 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003115 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003116 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003117 while (ASCII_ISALNUM(*p))
3118 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 /* check for non-alpha command */
3121 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3122 ++p;
3123 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003124 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3125 {
3126 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3127 * :delete with the 'l' flag. Same for 'p'. */
3128 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003129 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003130 break;
3131 if (i == len - 1)
3132 {
3133 --len;
3134 if (p[-1] == 'l')
3135 eap->flags |= EXFLAG_LIST;
3136 else
3137 eap->flags |= EXFLAG_PRINT;
3138 }
3139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003141 if (ASCII_ISLOWER(eap->cmd[0]))
3142 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003143 int c1 = eap->cmd[0];
3144 int c2 = eap->cmd[1];
3145
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003146 if (command_count != (int)CMD_SIZE)
3147 {
3148 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3149 getout(1);
3150 }
3151
3152 /* Use a precomputed index for fast look-up in cmdnames[]
3153 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003154 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3155 if (ASCII_ISLOWER(c2))
3156 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003159 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3162 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3163 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3164 (size_t)len) == 0)
3165 {
3166#ifdef FEAT_EVAL
3167 if (full != NULL
3168 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3169 *full = TRUE;
3170#endif
3171 break;
3172 }
3173
3174#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003175 /* Look for a user defined command as a last resort. Let ":Print" be
3176 * overruled by a user defined command. */
3177 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3178 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 while (ASCII_ISALNUM(*p))
3182 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 eap->cmdidx = CMD_SIZE;
3188 }
3189
3190 return p;
3191}
3192
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193#ifdef FEAT_USR_CMDS
3194/*
3195 * Search for a user command that matches "eap->cmd".
3196 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3197 * Return a pointer to just after the command.
3198 * Return NULL if there is no matching command.
3199 */
3200 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003201find_ucmd(
3202 exarg_T *eap,
3203 char_u *p, /* end of the command (possibly including count) */
3204 int *full, /* set to TRUE for a full match */
3205 expand_T *xp, /* used for completion, NULL otherwise */
3206 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207{
3208 int len = (int)(p - eap->cmd);
3209 int j, k, matchlen = 0;
3210 ucmd_T *uc;
3211 int found = FALSE;
3212 int possible = FALSE;
3213 char_u *cp, *np; /* Point into typed cmd and test name */
3214 garray_T *gap;
3215 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3216 only full match global is accepted. */
3217
3218 /*
3219 * Look for buffer-local user commands first, then global ones.
3220 */
3221 gap = &curbuf->b_ucmds;
3222 for (;;)
3223 {
3224 for (j = 0; j < gap->ga_len; ++j)
3225 {
3226 uc = USER_CMD_GA(gap, j);
3227 cp = eap->cmd;
3228 np = uc->uc_name;
3229 k = 0;
3230 while (k < len && *np != NUL && *cp++ == *np++)
3231 k++;
3232 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3233 {
3234 /* If finding a second match, the command is ambiguous. But
3235 * not if a buffer-local command wasn't a full match and a
3236 * global command is a full match. */
3237 if (k == len && found && *np != NUL)
3238 {
3239 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 amb_local = TRUE;
3242 }
3243
3244 if (!found || (k == len && *np == NUL))
3245 {
3246 /* If we matched up to a digit, then there could
3247 * be another command including the digit that we
3248 * should use instead.
3249 */
3250 if (k == len)
3251 found = TRUE;
3252 else
3253 possible = TRUE;
3254
3255 if (gap == &ucmds)
3256 eap->cmdidx = CMD_USER;
3257 else
3258 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003259 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003261 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262
3263# ifdef FEAT_CMDL_COMPL
3264 if (compl != NULL)
3265 *compl = uc->uc_compl;
3266# ifdef FEAT_EVAL
3267 if (xp != NULL)
3268 {
3269 xp->xp_arg = uc->uc_compl_arg;
3270 xp->xp_scriptID = uc->uc_scriptID;
3271 }
3272# endif
3273# endif
3274 /* Do not search for further abbreviations
3275 * if this is an exact match. */
3276 matchlen = k;
3277 if (k == len && *np == NUL)
3278 {
3279 if (full != NULL)
3280 *full = TRUE;
3281 amb_local = FALSE;
3282 break;
3283 }
3284 }
3285 }
3286 }
3287
3288 /* Stop if we found a full match or searched all. */
3289 if (j < gap->ga_len || gap == &ucmds)
3290 break;
3291 gap = &ucmds;
3292 }
3293
3294 /* Only found ambiguous matches. */
3295 if (amb_local)
3296 {
3297 if (xp != NULL)
3298 xp->xp_context = EXPAND_UNSUCCESSFUL;
3299 return NULL;
3300 }
3301
3302 /* The match we found may be followed immediately by a number. Move "p"
3303 * back to point to it. */
3304 if (found || possible)
3305 return p + (matchlen - len);
3306 return p;
3307}
3308#endif
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003311static struct cmdmod
3312{
3313 char *name;
3314 int minlen;
3315 int has_count; /* :123verbose :3tab */
3316} cmdmods[] = {
3317 {"aboveleft", 3, FALSE},
3318 {"belowright", 3, FALSE},
3319 {"botright", 2, FALSE},
3320 {"browse", 3, FALSE},
3321 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003322 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003323 {"hide", 3, FALSE},
3324 {"keepalt", 5, FALSE},
3325 {"keepjumps", 5, FALSE},
3326 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003327 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003328 {"leftabove", 5, FALSE},
3329 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003330 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003331 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003332 {"rightbelow", 6, FALSE},
3333 {"sandbox", 3, FALSE},
3334 {"silent", 3, FALSE},
3335 {"tab", 3, TRUE},
3336 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003337 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003338 {"verbose", 4, TRUE},
3339 {"vertical", 4, FALSE},
3340};
3341
3342/*
3343 * Return length of a command modifier (including optional count).
3344 * Return zero when it's not a modifier.
3345 */
3346 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003348{
3349 int i, j;
3350 char_u *p = cmd;
3351
3352 if (VIM_ISDIGIT(*cmd))
3353 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003354 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355 {
3356 for (j = 0; p[j] != NUL; ++j)
3357 if (p[j] != cmdmods[i].name[j])
3358 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003359 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003361 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 }
3363 return 0;
3364}
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Return > 0 if an Ex command "name" exists.
3368 * Return 2 if there is an exact match.
3369 * Return 3 if there is an ambiguous match.
3370 */
3371 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003372cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 exarg_T ea;
3375 int full = FALSE;
3376 int i;
3377 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003378 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
3380 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003381 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 for (j = 0; name[j] != NUL; ++j)
3384 if (name[j] != cmdmods[i].name[j])
3385 break;
3386 if (name[j] == NUL && j >= cmdmods[i].minlen)
3387 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3388 }
3389
Bram Moolenaara9587612006-05-04 21:47:50 +00003390 /* Check built-in commands and user defined commands.
3391 * For ":2match" and ":3match" we need to skip the number. */
3392 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003394 p = find_command(&ea, &full);
3395 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003397 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3398 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003399 if (*skipwhite(p) != NUL)
3400 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3402}
3403#endif
3404
3405/*
3406 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3407 * we don't need/want deleted. Maybe this could be done better if we didn't
3408 * repeat all this stuff. The only problem is that they may not stay
3409 * perfectly compatible with each other, but then the command line syntax
3410 * probably won't change that much -- webb.
3411 */
3412 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003413set_one_cmd_context(
3414 expand_T *xp,
3415 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 char_u *p;
3418 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003419 int len = 0;
3420 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3422 int compl = EXPAND_NOTHING;
3423#endif
3424#ifdef FEAT_CMDL_COMPL
3425 int delim;
3426#endif
3427 int forceit = FALSE;
3428 int usefilter = FALSE; /* filter instead of file name */
3429
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003430 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 xp->xp_pattern = buff;
3432 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003433 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003436 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
3438 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3439 ;
3440 xp->xp_pattern = cmd;
3441
3442 if (*cmd == NUL)
3443 return NULL;
3444 if (*cmd == '"') /* ignore comment lines */
3445 {
3446 xp->xp_context = EXPAND_NOTHING;
3447 return NULL;
3448 }
3449
3450/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003451 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
3453 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 xp->xp_pattern = cmd;
3455 if (*cmd == NUL)
3456 return NULL;
3457 if (*cmd == '"')
3458 {
3459 xp->xp_context = EXPAND_NOTHING;
3460 return NULL;
3461 }
3462
3463 if (*cmd == '|' || *cmd == '\n')
3464 return cmd + 1; /* There's another command */
3465
3466 /*
3467 * Isolate the command and search for it in the command table.
3468 * Exceptions:
3469 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003470 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3472 */
3473 if (*cmd == 'k' && cmd[1] != 'e')
3474 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 p = cmd + 1;
3477 }
3478 else
3479 {
3480 p = cmd;
3481 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3482 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003483 /* a user command may contain digits */
3484 if (ASCII_ISUPPER(cmd[0]))
3485 while (ASCII_ISALNUM(*p) || *p == '*')
3486 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003487 /* for python 3.x: ":py3*" commands completion */
3488 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003489 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003490 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003491 while (ASCII_ISALPHA(*p) || *p == '*')
3492 ++p;
3493 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003494 /* check for non-alpha command */
3495 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3496 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 xp->xp_context = EXPAND_UNSUCCESSFUL;
3502 return NULL;
3503 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003505 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3506 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3507 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 break;
3509
3510#ifdef FEAT_USR_CMDS
3511 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3513 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#endif
3515 }
3516
3517 /*
3518 * If the cursor is touching the command, and it ends in an alpha-numeric
3519 * character, complete the command name.
3520 */
3521 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3522 return NULL;
3523
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3527 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003528 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 p = cmd + 1;
3530 }
3531#ifdef FEAT_USR_CMDS
3532 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3533 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 ea.cmd = cmd;
3535 p = find_ucmd(&ea, p, NULL, xp,
3536# if defined(FEAT_CMDL_COMPL)
3537 &compl
3538# else
3539 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003542 if (p == NULL)
3543 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545#endif
3546 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003547 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 /* Not still touching the command and it was an illegal one */
3550 xp->xp_context = EXPAND_UNSUCCESSFUL;
3551 return NULL;
3552 }
3553
3554 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3555
3556 if (*p == '!') /* forced commands */
3557 {
3558 forceit = TRUE;
3559 ++p;
3560 }
3561
3562/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003563 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003565 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003566 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 arg = skipwhite(p);
3569
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 if (*arg == '>') /* append */
3573 {
3574 if (*++arg == '>')
3575 ++arg;
3576 arg = skipwhite(arg);
3577 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 ++arg;
3581 usefilter = TRUE;
3582 }
3583 }
3584
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 usefilter = forceit; /* :r! filter if forced */
3588 if (*arg == '!') /* :r !filter */
3589 {
3590 ++arg;
3591 usefilter = TRUE;
3592 }
3593 }
3594
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 while (*arg == *cmd) /* allow any number of '>' or '<' */
3598 ++arg;
3599 arg = skipwhite(arg);
3600 }
3601
3602 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003603 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 /* Check if we're in the +command */
3606 p = arg + 1;
3607 arg = skip_cmd_arg(arg, FALSE);
3608
3609 /* Still touching the command after '+'? */
3610 if (*arg == NUL)
3611 return p;
3612
3613 /* Skip space(s) after +command to get to the real argument */
3614 arg = skipwhite(arg);
3615 }
3616
3617 /*
3618 * Check for '|' to separate commands and '"' to start comments.
3619 * Don't do this for ":read !cmd" and ":write !cmd".
3620 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 p = arg;
3624 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 p += 2;
3627 while (*p)
3628 {
3629 if (*p == Ctrl_V)
3630 {
3631 if (p[1] != NUL)
3632 ++p;
3633 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 || *p == '|' || *p == '\n')
3636 {
3637 if (*(p - 1) != '\\')
3638 {
3639 if (*p == '|' || *p == '\n')
3640 return p + 1;
3641 return NULL; /* It's a comment */
3642 }
3643 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003644 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 }
3647
3648 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 vim_strchr((char_u *)"|\"", *arg) == NULL)
3651 return NULL;
3652
3653 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003654 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003656 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003657 while (*p && p < buff + len)
3658 {
3659 if (*p == ' ' || *p == TAB)
3660 {
3661 /* argument starts after a space */
3662 xp->xp_pattern = ++p;
3663 }
3664 else
3665 {
3666 if (*p == '\\' && *(p + 1) != NUL)
3667 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003668 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003669 }
3670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003672 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003674 int c;
3675 int in_quote = FALSE;
3676 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 /*
3679 * Allow spaces within back-quotes to count as part of the argument
3680 * being expanded.
3681 */
3682 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003683 p = xp->xp_pattern;
3684 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003686#ifdef FEAT_MBYTE
3687 if (has_mbyte)
3688 c = mb_ptr2char(p);
3689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003691 c = *p;
3692 if (c == '\\' && p[1] != NUL)
3693 ++p;
3694 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696 if (!in_quote)
3697 {
3698 xp->xp_pattern = p;
3699 bow = p + 1;
3700 }
3701 in_quote = !in_quote;
3702 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003703 /* An argument can contain just about everything, except
3704 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003705 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003706#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003707 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003709 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003710 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003711 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003712 while (*p != NUL)
3713 {
3714#ifdef FEAT_MBYTE
3715 if (has_mbyte)
3716 c = mb_ptr2char(p);
3717 else
3718#endif
3719 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003720 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003721 break;
3722#ifdef FEAT_MBYTE
3723 if (has_mbyte)
3724 len = (*mb_ptr2len)(p);
3725 else
3726#endif
3727 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003728 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003729 }
3730 if (in_quote)
3731 bow = p;
3732 else
3733 xp->xp_pattern = p;
3734 p -= len;
3735 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003736 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 /*
3740 * If we are still inside the quotes, and we passed a space, just
3741 * expand from there.
3742 */
3743 if (bow != NULL && in_quote)
3744 xp->xp_pattern = bow;
3745 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003746
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003747 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003748 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003749 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003750#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003751 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003752#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003753 /* When still after the command name expand executables. */
3754 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003755 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 /* Check for environment variable */
3759 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003760#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 || *xp->xp_pattern == '%'
3762#endif
3763 )
3764 {
3765 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3766 if (!vim_isIDc(*p))
3767 break;
3768 if (*p == NUL)
3769 {
3770 xp->xp_context = EXPAND_ENV_VARS;
3771 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003772#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3773 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003774 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003775 compl = EXPAND_ENV_VARS;
3776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003779#if defined(FEAT_CMDL_COMPL)
3780 /* Check for user names */
3781 if (*xp->xp_pattern == '~')
3782 {
3783 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3784 ;
3785 /* Complete ~user only if it partially matches a user name.
3786 * A full match ~user<Tab> will be replaced by user's home
3787 * directory i.e. something like ~user<Tab> -> /home/user/ */
3788 if (*p == NUL && p > xp->xp_pattern + 1
3789 && match_user(xp->xp_pattern + 1) == 1)
3790 {
3791 xp->xp_context = EXPAND_USER;
3792 ++xp->xp_pattern;
3793 }
3794 }
3795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797
3798/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003799 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003801 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003803 case CMD_find:
3804 case CMD_sfind:
3805 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003806 if (xp->xp_context == EXPAND_FILES)
3807 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_cd:
3810 case CMD_chdir:
3811 case CMD_lcd:
3812 case CMD_lchdir:
3813 if (xp->xp_context == EXPAND_FILES)
3814 xp->xp_context = EXPAND_DIRECTORIES;
3815 break;
3816 case CMD_help:
3817 xp->xp_context = EXPAND_HELP;
3818 xp->xp_pattern = arg;
3819 break;
3820
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003821 /* Command modifiers: return the argument.
3822 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003824 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_belowright:
3826 case CMD_botright:
3827 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003828 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003829 case CMD_cdo:
3830 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003832 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_folddoclosed:
3834 case CMD_folddoopen:
3835 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003836 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_keepjumps:
3838 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003839 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003840 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003842 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003844 case CMD_noautocmd:
3845 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003847 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003849 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003850 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_topleft:
3852 case CMD_verbose:
3853 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003854 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return arg;
3856
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003857 case CMD_filter:
3858 if (*arg != NUL)
3859 arg = skip_vimgrep_pat(arg, NULL, NULL);
3860 if (arg == NULL || *arg == NUL)
3861 {
3862 xp->xp_context = EXPAND_NOTHING;
3863 return NULL;
3864 }
3865 return skipwhite(arg);
3866
Bram Moolenaar4f688582007-07-24 12:34:30 +00003867#ifdef FEAT_CMDL_COMPL
3868# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 case CMD_match:
3870 if (*arg == NUL || !ends_excmd(*arg))
3871 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003872 /* also complete "None" */
3873 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 arg = skipwhite(skiptowhite(arg));
3875 if (*arg != NUL)
3876 {
3877 xp->xp_context = EXPAND_NOTHING;
3878 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3879 }
3880 }
3881 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884/*
3885 * All completion for the +cmdline_compl feature goes here.
3886 */
3887
3888# ifdef FEAT_USR_CMDS
3889 case CMD_command:
3890 /* Check for attributes */
3891 while (*arg == '-')
3892 {
3893 arg++; /* Skip "-" */
3894 p = skiptowhite(arg);
3895 if (*p == NUL)
3896 {
3897 /* Cursor is still in the attribute */
3898 p = vim_strchr(arg, '=');
3899 if (p == NULL)
3900 {
3901 /* No "=", so complete attribute names */
3902 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3903 xp->xp_pattern = arg;
3904 return NULL;
3905 }
3906
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003907 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * their arguments as well.
3909 */
3910 if (STRNICMP(arg, "complete", p - arg) == 0)
3911 {
3912 xp->xp_context = EXPAND_USER_COMPLETE;
3913 xp->xp_pattern = p + 1;
3914 return NULL;
3915 }
3916 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3917 {
3918 xp->xp_context = EXPAND_USER_NARGS;
3919 xp->xp_pattern = p + 1;
3920 return NULL;
3921 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003922 else if (STRNICMP(arg, "addr", p - arg) == 0)
3923 {
3924 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3925 xp->xp_pattern = p + 1;
3926 return NULL;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 return NULL;
3929 }
3930 arg = skipwhite(p);
3931 }
3932
3933 /* After the attributes comes the new command name */
3934 p = skiptowhite(arg);
3935 if (*p == NUL)
3936 {
3937 xp->xp_context = EXPAND_USER_COMMANDS;
3938 xp->xp_pattern = arg;
3939 break;
3940 }
3941
3942 /* And finally comes a normal command */
3943 return skipwhite(p);
3944
3945 case CMD_delcommand:
3946 xp->xp_context = EXPAND_USER_COMMANDS;
3947 xp->xp_pattern = arg;
3948 break;
3949# endif
3950
3951 case CMD_global:
3952 case CMD_vglobal:
3953 delim = *arg; /* get the delimiter */
3954 if (delim)
3955 ++arg; /* skip delimiter if there is one */
3956
3957 while (arg[0] != NUL && arg[0] != delim)
3958 {
3959 if (arg[0] == '\\' && arg[1] != NUL)
3960 ++arg;
3961 ++arg;
3962 }
3963 if (arg[0] != NUL)
3964 return arg + 1;
3965 break;
3966 case CMD_and:
3967 case CMD_substitute:
3968 delim = *arg;
3969 if (delim)
3970 {
3971 /* skip "from" part */
3972 ++arg;
3973 arg = skip_regexp(arg, delim, p_magic, NULL);
3974 }
3975 /* skip "to" part */
3976 while (arg[0] != NUL && arg[0] != delim)
3977 {
3978 if (arg[0] == '\\' && arg[1] != NUL)
3979 ++arg;
3980 ++arg;
3981 }
3982 if (arg[0] != NUL) /* skip delimiter */
3983 ++arg;
3984 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3985 ++arg;
3986 if (arg[0] != NUL)
3987 return arg;
3988 break;
3989 case CMD_isearch:
3990 case CMD_dsearch:
3991 case CMD_ilist:
3992 case CMD_dlist:
3993 case CMD_ijump:
3994 case CMD_psearch:
3995 case CMD_djump:
3996 case CMD_isplit:
3997 case CMD_dsplit:
3998 arg = skipwhite(skipdigits(arg)); /* skip count */
3999 if (*arg == '/') /* Match regexp, not just whole words */
4000 {
4001 for (++arg; *arg && *arg != '/'; arg++)
4002 if (*arg == '\\' && arg[1] != NUL)
4003 arg++;
4004 if (*arg)
4005 {
4006 arg = skipwhite(arg + 1);
4007
4008 /* Check for trailing illegal characters */
4009 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4010 xp->xp_context = EXPAND_NOTHING;
4011 else
4012 return arg;
4013 }
4014 }
4015 break;
4016#ifdef FEAT_AUTOCMD
4017 case CMD_autocmd:
4018 return set_context_in_autocmd(xp, arg, FALSE);
4019
4020 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004021 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return set_context_in_autocmd(xp, arg, TRUE);
4023#endif
4024 case CMD_set:
4025 set_context_in_set_cmd(xp, arg, 0);
4026 break;
4027 case CMD_setglobal:
4028 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4029 break;
4030 case CMD_setlocal:
4031 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4032 break;
4033 case CMD_tag:
4034 case CMD_stag:
4035 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004036 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 case CMD_tselect:
4038 case CMD_stselect:
4039 case CMD_ptselect:
4040 case CMD_tjump:
4041 case CMD_stjump:
4042 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004043 if (*p_wop != NUL)
4044 xp->xp_context = EXPAND_TAGS_LISTFILES;
4045 else
4046 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 xp->xp_pattern = arg;
4048 break;
4049 case CMD_augroup:
4050 xp->xp_context = EXPAND_AUGROUP;
4051 xp->xp_pattern = arg;
4052 break;
4053#ifdef FEAT_SYN_HL
4054 case CMD_syntax:
4055 set_context_in_syntax_cmd(xp, arg);
4056 break;
4057#endif
4058#ifdef FEAT_EVAL
4059 case CMD_let:
4060 case CMD_if:
4061 case CMD_elseif:
4062 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004063 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 case CMD_echo:
4065 case CMD_echon:
4066 case CMD_execute:
4067 case CMD_echomsg:
4068 case CMD_echoerr:
4069 case CMD_call:
4070 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004071 case CMD_cexpr:
4072 case CMD_caddexpr:
4073 case CMD_cgetexpr:
4074 case CMD_lexpr:
4075 case CMD_laddexpr:
4076 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004077 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 break;
4079
4080 case CMD_unlet:
4081 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4082 arg = xp->xp_pattern + 1;
4083 xp->xp_context = EXPAND_USER_VARS;
4084 xp->xp_pattern = arg;
4085 break;
4086
4087 case CMD_function:
4088 case CMD_delfunction:
4089 xp->xp_context = EXPAND_USER_FUNC;
4090 xp->xp_pattern = arg;
4091 break;
4092
4093 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004094 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 break;
4096#endif
4097 case CMD_highlight:
4098 set_context_in_highlight_cmd(xp, arg);
4099 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004100#ifdef FEAT_CSCOPE
4101 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004102 case CMD_lcscope:
4103 case CMD_scscope:
4104 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004105 break;
4106#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004107#ifdef FEAT_SIGNS
4108 case CMD_sign:
4109 set_context_in_sign_cmd(xp, arg);
4110 break;
4111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#ifdef FEAT_LISTCMDS
4113 case CMD_bdelete:
4114 case CMD_bwipeout:
4115 case CMD_bunload:
4116 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4117 arg = xp->xp_pattern + 1;
4118 /*FALLTHROUGH*/
4119 case CMD_buffer:
4120 case CMD_sbuffer:
4121 case CMD_checktime:
4122 xp->xp_context = EXPAND_BUFFERS;
4123 xp->xp_pattern = arg;
4124 break;
4125#endif
4126#ifdef FEAT_USR_CMDS
4127 case CMD_USER:
4128 case CMD_USER_BUF:
4129 if (compl != EXPAND_NOTHING)
4130 {
4131 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004132 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
4134# ifdef FEAT_MENU
4135 if (compl == EXPAND_MENUS)
4136 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4137# endif
4138 if (compl == EXPAND_COMMANDS)
4139 return arg;
4140 if (compl == EXPAND_MAPPINGS)
4141 return set_context_in_map_cmd(xp, (char_u *)"map",
4142 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004143 /* Find start of last argument. */
4144 p = arg;
4145 while (*p)
4146 {
4147 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004148 /* argument starts after a space */
4149 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004150 else if (*p == '\\' && *(p + 1) != NUL)
4151 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004152 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 xp->xp_pattern = arg;
4155 }
4156 xp->xp_context = compl;
4157 }
4158 break;
4159#endif
4160 case CMD_map: case CMD_noremap:
4161 case CMD_nmap: case CMD_nnoremap:
4162 case CMD_vmap: case CMD_vnoremap:
4163 case CMD_omap: case CMD_onoremap:
4164 case CMD_imap: case CMD_inoremap:
4165 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004166 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004167 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004168 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004169 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004171 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 case CMD_unmap:
4173 case CMD_nunmap:
4174 case CMD_vunmap:
4175 case CMD_ounmap:
4176 case CMD_iunmap:
4177 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004178 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004179 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004180 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004181 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004183 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004184 case CMD_mapclear:
4185 case CMD_nmapclear:
4186 case CMD_vmapclear:
4187 case CMD_omapclear:
4188 case CMD_imapclear:
4189 case CMD_cmapclear:
4190 case CMD_lmapclear:
4191 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004192 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004193 case CMD_xmapclear:
4194 xp->xp_context = EXPAND_MAPCLEAR;
4195 xp->xp_pattern = arg;
4196 break;
4197
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 case CMD_abbreviate: case CMD_noreabbrev:
4199 case CMD_cabbrev: case CMD_cnoreabbrev:
4200 case CMD_iabbrev: case CMD_inoreabbrev:
4201 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004202 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 case CMD_unabbreviate:
4204 case CMD_cunabbrev:
4205 case CMD_iunabbrev:
4206 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004207 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#ifdef FEAT_MENU
4209 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4210 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4211 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4212 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4213 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4214 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4215 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4216 case CMD_tmenu: case CMD_tunmenu:
4217 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4218 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4219#endif
4220
4221 case CMD_colorscheme:
4222 xp->xp_context = EXPAND_COLORS;
4223 xp->xp_pattern = arg;
4224 break;
4225
4226 case CMD_compiler:
4227 xp->xp_context = EXPAND_COMPILER;
4228 xp->xp_pattern = arg;
4229 break;
4230
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004231 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004232 xp->xp_context = EXPAND_OWNSYNTAX;
4233 xp->xp_pattern = arg;
4234 break;
4235
4236 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004237 xp->xp_context = EXPAND_FILETYPE;
4238 xp->xp_pattern = arg;
4239 break;
4240
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004241 case CMD_packadd:
4242 xp->xp_context = EXPAND_PACKADD;
4243 xp->xp_pattern = arg;
4244 break;
4245
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4247 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4248 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004249 p = skiptowhite(arg);
4250 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 xp->xp_context = EXPAND_LANGUAGE;
4253 xp->xp_pattern = arg;
4254 }
4255 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004256 {
4257 if ( STRNCMP(arg, "messages", p - arg) == 0
4258 || STRNCMP(arg, "ctype", p - arg) == 0
4259 || STRNCMP(arg, "time", p - arg) == 0)
4260 {
4261 xp->xp_context = EXPAND_LOCALES;
4262 xp->xp_pattern = skipwhite(p);
4263 }
4264 else
4265 xp->xp_context = EXPAND_NOTHING;
4266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004269#if defined(FEAT_PROFILE)
4270 case CMD_profile:
4271 set_context_in_profile_cmd(xp, arg);
4272 break;
4273#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004274 case CMD_behave:
4275 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004276 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004279 case CMD_messages:
4280 xp->xp_context = EXPAND_MESSAGES;
4281 xp->xp_pattern = arg;
4282 break;
4283
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004284#if defined(FEAT_CMDHIST)
4285 case CMD_history:
4286 xp->xp_context = EXPAND_HISTORY;
4287 xp->xp_pattern = arg;
4288 break;
4289#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004290#if defined(FEAT_PROFILE)
4291 case CMD_syntime:
4292 xp->xp_context = EXPAND_SYNTIME;
4293 xp->xp_pattern = arg;
4294 break;
4295#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297#endif /* FEAT_CMDL_COMPL */
4298
4299 default:
4300 break;
4301 }
4302 return NULL;
4303}
4304
4305/*
4306 * skip a range specifier of the form: addr [,addr] [;addr] ..
4307 *
4308 * Backslashed delimiters after / or ? will be skipped, and commands will
4309 * not be expanded between /'s and ?'s or after "'".
4310 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004311 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * Returns the "cmd" pointer advanced to beyond the range.
4313 */
4314 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004315skip_range(
4316 char_u *cmd,
4317 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004319 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004321 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004323 if (*cmd == '\\')
4324 {
4325 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4326 ++cmd;
4327 else
4328 break;
4329 }
4330 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
4332 if (*++cmd == NUL && ctx != NULL)
4333 *ctx = EXPAND_NOTHING;
4334 }
4335 else if (*cmd == '/' || *cmd == '?')
4336 {
4337 delim = *cmd++;
4338 while (*cmd != NUL && *cmd != delim)
4339 if (*cmd++ == '\\' && *cmd != NUL)
4340 ++cmd;
4341 if (*cmd == NUL && ctx != NULL)
4342 *ctx = EXPAND_NOTHING;
4343 }
4344 if (*cmd != NUL)
4345 ++cmd;
4346 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004347
4348 /* Skip ":" and white space. */
4349 while (*cmd == ':')
4350 cmd = skipwhite(cmd + 1);
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 return cmd;
4353}
4354
4355/*
4356 * get a single EX address
4357 *
4358 * Set ptr to the next character after the part that was interpreted.
4359 * Set ptr to NULL when an error is encountered.
4360 *
4361 * Return MAXLNUM when no Ex address was found.
4362 */
4363 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004364get_address(
4365 exarg_T *eap UNUSED,
4366 char_u **ptr,
4367 int addr_type, /* flag: one of ADDR_LINES, ... */
4368 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004369 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004370 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 int c;
4373 int i;
4374 long n;
4375 char_u *cmd;
4376 pos_T pos;
4377 pos_T *fp;
4378 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004379 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 cmd = skipwhite(*ptr);
4382 lnum = MAXLNUM;
4383 do
4384 {
4385 switch (*cmd)
4386 {
4387 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 ++cmd;
4389 switch (addr_type)
4390 {
4391 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 lnum = curwin->w_cursor.lnum;
4393 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004395 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 break;
4397 case ADDR_ARGUMENTS:
4398 lnum = curwin->w_arg_idx + 1;
4399 break;
4400 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004401 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 lnum = curbuf->b_fnum;
4403 break;
4404 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004405 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004407 case ADDR_TABS_RELATIVE:
4408 EMSG(_(e_invrange));
4409 cmd = NULL;
4410 goto error;
4411 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004412#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004413 case ADDR_QUICKFIX:
4414 lnum = qf_get_cur_valid_idx(eap);
4415 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004416#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 }
4418 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 ++cmd;
4422 switch (addr_type)
4423 {
4424 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 lnum = curbuf->b_ml.ml_line_count;
4426 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004428 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 break;
4430 case ADDR_ARGUMENTS:
4431 lnum = ARGCOUNT;
4432 break;
4433 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004434 buf = lastbuf;
4435 while (buf->b_ml.ml_mfp == NULL)
4436 {
4437 if (buf->b_prev == NULL)
4438 break;
4439 buf = buf->b_prev;
4440 }
4441 lnum = buf->b_fnum;
4442 break;
4443 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 lnum = lastbuf->b_fnum;
4445 break;
4446 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004447 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004449 case ADDR_TABS_RELATIVE:
4450 EMSG(_(e_invrange));
4451 cmd = NULL;
4452 goto error;
4453 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004454#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004455 case ADDR_QUICKFIX:
4456 lnum = qf_get_size(eap);
4457 if (lnum == 0)
4458 lnum = 1;
4459 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004460#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 }
4462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 if (*++cmd == NUL)
4466 {
4467 cmd = NULL;
4468 goto error;
4469 }
4470 if (addr_type != ADDR_LINES)
4471 {
4472 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004473 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 goto error;
4475 }
4476 if (skip)
4477 ++cmd;
4478 else
4479 {
4480 /* Only accept a mark in another file when it is
4481 * used by itself: ":'M". */
4482 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4483 ++cmd;
4484 if (fp == (pos_T *)-1)
4485 /* Jumped to another file. */
4486 lnum = curwin->w_cursor.lnum;
4487 else
4488 {
4489 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 cmd = NULL;
4492 goto error;
4493 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 lnum = fp->lnum;
4495 }
4496 }
4497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 case '/':
4500 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 c = *cmd++;
4502 if (addr_type != ADDR_LINES)
4503 {
4504 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004505 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 goto error;
4507 }
4508 if (skip) /* skip "/pat/" */
4509 {
4510 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4511 if (*cmd == c)
4512 ++cmd;
4513 }
4514 else
4515 {
4516 pos = curwin->w_cursor; /* save curwin->w_cursor */
4517 /*
4518 * When '/' or '?' follows another address, start
4519 * from there.
4520 */
4521 if (lnum != MAXLNUM)
4522 curwin->w_cursor.lnum = lnum;
4523 /*
4524 * Start a forward search at the end of the line.
4525 * Start a backward search at the start of the line.
4526 * This makes sure we never match in the current
4527 * line, and can match anywhere in the
4528 * next/previous line.
4529 */
4530 if (c == '/')
4531 curwin->w_cursor.col = MAXCOL;
4532 else
4533 curwin->w_cursor.col = 0;
4534 searchcmdlen = 0;
4535 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004536 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 {
4538 curwin->w_cursor = pos;
4539 cmd = NULL;
4540 goto error;
4541 }
4542 lnum = curwin->w_cursor.lnum;
4543 curwin->w_cursor = pos;
4544 /* adjust command string pointer */
4545 cmd += searchcmdlen;
4546 }
4547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 ++cmd;
4551 if (addr_type != ADDR_LINES)
4552 {
4553 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004554 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 goto error;
4556 }
4557 if (*cmd == '&')
4558 i = RE_SUBST;
4559 else if (*cmd == '?' || *cmd == '/')
4560 i = RE_SEARCH;
4561 else
4562 {
4563 EMSG(_(e_backslash));
4564 cmd = NULL;
4565 goto error;
4566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 if (!skip)
4569 {
4570 /*
4571 * When search follows another address, start from
4572 * there.
4573 */
4574 if (lnum != MAXLNUM)
4575 pos.lnum = lnum;
4576 else
4577 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 /*
4580 * Start the search just like for the above
4581 * do_search().
4582 */
4583 if (*cmd != '?')
4584 pos.col = MAXCOL;
4585 else
4586 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004587#ifdef FEAT_VIRTUALEDIT
4588 pos.coladd = 0;
4589#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 if (searchit(curwin, curbuf, &pos,
4591 *cmd == '?' ? BACKWARD : FORWARD,
4592 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004593 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 lnum = pos.lnum;
4595 else
4596 {
4597 cmd = NULL;
4598 goto error;
4599 }
4600 }
4601 ++cmd;
4602 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4606 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608
4609 for (;;)
4610 {
4611 cmd = skipwhite(cmd);
4612 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4613 break;
4614
4615 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 {
4617 switch (addr_type)
4618 {
4619 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004620 /* "+1" is same as ".+1" */
4621 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 break;
4623 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004624 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 break;
4626 case ADDR_ARGUMENTS:
4627 lnum = curwin->w_arg_idx + 1;
4628 break;
4629 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004630 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004631 lnum = curbuf->b_fnum;
4632 break;
4633 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004634 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004636 case ADDR_TABS_RELATIVE:
4637 lnum = 1;
4638 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004639#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004640 case ADDR_QUICKFIX:
4641 lnum = qf_get_cur_valid_idx(eap);
4642 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004643#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 }
4645 }
4646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (VIM_ISDIGIT(*cmd))
4648 i = '+'; /* "number" is same as "+number" */
4649 else
4650 i = *cmd++;
4651 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4652 n = 1;
4653 else
4654 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004655
4656 if (addr_type == ADDR_TABS_RELATIVE)
4657 {
4658 EMSG(_(e_invrange));
4659 cmd = NULL;
4660 goto error;
4661 }
4662 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004663 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004664 lnum = compute_buffer_local_count(
4665 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004667 {
4668#ifdef FEAT_FOLDING
4669 /* Relative line addressing, need to adjust for folded lines
4670 * now, but only do it after the first address. */
4671 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4672 && address_count >= 2)
4673 (void)hasFolding(lnum, NULL, &lnum);
4674#endif
4675 if (i == '-')
4676 lnum -= n;
4677 else
4678 lnum += n;
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681 } while (*cmd == '/' || *cmd == '?');
4682
4683error:
4684 *ptr = cmd;
4685 return lnum;
4686}
4687
4688/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004689 * Get flags from an Ex command argument.
4690 */
4691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004693{
4694 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4695 {
4696 if (*eap->arg == 'l')
4697 eap->flags |= EXFLAG_LIST;
4698 else if (*eap->arg == 'p')
4699 eap->flags |= EXFLAG_PRINT;
4700 else
4701 eap->flags |= EXFLAG_NR;
4702 eap->arg = skipwhite(eap->arg + 1);
4703 }
4704}
4705
4706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 * Function called for command which is Not Implemented. NI!
4708 */
4709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004710ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 if (!eap->skip)
4713 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4714}
4715
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004716#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717/*
4718 * Function called for script command which is Not Implemented. NI!
4719 * Skips over ":perl <<EOF" constructs.
4720 */
4721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004722ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 if (!eap->skip)
4725 ex_ni(eap);
4726 else
4727 vim_free(script_get(eap, eap->arg));
4728}
4729#endif
4730
4731/*
4732 * Check range in Ex command for validity.
4733 * Return NULL when valid, error message when invalid.
4734 */
4735 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004738 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if ( eap->line1 < 0
4740 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004741 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004743
4744 if (eap->argt & RANGE)
4745 {
4746 switch(eap->addr_type)
4747 {
4748 case ADDR_LINES:
4749 if (!(eap->argt & NOTADR)
4750 && eap->line2 > curbuf->b_ml.ml_line_count
4751#ifdef FEAT_DIFF
4752 + (eap->cmdidx == CMD_diffget)
4753#endif
4754 )
4755 return (char_u *)_(e_invrange);
4756 break;
4757 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004758 /* add 1 if ARGCOUNT is 0 */
4759 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004760 return (char_u *)_(e_invrange);
4761 break;
4762 case ADDR_BUFFERS:
4763 if (eap->line1 < firstbuf->b_fnum
4764 || eap->line2 > lastbuf->b_fnum)
4765 return (char_u *)_(e_invrange);
4766 break;
4767 case ADDR_LOADED_BUFFERS:
4768 buf = firstbuf;
4769 while (buf->b_ml.ml_mfp == NULL)
4770 {
4771 if (buf->b_next == NULL)
4772 return (char_u *)_(e_invrange);
4773 buf = buf->b_next;
4774 }
4775 if (eap->line1 < buf->b_fnum)
4776 return (char_u *)_(e_invrange);
4777 buf = lastbuf;
4778 while (buf->b_ml.ml_mfp == NULL)
4779 {
4780 if (buf->b_prev == NULL)
4781 return (char_u *)_(e_invrange);
4782 buf = buf->b_prev;
4783 }
4784 if (eap->line2 > buf->b_fnum)
4785 return (char_u *)_(e_invrange);
4786 break;
4787 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004788 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004789 return (char_u *)_(e_invrange);
4790 break;
4791 case ADDR_TABS:
4792 if (eap->line2 > LAST_TAB_NR)
4793 return (char_u *)_(e_invrange);
4794 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004795 case ADDR_TABS_RELATIVE:
4796 /* Do nothing */
4797 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004798#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004799 case ADDR_QUICKFIX:
4800 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4801 return (char_u *)_(e_invrange);
4802 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004803#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004804 }
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return NULL;
4807}
4808
4809/*
4810 * Correct the range for zero line number, if required.
4811 */
4812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004813correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
4815 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4816 {
4817 if (eap->line1 == 0)
4818 eap->line1 = 1;
4819 if (eap->line2 == 0)
4820 eap->line2 = 1;
4821 }
4822}
4823
Bram Moolenaar748bf032005-02-02 23:04:36 +00004824#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004825static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004826
4827/*
4828 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4829 * pattern. Otherwise return eap->arg.
4830 */
4831 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004832skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004833{
4834 char_u *p = eap->arg;
4835
Bram Moolenaara37420f2006-02-04 22:37:47 +00004836 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4837 || eap->cmdidx == CMD_vimgrepadd
4838 || eap->cmdidx == CMD_lvimgrepadd
4839 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004840 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004841 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004842 if (p == NULL)
4843 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004844 }
4845 return p;
4846}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004847
4848/*
4849 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4850 * in the command line, so that things like % get expanded.
4851 */
4852 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004854{
4855 char_u *new_cmdline;
4856 char_u *program;
4857 char_u *pos;
4858 char_u *ptr;
4859 int len;
4860 int i;
4861
4862 /*
4863 * Don't do it when ":vimgrep" is used for ":grep".
4864 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004865 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4866 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4867 || eap->cmdidx == CMD_grepadd
4868 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004869 && !grep_internal(eap->cmdidx))
4870 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004871 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4872 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004873 {
4874 if (*curbuf->b_p_gp == NUL)
4875 program = p_gp;
4876 else
4877 program = curbuf->b_p_gp;
4878 }
4879 else
4880 {
4881 if (*curbuf->b_p_mp == NUL)
4882 program = p_mp;
4883 else
4884 program = curbuf->b_p_mp;
4885 }
4886
4887 p = skipwhite(p);
4888
4889 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4890 {
4891 /* replace $* by given arguments */
4892 i = 1;
4893 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4894 ++i;
4895 len = (int)STRLEN(p);
4896 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4897 if (new_cmdline == NULL)
4898 return NULL; /* out of memory */
4899 ptr = new_cmdline;
4900 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4901 {
4902 i = (int)(pos - program);
4903 STRNCPY(ptr, program, i);
4904 STRCPY(ptr += i, p);
4905 ptr += len;
4906 program = pos + 2;
4907 }
4908 STRCPY(ptr, program);
4909 }
4910 else
4911 {
4912 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4913 if (new_cmdline == NULL)
4914 return NULL; /* out of memory */
4915 STRCPY(new_cmdline, program);
4916 STRCAT(new_cmdline, " ");
4917 STRCAT(new_cmdline, p);
4918 }
4919 msg_make(p);
4920
4921 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4922 vim_free(*cmdlinep);
4923 *cmdlinep = new_cmdline;
4924 p = new_cmdline;
4925 }
4926 return p;
4927}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004928#endif
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930/*
4931 * Expand file name in Ex command argument.
4932 * Return FAIL for failure, OK otherwise.
4933 */
4934 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935expand_filename(
4936 exarg_T *eap,
4937 char_u **cmdlinep,
4938 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 int has_wildcards; /* need to expand wildcards */
4941 char_u *repl;
4942 int srclen;
4943 char_u *p;
4944 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004945 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaar748bf032005-02-02 23:04:36 +00004947#ifdef FEAT_QUICKFIX
4948 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4949 p = skip_grep_pat(eap);
4950#else
4951 p = eap->arg;
4952#endif
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 /*
4955 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4956 * the file name contains a wildcard it should not cause expanding.
4957 * (it will be expanded anyway if there is a wildcard before replacing).
4958 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004959 has_wildcards = mch_has_wildcard(p);
4960 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004962#ifdef FEAT_EVAL
4963 /* Skip over `=expr`, wildcards in it are not expanded. */
4964 if (p[0] == '`' && p[1] == '=')
4965 {
4966 p += 2;
4967 (void)skip_expr(&p);
4968 if (*p == '`')
4969 ++p;
4970 continue;
4971 }
4972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 /*
4974 * Quick check if this cannot be the start of a special string.
4975 * Also removes backslash before '%', '#' and '<'.
4976 */
4977 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4978 {
4979 ++p;
4980 continue;
4981 }
4982
4983 /*
4984 * Try to find a match at this position.
4985 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004986 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4987 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (*errormsgp != NULL) /* error detected */
4989 return FAIL;
4990 if (repl == NULL) /* no match found */
4991 {
4992 p += srclen;
4993 continue;
4994 }
4995
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004996 /* Wildcards won't be expanded below, the replacement is taken
4997 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4998 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4999 {
5000 char_u *l = repl;
5001
5002 repl = expand_env_save(repl);
5003 vim_free(l);
5004 }
5005
Bram Moolenaar63b92542007-03-27 14:57:09 +00005006 /* Need to escape white space et al. with a backslash.
5007 * Don't do this for:
5008 * - replacement that already has been escaped: "##"
5009 * - shell commands (may have to use quotes instead).
5010 * - non-unix systems when there is a single argument (spaces don't
5011 * separate arguments then).
5012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005014 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 && eap->cmdidx != CMD_grep
5017 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005018 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005019 && eap->cmdidx != CMD_lgrep
5020 && eap->cmdidx != CMD_lgrepadd
5021 && eap->cmdidx != CMD_lmake
5022 && eap->cmdidx != CMD_make
5023 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024#ifndef UNIX
5025 && !(eap->argt & NOSPC)
5026#endif
5027 )
5028 {
5029 char_u *l;
5030#ifdef BACKSLASH_IN_FILENAME
5031 /* Don't escape a backslash here, because rem_backslash() doesn't
5032 * remove it later. */
5033 static char_u *nobslash = (char_u *)" \t\"|";
5034# define ESCAPE_CHARS nobslash
5035#else
5036# define ESCAPE_CHARS escape_chars
5037#endif
5038
5039 for (l = repl; *l; ++l)
5040 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5041 {
5042 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5043 if (l != NULL)
5044 {
5045 vim_free(repl);
5046 repl = l;
5047 }
5048 break;
5049 }
5050 }
5051
5052 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005053 if ((eap->usefilter || eap->cmdidx == CMD_bang
5054 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005055 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 char_u *l;
5058
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005059 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (l != NULL)
5061 {
5062 vim_free(repl);
5063 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 }
5066
5067 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5068 vim_free(repl);
5069 if (p == NULL)
5070 return FAIL;
5071 }
5072
5073 /*
5074 * One file argument: Expand wildcards.
5075 * Don't do this with ":r !command" or ":w !command".
5076 */
5077 if ((eap->argt & NOSPC) && !eap->usefilter)
5078 {
5079 /*
5080 * May do this twice:
5081 * 1. Replace environment variables.
5082 * 2. Replace any other wildcards, remove backslashes.
5083 */
5084 for (n = 1; n <= 2; ++n)
5085 {
5086 if (n == 2)
5087 {
5088#ifdef UNIX
5089 /*
5090 * Only for Unix we check for more than one file name.
5091 * For other systems spaces are considered to be part
5092 * of the file name.
5093 * Only check here if there is no wildcard, otherwise
5094 * ExpandOne() will check for errors. This allows
5095 * ":e `ls ve*.c`" on Unix.
5096 */
5097 if (!has_wildcards)
5098 for (p = eap->arg; *p; ++p)
5099 {
5100 /* skip escaped characters */
5101 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5102 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005103 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5106 return FAIL;
5107 }
5108 }
5109#endif
5110
5111 /*
5112 * Halve the number of backslashes (this is Vi compatible).
5113 * For Unix and OS/2, when wildcards are expanded, this is
5114 * done by ExpandOne() below.
5115 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005116#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (!has_wildcards)
5118#endif
5119 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121
5122 if (has_wildcards)
5123 {
5124 if (n == 1)
5125 {
5126 /*
5127 * First loop: May expand environment variables. This
5128 * can be done much faster with expand_env() than with
5129 * something else (e.g., calling a shell).
5130 * After expanding environment variables, check again
5131 * if there are still wildcards present.
5132 */
5133 if (vim_strchr(eap->arg, '$') != NULL
5134 || vim_strchr(eap->arg, '~') != NULL)
5135 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005136 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005137 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 has_wildcards = mch_has_wildcard(NameBuff);
5139 p = NameBuff;
5140 }
5141 else
5142 p = NULL;
5143 }
5144 else /* n == 2 */
5145 {
5146 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005147 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 ExpandInit(&xpc);
5150 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005151 if (p_wic)
5152 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005154 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (p == NULL)
5156 return FAIL;
5157 }
5158 if (p != NULL)
5159 {
5160 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5161 p, cmdlinep);
5162 if (n == 2) /* p came from ExpandOne() */
5163 vim_free(p);
5164 }
5165 }
5166 }
5167 }
5168 return OK;
5169}
5170
5171/*
5172 * Replace part of the command line, keeping eap->cmd, eap->arg and
5173 * eap->nextcmd correct.
5174 * "src" points to the part that is to be replaced, of length "srclen".
5175 * "repl" is the replacement string.
5176 * Returns a pointer to the character after the replaced string.
5177 * Returns NULL for failure.
5178 */
5179 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005180repl_cmdline(
5181 exarg_T *eap,
5182 char_u *src,
5183 int srclen,
5184 char_u *repl,
5185 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 int len;
5188 int i;
5189 char_u *new_cmdline;
5190
5191 /*
5192 * The new command line is build in new_cmdline[].
5193 * First allocate it.
5194 * Careful: a "+cmd" argument may have been NUL terminated.
5195 */
5196 len = (int)STRLEN(repl);
5197 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005198 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5200 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5201 return NULL; /* out of memory! */
5202
5203 /*
5204 * Copy the stuff before the expanded part.
5205 * Copy the expanded stuff.
5206 * Copy what came after the expanded part.
5207 * Copy the next commands, if there are any.
5208 */
5209 i = (int)(src - *cmdlinep); /* length of part before match */
5210 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005211
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 mch_memmove(new_cmdline + i, repl, (size_t)len);
5213 i += len; /* remember the end of the string */
5214 STRCPY(new_cmdline + i, src + srclen);
5215 src = new_cmdline + i; /* remember where to continue */
5216
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005217 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 i = (int)STRLEN(new_cmdline) + 1;
5220 STRCPY(new_cmdline + i, eap->nextcmd);
5221 eap->nextcmd = new_cmdline + i;
5222 }
5223 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5224 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5225 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5226 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5227 vim_free(*cmdlinep);
5228 *cmdlinep = new_cmdline;
5229
5230 return src;
5231}
5232
5233/*
5234 * Check for '|' to separate commands and '"' to start comments.
5235 */
5236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005237separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 char_u *p;
5240
Bram Moolenaar86b68352004-12-27 21:59:20 +00005241#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005242 p = skip_grep_pat(eap);
5243#else
5244 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005245#endif
5246
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005247 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
5249 if (*p == Ctrl_V)
5250 {
5251 if (eap->argt & (USECTRLV | XFILE))
5252 ++p; /* skip CTRL-V and next char */
5253 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005254 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005255 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 if (*p == NUL) /* stop at NUL after CTRL-V */
5257 break;
5258 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005259
5260#ifdef FEAT_EVAL
5261 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005262 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005263 {
5264 p += 2;
5265 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005266 }
5267#endif
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 /* Check for '"': start of comment or '|': next command */
5270 /* :@" and :*" do not start a comment!
5271 * :redir @" doesn't either. */
5272 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5273 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5274 || p != eap->arg)
5275 && (eap->cmdidx != CMD_redir
5276 || p != eap->arg + 1 || p[-1] != '@'))
5277 || *p == '|' || *p == '\n')
5278 {
5279 /*
5280 * We remove the '\' before the '|', unless USECTRLV is used
5281 * AND 'b' is present in 'cpoptions'.
5282 */
5283 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5284 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5285 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005286 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 --p;
5288 }
5289 else
5290 {
5291 eap->nextcmd = check_nextcmd(p);
5292 *p = NUL;
5293 break;
5294 }
5295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5299 del_trailing_spaces(eap->arg);
5300}
5301
5302/*
5303 * get + command from ex argument
5304 */
5305 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005306getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
5308 char_u *arg = *argp;
5309 char_u *command = NULL;
5310
5311 if (*arg == '+') /* +[command] */
5312 {
5313 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005314 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 command = dollar_command;
5316 else
5317 {
5318 command = arg;
5319 arg = skip_cmd_arg(command, TRUE);
5320 if (*arg != NUL)
5321 *arg++ = NUL; /* terminate command with NUL */
5322 }
5323
5324 arg = skipwhite(arg); /* skip over spaces */
5325 *argp = arg;
5326 }
5327 return command;
5328}
5329
5330/*
5331 * Find end of "+command" argument. Skip over "\ " and "\\".
5332 */
5333 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005334skip_cmd_arg(
5335 char_u *p,
5336 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 while (*p && !vim_isspace(*p))
5339 {
5340 if (*p == '\\' && p[1] != NUL)
5341 {
5342 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005343 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 else
5345 ++p;
5346 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005347 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 return p;
5350}
5351
5352/*
5353 * Get "++opt=arg" argument.
5354 * Return FAIL or OK.
5355 */
5356 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005357getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 char_u *arg = eap->arg + 2;
5360 int *pp = NULL;
5361#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005362 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 char_u *p;
5364#endif
5365
5366 /* ":edit ++[no]bin[ary] file" */
5367 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5368 {
5369 if (*arg == 'n')
5370 {
5371 arg += 2;
5372 eap->force_bin = FORCE_NOBIN;
5373 }
5374 else
5375 eap->force_bin = FORCE_BIN;
5376 if (!checkforcmd(&arg, "binary", 3))
5377 return FAIL;
5378 eap->arg = skipwhite(arg);
5379 return OK;
5380 }
5381
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005382 /* ":read ++edit file" */
5383 if (STRNCMP(arg, "edit", 4) == 0)
5384 {
5385 eap->read_edit = TRUE;
5386 eap->arg = skipwhite(arg + 4);
5387 return OK;
5388 }
5389
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (STRNCMP(arg, "ff", 2) == 0)
5391 {
5392 arg += 2;
5393 pp = &eap->force_ff;
5394 }
5395 else if (STRNCMP(arg, "fileformat", 10) == 0)
5396 {
5397 arg += 10;
5398 pp = &eap->force_ff;
5399 }
5400#ifdef FEAT_MBYTE
5401 else if (STRNCMP(arg, "enc", 3) == 0)
5402 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005403 if (STRNCMP(arg, "encoding", 8) == 0)
5404 arg += 8;
5405 else
5406 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 pp = &eap->force_enc;
5408 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005409 else if (STRNCMP(arg, "bad", 3) == 0)
5410 {
5411 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005412 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414#endif
5415
5416 if (pp == NULL || *arg != '=')
5417 return FAIL;
5418
5419 ++arg;
5420 *pp = (int)(arg - eap->cmd);
5421 arg = skip_cmd_arg(arg, FALSE);
5422 eap->arg = skipwhite(arg);
5423 *arg = NUL;
5424
5425#ifdef FEAT_MBYTE
5426 if (pp == &eap->force_ff)
5427 {
5428#endif
5429 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5430 return FAIL;
5431#ifdef FEAT_MBYTE
5432 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005433 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
5435 /* Make 'fileencoding' lower case. */
5436 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5437 *p = TOLOWER_ASC(*p);
5438 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005439 else
5440 {
5441 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5442 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005443 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005444 if (STRICMP(p, "keep") == 0)
5445 eap->bad_char = BAD_KEEP;
5446 else if (STRICMP(p, "drop") == 0)
5447 eap->bad_char = BAD_DROP;
5448 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5449 eap->bad_char = *p;
5450 else
5451 return FAIL;
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453#endif
5454
5455 return OK;
5456}
5457
5458/*
5459 * ":abbreviate" and friends.
5460 */
5461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 do_exmap(eap, TRUE); /* almost the same as mapping */
5465}
5466
5467/*
5468 * ":map" and friends.
5469 */
5470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 /*
5474 * If we are sourcing .exrc or .vimrc in current directory we
5475 * print the mappings for security reasons.
5476 */
5477 if (secure)
5478 {
5479 secure = 2;
5480 msg_outtrans(eap->cmd);
5481 msg_putchar('\n');
5482 }
5483 do_exmap(eap, FALSE);
5484}
5485
5486/*
5487 * ":unmap" and friends.
5488 */
5489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005490ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 do_exmap(eap, FALSE);
5493}
5494
5495/*
5496 * ":mapclear" and friends.
5497 */
5498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5502}
5503
5504/*
5505 * ":abclear" and friends.
5506 */
5507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5511}
5512
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005513#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 /*
5518 * Disallow auto commands from .exrc and .vimrc in current
5519 * directory for security reasons.
5520 */
5521 if (secure)
5522 {
5523 secure = 2;
5524 eap->errmsg = e_curdir;
5525 }
5526 else if (eap->cmdidx == CMD_autocmd)
5527 do_autocmd(eap->arg, eap->forceit);
5528 else
5529 do_augroup(eap->arg, eap->forceit);
5530}
5531
5532/*
5533 * ":doautocmd": Apply the automatic commands to the current buffer.
5534 */
5535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005536ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005538 char_u *arg = eap->arg;
5539 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005540 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005541
Bram Moolenaar1610d052016-06-09 22:53:01 +02005542 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5543 /* Only when there is no <nomodeline>. */
5544 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005545 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546}
5547#endif
5548
5549#ifdef FEAT_LISTCMDS
5550/*
5551 * :[N]bunload[!] [N] [bufname] unload buffer
5552 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5553 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5554 */
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 eap->errmsg = do_bufdel(
5559 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5560 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5561 : DOBUF_UNLOAD, eap->arg,
5562 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5563}
5564
5565/*
5566 * :[N]buffer [N] to buffer N
5567 * :[N]sbuffer [N] to buffer N
5568 */
5569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005570ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 if (*eap->arg)
5573 eap->errmsg = e_trailing;
5574 else
5575 {
5576 if (eap->addr_count == 0) /* default is current buffer */
5577 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5578 else
5579 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005580 if (eap->do_ecmd_cmd != NULL)
5581 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583}
5584
5585/*
5586 * :[N]bmodified [N] to next mod. buffer
5587 * :[N]sbmodified [N] to next mod. buffer
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005593 if (eap->do_ecmd_cmd != NULL)
5594 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597/*
5598 * :[N]bnext [N] to next buffer
5599 * :[N]sbnext [N] split and to next buffer
5600 */
5601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005605 if (eap->do_ecmd_cmd != NULL)
5606 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608
5609/*
5610 * :[N]bNext [N] to previous buffer
5611 * :[N]bprevious [N] to previous buffer
5612 * :[N]sbNext [N] split and to previous buffer
5613 * :[N]sbprevious [N] split and to previous buffer
5614 */
5615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
5618 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005619 if (eap->do_ecmd_cmd != NULL)
5620 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
5622
5623/*
5624 * :brewind to first buffer
5625 * :bfirst to first buffer
5626 * :sbrewind split and to first buffer
5627 * :sbfirst split and to first buffer
5628 */
5629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005633 if (eap->do_ecmd_cmd != NULL)
5634 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635}
5636
5637/*
5638 * :blast to last buffer
5639 * :sblast split and to last buffer
5640 */
5641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005642ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
5644 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005645 if (eap->do_ecmd_cmd != NULL)
5646 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647}
5648#endif
5649
5650 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005651ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 return (c == NUL || c == '|' || c == '"' || c == '\n');
5654}
5655
5656#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5657 || defined(PROTO)
5658/*
5659 * Return the next command, after the first '|' or '\n'.
5660 * Return NULL if not found.
5661 */
5662 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
5665 while (*p != '|' && *p != '\n')
5666 {
5667 if (*p == NUL)
5668 return NULL;
5669 ++p;
5670 }
5671 return (p + 1);
5672}
5673#endif
5674
5675/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005676 * Check if *p is a separator between Ex commands, skipping over white space.
5677 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 */
5679 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005682 char_u *s = skipwhite(p);
5683
5684 if (*s == '|' || *s == '\n')
5685 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 else
5687 return NULL;
5688}
5689
5690/*
5691 * - if there are more files to edit
5692 * - and this is the last window
5693 * - and forceit not used
5694 * - and not repeated twice on a row
5695 * return FAIL and give error message if 'message' TRUE
5696 * return OK otherwise
5697 */
5698 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699check_more(
5700 int message, /* when FALSE check only, no messages */
5701 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5704
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005705 if (!forceit && only_one_window()
5706 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
5708 if (message)
5709 {
5710#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5711 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5712 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005713 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005716 vim_strncpy(buff,
5717 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005718 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005720 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 _("%d more files to edit. Quit anyway?"), n);
5722 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5723 return OK;
5724 return FAIL;
5725 }
5726#endif
5727 if (n == 1)
5728 EMSG(_("E173: 1 more file to edit"));
5729 else
5730 EMSGN(_("E173: %ld more files to edit"), n);
5731 quitmore = 2; /* next try to quit is allowed */
5732 }
5733 return FAIL;
5734 }
5735 return OK;
5736}
5737
5738#ifdef FEAT_CMDL_COMPL
5739/*
5740 * Function given to ExpandGeneric() to obtain the list of command names.
5741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005743get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
5745 if (idx >= (int)CMD_SIZE)
5746# ifdef FEAT_USR_CMDS
5747 return get_user_command_name(idx);
5748# else
5749 return NULL;
5750# endif
5751 return cmdnames[idx].cmd_name;
5752}
5753#endif
5754
5755#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005756static 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);
5757static void uc_list(char_u *name, size_t name_len);
5758static 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);
5759static char_u *uc_split_args(char_u *arg, size_t *lenp);
5760static 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 +00005761
5762 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763uc_add_command(
5764 char_u *name,
5765 size_t name_len,
5766 char_u *rep,
5767 long argt,
5768 long def,
5769 int flags,
5770 int compl,
5771 char_u *compl_arg,
5772 int addr_type,
5773 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 ucmd_T *cmd = NULL;
5776 char_u *p;
5777 int i;
5778 int cmp = 1;
5779 char_u *rep_buf = NULL;
5780 garray_T *gap;
5781
Bram Moolenaar9c102382006-05-03 21:26:49 +00005782 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (rep_buf == NULL)
5784 {
5785 /* Can't replace termcodes - try using the string as is */
5786 rep_buf = vim_strsave(rep);
5787
5788 /* Give up if out of memory */
5789 if (rep_buf == NULL)
5790 return FAIL;
5791 }
5792
5793 /* get address of growarray: global or in curbuf */
5794 if (flags & UC_BUFFER)
5795 {
5796 gap = &curbuf->b_ucmds;
5797 if (gap->ga_itemsize == 0)
5798 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5799 }
5800 else
5801 gap = &ucmds;
5802
5803 /* Search for the command in the already defined commands. */
5804 for (i = 0; i < gap->ga_len; ++i)
5805 {
5806 size_t len;
5807
5808 cmd = USER_CMD_GA(gap, i);
5809 len = STRLEN(cmd->uc_name);
5810 cmp = STRNCMP(name, cmd->uc_name, name_len);
5811 if (cmp == 0)
5812 {
5813 if (name_len < len)
5814 cmp = -1;
5815 else if (name_len > len)
5816 cmp = 1;
5817 }
5818
5819 if (cmp == 0)
5820 {
5821 if (!force)
5822 {
5823 EMSG(_("E174: Command already exists: add ! to replace it"));
5824 goto fail;
5825 }
5826
5827 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005828 cmd->uc_rep = NULL;
5829#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5830 vim_free(cmd->uc_compl_arg);
5831 cmd->uc_compl_arg = NULL;
5832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 break;
5834 }
5835
5836 /* Stop as soon as we pass the name to add */
5837 if (cmp < 0)
5838 break;
5839 }
5840
5841 /* Extend the array unless we're replacing an existing command */
5842 if (cmp != 0)
5843 {
5844 if (ga_grow(gap, 1) != OK)
5845 goto fail;
5846 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5847 goto fail;
5848
5849 cmd = USER_CMD_GA(gap, i);
5850 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5851
5852 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854 cmd->uc_name = p;
5855 }
5856
5857 cmd->uc_rep = rep_buf;
5858 cmd->uc_argt = argt;
5859 cmd->uc_def = def;
5860 cmd->uc_compl = compl;
5861#ifdef FEAT_EVAL
5862 cmd->uc_scriptID = current_SID;
5863# ifdef FEAT_CMDL_COMPL
5864 cmd->uc_compl_arg = compl_arg;
5865# endif
5866#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005867 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869 return OK;
5870
5871fail:
5872 vim_free(rep_buf);
5873#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5874 vim_free(compl_arg);
5875#endif
5876 return FAIL;
5877}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005880#if defined(FEAT_USR_CMDS)
5881static struct
5882{
5883 int expand;
5884 char *name;
5885} addr_type_complete[] =
5886{
5887 {ADDR_ARGUMENTS, "arguments"},
5888 {ADDR_LINES, "lines"},
5889 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5890 {ADDR_TABS, "tabs"},
5891 {ADDR_BUFFERS, "buffers"},
5892 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005893 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005894 {-1, NULL}
5895};
5896#endif
5897
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005898#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899/*
5900 * List of names for completion for ":command" with the EXPAND_ flag.
5901 * Must be alphabetical for completion.
5902 */
5903static struct
5904{
5905 int expand;
5906 char *name;
5907} command_complete[] =
5908{
5909 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005910 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005912 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005914 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005915#if defined(FEAT_CSCOPE)
5916 {EXPAND_CSCOPE, "cscope"},
5917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5919 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005920 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921#endif
5922 {EXPAND_DIRECTORIES, "dir"},
5923 {EXPAND_ENV_VARS, "environment"},
5924 {EXPAND_EVENTS, "event"},
5925 {EXPAND_EXPRESSION, "expression"},
5926 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005927 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005928 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {EXPAND_FUNCTIONS, "function"},
5930 {EXPAND_HELP, "help"},
5931 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005932#if defined(FEAT_CMDHIST)
5933 {EXPAND_HISTORY, "history"},
5934#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005935#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005936 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005937 {EXPAND_LOCALES, "locale"},
5938#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005939 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {EXPAND_MAPPINGS, "mapping"},
5941 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005942 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005943 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005944#if defined(FEAT_PROFILE)
5945 {EXPAND_SYNTIME, "syntime"},
5946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005948 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005949 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005950#if defined(FEAT_SIGNS)
5951 {EXPAND_SIGN, "sign"},
5952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {EXPAND_TAGS, "tag"},
5954 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005955 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {EXPAND_USER_VARS, "var"},
5957 {0, NULL}
5958};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005961#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005963uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964{
5965 int i, j;
5966 int found = FALSE;
5967 ucmd_T *cmd;
5968 int len;
5969 long a;
5970 garray_T *gap;
5971
5972 gap = &curbuf->b_ucmds;
5973 for (;;)
5974 {
5975 for (i = 0; i < gap->ga_len; ++i)
5976 {
5977 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005978 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
Bram Moolenaard29459b2016-08-26 22:29:11 +02005980 /* Skip commands which don't match the requested prefix and
5981 * commands filtered out. */
5982 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5983 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 continue;
5985
5986 /* Put out the title first time */
5987 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005988 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 found = TRUE;
5990 msg_putchar('\n');
5991 if (got_int)
5992 break;
5993
5994 /* Special cases */
5995 msg_putchar(a & BANG ? '!' : ' ');
5996 msg_putchar(a & REGSTR ? '"' : ' ');
5997 msg_putchar(gap != &ucmds ? 'b' : ' ');
5998 msg_putchar(' ');
5999
Bram Moolenaar8820b482017-03-16 17:23:31 +01006000 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 len = (int)STRLEN(cmd->uc_name) + 4;
6002
6003 do {
6004 msg_putchar(' ');
6005 ++len;
6006 } while (len < 16);
6007
6008 len = 0;
6009
6010 /* Arguments */
6011 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6012 {
6013 case 0: IObuff[len++] = '0'; break;
6014 case (EXTRA): IObuff[len++] = '*'; break;
6015 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6016 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6017 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6018 }
6019
6020 do {
6021 IObuff[len++] = ' ';
6022 } while (len < 5);
6023
6024 /* Range */
6025 if (a & (RANGE|COUNT))
6026 {
6027 if (a & COUNT)
6028 {
6029 /* -count=N */
6030 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6031 len += (int)STRLEN(IObuff + len);
6032 }
6033 else if (a & DFLALL)
6034 IObuff[len++] = '%';
6035 else if (cmd->uc_def >= 0)
6036 {
6037 /* -range=N */
6038 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6039 len += (int)STRLEN(IObuff + len);
6040 }
6041 else
6042 IObuff[len++] = '.';
6043 }
6044
6045 do {
6046 IObuff[len++] = ' ';
6047 } while (len < 11);
6048
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006049 /* Address Type */
6050 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6051 if (addr_type_complete[j].expand != ADDR_LINES
6052 && addr_type_complete[j].expand == cmd->uc_addr_type)
6053 {
6054 STRCPY(IObuff + len, addr_type_complete[j].name);
6055 len += (int)STRLEN(IObuff + len);
6056 break;
6057 }
6058
6059 do {
6060 IObuff[len++] = ' ';
6061 } while (len < 21);
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 /* Completion */
6064 for (j = 0; command_complete[j].expand != 0; ++j)
6065 if (command_complete[j].expand == cmd->uc_compl)
6066 {
6067 STRCPY(IObuff + len, command_complete[j].name);
6068 len += (int)STRLEN(IObuff + len);
6069 break;
6070 }
6071
6072 do {
6073 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006074 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076 IObuff[len] = '\0';
6077 msg_outtrans(IObuff);
6078
6079 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006080#ifdef FEAT_EVAL
6081 if (p_verbose > 0)
6082 last_set_msg(cmd->uc_scriptID);
6083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 out_flush();
6085 ui_breakcheck();
6086 if (got_int)
6087 break;
6088 }
6089 if (gap == &ucmds || i < gap->ga_len)
6090 break;
6091 gap = &ucmds;
6092 }
6093
6094 if (!found)
6095 MSG(_("No user-defined commands found"));
6096}
6097
6098 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006099uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6102 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6103 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6104 0xb9, 0x7f, 0};
6105 int i;
6106
6107 for (i = 0; fcmd[i]; ++i)
6108 IObuff[i] = fcmd[i] - 0x40;
6109 IObuff[i] = 0;
6110 return IObuff;
6111}
6112
6113 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006114uc_scan_attr(
6115 char_u *attr,
6116 size_t len,
6117 long *argt,
6118 long *def,
6119 int *flags,
6120 int *compl,
6121 char_u **compl_arg,
6122 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124 char_u *p;
6125
6126 if (len == 0)
6127 {
6128 EMSG(_("E175: No attribute specified"));
6129 return FAIL;
6130 }
6131
6132 /* First, try the simple attributes (no arguments) */
6133 if (STRNICMP(attr, "bang", len) == 0)
6134 *argt |= BANG;
6135 else if (STRNICMP(attr, "buffer", len) == 0)
6136 *flags |= UC_BUFFER;
6137 else if (STRNICMP(attr, "register", len) == 0)
6138 *argt |= REGSTR;
6139 else if (STRNICMP(attr, "bar", len) == 0)
6140 *argt |= TRLBAR;
6141 else
6142 {
6143 int i;
6144 char_u *val = NULL;
6145 size_t vallen = 0;
6146 size_t attrlen = len;
6147
6148 /* Look for the attribute name - which is the part before any '=' */
6149 for (i = 0; i < (int)len; ++i)
6150 {
6151 if (attr[i] == '=')
6152 {
6153 val = &attr[i + 1];
6154 vallen = len - i - 1;
6155 attrlen = i;
6156 break;
6157 }
6158 }
6159
6160 if (STRNICMP(attr, "nargs", attrlen) == 0)
6161 {
6162 if (vallen == 1)
6163 {
6164 if (*val == '0')
6165 /* Do nothing - this is the default */;
6166 else if (*val == '1')
6167 *argt |= (EXTRA | NOSPC | NEEDARG);
6168 else if (*val == '*')
6169 *argt |= EXTRA;
6170 else if (*val == '?')
6171 *argt |= (EXTRA | NOSPC);
6172 else if (*val == '+')
6173 *argt |= (EXTRA | NEEDARG);
6174 else
6175 goto wrong_nargs;
6176 }
6177 else
6178 {
6179wrong_nargs:
6180 EMSG(_("E176: Invalid number of arguments"));
6181 return FAIL;
6182 }
6183 }
6184 else if (STRNICMP(attr, "range", attrlen) == 0)
6185 {
6186 *argt |= RANGE;
6187 if (vallen == 1 && *val == '%')
6188 *argt |= DFLALL;
6189 else if (val != NULL)
6190 {
6191 p = val;
6192 if (*def >= 0)
6193 {
6194two_count:
6195 EMSG(_("E177: Count cannot be specified twice"));
6196 return FAIL;
6197 }
6198
6199 *def = getdigits(&p);
6200 *argt |= (ZEROR | NOTADR);
6201
6202 if (p != val + vallen || vallen == 0)
6203 {
6204invalid_count:
6205 EMSG(_("E178: Invalid default value for count"));
6206 return FAIL;
6207 }
6208 }
6209 }
6210 else if (STRNICMP(attr, "count", attrlen) == 0)
6211 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006212 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 if (val != NULL)
6215 {
6216 p = val;
6217 if (*def >= 0)
6218 goto two_count;
6219
6220 *def = getdigits(&p);
6221
6222 if (p != val + vallen)
6223 goto invalid_count;
6224 }
6225
6226 if (*def < 0)
6227 *def = 0;
6228 }
6229 else if (STRNICMP(attr, "complete", attrlen) == 0)
6230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 if (val == NULL)
6232 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006233 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 return FAIL;
6235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006237 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6238 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006241 else if (STRNICMP(attr, "addr", attrlen) == 0)
6242 {
6243 *argt |= RANGE;
6244 if (val == NULL)
6245 {
6246 EMSG(_("E179: argument required for -addr"));
6247 return FAIL;
6248 }
6249 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6250 == FAIL)
6251 return FAIL;
6252 if (addr_type_arg != ADDR_LINES)
6253 *argt |= (ZEROR | NOTADR) ;
6254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 else
6256 {
6257 char_u ch = attr[len];
6258 attr[len] = '\0';
6259 EMSG2(_("E181: Invalid attribute: %s"), attr);
6260 attr[len] = ch;
6261 return FAIL;
6262 }
6263 }
6264
6265 return OK;
6266}
6267
Bram Moolenaara850a712009-01-28 14:42:59 +00006268/*
6269 * ":command ..."
6270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 char_u *name;
6275 char_u *end;
6276 char_u *p;
6277 long argt = 0;
6278 long def = -1;
6279 int flags = 0;
6280 int compl = EXPAND_NOTHING;
6281 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006282 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006284 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
6286 p = eap->arg;
6287
6288 /* Check for attributes */
6289 while (*p == '-')
6290 {
6291 ++p;
6292 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006293 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6294 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 == FAIL)
6296 return;
6297 p = skipwhite(end);
6298 }
6299
6300 /* Get the name (if any) and skip to the following argument */
6301 name = p;
6302 if (ASCII_ISALPHA(*p))
6303 while (ASCII_ISALNUM(*p))
6304 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006305 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
6307 EMSG(_("E182: Invalid command name"));
6308 return;
6309 }
6310 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006311 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 /* If there is nothing after the name, and no attributes were specified,
6314 * we are listing commands
6315 */
6316 p = skipwhite(end);
6317 if (!has_attr && ends_excmd(*p))
6318 {
6319 uc_list(name, end - name);
6320 }
6321 else if (!ASCII_ISUPPER(*name))
6322 {
6323 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6324 return;
6325 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006326 else if ((name_len == 1 && *name == 'X')
6327 || (name_len <= 4
6328 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6329 {
6330 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6331 return;
6332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 else
6334 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006335 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336}
6337
6338/*
6339 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 * Clear all user commands, global and for current buffer.
6341 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006343ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 uc_clear(&ucmds);
6346 uc_clear(&curbuf->b_ucmds);
6347}
6348
6349/*
6350 * Clear all user commands for "gap".
6351 */
6352 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006353uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
6355 int i;
6356 ucmd_T *cmd;
6357
6358 for (i = 0; i < gap->ga_len; ++i)
6359 {
6360 cmd = USER_CMD_GA(gap, i);
6361 vim_free(cmd->uc_name);
6362 vim_free(cmd->uc_rep);
6363# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6364 vim_free(cmd->uc_compl_arg);
6365# endif
6366 }
6367 ga_clear(gap);
6368}
6369
6370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006371ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 int i = 0;
6374 ucmd_T *cmd = NULL;
6375 int cmp = -1;
6376 garray_T *gap;
6377
6378 gap = &curbuf->b_ucmds;
6379 for (;;)
6380 {
6381 for (i = 0; i < gap->ga_len; ++i)
6382 {
6383 cmd = USER_CMD_GA(gap, i);
6384 cmp = STRCMP(eap->arg, cmd->uc_name);
6385 if (cmp <= 0)
6386 break;
6387 }
6388 if (gap == &ucmds || cmp == 0)
6389 break;
6390 gap = &ucmds;
6391 }
6392
6393 if (cmp != 0)
6394 {
6395 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6396 return;
6397 }
6398
6399 vim_free(cmd->uc_name);
6400 vim_free(cmd->uc_rep);
6401# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6402 vim_free(cmd->uc_compl_arg);
6403# endif
6404
6405 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 if (i < gap->ga_len)
6408 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6409}
6410
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006411/*
6412 * split and quote args for <f-args>
6413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006415uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416{
6417 char_u *buf;
6418 char_u *p;
6419 char_u *q;
6420 int len;
6421
6422 /* Precalculate length */
6423 p = arg;
6424 len = 2; /* Initial and final quotes */
6425
6426 while (*p)
6427 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006428 if (p[0] == '\\' && p[1] == '\\')
6429 {
6430 len += 2;
6431 p += 2;
6432 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006433 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
6435 len += 1;
6436 p += 2;
6437 }
6438 else if (*p == '\\' || *p == '"')
6439 {
6440 len += 2;
6441 p += 1;
6442 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006443 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
6445 p = skipwhite(p);
6446 if (*p == NUL)
6447 break;
6448 len += 3; /* "," */
6449 }
6450 else
6451 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006452#ifdef FEAT_MBYTE
6453 int charlen = (*mb_ptr2len)(p);
6454 len += charlen;
6455 p += charlen;
6456#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 ++len;
6458 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 }
6461 }
6462
6463 buf = alloc(len + 1);
6464 if (buf == NULL)
6465 {
6466 *lenp = 0;
6467 return buf;
6468 }
6469
6470 p = arg;
6471 q = buf;
6472 *q++ = '"';
6473 while (*p)
6474 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006475 if (p[0] == '\\' && p[1] == '\\')
6476 {
6477 *q++ = '\\';
6478 *q++ = '\\';
6479 p += 2;
6480 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006481 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 {
6483 *q++ = p[1];
6484 p += 2;
6485 }
6486 else if (*p == '\\' || *p == '"')
6487 {
6488 *q++ = '\\';
6489 *q++ = *p++;
6490 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006491 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
6493 p = skipwhite(p);
6494 if (*p == NUL)
6495 break;
6496 *q++ = '"';
6497 *q++ = ',';
6498 *q++ = '"';
6499 }
6500 else
6501 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006502 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 }
6504 }
6505 *q++ = '"';
6506 *q = 0;
6507
6508 *lenp = len;
6509 return buf;
6510}
6511
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006512 static size_t
6513add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6514{
6515 size_t result;
6516
6517 result = STRLEN(mod_str);
6518 if (*multi_mods)
6519 result += 1;
6520 if (buf != NULL)
6521 {
6522 if (*multi_mods)
6523 STRCAT(buf, " ");
6524 STRCAT(buf, mod_str);
6525 }
6526
6527 *multi_mods = 1;
6528
6529 return result;
6530}
6531
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532/*
6533 * Check for a <> code in a user command.
6534 * "code" points to the '<'. "len" the length of the <> (inclusive).
6535 * "buf" is where the result is to be added.
6536 * "split_buf" points to a buffer used for splitting, caller should free it.
6537 * "split_len" is the length of what "split_buf" contains.
6538 * Returns the length of the replacement, which has been added to "buf".
6539 * Returns -1 if there was no match, and only the "<" has been copied.
6540 */
6541 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006542uc_check_code(
6543 char_u *code,
6544 size_t len,
6545 char_u *buf,
6546 ucmd_T *cmd, /* the user command we're expanding */
6547 exarg_T *eap, /* ex arguments */
6548 char_u **split_buf,
6549 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 size_t result = 0;
6552 char_u *p = code + 1;
6553 size_t l = len - 2;
6554 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006555 enum {
6556 ct_ARGS,
6557 ct_BANG,
6558 ct_COUNT,
6559 ct_LINE1,
6560 ct_LINE2,
6561 ct_RANGE,
6562 ct_MODS,
6563 ct_REGISTER,
6564 ct_LT,
6565 ct_NONE
6566 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6569 {
6570 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6571 p += 2;
6572 l -= 2;
6573 }
6574
Bram Moolenaar371d5402006-03-20 21:47:49 +00006575 ++l;
6576 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006578 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006580 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006582 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006584 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006586 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006588 else if (STRNICMP(p, "range>", l) == 0)
6589 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006590 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006592 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006594 else if (STRNICMP(p, "mods>", l) == 0)
6595 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596
6597 switch (type)
6598 {
6599 case ct_ARGS:
6600 /* Simple case first */
6601 if (*eap->arg == NUL)
6602 {
6603 if (quote == 1)
6604 {
6605 result = 2;
6606 if (buf != NULL)
6607 STRCPY(buf, "''");
6608 }
6609 else
6610 result = 0;
6611 break;
6612 }
6613
6614 /* When specified there is a single argument don't split it.
6615 * Works for ":Cmd %" when % is "a b c". */
6616 if ((eap->argt & NOSPC) && quote == 2)
6617 quote = 1;
6618
6619 switch (quote)
6620 {
6621 case 0: /* No quoting, no splitting */
6622 result = STRLEN(eap->arg);
6623 if (buf != NULL)
6624 STRCPY(buf, eap->arg);
6625 break;
6626 case 1: /* Quote, but don't split */
6627 result = STRLEN(eap->arg) + 2;
6628 for (p = eap->arg; *p; ++p)
6629 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006630#ifdef FEAT_MBYTE
6631 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6632 /* DBCS can contain \ in a trail byte, skip the
6633 * double-byte character. */
6634 ++p;
6635 else
6636#endif
6637 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 ++result;
6639 }
6640
6641 if (buf != NULL)
6642 {
6643 *buf++ = '"';
6644 for (p = eap->arg; *p; ++p)
6645 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006646#ifdef FEAT_MBYTE
6647 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6648 /* DBCS can contain \ in a trail byte, copy the
6649 * double-byte character to avoid escaping. */
6650 *buf++ = *p++;
6651 else
6652#endif
6653 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 *buf++ = '\\';
6655 *buf++ = *p;
6656 }
6657 *buf = '"';
6658 }
6659
6660 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006661 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 /* This is hard, so only do it once, and cache the result */
6663 if (*split_buf == NULL)
6664 *split_buf = uc_split_args(eap->arg, split_len);
6665
6666 result = *split_len;
6667 if (buf != NULL && result != 0)
6668 STRCPY(buf, *split_buf);
6669
6670 break;
6671 }
6672 break;
6673
6674 case ct_BANG:
6675 result = eap->forceit ? 1 : 0;
6676 if (quote)
6677 result += 2;
6678 if (buf != NULL)
6679 {
6680 if (quote)
6681 *buf++ = '"';
6682 if (eap->forceit)
6683 *buf++ = '!';
6684 if (quote)
6685 *buf = '"';
6686 }
6687 break;
6688
6689 case ct_LINE1:
6690 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006691 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 case ct_COUNT:
6693 {
6694 char num_buf[20];
6695 long num = (type == ct_LINE1) ? eap->line1 :
6696 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006697 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6699 size_t num_len;
6700
6701 sprintf(num_buf, "%ld", num);
6702 num_len = STRLEN(num_buf);
6703 result = num_len;
6704
6705 if (quote)
6706 result += 2;
6707
6708 if (buf != NULL)
6709 {
6710 if (quote)
6711 *buf++ = '"';
6712 STRCPY(buf, num_buf);
6713 buf += num_len;
6714 if (quote)
6715 *buf = '"';
6716 }
6717
6718 break;
6719 }
6720
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006721 case ct_MODS:
6722 {
6723 int multi_mods = 0;
6724 typedef struct {
6725 int *varp;
6726 char *name;
6727 } mod_entry_T;
6728 static mod_entry_T mod_entries[] = {
6729#ifdef FEAT_BROWSE_CMD
6730 {&cmdmod.browse, "browse"},
6731#endif
6732#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6733 {&cmdmod.confirm, "confirm"},
6734#endif
6735 {&cmdmod.hide, "hide"},
6736 {&cmdmod.keepalt, "keepalt"},
6737 {&cmdmod.keepjumps, "keepjumps"},
6738 {&cmdmod.keepmarks, "keepmarks"},
6739 {&cmdmod.keeppatterns, "keeppatterns"},
6740 {&cmdmod.lockmarks, "lockmarks"},
6741 {&cmdmod.noswapfile, "noswapfile"},
6742 {NULL, NULL}
6743 };
6744 int i;
6745
6746 result = quote ? 2 : 0;
6747 if (buf != NULL)
6748 {
6749 if (quote)
6750 *buf++ = '"';
6751 *buf = '\0';
6752 }
6753
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006754 /* :aboveleft and :leftabove */
6755 if (cmdmod.split & WSP_ABOVE)
6756 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6757 /* :belowright and :rightbelow */
6758 if (cmdmod.split & WSP_BELOW)
6759 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6760 /* :botright */
6761 if (cmdmod.split & WSP_BOT)
6762 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006763
6764 /* the modifiers that are simple flags */
6765 for (i = 0; mod_entries[i].varp != NULL; ++i)
6766 if (*mod_entries[i].varp)
6767 result += add_cmd_modifier(buf, mod_entries[i].name,
6768 &multi_mods);
6769
6770 /* TODO: How to support :noautocmd? */
6771#ifdef HAVE_SANDBOX
6772 /* TODO: How to support :sandbox?*/
6773#endif
6774 /* :silent */
6775 if (msg_silent > 0)
6776 result += add_cmd_modifier(buf,
6777 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006778 /* :tab */
6779 if (cmdmod.tab > 0)
6780 result += add_cmd_modifier(buf, "tab", &multi_mods);
6781 /* :topleft */
6782 if (cmdmod.split & WSP_TOP)
6783 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006784 /* TODO: How to support :unsilent?*/
6785 /* :verbose */
6786 if (p_verbose > 0)
6787 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006788 /* :vertical */
6789 if (cmdmod.split & WSP_VERT)
6790 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006791 if (quote && buf != NULL)
6792 {
6793 buf += result - 2;
6794 *buf = '"';
6795 }
6796 break;
6797 }
6798
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 case ct_REGISTER:
6800 result = eap->regname ? 1 : 0;
6801 if (quote)
6802 result += 2;
6803 if (buf != NULL)
6804 {
6805 if (quote)
6806 *buf++ = '\'';
6807 if (eap->regname)
6808 *buf++ = eap->regname;
6809 if (quote)
6810 *buf = '\'';
6811 }
6812 break;
6813
6814 case ct_LT:
6815 result = 1;
6816 if (buf != NULL)
6817 *buf = '<';
6818 break;
6819
6820 default:
6821 /* Not recognized: just copy the '<' and return -1. */
6822 result = (size_t)-1;
6823 if (buf != NULL)
6824 *buf = '<';
6825 break;
6826 }
6827
6828 return result;
6829}
6830
6831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006832do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
6834 char_u *buf;
6835 char_u *p;
6836 char_u *q;
6837
6838 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006839 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006840 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 size_t len, totlen;
6842
6843 size_t split_len = 0;
6844 char_u *split_buf = NULL;
6845 ucmd_T *cmd;
6846#ifdef FEAT_EVAL
6847 scid_T save_current_SID = current_SID;
6848#endif
6849
6850 if (eap->cmdidx == CMD_USER)
6851 cmd = USER_CMD(eap->useridx);
6852 else
6853 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6854
6855 /*
6856 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006857 * First round: "buf" is NULL, compute length, allocate "buf".
6858 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 */
6860 buf = NULL;
6861 for (;;)
6862 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006863 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006864 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006866
6867 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006869 start = vim_strchr(p, '<');
6870 if (start != NULL)
6871 end = vim_strchr(start + 1, '>');
6872 if (buf != NULL)
6873 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006874 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6875 ;
6876 if (*ksp == K_SPECIAL
6877 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006878 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6879# ifdef FEAT_GUI
6880 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6881# endif
6882 ))
6883 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006884 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006885 * KS_SPECIAL KE_FILLER, like for mappings, but
6886 * do_cmdline() doesn't handle that, so convert it back.
6887 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6888 len = ksp - p;
6889 if (len > 0)
6890 {
6891 mch_memmove(q, p, len);
6892 q += len;
6893 }
6894 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6895 p = ksp + 3;
6896 continue;
6897 }
6898 }
6899
6900 /* break if there no <item> is found */
6901 if (start == NULL || end == NULL)
6902 break;
6903
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 /* Include the '>' */
6905 ++end;
6906
6907 /* Take everything up to the '<' */
6908 len = start - p;
6909 if (buf == NULL)
6910 totlen += len;
6911 else
6912 {
6913 mch_memmove(q, p, len);
6914 q += len;
6915 }
6916
6917 len = uc_check_code(start, end - start, q, cmd, eap,
6918 &split_buf, &split_len);
6919 if (len == (size_t)-1)
6920 {
6921 /* no match, continue after '<' */
6922 p = start + 1;
6923 len = 1;
6924 }
6925 else
6926 p = end;
6927 if (buf == NULL)
6928 totlen += len;
6929 else
6930 q += len;
6931 }
6932 if (buf != NULL) /* second time here, finished */
6933 {
6934 STRCPY(q, p);
6935 break;
6936 }
6937
6938 totlen += STRLEN(p); /* Add on the trailing characters */
6939 buf = alloc((unsigned)(totlen + 1));
6940 if (buf == NULL)
6941 {
6942 vim_free(split_buf);
6943 return;
6944 }
6945 }
6946
6947#ifdef FEAT_EVAL
6948 current_SID = cmd->uc_scriptID;
6949#endif
6950 (void)do_cmdline(buf, eap->getline, eap->cookie,
6951 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6952#ifdef FEAT_EVAL
6953 current_SID = save_current_SID;
6954#endif
6955 vim_free(buf);
6956 vim_free(split_buf);
6957}
6958
6959# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6960 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006961get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
6963 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6964}
6965
6966/*
6967 * Function given to ExpandGeneric() to obtain the list of user command names.
6968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006970get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
6972 if (idx < curbuf->b_ucmds.ga_len)
6973 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6974 idx -= curbuf->b_ucmds.ga_len;
6975 if (idx < ucmds.ga_len)
6976 return USER_CMD(idx)->uc_name;
6977 return NULL;
6978}
6979
6980/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006981 * Function given to ExpandGeneric() to obtain the list of user address type names.
6982 */
6983 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006984get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006985{
6986 return (char_u *)addr_type_complete[idx].name;
6987}
6988
6989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 * Function given to ExpandGeneric() to obtain the list of user command
6991 * attributes.
6992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006997 {"addr", "bang", "bar", "buffer", "complete",
6998 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007000 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 return NULL;
7002 return (char_u *)user_cmd_flags[idx];
7003}
7004
7005/*
7006 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007009get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
7011 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7012
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007013 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 return NULL;
7015 return (char_u *)user_cmd_nargs[idx];
7016}
7017
7018/*
7019 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007022get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 return (char_u *)command_complete[idx].name;
7025}
7026# endif /* FEAT_CMDL_COMPL */
7027
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007028/*
7029 * Parse address type argument
7030 */
7031 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032parse_addr_type_arg(
7033 char_u *value,
7034 int vallen,
7035 long *argt,
7036 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007037{
7038 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007039
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007040 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7041 {
7042 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7043 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7044 if (a && b)
7045 {
7046 *addr_type_arg = addr_type_complete[i].expand;
7047 break;
7048 }
7049 }
7050
7051 if (addr_type_complete[i].expand == -1)
7052 {
7053 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007054
Bram Moolenaar1c465442017-03-12 20:10:05 +01007055 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007056 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007057 err[i] = NUL;
7058 EMSG2(_("E180: Invalid address type value: %s"), err);
7059 return FAIL;
7060 }
7061
7062 if (*addr_type_arg != ADDR_LINES)
7063 *argt |= NOTADR;
7064
7065 return OK;
7066}
7067
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068#endif /* FEAT_USR_CMDS */
7069
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007070#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7071/*
7072 * Parse a completion argument "value[vallen]".
7073 * The detected completion goes in "*complp", argument type in "*argt".
7074 * When there is an argument, for function and user defined completion, it's
7075 * copied to allocated memory and stored in "*compl_arg".
7076 * Returns FAIL if something is wrong.
7077 */
7078 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007079parse_compl_arg(
7080 char_u *value,
7081 int vallen,
7082 int *complp,
7083 long *argt,
7084 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007085{
7086 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007087# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007088 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007089# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007090 int i;
7091 int valend = vallen;
7092
7093 /* Look for any argument part - which is the part after any ',' */
7094 for (i = 0; i < vallen; ++i)
7095 {
7096 if (value[i] == ',')
7097 {
7098 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007099# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007100 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007101# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007102 valend = i;
7103 break;
7104 }
7105 }
7106
7107 for (i = 0; command_complete[i].expand != 0; ++i)
7108 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007109 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007110 && STRNCMP(value, command_complete[i].name, valend) == 0)
7111 {
7112 *complp = command_complete[i].expand;
7113 if (command_complete[i].expand == EXPAND_BUFFERS)
7114 *argt |= BUFNAME;
7115 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7116 || command_complete[i].expand == EXPAND_FILES)
7117 *argt |= XFILE;
7118 break;
7119 }
7120 }
7121
7122 if (command_complete[i].expand == 0)
7123 {
7124 EMSG2(_("E180: Invalid complete value: %s"), value);
7125 return FAIL;
7126 }
7127
7128# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7129 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7130 && arg != NULL)
7131# else
7132 if (arg != NULL)
7133# endif
7134 {
7135 EMSG(_("E468: Completion argument only allowed for custom completion"));
7136 return FAIL;
7137 }
7138
7139# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7140 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7141 && arg == NULL)
7142 {
7143 EMSG(_("E467: Custom completion requires a function argument"));
7144 return FAIL;
7145 }
7146
7147 if (arg != NULL)
7148 *compl_arg = vim_strnsave(arg, (int)arglen);
7149# endif
7150 return OK;
7151}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007152
7153 int
7154cmdcomplete_str_to_type(char_u *complete_str)
7155{
7156 int i;
7157
7158 for (i = 0; command_complete[i].expand != 0; ++i)
7159 if (STRCMP(complete_str, command_complete[i].name) == 0)
7160 return command_complete[i].expand;
7161
7162 return EXPAND_NOTHING;
7163}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007164#endif
7165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007167ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
Bram Moolenaare6850792010-05-14 15:28:44 +02007169 if (*eap->arg == NUL)
7170 {
7171#ifdef FEAT_EVAL
7172 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7173 char_u *p = NULL;
7174
7175 if (expr != NULL)
7176 {
7177 ++emsg_off;
7178 p = eval_to_string(expr, NULL, FALSE);
7179 --emsg_off;
7180 vim_free(expr);
7181 }
7182 if (p != NULL)
7183 {
7184 MSG(p);
7185 vim_free(p);
7186 }
7187 else
7188 MSG("default");
7189#else
7190 MSG(_("unknown"));
7191#endif
7192 }
7193 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007194 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195}
7196
7197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007198ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 if (*eap->arg == NUL && eap->cmd[2] == '!')
7201 MSG(_("Greetings, Vim user!"));
7202 do_highlight(eap->arg, eap->forceit, FALSE);
7203}
7204
7205
7206/*
7207 * Call this function if we thought we were going to exit, but we won't
7208 * (because of an error). May need to restore the terminal mode.
7209 */
7210 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007211not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 exiting = FALSE;
7214 settmode(TMODE_RAW);
7215}
7216
7217/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007218 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 */
7220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007221ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007223 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007224
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225#ifdef FEAT_CMDWIN
7226 if (cmdwin_type != 0)
7227 {
7228 cmdwin_result = Ctrl_C;
7229 return;
7230 }
7231#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007232 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007233 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007234 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007235 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007236 return;
7237 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007238 if (eap->addr_count > 0)
7239 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007240 int wnr = eap->line2;
7241
7242 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7243 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007244 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007245 }
7246 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007247 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007248
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007249#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007250 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007251 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007252 * being closed (can only happen in autocommands). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02007253 if (curbuf_locked() || !win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007254 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007255 return;
7256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
7258#ifdef FEAT_NETBEANS_INTG
7259 netbeansForcedQuit = eap->forceit;
7260#endif
7261
7262 /*
7263 * If there are more files or windows we won't exit.
7264 */
7265 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7266 exiting = TRUE;
Bram Moolenaareb44a682017-08-03 22:44:55 +02007267 if ((!buf_hide(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007268 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7269 | (eap->forceit ? CCGD_FORCEIT : 0)
7270 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007272 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
7274 not_exiting();
7275 }
7276 else
7277 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007278 /* quit last window
7279 * Note: only_one_window() returns true, even so a help window is
7280 * still open. In that case only quit, if no address has been
7281 * specified. Example:
7282 * :h|wincmd w|1q - don't quit
7283 * :h|wincmd w|q - quit
7284 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007285 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 getout(0);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007287#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007291 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 }
7293}
7294
7295/*
7296 * ":cquit".
7297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007299ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301 getout(1); /* this does not always pass on the exit code to the Manx
7302 compiler. why? */
7303}
7304
7305/*
7306 * ":qall": try to quit all windows
7307 */
7308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007309ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311# ifdef FEAT_CMDWIN
7312 if (cmdwin_type != 0)
7313 {
7314 if (eap->forceit)
7315 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7316 else
7317 cmdwin_result = K_XF2;
7318 return;
7319 }
7320# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007321
7322 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007323 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007324 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007325 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007326 return;
7327 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007328#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007329 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7330 /* Refuse to quit when locked or when the buffer in the last window is
7331 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007332 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007333 return;
7334#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007337 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 getout(0);
7339 not_exiting();
7340}
7341
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342/*
7343 * ":close": close current window, unless it is the last one
7344 */
7345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007346ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007348 win_T *win;
7349 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007350#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007352 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007354#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007355 if (!text_locked()
7356#ifdef FEAT_AUTOCMD
7357 && !curbuf_locked()
7358#endif
7359 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007360 {
7361 if (eap->addr_count == 0)
7362 ex_win_close(eap->forceit, curwin, NULL);
7363 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007364 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007365 {
7366 winnr++;
7367 if (winnr == eap->line2)
7368 break;
7369 }
7370 if (win == NULL)
7371 win = lastwin;
7372 ex_win_close(eap->forceit, win, NULL);
7373 }
7374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375}
7376
Bram Moolenaar4033c552017-09-16 20:54:51 +02007377#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378/*
7379 * ":pclose": Close any preview window.
7380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007382ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383{
7384 win_T *win;
7385
Bram Moolenaar29323592016-07-24 22:04:11 +02007386 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387 if (win->w_p_pvw)
7388 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007389 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390 break;
7391 }
7392}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007393#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007394
Bram Moolenaarf740b292006-02-16 22:11:02 +00007395/*
7396 * Close window "win" and take care of handling closing the last window for a
7397 * modified buffer.
7398 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007400ex_win_close(
7401 int forceit,
7402 win_T *win,
7403 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 int need_hide;
7406 buf_T *buf = win->w_buffer;
7407
7408 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007409 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007411#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if ((p_confirm || cmdmod.confirm) && p_write)
7413 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007414 bufref_T bufref;
7415
7416 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007418 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 return;
7420 need_hide = FALSE;
7421 }
7422 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007425 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 return;
7427 }
7428 }
7429
Bram Moolenaar4033c552017-09-16 20:54:51 +02007430#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007432#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007435 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007436 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007437 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007438 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007442 * Handle the argument for a tabpage related ex command.
7443 * Returns a tabpage number.
7444 * When an error is encountered then eap->errmsg is set.
7445 */
7446 static int
7447get_tabpage_arg(exarg_T *eap)
7448{
7449 int tab_number;
7450 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7451
7452 if (eap->arg && *eap->arg != NUL)
7453 {
7454 char_u *p = eap->arg;
7455 char_u *p_save;
7456 int relative = 0; /* argument +N/-N means: go to N places to the
7457 * right/left relative to the current position. */
7458
7459 if (*p == '-')
7460 {
7461 relative = -1;
7462 p++;
7463 }
7464 else if (*p == '+')
7465 {
7466 relative = 1;
7467 p++;
7468 }
7469
7470 p_save = p;
7471 tab_number = getdigits(&p);
7472
7473 if (relative == 0)
7474 {
7475 if (STRCMP(p, "$") == 0)
7476 tab_number = LAST_TAB_NR;
7477 else if (p == p_save || *p_save == '-' || *p != NUL
7478 || tab_number > LAST_TAB_NR)
7479 {
7480 /* No numbers as argument. */
7481 eap->errmsg = e_invarg;
7482 goto theend;
7483 }
7484 }
7485 else
7486 {
7487 if (*p_save == NUL)
7488 tab_number = 1;
7489 else if (p == p_save || *p_save == '-' || *p != NUL
7490 || tab_number == 0)
7491 {
7492 /* No numbers as argument. */
7493 eap->errmsg = e_invarg;
7494 goto theend;
7495 }
7496 tab_number = tab_number * relative + tabpage_index(curtab);
7497 if (!unaccept_arg0 && relative == -1)
7498 --tab_number;
7499 }
7500 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7501 eap->errmsg = e_invarg;
7502 }
7503 else if (eap->addr_count > 0)
7504 {
7505 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007506 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007507 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007508 tab_number = 0;
7509 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007510 else
7511 {
7512 tab_number = eap->line2;
7513 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7514 {
7515 --tab_number;
7516 if (tab_number < unaccept_arg0)
7517 eap->errmsg = e_invarg;
7518 }
7519 }
7520 }
7521 else
7522 {
7523 switch (eap->cmdidx)
7524 {
7525 case CMD_tabnext:
7526 tab_number = tabpage_index(curtab) + 1;
7527 if (tab_number > LAST_TAB_NR)
7528 tab_number = 1;
7529 break;
7530 case CMD_tabmove:
7531 tab_number = LAST_TAB_NR;
7532 break;
7533 default:
7534 tab_number = tabpage_index(curtab);
7535 }
7536 }
7537
7538theend:
7539 return tab_number;
7540}
7541
7542/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007543 * ":tabclose": close current tab page, unless it is the last one.
7544 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 */
7546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007547ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007549 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007550 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007552# ifdef FEAT_CMDWIN
7553 if (cmdwin_type != 0)
7554 cmdwin_result = K_IGNORE;
7555 else
7556# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007557 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007558 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007561 tab_number = get_tabpage_arg(eap);
7562 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007563 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007564 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007565 if (tp == NULL)
7566 {
7567 beep_flush();
7568 return;
7569 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007570 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007571 {
7572 tabpage_close_other(tp, eap->forceit);
7573 return;
7574 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007575 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007576#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007577 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007578#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007579 )
7580 tabpage_close(eap->forceit);
7581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007583}
7584
7585/*
7586 * ":tabonly": close all tab pages except the current one
7587 */
7588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007589ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007590{
7591 tabpage_T *tp;
7592 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007593 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007594
7595# ifdef FEAT_CMDWIN
7596 if (cmdwin_type != 0)
7597 cmdwin_result = K_IGNORE;
7598 else
7599# endif
7600 if (first_tabpage->tp_next == NULL)
7601 MSG(_("Already only one tab page"));
7602 else
7603 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007604 tab_number = get_tabpage_arg(eap);
7605 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007606 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007607 goto_tabpage(tab_number);
7608 /* Repeat this up to a 1000 times, because autocommands may
7609 * mess up the lists. */
7610 for (done = 0; done < 1000; ++done)
7611 {
7612 FOR_ALL_TABPAGES(tp)
7613 if (tp->tp_topframe != topframe)
7614 {
7615 tabpage_close_other(tp, eap->forceit);
7616 /* if we failed to close it quit */
7617 if (valid_tabpage(tp))
7618 done = 1000;
7619 /* start over, "tp" is now invalid */
7620 break;
7621 }
7622 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007623 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007624 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007625 }
7626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
7629/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007630 * Close the current tab page.
7631 */
7632 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007633tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007634{
7635 /* First close all the windows but the current one. If that worked then
7636 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007637 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007638 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007639 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007640 ex_win_close(forceit, curwin, NULL);
7641# ifdef FEAT_GUI
7642 need_mouse_correct = TRUE;
7643# endif
7644}
7645
7646/*
7647 * Close tab page "tp", which is not the current tab page.
7648 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007649 * Also takes care of the tab pages line disappearing when closing the
7650 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007651 */
7652 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007653tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007654{
7655 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007656 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007657 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007658
7659 /* Limit to 1000 windows, autocommands may add a window while we close
7660 * one. OK, so I'm paranoid... */
7661 while (++done < 1000)
7662 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007663 wp = tp->tp_firstwin;
7664 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007665
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007666 /* Autocommands may delete the tab page under our fingers and we may
7667 * fail to close a window with a modified buffer. */
7668 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007669 break;
7670 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007671
Bram Moolenaar29323592016-07-24 22:04:11 +02007672#ifdef FEAT_AUTOCMD
7673 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7674#endif
7675
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007676 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007677 if (h != tabline_height())
7678 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007679}
7680
7681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 * ":only".
7683 */
7684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007685ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007687 win_T *wp;
7688 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689# ifdef FEAT_GUI
7690 need_mouse_correct = TRUE;
7691# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007692 if (eap->addr_count > 0)
7693 {
7694 wnr = eap->line2;
7695 for (wp = firstwin; --wnr > 0; )
7696 {
7697 if (wp->w_next == NULL)
7698 break;
7699 else
7700 wp = wp->w_next;
7701 }
7702 win_goto(wp);
7703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 close_others(TRUE, eap->forceit);
7705}
7706
7707/*
7708 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007709 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007711 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007712ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 if (eap->addr_count == 0)
7715 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007716 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718
7719 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007720ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007722 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007723 if (!eap->skip)
7724 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007725#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007726 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007727#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007728 if (eap->addr_count == 0)
7729 win_close(curwin, FALSE); /* don't free buffer */
7730 else
7731 {
7732 int winnr = 0;
7733 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007734
Bram Moolenaar2256c992016-11-15 21:17:07 +01007735 FOR_ALL_WINDOWS(win)
7736 {
7737 winnr++;
7738 if (winnr == eap->line2)
7739 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007740 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007741 if (win == NULL)
7742 win = lastwin;
7743 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
7746}
7747
7748/*
7749 * ":stop" and ":suspend": Suspend Vim.
7750 */
7751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 /*
7755 * Disallow suspending for "rvim".
7756 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007757 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
7759 if (!eap->forceit)
7760 autowrite_all();
7761 windgoto((int)Rows - 1, 0);
7762 out_char('\n');
7763 out_flush();
7764 stoptermcap();
7765 out_flush(); /* needed for SUN to restore xterm buffer */
7766#ifdef FEAT_TITLE
7767 mch_restore_title(3); /* restore window titles */
7768#endif
7769 ui_suspend(); /* call machine specific function */
7770#ifdef FEAT_TITLE
7771 maketitle();
7772 resettitle(); /* force updating the title */
7773#endif
7774 starttermcap();
7775 scroll_start(); /* scroll screen before redrawing */
7776 redraw_later_clear();
7777 shell_resized(); /* may have resized window */
7778 }
7779}
7780
7781/*
7782 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7783 */
7784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007785ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787#ifdef FEAT_CMDWIN
7788 if (cmdwin_type != 0)
7789 {
7790 cmdwin_result = Ctrl_C;
7791 return;
7792 }
7793#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007794 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007795 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007796 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007797 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007798 return;
7799 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007800#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007801 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7802 /* Refuse to quit when locked or when the buffer in the last window is
7803 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007804 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007805 return;
7806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807
7808 /*
7809 * if more files or windows we won't exit
7810 */
7811 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7812 exiting = TRUE;
7813 if ( ((eap->cmdidx == CMD_wq
7814 || curbufIsChanged())
7815 && do_write(eap) == FAIL)
7816 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007817 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {
7819 not_exiting();
7820 }
7821 else
7822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 getout(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825# ifdef FEAT_GUI
7826 need_mouse_correct = TRUE;
7827# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007828 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007829 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
7831}
7832
7833/*
7834 * ":print", ":list", ":number".
7835 */
7836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007837ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007839 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7840 EMSG(_(e_emptybuf));
7841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007843 for ( ;!got_int; ui_breakcheck())
7844 {
7845 print_line(eap->line1,
7846 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7847 || (eap->flags & EXFLAG_NR)),
7848 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7849 if (++eap->line1 > eap->line2)
7850 break;
7851 out_flush(); /* show one line at a time */
7852 }
7853 setpcmark();
7854 /* put cursor at last line */
7855 curwin->w_cursor.lnum = eap->line2;
7856 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860}
7861
7862#ifdef FEAT_BYTEOFF
7863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007864ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 goto_byte(eap->line2);
7867}
7868#endif
7869
7870/*
7871 * ":shell".
7872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007874ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 do_shell(NULL, 0);
7877}
7878
Bram Moolenaar4033c552017-09-16 20:54:51 +02007879#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007881 || defined(FEAT_GUI_MSWIN) \
7882 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 || defined(PROTO)
7884
7885/*
7886 * Handle a file drop. The code is here because a drop is *nearly* like an
7887 * :args command, but not quite (we have a list of exact filenames, so we
7888 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7889 * code is very similar to :args and hence needs access to a lot of the static
7890 * functions in this file.
7891 *
7892 * The list should be allocated using alloc(), as should each item in the
7893 * list. This function takes over responsibility for freeing the list.
7894 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007895 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007896 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 */
7898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007899handle_drop(
7900 int filec, /* the number of files dropped */
7901 char_u **filev, /* the list of files dropped */
7902 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 exarg_T ea;
7905 int save_msg_scroll = msg_scroll;
7906
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007907 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007908 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007910#ifdef FEAT_AUTOCMD
7911 if (curbuf_locked())
7912 return;
7913#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007914 /* When the screen is being updated we should not change buffers and
7915 * windows structures, it may cause freed memory to be used. */
7916 if (updating_screen)
7917 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918
7919 /* Check whether the current buffer is changed. If so, we will need
7920 * to split the current window or data could be lost.
7921 * We don't need to check if the 'hidden' option is set, as in this
7922 * case the buffer won't be lost.
7923 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007924 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
7926 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007927 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 --emsg_off;
7929 }
7930 if (split)
7931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 if (win_split(0, 0) == FAIL)
7933 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007934 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
7936 /* When splitting the window, create a new alist. Otherwise the
7937 * existing one is overwritten. */
7938 alist_unlink(curwin->w_alist);
7939 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 }
7941
7942 /*
7943 * Set up the new argument list.
7944 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007945 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946
7947 /*
7948 * Move to the first file.
7949 */
7950 /* Fake up a minimal "next" command for do_argfile() */
7951 vim_memset(&ea, 0, sizeof(ea));
7952 ea.cmd = (char_u *)"next";
7953 do_argfile(&ea, 0);
7954
7955 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7956 * mode that is not needed here. */
7957 need_start_insertmode = FALSE;
7958
7959 /* Restore msg_scroll, otherwise a following command may cause scrolling
7960 * unexpectedly. The screen will be redrawn by the caller, thus
7961 * msg_scroll being set by displaying a message is irrelevant. */
7962 msg_scroll = save_msg_scroll;
7963}
7964#endif
7965
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966/*
7967 * Clear an argument list: free all file names and reset it to zero entries.
7968 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007969 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007970alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971{
7972 while (--al->al_ga.ga_len >= 0)
7973 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7974 ga_clear(&al->al_ga);
7975}
7976
7977/*
7978 * Init an argument list.
7979 */
7980 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007981alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7984}
7985
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986/*
7987 * Remove a reference from an argument list.
7988 * Ignored when the argument list is the global one.
7989 * If the argument list is no longer used by any window, free it.
7990 */
7991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007992alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
7994 if (al != &global_alist && --al->al_refcount <= 0)
7995 {
7996 alist_clear(al);
7997 vim_free(al);
7998 }
7999}
8000
Bram Moolenaar4033c552017-09-16 20:54:51 +02008001#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002/*
8003 * Create a new argument list and use it for the current window.
8004 */
8005 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008006alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8009 if (curwin->w_alist == NULL)
8010 {
8011 curwin->w_alist = &global_alist;
8012 ++global_alist.al_refcount;
8013 }
8014 else
8015 {
8016 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008017 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 alist_init(curwin->w_alist);
8019 }
8020}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021#endif
8022
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008023#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024/*
8025 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008026 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8027 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 */
8029 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008030alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
8032 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008033 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 char_u **new_arg_files;
8035 int new_arg_file_count;
8036 char_u *save_p_su = p_su;
8037 int i;
8038
8039 /* Don't use 'suffixes' here. This should work like the shell did the
8040 * expansion. Also, the vimrc file isn't read yet, thus the user
8041 * can't set the options. */
8042 p_su = empty_option;
8043 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8044 if (old_arg_files != NULL)
8045 {
8046 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008047 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8048 old_arg_count = GARGCOUNT;
8049 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008051 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 && new_arg_file_count > 0)
8053 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008054 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8055 TRUE, fnum_list, fnum_len);
8056 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059 p_su = save_p_su;
8060}
8061#endif
8062
8063/*
8064 * Set the argument list for the current window.
8065 * Takes over the allocated files[] and the allocated fnames in it.
8066 */
8067 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008068alist_set(
8069 alist_T *al,
8070 int count,
8071 char_u **files,
8072 int use_curbuf,
8073 int *fnum_list,
8074 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 int i;
8077
8078 alist_clear(al);
8079 if (ga_grow(&al->al_ga, count) == OK)
8080 {
8081 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008082 {
8083 if (got_int)
8084 {
8085 /* When adding many buffers this can take a long time. Allow
8086 * interrupting here. */
8087 while (i < count)
8088 vim_free(files[i++]);
8089 break;
8090 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008091
8092 /* May set buffer name of a buffer previously used for the
8093 * argument list, so that it's re-used by alist_add. */
8094 if (fnum_list != NULL && i < fnum_len)
8095 buf_set_name(fnum_list[i], files[i]);
8096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008098 ui_breakcheck();
8099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 vim_free(files);
8101 }
8102 else
8103 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 arg_had_last = FALSE;
8106}
8107
8108/*
8109 * Add file "fname" to argument list "al".
8110 * "fname" must have been allocated and "al" must have been checked for room.
8111 */
8112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008113alist_add(
8114 alist_T *al,
8115 char_u *fname,
8116 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117{
8118 if (fname == NULL) /* don't add NULL file names */
8119 return;
8120#ifdef BACKSLASH_IN_FILENAME
8121 slash_adjust(fname);
8122#endif
8123 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8124 if (set_fnum > 0)
8125 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8126 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8127 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128}
8129
8130#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8131/*
8132 * Adjust slashes in file names. Called after 'shellslash' was set.
8133 */
8134 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008135alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008139 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141 for (i = 0; i < GARGCOUNT; ++i)
8142 if (GARGLIST[i].ae_fname != NULL)
8143 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008144 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (wp->w_alist != &global_alist)
8146 for (i = 0; i < WARGCOUNT(wp); ++i)
8147 if (WARGLIST(wp)[i].ae_fname != NULL)
8148 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149}
8150#endif
8151
8152/*
8153 * ":preserve".
8154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008158 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 ml_preserve(curbuf, TRUE);
8160}
8161
8162/*
8163 * ":recover".
8164 */
8165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008166ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
8168 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8169 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008170 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8171 | CCGD_MULTWIN
8172 | (eap->forceit ? CCGD_FORCEIT : 0)
8173 | CCGD_EXCMD)
8174
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 && (*eap->arg == NUL
8176 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8177 ml_recover();
8178 recoverymode = FALSE;
8179}
8180
8181/*
8182 * Command modifier used in a wrong way.
8183 */
8184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008185ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
8187 eap->errmsg = e_invcmd;
8188}
8189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190/*
8191 * :sview [+command] file split window with new file, read-only
8192 * :split [[+command] file] split window with current or new file
8193 * :vsplit [[+command] file] split window vertically with current or new file
8194 * :new [[+command] file] split window with no or new file
8195 * :vnew [[+command] file] split vertically window with no or new file
8196 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008197 *
8198 * :tabedit open new Tab page with empty window
8199 * :tabedit [+command] file open new Tab page and edit "file"
8200 * :tabnew [[+command] file] just like :tabedit
8201 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 */
8203 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008204ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008206 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008207#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008209#endif
8210#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213
Bram Moolenaar4033c552017-09-16 20:54:51 +02008214#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217
Bram Moolenaar4033c552017-09-16 20:54:51 +02008218#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008220 * quickfix windows. But it's OK when doing ":tab split". */
8221 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {
8223 if (eap->cmdidx == CMD_split)
8224 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 if (eap->cmdidx == CMD_vsplit)
8226 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
Bram Moolenaar4033c552017-09-16 20:54:51 +02008230#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008231 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
8233 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8234 FNAME_MESS, TRUE, curbuf->b_ffname);
8235 if (fname == NULL)
8236 goto theend;
8237 eap->arg = fname;
8238 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008239# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008240 else
8241# endif
8242#endif
8243#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 && eap->cmdidx != CMD_new)
8247 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008248# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008249 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008250# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008251 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008252# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008253 au_has_group((char_u *)"FileExplorer"))
8254 {
8255 /* No browsing supported but we do have the file explorer:
8256 * Edit the directory. */
8257 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8258 eap->arg = (char_u *)".";
8259 }
8260 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008261# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008262 {
8263 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008265 if (fname == NULL)
8266 goto theend;
8267 eap->arg = fname;
8268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 }
8270 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008273 /*
8274 * Either open new tab page or split the window.
8275 */
8276 if (eap->cmdidx == CMD_tabedit
8277 || eap->cmdidx == CMD_tabfind
8278 || eap->cmdidx == CMD_tabnew)
8279 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008280 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8281 : eap->addr_count == 0 ? 0
8282 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008283 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008284 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008285
8286 /* set the alternate buffer for the window we came from */
8287 if (curwin != old_curwin
8288 && win_valid(old_curwin)
8289 && old_curwin->w_buffer != curbuf
8290 && !cmdmod.keepalt)
8291 old_curwin->w_alt_fnum = curbuf->b_fnum;
8292 }
8293 }
8294 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8296 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008297# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 /* Reset 'scrollbind' when editing another file, but keep it when
8299 * doing ":split" without arguments. */
8300 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008301# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008305 {
8306 RESET_BINDING(curwin);
8307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 else
8309 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 do_exedit(eap, old_curwin);
8312 }
8313
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008318# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319theend:
8320 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008323
8324/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008325 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008326 */
8327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008328tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008329{
8330 exarg_T ea;
8331
8332 vim_memset(&ea, 0, sizeof(ea));
8333 ea.cmdidx = CMD_tabnew;
8334 ea.cmd = (char_u *)"tabn";
8335 ea.arg = (char_u *)"";
8336 ex_splitview(&ea);
8337}
8338
8339/*
8340 * :tabnext command
8341 */
8342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008343ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008344{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008345 int tab_number;
8346
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008347 switch (eap->cmdidx)
8348 {
8349 case CMD_tabfirst:
8350 case CMD_tabrewind:
8351 goto_tabpage(1);
8352 break;
8353 case CMD_tablast:
8354 goto_tabpage(9999);
8355 break;
8356 case CMD_tabprevious:
8357 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008358 if (eap->arg && *eap->arg != NUL)
8359 {
8360 char_u *p = eap->arg;
8361 char_u *p_save = p;
8362
8363 tab_number = getdigits(&p);
8364 if (p == p_save || *p_save == '-' || *p != NUL
8365 || tab_number == 0)
8366 {
8367 /* No numbers as argument. */
8368 eap->errmsg = e_invarg;
8369 return;
8370 }
8371 }
8372 else
8373 {
8374 if (eap->addr_count == 0)
8375 tab_number = 1;
8376 else
8377 {
8378 tab_number = eap->line2;
8379 if (tab_number < 1)
8380 {
8381 eap->errmsg = e_invrange;
8382 return;
8383 }
8384 }
8385 }
8386 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008387 break;
8388 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008389 tab_number = get_tabpage_arg(eap);
8390 if (eap->errmsg == NULL)
8391 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008392 break;
8393 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008394}
8395
8396/*
8397 * :tabmove command
8398 */
8399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008400ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008401{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008402 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008403
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008404 tab_number = get_tabpage_arg(eap);
8405 if (eap->errmsg == NULL)
8406 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008407}
8408
8409/*
8410 * :tabs command: List tabs and their contents.
8411 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008413ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008414{
8415 tabpage_T *tp;
8416 win_T *wp;
8417 int tabcount = 1;
8418
8419 msg_start();
8420 msg_scroll = TRUE;
8421 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8422 {
8423 msg_putchar('\n');
8424 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008425 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426 out_flush(); /* output one line at a time */
8427 ui_breakcheck();
8428
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008429 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008430 wp = firstwin;
8431 else
8432 wp = tp->tp_firstwin;
8433 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8434 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008435 msg_putchar('\n');
8436 msg_putchar(wp == curwin ? '>' : ' ');
8437 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008438 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8439 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008440 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008441 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008442 else
8443 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8444 IObuff, IOSIZE, TRUE);
8445 msg_outtrans(IObuff);
8446 out_flush(); /* output one line at a time */
8447 ui_breakcheck();
8448 }
8449 }
8450}
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452/*
8453 * ":mode": Set screen mode.
8454 * If no argument given, just get the screen size and redraw.
8455 */
8456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008457ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
8459 if (*eap->arg == NUL)
8460 shell_resized();
8461 else
8462 mch_screenmode(eap->arg);
8463}
8464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465/*
8466 * ":resize".
8467 * set, increment or decrement current window height
8468 */
8469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008470ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 int n;
8473 win_T *wp = curwin;
8474
8475 if (eap->addr_count > 0)
8476 {
8477 n = eap->line2;
8478 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8479 ;
8480 }
8481
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008482# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (cmdmod.split & WSP_VERT)
8487 {
8488 if (*eap->arg == '-' || *eap->arg == '+')
8489 n += W_WIDTH(curwin);
8490 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8491 n = 9999;
8492 win_setwidth_win((int)n, wp);
8493 }
8494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 {
8496 if (*eap->arg == '-' || *eap->arg == '+')
8497 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008498 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 n = 9999;
8500 win_setheight_win((int)n, wp);
8501 }
8502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504/*
8505 * ":find [+command] <file>" command.
8506 */
8507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008508ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509{
8510#ifdef FEAT_SEARCHPATH
8511 char_u *fname;
8512 int count;
8513
8514 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8515 TRUE, curbuf->b_ffname);
8516 if (eap->addr_count > 0)
8517 {
8518 /* Repeat finding the file "count" times. This matters when it
8519 * appears several times in the path. */
8520 count = eap->line2;
8521 while (fname != NULL && --count > 0)
8522 {
8523 vim_free(fname);
8524 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8525 FALSE, curbuf->b_ffname);
8526 }
8527 }
8528
8529 if (fname != NULL)
8530 {
8531 eap->arg = fname;
8532#endif
8533 do_exedit(eap, NULL);
8534#ifdef FEAT_SEARCHPATH
8535 vim_free(fname);
8536 }
8537#endif
8538}
8539
8540/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008541 * ":open" simulation: for now just work like ":visual".
8542 */
8543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008544ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008545{
8546 regmatch_T regmatch;
8547 char_u *p;
8548
8549 curwin->w_cursor.lnum = eap->line2;
8550 beginline(BL_SOL | BL_FIX);
8551 if (*eap->arg == '/')
8552 {
8553 /* ":open /pattern/": put cursor in column found with pattern */
8554 ++eap->arg;
8555 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8556 *p = NUL;
8557 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8558 if (regmatch.regprog != NULL)
8559 {
8560 regmatch.rm_ic = p_ic;
8561 p = ml_get_curline();
8562 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008563 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008564 else
8565 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008566 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008567 }
8568 /* Move to the NUL, ignore any other arguments. */
8569 eap->arg += STRLEN(eap->arg);
8570 }
8571 check_cursor();
8572
8573 eap->cmdidx = CMD_visual;
8574 do_exedit(eap, NULL);
8575}
8576
8577/*
8578 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 */
8580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008581ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583 do_exedit(eap, NULL);
8584}
8585
8586/*
8587 * ":edit <file>" command and alikes.
8588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008590do_exedit(
8591 exarg_T *eap,
8592 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008596 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597
8598 /*
8599 * ":vi" command ends Ex mode.
8600 */
8601 if (exmode_active && (eap->cmdidx == CMD_visual
8602 || eap->cmdidx == CMD_view))
8603 {
8604 exmode_active = FALSE;
8605 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008606 {
8607 /* Special case: ":global/pat/visual\NLvi-commands" */
8608 if (global_busy)
8609 {
8610 int rd = RedrawingDisabled;
8611 int nwr = no_wait_return;
8612 int ms = msg_scroll;
8613#ifdef FEAT_GUI
8614 int he = hold_gui_events;
8615#endif
8616
8617 if (eap->nextcmd != NULL)
8618 {
8619 stuffReadbuff(eap->nextcmd);
8620 eap->nextcmd = NULL;
8621 }
8622
8623 if (exmode_was != EXMODE_VIM)
8624 settmode(TMODE_RAW);
8625 RedrawingDisabled = 0;
8626 no_wait_return = 0;
8627 need_wait_return = FALSE;
8628 msg_scroll = 0;
8629#ifdef FEAT_GUI
8630 hold_gui_events = 0;
8631#endif
8632 must_redraw = CLEAR;
8633
8634 main_loop(FALSE, TRUE);
8635
8636 RedrawingDisabled = rd;
8637 no_wait_return = nwr;
8638 msg_scroll = ms;
8639#ifdef FEAT_GUI
8640 hold_gui_events = he;
8641#endif
8642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
8646
8647 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008648 || eap->cmdidx == CMD_tabnew
8649 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008650 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008652 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 setpcmark();
8654 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008655 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8656 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008658 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 || *eap->arg != NUL
8660#ifdef FEAT_BROWSE
8661 || cmdmod.browse
8662#endif
8663 )
8664 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008665#ifdef FEAT_AUTOCMD
8666 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8667 * can bring us here, others are stopped earlier. */
8668 if (*eap->arg != NUL && curbuf_locked())
8669 return;
8670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 n = readonlymode;
8672 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8673 readonlymode = TRUE;
8674 else if (eap->cmdidx == CMD_enew)
8675 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8676 empty buffer */
8677 setpcmark();
8678 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8679 NULL, eap,
8680 /* ":edit" goes to first line if Vi compatible */
8681 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8682 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8683 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008684 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008686 /* after a split we can use an existing buffer */
8687 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688#ifdef FEAT_LISTCMDS
8689 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8690#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008691 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
8693 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (old_curwin != NULL)
8695 {
8696 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008697 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02008699#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008700 cleanup_T cs;
8701
8702 /* Reset the error/interrupt/exception state here so that
8703 * aborting() returns FALSE when closing a window. */
8704 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008705#endif
8706#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008708#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008709 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008710
Bram Moolenaar4033c552017-09-16 20:54:51 +02008711#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008712 /* Restore the error/interrupt/exception state if not
8713 * discarded by a new aborting error, interrupt, or
8714 * uncaught exception. */
8715 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 }
8718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 }
8720 else if (readonlymode && curbuf->b_nwindows == 1)
8721 {
8722 /* When editing an already visited buffer, 'readonly' won't be set
8723 * but the previous value is kept. With ":view" and ":sview" we
8724 * want the file to be readonly, except when another window is
8725 * editing the same buffer. */
8726 curbuf->b_p_ro = TRUE;
8727 }
8728 readonlymode = n;
8729 }
8730 else
8731 {
8732 if (eap->do_ecmd_cmd != NULL)
8733 do_cmdline_cmd(eap->do_ecmd_cmd);
8734#ifdef FEAT_TITLE
8735 n = curwin->w_arg_idx_invalid;
8736#endif
8737 check_arg_idx(curwin);
8738#ifdef FEAT_TITLE
8739 if (n != curwin->w_arg_idx_invalid)
8740 maketitle();
8741#endif
8742 }
8743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 /*
8745 * if ":split file" worked, set alternate file name in old window to new
8746 * file
8747 */
8748 if (old_curwin != NULL
8749 && *eap->arg != NUL
8750 && curwin != old_curwin
8751 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008752 && old_curwin->w_buffer != curbuf
8753 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
8756 ex_no_reprint = TRUE;
8757}
8758
8759#ifndef FEAT_GUI
8760/*
8761 * ":gui" and ":gvim" when there is no GUI.
8762 */
8763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008764ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 eap->errmsg = e_nogvim;
8767}
8768#endif
8769
8770#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008772ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 gui_make_tearoff(eap->arg);
8775}
8776#endif
8777
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008778#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008780ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008782 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783}
8784#endif
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8790 MSG(_("No swap file"));
8791 else
8792 msg(curbuf->b_ml.ml_mfp->mf_fname);
8793}
8794
8795/*
8796 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8797 * offset.
8798 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008801ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
8803#ifdef FEAT_SCROLLBIND
8804 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008805 win_T *save_curwin = curwin;
8806 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 long topline;
8808 long y;
8809 linenr_T old_linenr = curwin->w_cursor.lnum;
8810
8811 setpcmark();
8812
8813 /*
8814 * determine max topline
8815 */
8816 if (curwin->w_p_scb)
8817 {
8818 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008819 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
8821 if (wp->w_p_scb && wp->w_buffer)
8822 {
8823 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8824 if (topline > y)
8825 topline = y;
8826 }
8827 }
8828 if (topline < 1)
8829 topline = 1;
8830 }
8831 else
8832 {
8833 topline = 1;
8834 }
8835
8836
8837 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008838 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008840 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {
8842 if (curwin->w_p_scb)
8843 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008844 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 y = topline - curwin->w_topline;
8846 if (y > 0)
8847 scrollup(y, TRUE);
8848 else
8849 scrolldown(-y, TRUE);
8850 curwin->w_scbind_pos = topline;
8851 redraw_later(VALID);
8852 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
8855 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008856 curwin = save_curwin;
8857 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (curwin->w_p_scb)
8859 {
8860 did_syncbind = TRUE;
8861 checkpcmark();
8862 if (old_linenr != curwin->w_cursor.lnum)
8863 {
8864 char_u ctrl_o[2];
8865
8866 ctrl_o[0] = Ctrl_O;
8867 ctrl_o[1] = 0;
8868 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8869 }
8870 }
8871#endif
8872}
8873
8874
8875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008876ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008878 int i;
8879 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8880 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
8882 if (eap->usefilter) /* :r!cmd */
8883 do_bang(1, eap, FALSE, FALSE, TRUE);
8884 else
8885 {
8886 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8887 return;
8888
8889#ifdef FEAT_BROWSE
8890 if (cmdmod.browse)
8891 {
8892 char_u *browseFile;
8893
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008894 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 NULL, NULL, NULL, curbuf);
8896 if (browseFile != NULL)
8897 {
8898 i = readfile(browseFile, NULL,
8899 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8900 vim_free(browseFile);
8901 }
8902 else
8903 i = OK;
8904 }
8905 else
8906#endif
8907 if (*eap->arg == NUL)
8908 {
8909 if (check_fname() == FAIL) /* check for no file name */
8910 return;
8911 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8912 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8913 }
8914 else
8915 {
8916 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8917 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8918 i = readfile(eap->arg, NULL,
8919 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8920
8921 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008922 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
8924#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8925 if (!aborting())
8926#endif
8927 EMSG2(_(e_notopen), eap->arg);
8928 }
8929 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008930 {
8931 if (empty && exmode_active)
8932 {
8933 /* Delete the empty line that remains. Historically ex does
8934 * this but vi doesn't. */
8935 if (eap->line2 == 0)
8936 lnum = curbuf->b_ml.ml_line_count;
8937 else
8938 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008939 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008940 {
8941 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008942 if (curwin->w_cursor.lnum > 1
8943 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008944 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008945 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008946 }
8947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951}
8952
Bram Moolenaarea408852005-06-25 22:49:46 +00008953static char_u *prev_dir = NULL;
8954
8955#if defined(EXITFREE) || defined(PROTO)
8956 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008957free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008958{
8959 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008960 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008961
8962 vim_free(globaldir);
8963 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008964}
8965#endif
8966
Bram Moolenaarf4258302013-06-02 18:20:17 +02008967/*
8968 * Deal with the side effects of changing the current directory.
8969 * When "local" is TRUE then this was after an ":lcd" command.
8970 */
8971 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008972post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008973{
8974 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008975 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008976 if (local)
8977 {
8978 /* If still in global directory, need to remember current
8979 * directory as global directory. */
8980 if (globaldir == NULL && prev_dir != NULL)
8981 globaldir = vim_strsave(prev_dir);
8982 /* Remember this local directory for the window. */
8983 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8984 curwin->w_localdir = vim_strsave(NameBuff);
8985 }
8986 else
8987 {
8988 /* We are now in the global directory, no need to remember its
8989 * name. */
8990 vim_free(globaldir);
8991 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008992 }
8993
8994 shorten_fnames(TRUE);
8995}
8996
Bram Moolenaarea408852005-06-25 22:49:46 +00008997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998/*
8999 * ":cd", ":lcd", ":chdir" and ":lchdir".
9000 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009001 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009002ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 char_u *new_dir;
9005 char_u *tofree;
9006
9007 new_dir = eap->arg;
9008#if !defined(UNIX) && !defined(VMS)
9009 /* for non-UNIX ":cd" means: print current directory */
9010 if (*new_dir == NUL)
9011 ex_pwd(NULL);
9012 else
9013#endif
9014 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009015#ifdef FEAT_AUTOCMD
9016 if (allbuf_locked())
9017 return;
9018#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009019 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9020 && !eap->forceit)
9021 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009022 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009023 return;
9024 }
9025
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 /* ":cd -": Change to previous directory */
9027 if (STRCMP(new_dir, "-") == 0)
9028 {
9029 if (prev_dir == NULL)
9030 {
9031 EMSG(_("E186: No previous directory"));
9032 return;
9033 }
9034 new_dir = prev_dir;
9035 }
9036
9037 /* Save current directory for next ":cd -" */
9038 tofree = prev_dir;
9039 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9040 prev_dir = vim_strsave(NameBuff);
9041 else
9042 prev_dir = NULL;
9043
9044#if defined(UNIX) || defined(VMS)
9045 /* for UNIX ":cd" means: go to home directory */
9046 if (*new_dir == NUL)
9047 {
9048 /* use NameBuff for home directory name */
9049# ifdef VMS
9050 char_u *p;
9051
9052 p = mch_getenv((char_u *)"SYS$LOGIN");
9053 if (p == NULL || *p == NUL) /* empty is the same as not set */
9054 NameBuff[0] = NUL;
9055 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009056 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057# else
9058 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9059# endif
9060 new_dir = NameBuff;
9061 }
9062#endif
9063 if (new_dir == NULL || vim_chdir(new_dir))
9064 EMSG(_(e_failed));
9065 else
9066 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009067 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068
9069 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009070 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 ex_pwd(eap);
9072 }
9073 vim_free(tofree);
9074 }
9075}
9076
9077/*
9078 * ":pwd".
9079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009081ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9084 {
9085#ifdef BACKSLASH_IN_FILENAME
9086 slash_adjust(NameBuff);
9087#endif
9088 msg(NameBuff);
9089 }
9090 else
9091 EMSG(_("E187: Unknown"));
9092}
9093
9094/*
9095 * ":=".
9096 */
9097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009098ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
9100 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009101 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
9104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009105ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009107 int n;
9108 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
9110 if (cursor_valid())
9111 {
9112 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9113 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009114 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009116
9117 len = eap->line2;
9118 switch (*eap->arg)
9119 {
9120 case 'm': break;
9121 case NUL: len *= 1000L; break;
9122 default: EMSG2(_(e_invarg2), eap->arg); return;
9123 }
9124 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9129 */
9130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009131do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009134 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
9136 cursor_on();
9137 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009138 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009140 wait_now = msec - done > 1000L ? 1000L : msec - done;
9141#ifdef FEAT_TIMERS
9142 {
9143 long due_time = check_due_timer();
9144
9145 if (due_time > 0 && due_time < wait_now)
9146 wait_now = due_time;
9147 }
9148#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009149#ifdef FEAT_JOB_CHANNEL
9150 if (has_any_channel() && wait_now > 100L)
9151 wait_now = 100L;
9152#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009153 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009154#ifdef FEAT_JOB_CHANNEL
9155 if (has_any_channel())
9156 ui_breakcheck_force(TRUE);
9157 else
9158#endif
9159 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009160#ifdef MESSAGE_QUEUE
9161 /* Process the netbeans and clientserver messages that may have been
9162 * received in the call to ui_breakcheck() when the GUI is in use. This
9163 * may occur when running a test case. */
9164 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 }
9167}
9168
9169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009170do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171{
9172 int mode;
9173 char_u *cmdp;
9174
9175 cmdp = eap->cmd;
9176 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9177
9178 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9179 eap->arg, mode, isabbrev))
9180 {
9181 case 1: EMSG(_(e_invarg));
9182 break;
9183 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9184 break;
9185 }
9186}
9187
9188/*
9189 * ":winsize" command (obsolete).
9190 */
9191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009192ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 int w, h;
9195 char_u *arg = eap->arg;
9196 char_u *p;
9197
9198 w = getdigits(&arg);
9199 arg = skipwhite(arg);
9200 p = arg;
9201 h = getdigits(&arg);
9202 if (*p != NUL && *arg == NUL)
9203 set_shellsize(w, h, TRUE);
9204 else
9205 EMSG(_("E465: :winsize requires two number arguments"));
9206}
9207
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009209ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 int xchar = NUL;
9212 char_u *p;
9213
9214 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9215 {
9216 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9217 if (eap->arg[1] == NUL)
9218 {
9219 EMSG(_(e_invarg));
9220 return;
9221 }
9222 xchar = eap->arg[1];
9223 p = eap->arg + 2;
9224 }
9225 else
9226 p = eap->arg + 1;
9227
9228 eap->nextcmd = check_nextcmd(p);
9229 p = skipwhite(p);
9230 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9231 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009232 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 {
9234 /* Pass flags on for ":vertical wincmd ]". */
9235 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009236 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9238 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009239 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 }
9241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242
Bram Moolenaar843ee412004-06-30 16:16:41 +00009243#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244/*
9245 * ":winpos".
9246 */
9247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009248ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249{
9250 int x, y;
9251 char_u *arg = eap->arg;
9252 char_u *p;
9253
9254 if (*arg == NUL)
9255 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009256# if defined(FEAT_GUI) || defined(MSWIN)
9257# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009259# else
9260 if (mch_get_winpos(&x, &y) != FAIL)
9261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
9263 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9264 msg(IObuff);
9265 }
9266 else
9267# endif
9268 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9269 }
9270 else
9271 {
9272 x = getdigits(&arg);
9273 arg = skipwhite(arg);
9274 p = arg;
9275 y = getdigits(&arg);
9276 if (*p == NUL || *arg != NUL)
9277 {
9278 EMSG(_("E466: :winpos requires two number arguments"));
9279 return;
9280 }
9281# ifdef FEAT_GUI
9282 if (gui.in_use)
9283 gui_mch_set_winpos(x, y);
9284 else if (gui.starting)
9285 {
9286 /* Remember the coordinates for when the window is opened. */
9287 gui_win_x = x;
9288 gui_win_y = y;
9289 }
9290# ifdef HAVE_TGETENT
9291 else
9292# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009293# else
9294# ifdef MSWIN
9295 mch_set_winpos(x, y);
9296# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297# endif
9298# ifdef HAVE_TGETENT
9299 if (*T_CWP)
9300 term_set_winpos(x, y);
9301# endif
9302 }
9303}
9304#endif
9305
9306/*
9307 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9308 */
9309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009310ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311{
9312 oparg_T oa;
9313
9314 clear_oparg(&oa);
9315 oa.regname = eap->regname;
9316 oa.start.lnum = eap->line1;
9317 oa.end.lnum = eap->line2;
9318 oa.line_count = eap->line2 - eap->line1 + 1;
9319 oa.motion_type = MLINE;
9320#ifdef FEAT_VIRTUALEDIT
9321 virtual_op = FALSE;
9322#endif
9323 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9324 {
9325 setpcmark();
9326 curwin->w_cursor.lnum = eap->line1;
9327 beginline(BL_SOL | BL_FIX);
9328 }
9329
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009330 if (VIsual_active)
9331 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009332
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 switch (eap->cmdidx)
9334 {
9335 case CMD_delete:
9336 oa.op_type = OP_DELETE;
9337 op_delete(&oa);
9338 break;
9339
9340 case CMD_yank:
9341 oa.op_type = OP_YANK;
9342 (void)op_yank(&oa, FALSE, TRUE);
9343 break;
9344
9345 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009346 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009348 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9349#else
9350 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009352 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 oa.op_type = OP_RSHIFT;
9354 else
9355 oa.op_type = OP_LSHIFT;
9356 op_shift(&oa, FALSE, eap->amount);
9357 break;
9358 }
9359#ifdef FEAT_VIRTUALEDIT
9360 virtual_op = MAYBE;
9361#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009362 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363}
9364
9365/*
9366 * ":put".
9367 */
9368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009369ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370{
9371 /* ":0put" works like ":1put!". */
9372 if (eap->line2 == 0)
9373 {
9374 eap->line2 = 1;
9375 eap->forceit = TRUE;
9376 }
9377 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009378 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9379 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380}
9381
9382/*
9383 * Handle ":copy" and ":move".
9384 */
9385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009386ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387{
9388 long n;
9389
Bram Moolenaarded27822017-01-02 14:27:34 +01009390 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 if (eap->arg == NULL) /* error detected */
9392 {
9393 eap->nextcmd = NULL;
9394 return;
9395 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009396 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
9398 /*
9399 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9400 */
9401 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9402 {
9403 EMSG(_(e_invaddr));
9404 return;
9405 }
9406
9407 if (eap->cmdidx == CMD_move)
9408 {
9409 if (do_move(eap->line1, eap->line2, n) == FAIL)
9410 return;
9411 }
9412 else
9413 ex_copy(eap->line1, eap->line2, n);
9414 u_clearline();
9415 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009416 ex_may_print(eap);
9417}
9418
9419/*
9420 * Print the current line if flags were given to the Ex command.
9421 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009422 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009423ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009424{
9425 if (eap->flags != 0)
9426 {
9427 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9428 (eap->flags & EXFLAG_LIST));
9429 ex_no_reprint = TRUE;
9430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431}
9432
9433/*
9434 * ":smagic" and ":snomagic".
9435 */
9436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009437ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 int magic_save = p_magic;
9440
9441 p_magic = (eap->cmdidx == CMD_smagic);
9442 do_sub(eap);
9443 p_magic = magic_save;
9444}
9445
9446/*
9447 * ":join".
9448 */
9449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009450ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 curwin->w_cursor.lnum = eap->line1;
9453 if (eap->line1 == eap->line2)
9454 {
9455 if (eap->addr_count >= 2) /* :2,2join does nothing */
9456 return;
9457 if (eap->line2 == curbuf->b_ml.ml_line_count)
9458 {
9459 beep_flush();
9460 return;
9461 }
9462 ++eap->line2;
9463 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009464 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009466 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
9469/*
9470 * ":[addr]@r" or ":[addr]*r": execute register
9471 */
9472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009473ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009476 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477
9478 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009479 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480
9481#ifdef USE_ON_FLY_SCROLL
9482 dont_scroll = TRUE; /* disallow scrolling here */
9483#endif
9484
9485 /* get the register name. No name means to use the previous one */
9486 c = *eap->arg;
9487 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9488 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009489 /* Put the register in the typeahead buffer with the "silent" flag. */
9490 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9491 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 else
9496 {
9497 int save_efr = exec_from_reg;
9498
9499 exec_from_reg = TRUE;
9500
9501 /*
9502 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009503 * Continue until the stuff buffer is empty and all added characters
9504 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009506 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9508
9509 exec_from_reg = save_efr;
9510 }
9511}
9512
9513/*
9514 * ":!".
9515 */
9516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009517ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9520}
9521
9522/*
9523 * ":undo".
9524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009526ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009528 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009529 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009530 else
9531 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532}
9533
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009534#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009536ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009537{
9538 char_u hash[UNDO_HASH_SIZE];
9539
9540 u_compute_hash(hash);
9541 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9542}
9543
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009545ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009546{
9547 char_u hash[UNDO_HASH_SIZE];
9548
9549 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009550 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009551}
9552#endif
9553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554/*
9555 * ":redo".
9556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009558ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559{
9560 u_redo(1);
9561}
9562
9563/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009564 * ":earlier" and ":later".
9565 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009567ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009568{
9569 long count = 0;
9570 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009571 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009572 char_u *p = eap->arg;
9573
9574 if (*p == NUL)
9575 count = 1;
9576 else if (isdigit(*p))
9577 {
9578 count = getdigits(&p);
9579 switch (*p)
9580 {
9581 case 's': ++p; sec = TRUE; break;
9582 case 'm': ++p; sec = TRUE; count *= 60; break;
9583 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009584 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9585 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009586 }
9587 }
9588
9589 if (*p != NUL)
9590 EMSG2(_(e_invarg2), eap->arg);
9591 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009592 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9593 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009594}
9595
9596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 * ":redir": start/stop redirection.
9598 */
9599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009600ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 char *mode;
9603 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009604 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605
Bram Moolenaarba768492016-07-08 15:32:54 +02009606#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009607 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009608 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009609 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009610 return;
9611 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009612#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009613
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 if (STRICMP(eap->arg, "END") == 0)
9615 close_redir();
9616 else
9617 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009618 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009620 ++arg;
9621 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009623 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 mode = "a";
9625 }
9626 else
9627 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009628 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
9630 close_redir();
9631
9632 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009633 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 if (fname == NULL)
9635 return;
9636#ifdef FEAT_BROWSE
9637 if (cmdmod.browse)
9638 {
9639 char_u *browseFile;
9640
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009641 browseFile = do_browse(BROWSE_SAVE,
9642 (char_u *)_("Save Redirection"),
9643 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 if (browseFile == NULL)
9645 return; /* operation cancelled */
9646 vim_free(fname);
9647 fname = browseFile;
9648 eap->forceit = TRUE; /* since dialog already asked */
9649 }
9650#endif
9651
9652 redir_fd = open_exfile(fname, eap->forceit, mode);
9653 vim_free(fname);
9654 }
9655#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009656 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 {
9658 /* redirect to a register a-z (resp. A-Z for appending) */
9659 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009660 ++arg;
9661 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009663 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009664 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009666 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009668 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009669 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009670 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009673 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009674 if (*arg == '>')
9675 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009676 /* Make register empty when not using @A-@Z and the
9677 * command is valid. */
9678 if (*arg == NUL && !isupper(redir_reg))
9679 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009681 }
9682 if (*arg != NUL)
9683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009684 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009685 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009686 }
9687 }
9688 else if (*arg == '=' && arg[1] == '>')
9689 {
9690 int append;
9691
9692 /* redirect to a variable */
9693 close_redir();
9694 arg += 2;
9695
9696 if (*arg == '>')
9697 {
9698 ++arg;
9699 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 }
9701 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009702 append = FALSE;
9703
9704 if (var_redir_start(skipwhite(arg), append) == OK)
9705 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 }
9707#endif
9708
9709 /* TODO: redirect to a buffer */
9710
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009712 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009714
9715 /* Make sure redirection is not off. Can happen for cmdline completion
9716 * that indirectly invokes a command to catch its output. */
9717 if (redir_fd != NULL
9718#ifdef FEAT_EVAL
9719 || redir_reg || redir_vname
9720#endif
9721 )
9722 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
9725/*
9726 * ":redraw": force redraw
9727 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009728 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009729ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 int r = RedrawingDisabled;
9732 int p = p_lz;
9733
9734 RedrawingDisabled = 0;
9735 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009736 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009738 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739#ifdef FEAT_TITLE
9740 if (need_maketitle)
9741 maketitle();
9742#endif
9743 RedrawingDisabled = r;
9744 p_lz = p;
9745
9746 /* Reset msg_didout, so that a message that's there is overwritten. */
9747 msg_didout = FALSE;
9748 msg_col = 0;
9749
Bram Moolenaar56667a52013-07-17 11:54:28 +02009750 /* No need to wait after an intentional redraw. */
9751 need_wait_return = FALSE;
9752
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 out_flush();
9754}
9755
9756/*
9757 * ":redrawstatus": force redraw of status line(s)
9758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009760ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 int r = RedrawingDisabled;
9763 int p = p_lz;
9764
9765 RedrawingDisabled = 0;
9766 p_lz = FALSE;
9767 if (eap->forceit)
9768 status_redraw_all();
9769 else
9770 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009771 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 RedrawingDisabled = r;
9773 p_lz = p;
9774 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775}
9776
9777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009778close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780 if (redir_fd != NULL)
9781 {
9782 fclose(redir_fd);
9783 redir_fd = NULL;
9784 }
9785#ifdef FEAT_EVAL
9786 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009787 if (redir_vname)
9788 {
9789 var_redir_stop();
9790 redir_vname = 0;
9791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792#endif
9793}
9794
9795#if defined(FEAT_SESSION) && defined(USE_CRNL)
9796# define MKSESSION_NL
9797static int mksession_nl = FALSE; /* use NL only in put_eol() */
9798#endif
9799
9800/*
9801 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9802 */
9803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009804ex_mkrc(
9805 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 FILE *fd;
9808 int failed = FALSE;
9809 char_u *fname;
9810#ifdef FEAT_BROWSE
9811 char_u *browseFile = NULL;
9812#endif
9813#ifdef FEAT_SESSION
9814 int view_session = FALSE;
9815 int using_vdir = FALSE; /* using 'viewdir'? */
9816 char_u *viewFile = NULL;
9817 unsigned *flagp;
9818#endif
9819
9820 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9821 {
9822#ifdef FEAT_SESSION
9823 view_session = TRUE;
9824#else
9825 ex_ni(eap);
9826 return;
9827#endif
9828 }
9829
9830#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009831 /* Use the short file name until ":lcd" is used. We also don't use the
9832 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009833 did_lcd = FALSE;
9834
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9836 if (eap->cmdidx == CMD_mkview
9837 && (*eap->arg == NUL
9838 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9839 {
9840 eap->forceit = TRUE;
9841 fname = get_view_file(*eap->arg);
9842 if (fname == NULL)
9843 return;
9844 viewFile = fname;
9845 using_vdir = TRUE;
9846 }
9847 else
9848#endif
9849 if (*eap->arg != NUL)
9850 fname = eap->arg;
9851 else if (eap->cmdidx == CMD_mkvimrc)
9852 fname = (char_u *)VIMRC_FILE;
9853#ifdef FEAT_SESSION
9854 else if (eap->cmdidx == CMD_mksession)
9855 fname = (char_u *)SESSION_FILE;
9856#endif
9857 else
9858 fname = (char_u *)EXRC_FILE;
9859
9860#ifdef FEAT_BROWSE
9861 if (cmdmod.browse)
9862 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009863 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864# ifdef FEAT_SESSION
9865 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9866 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9867# endif
9868 (char_u *)_("Save Setup"),
9869 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9870 if (browseFile == NULL)
9871 goto theend;
9872 fname = browseFile;
9873 eap->forceit = TRUE; /* since dialog already asked */
9874 }
9875#endif
9876
9877#if defined(FEAT_SESSION) && defined(vim_mkdir)
9878 /* When using 'viewdir' may have to create the directory. */
9879 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009880 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881#endif
9882
9883 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9884 if (fd != NULL)
9885 {
9886#ifdef FEAT_SESSION
9887 if (eap->cmdidx == CMD_mkview)
9888 flagp = &vop_flags;
9889 else
9890 flagp = &ssop_flags;
9891#endif
9892
9893#ifdef MKSESSION_NL
9894 /* "unix" in 'sessionoptions': use NL line separator */
9895 if (view_session && (*flagp & SSOP_UNIX))
9896 mksession_nl = TRUE;
9897#endif
9898
9899 /* Write the version command for :mkvimrc */
9900 if (eap->cmdidx == CMD_mkvimrc)
9901 (void)put_line(fd, "version 6.0");
9902
9903#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009904 if (eap->cmdidx == CMD_mksession)
9905 {
9906 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9907 failed = TRUE;
9908 }
9909
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 if (eap->cmdidx != CMD_mkview)
9911#endif
9912 {
9913 /* Write setting 'compatible' first, because it has side effects.
9914 * For that same reason only do it when needed. */
9915 if (p_cp)
9916 (void)put_line(fd, "if !&cp | set cp | endif");
9917 else
9918 (void)put_line(fd, "if &cp | set nocp | endif");
9919 }
9920
9921#ifdef FEAT_SESSION
9922 if (!view_session
9923 || (eap->cmdidx == CMD_mksession
9924 && (*flagp & SSOP_OPTIONS)))
9925#endif
9926 failed |= (makemap(fd, NULL) == FAIL
9927 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9928
9929#ifdef FEAT_SESSION
9930 if (!failed && view_session)
9931 {
9932 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9933 failed = TRUE;
9934 if (eap->cmdidx == CMD_mksession)
9935 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009936 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937
Bram Moolenaard9462e32011-04-11 21:35:11 +02009938 dirnow = alloc(MAXPATHL);
9939 if (dirnow == NULL)
9940 failed = TRUE;
9941 else
9942 {
9943 /*
9944 * Change to session file's dir.
9945 */
9946 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009948 *dirnow = NUL;
9949 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9950 {
9951 if (vim_chdirfile(fname) == OK)
9952 shorten_fnames(TRUE);
9953 }
9954 else if (*dirnow != NUL
9955 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9956 {
9957 if (mch_chdir((char *)globaldir) == 0)
9958 shorten_fnames(TRUE);
9959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960
Bram Moolenaard9462e32011-04-11 21:35:11 +02009961 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962
Bram Moolenaard9462e32011-04-11 21:35:11 +02009963 /* restore original dir */
9964 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009966 {
9967 if (mch_chdir((char *)dirnow) != 0)
9968 EMSG(_(e_prev_dir));
9969 shorten_fnames(TRUE);
9970 }
9971 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 }
9973 }
9974 else
9975 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009976 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9977 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 }
9979 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9980 == FAIL)
9981 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009982 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9983 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009984 if (eap->cmdidx == CMD_mksession)
9985 {
9986 if (put_line(fd, "unlet SessionLoad") == FAIL)
9987 failed = TRUE;
9988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 }
9990#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009991 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9992 failed = TRUE;
9993
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 failed |= fclose(fd);
9995
9996 if (failed)
9997 EMSG(_(e_write));
9998#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9999 else if (eap->cmdidx == CMD_mksession)
10000 {
10001 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010002 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003
Bram Moolenaard9462e32011-04-11 21:35:11 +020010004 tbuf = alloc(MAXPATHL);
10005 if (tbuf != NULL)
10006 {
10007 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10008 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10009 vim_free(tbuf);
10010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 }
10012#endif
10013#ifdef MKSESSION_NL
10014 mksession_nl = FALSE;
10015#endif
10016 }
10017
10018#ifdef FEAT_BROWSE
10019theend:
10020 vim_free(browseFile);
10021#endif
10022#ifdef FEAT_SESSION
10023 vim_free(viewFile);
10024#endif
10025}
10026
Bram Moolenaardf177f62005-02-22 08:39:57 +000010027#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10028 || defined(PROTO)
10029 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010030vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010031{
10032 if (vim_mkdir(name, prot) != 0)
10033 {
10034 EMSG2(_("E739: Cannot create directory: %s"), name);
10035 return FAIL;
10036 }
10037 return OK;
10038}
10039#endif
10040
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041/*
10042 * Open a file for writing for an Ex command, with some checks.
10043 * Return file descriptor, or NULL on failure.
10044 */
10045 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010046open_exfile(
10047 char_u *fname,
10048 int forceit,
10049 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050{
10051 FILE *fd;
10052
10053#ifdef UNIX
10054 /* with Unix it is possible to open a directory */
10055 if (mch_isdir(fname))
10056 {
10057 EMSG2(_(e_isadir2), fname);
10058 return NULL;
10059 }
10060#endif
10061 if (!forceit && *mode != 'a' && vim_fexists(fname))
10062 {
10063 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10064 return NULL;
10065 }
10066
10067 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10068 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10069
10070 return fd;
10071}
10072
10073/*
10074 * ":mark" and ":k".
10075 */
10076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010077ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
10079 pos_T pos;
10080
10081 if (*eap->arg == NUL) /* No argument? */
10082 EMSG(_(e_argreq));
10083 else if (eap->arg[1] != NUL) /* more than one character? */
10084 EMSG(_(e_trailing));
10085 else
10086 {
10087 pos = curwin->w_cursor; /* save curwin->w_cursor */
10088 curwin->w_cursor.lnum = eap->line2;
10089 beginline(BL_WHITE | BL_FIX);
10090 if (setmark(*eap->arg) == FAIL) /* set mark */
10091 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10092 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10093 }
10094}
10095
10096/*
10097 * Update w_topline, w_leftcol and the cursor position.
10098 */
10099 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010100update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 check_cursor(); /* put cursor on valid line */
10103 update_topline();
10104 if (!curwin->w_p_wrap)
10105 validate_cursor();
10106 update_curswant();
10107}
10108
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109/*
10110 * ":normal[!] {commands}": Execute normal mode commands.
10111 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010113ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 int save_msg_scroll = msg_scroll;
10116 int save_restart_edit = restart_edit;
10117 int save_msg_didout = msg_didout;
10118 int save_State = State;
10119 tasave_T tabuf;
10120 int save_insertmode = p_im;
10121 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010122 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123#ifdef FEAT_MBYTE
10124 char_u *arg = NULL;
10125 int l;
10126 char_u *p;
10127#endif
10128
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010129 if (ex_normal_lock > 0)
10130 {
10131 EMSG(_(e_secure));
10132 return;
10133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 if (ex_normal_busy >= p_mmd)
10135 {
10136 EMSG(_("E192: Recursive use of :normal too deep"));
10137 return;
10138 }
10139 ++ex_normal_busy;
10140
10141 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10142 restart_edit = 0; /* don't go to Insert mode */
10143 p_im = FALSE; /* don't use 'insertmode' */
10144
10145#ifdef FEAT_MBYTE
10146 /*
10147 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10148 * this for the K_SPECIAL leading byte, otherwise special keys will not
10149 * work.
10150 */
10151 if (has_mbyte)
10152 {
10153 int len = 0;
10154
10155 /* Count the number of characters to be escaped. */
10156 for (p = eap->arg; *p != NUL; ++p)
10157 {
10158# ifdef FEAT_GUI
10159 if (*p == CSI) /* leadbyte CSI */
10160 len += 2;
10161# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010162 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10164# ifdef FEAT_GUI
10165 || *p == CSI
10166# endif
10167 )
10168 len += 2;
10169 }
10170 if (len > 0)
10171 {
10172 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10173 if (arg != NULL)
10174 {
10175 len = 0;
10176 for (p = eap->arg; *p != NUL; ++p)
10177 {
10178 arg[len++] = *p;
10179# ifdef FEAT_GUI
10180 if (*p == CSI)
10181 {
10182 arg[len++] = KS_EXTRA;
10183 arg[len++] = (int)KE_CSI;
10184 }
10185# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010186 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 {
10188 arg[len++] = *++p;
10189 if (*p == K_SPECIAL)
10190 {
10191 arg[len++] = KS_SPECIAL;
10192 arg[len++] = KE_FILLER;
10193 }
10194# ifdef FEAT_GUI
10195 else if (*p == CSI)
10196 {
10197 arg[len++] = KS_EXTRA;
10198 arg[len++] = (int)KE_CSI;
10199 }
10200# endif
10201 }
10202 arg[len] = NUL;
10203 }
10204 }
10205 }
10206 }
10207#endif
10208
10209 /*
10210 * Save the current typeahead. This is required to allow using ":normal"
10211 * from an event handler and makes sure we don't hang when the argument
10212 * ends with half a command.
10213 */
10214 save_typeahead(&tabuf);
10215 if (tabuf.typebuf_valid)
10216 {
10217 /*
10218 * Repeat the :normal command for each line in the range. When no
10219 * range given, execute it just once, without positioning the cursor
10220 * first.
10221 */
10222 do
10223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 if (eap->addr_count != 0)
10225 {
10226 curwin->w_cursor.lnum = eap->line1++;
10227 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010228 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 }
10230
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010231 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232#ifdef FEAT_MBYTE
10233 arg != NULL ? arg :
10234#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010235 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 }
10237 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10238 }
10239
10240 /* Might not return to the main loop when in an event handler. */
10241 update_topline_cursor();
10242
10243 /* Restore the previous typeahead. */
10244 restore_typeahead(&tabuf);
10245
10246 --ex_normal_busy;
10247 msg_scroll = save_msg_scroll;
10248 restart_edit = save_restart_edit;
10249 p_im = save_insertmode;
10250 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010251 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10253
10254 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010255 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010257#ifdef FEAT_MOUSE
10258 setmouse();
10259#endif
10260#ifdef CURSOR_SHAPE
10261 ui_cursor_shape(); /* may show different cursor shape */
10262#endif
10263
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#ifdef FEAT_MBYTE
10265 vim_free(arg);
10266#endif
10267}
10268
10269/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010270 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 */
10272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010273ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010275 if (eap->forceit)
10276 {
10277 coladvance((colnr_T)MAXCOL);
10278 curwin->w_curswant = MAXCOL;
10279 curwin->w_set_curswant = FALSE;
10280 }
10281
Bram Moolenaara40c5002005-01-09 21:16:21 +000010282 /* Ignore the command when already in Insert mode. Inserting an
10283 * expression register that invokes a function can do this. */
10284 if (State & INSERT)
10285 return;
10286
Bram Moolenaar64969662005-12-14 21:59:55 +000010287 if (eap->cmdidx == CMD_startinsert)
10288 restart_edit = 'a';
10289 else if (eap->cmdidx == CMD_startreplace)
10290 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010292 restart_edit = 'V';
10293
10294 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010296 if (eap->cmdidx == CMD_startinsert)
10297 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 curwin->w_curswant = 0; /* avoid MAXCOL */
10299 }
10300}
10301
10302/*
10303 * ":stopinsert"
10304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010306ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307{
10308 restart_edit = 0;
10309 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010310 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010313/*
10314 * Execute normal mode command "cmd".
10315 * "remap" can be REMAP_NONE or REMAP_YES.
10316 */
10317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010318exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010319{
Bram Moolenaar25281632016-01-21 23:32:32 +010010320 /* Stuff the argument into the typeahead buffer. */
10321 ins_typebuf(cmd, remap, 0, TRUE, silent);
10322 exec_normal(FALSE);
10323}
Bram Moolenaar25281632016-01-21 23:32:32 +010010324
Bram Moolenaar25281632016-01-21 23:32:32 +010010325/*
10326 * Execute normal_cmd() until there is no typeahead left.
10327 */
10328 void
10329exec_normal(int was_typed)
10330{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010331 oparg_T oa;
10332
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010333 clear_oparg(&oa);
10334 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010335 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10336 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010337 {
10338 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010339 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010340 }
10341}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010342
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343#ifdef FEAT_FIND_ID
10344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010345ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346{
10347 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10348 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10349 (linenr_T)1, (linenr_T)MAXLNUM);
10350}
10351
Bram Moolenaar4033c552017-09-16 20:54:51 +020010352#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353/*
10354 * ":psearch"
10355 */
10356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010357ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
10359 g_do_tagpreview = p_pvh;
10360 ex_findpat(eap);
10361 g_do_tagpreview = 0;
10362}
10363#endif
10364
10365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010366ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367{
10368 int whole = TRUE;
10369 long n;
10370 char_u *p;
10371 int action;
10372
10373 switch (cmdnames[eap->cmdidx].cmd_name[2])
10374 {
10375 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10376 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10377 action = ACTION_GOTO;
10378 else
10379 action = ACTION_SHOW;
10380 break;
10381 case 'i': /* ":ilist" and ":dlist" */
10382 action = ACTION_SHOW_ALL;
10383 break;
10384 case 'u': /* ":ijump" and ":djump" */
10385 action = ACTION_GOTO;
10386 break;
10387 default: /* ":isplit" and ":dsplit" */
10388 action = ACTION_SPLIT;
10389 break;
10390 }
10391
10392 n = 1;
10393 if (vim_isdigit(*eap->arg)) /* get count */
10394 {
10395 n = getdigits(&eap->arg);
10396 eap->arg = skipwhite(eap->arg);
10397 }
10398 if (*eap->arg == '/') /* Match regexp, not just whole words */
10399 {
10400 whole = FALSE;
10401 ++eap->arg;
10402 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10403 if (*p)
10404 {
10405 *p++ = NUL;
10406 p = skipwhite(p);
10407
10408 /* Check for trailing illegal characters */
10409 if (!ends_excmd(*p))
10410 eap->errmsg = e_trailing;
10411 else
10412 eap->nextcmd = check_nextcmd(p);
10413 }
10414 }
10415 if (!eap->skip)
10416 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10417 whole, !eap->forceit,
10418 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10419 n, action, eap->line1, eap->line2);
10420}
10421#endif
10422
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423
Bram Moolenaar4033c552017-09-16 20:54:51 +020010424#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425/*
10426 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10427 */
10428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010429ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010431 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10433}
10434
10435/*
10436 * ":pedit"
10437 */
10438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010439ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 win_T *curwin_save = curwin;
10442
10443 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010444 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010445 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 do_exedit(eap, NULL);
10447 keep_help_flag = FALSE;
10448 if (curwin != curwin_save && win_valid(curwin_save))
10449 {
10450 /* Return cursor to where we were */
10451 validate_cursor();
10452 redraw_later(VALID);
10453 win_enter(curwin_save, TRUE);
10454 }
10455 g_do_tagpreview = 0;
10456}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458
10459/*
10460 * ":stag", ":stselect" and ":stjump".
10461 */
10462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010463ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464{
10465 postponed_split = -1;
10466 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010467 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10469 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010470 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472
10473/*
10474 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10475 */
10476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010477ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10480}
10481
10482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010483ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
10485 int cmd;
10486
10487 switch (name[1])
10488 {
10489 case 'j': cmd = DT_JUMP; /* ":tjump" */
10490 break;
10491 case 's': cmd = DT_SELECT; /* ":tselect" */
10492 break;
10493 case 'p': cmd = DT_PREV; /* ":tprevious" */
10494 break;
10495 case 'N': cmd = DT_PREV; /* ":tNext" */
10496 break;
10497 case 'n': cmd = DT_NEXT; /* ":tnext" */
10498 break;
10499 case 'o': cmd = DT_POP; /* ":pop" */
10500 break;
10501 case 'f': /* ":tfirst" */
10502 case 'r': cmd = DT_FIRST; /* ":trewind" */
10503 break;
10504 case 'l': cmd = DT_LAST; /* ":tlast" */
10505 break;
10506 default: /* ":tag" */
10507#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010508 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010510 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 return;
10512 }
10513#endif
10514 cmd = DT_TAG;
10515 break;
10516 }
10517
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010518 if (name[0] == 'l')
10519 {
10520#ifndef FEAT_QUICKFIX
10521 ex_ni(eap);
10522 return;
10523#else
10524 cmd = DT_LTAG;
10525#endif
10526 }
10527
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10529 eap->forceit, TRUE);
10530}
10531
10532/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010533 * Check "str" for starting with a special cmdline variable.
10534 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10535 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10536 */
10537 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010538find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010539{
10540 int len;
10541 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010542 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010543 "%",
10544#define SPEC_PERC 0
10545 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010546#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010547 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010548#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010549 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010550#define SPEC_CCWORD (SPEC_CWORD + 1)
10551 "<cexpr>", /* expr under cursor */
10552#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010553 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010554#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010555 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010556#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010557 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010558#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010559#ifdef FEAT_AUTOCMD
10560 "<afile>", /* autocommand file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010561# define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010562 "<abuf>", /* autocommand buffer number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010563# define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010564 "<amatch>", /* autocommand match name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010565# define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010566#endif
10567#ifdef FEAT_CLIENTSERVER
10568 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010569# ifdef FEAT_AUTOCMD
Bram Moolenaar65f08472017-09-10 18:16:20 +020010570# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010571# else
Bram Moolenaar65f08472017-09-10 18:16:20 +020010572# define SPEC_CLIENT (SPEC_SLNUM + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010573# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010574#endif
10575 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010576
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010577 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010578 {
10579 len = (int)STRLEN(spec_str[i]);
10580 if (STRNCMP(src, spec_str[i], len) == 0)
10581 {
10582 *usedlen = len;
10583 return i;
10584 }
10585 }
10586 return -1;
10587}
10588
10589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 * Evaluate cmdline variables.
10591 *
10592 * change '%' to curbuf->b_ffname
10593 * '#' to curwin->w_altfile
10594 * '<cword>' to word under the cursor
10595 * '<cWORD>' to WORD under the cursor
10596 * '<cfile>' to path name under the cursor
10597 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010598 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 * '<afile>' to file name for autocommand
10600 * '<abuf>' to buffer number for autocommand
10601 * '<amatch>' to matching name for autocommand
10602 *
10603 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10604 * "" for error without a message) and NULL is returned.
10605 * Returns an allocated string if a valid match was found.
10606 * Returns NULL if no match was found. "usedlen" then still contains the
10607 * number of characters to skip.
10608 */
10609 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010610eval_vars(
10611 char_u *src, /* pointer into commandline */
10612 char_u *srcstart, /* beginning of valid memory for src */
10613 int *usedlen, /* characters after src that are used */
10614 linenr_T *lnump, /* line number for :e command, or NULL */
10615 char_u **errormsg, /* pointer to error message */
10616 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010617 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618{
10619 int i;
10620 char_u *s;
10621 char_u *result;
10622 char_u *resultbuf = NULL;
10623 int resultlen;
10624 buf_T *buf;
10625 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10626 int spec_idx;
10627#ifdef FEAT_MODIFY_FNAME
10628 int skip_mod = FALSE;
10629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631
10632 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010633 if (escaped != NULL)
10634 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635
10636 /*
10637 * Check if there is something to do.
10638 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010639 spec_idx = find_cmdline_var(src, usedlen);
10640 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 {
10642 *usedlen = 1;
10643 return NULL;
10644 }
10645
10646 /*
10647 * Skip when preceded with a backslash "\%" and "\#".
10648 * Note: In "\\%" the % is also not recognized!
10649 */
10650 if (src > srcstart && src[-1] == '\\')
10651 {
10652 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010653 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 return NULL;
10655 }
10656
10657 /*
10658 * word or WORD under cursor
10659 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010660 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10661 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010663 resultlen = find_ident_under_cursor(&result,
10664 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10665 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10666 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 if (resultlen == 0)
10668 {
10669 *errormsg = (char_u *)"";
10670 return NULL;
10671 }
10672 }
10673
10674 /*
10675 * '#': Alternate file name
10676 * '%': Current file name
10677 * File name under the cursor
10678 * File name for autocommand
10679 * and following modifiers
10680 */
10681 else
10682 {
10683 switch (spec_idx)
10684 {
10685 case SPEC_PERC: /* '%': current file */
10686 if (curbuf->b_fname == NULL)
10687 {
10688 result = (char_u *)"";
10689 valid = 0; /* Must have ":p:h" to be valid */
10690 }
10691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 break;
10694
10695 case SPEC_HASH: /* '#' or "#99": alternate file */
10696 if (src[1] == '#') /* "##": the argument list */
10697 {
10698 result = arg_all();
10699 resultbuf = result;
10700 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010701 if (escaped != NULL)
10702 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703#ifdef FEAT_MODIFY_FNAME
10704 skip_mod = TRUE;
10705#endif
10706 break;
10707 }
10708 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010709 if (*s == '<') /* "#<99" uses v:oldfiles */
10710 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 i = (int)getdigits(&s);
10712 *usedlen = (int)(s - src); /* length of what we expand */
10713
Bram Moolenaard812df62008-11-09 12:46:09 +000010714 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010716 if (*usedlen < 2)
10717 {
10718 /* Should we give an error message for #<text? */
10719 *usedlen = 1;
10720 return NULL;
10721 }
10722#ifdef FEAT_EVAL
10723 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10724 (long)i);
10725 if (result == NULL)
10726 {
10727 *errormsg = (char_u *)"";
10728 return NULL;
10729 }
10730#else
10731 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 }
10735 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010736 {
10737 buf = buflist_findnr(i);
10738 if (buf == NULL)
10739 {
10740 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10741 return NULL;
10742 }
10743 if (lnump != NULL)
10744 *lnump = ECMD_LAST;
10745 if (buf->b_fname == NULL)
10746 {
10747 result = (char_u *)"";
10748 valid = 0; /* Must have ":p:h" to be valid */
10749 }
10750 else
10751 result = buf->b_fname;
10752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 break;
10754
10755#ifdef FEAT_SEARCHPATH
10756 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010757 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 if (result == NULL)
10759 {
10760 *errormsg = (char_u *)"";
10761 return NULL;
10762 }
10763 resultbuf = result; /* remember allocated string */
10764 break;
10765#endif
10766
10767#ifdef FEAT_AUTOCMD
10768 case SPEC_AFILE: /* file name for autocommand */
10769 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010770 if (result != NULL && !autocmd_fname_full)
10771 {
10772 /* Still need to turn the fname into a full path. It is
10773 * postponed to avoid a delay when <afile> is not used. */
10774 autocmd_fname_full = TRUE;
10775 result = FullName_save(autocmd_fname, FALSE);
10776 vim_free(autocmd_fname);
10777 autocmd_fname = result;
10778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 if (result == NULL)
10780 {
10781 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10782 return NULL;
10783 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010784 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 break;
10786
10787 case SPEC_ABUF: /* buffer number for autocommand */
10788 if (autocmd_bufnr <= 0)
10789 {
10790 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10791 return NULL;
10792 }
10793 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10794 result = strbuf;
10795 break;
10796
10797 case SPEC_AMATCH: /* match name for autocommand */
10798 result = autocmd_match;
10799 if (result == NULL)
10800 {
10801 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10802 return NULL;
10803 }
10804 break;
10805
10806#endif
10807 case SPEC_SFILE: /* file name for ":so" command */
10808 result = sourcing_name;
10809 if (result == NULL)
10810 {
10811 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10812 return NULL;
10813 }
10814 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010815 case SPEC_SLNUM: /* line in file for ":so" command */
10816 if (sourcing_name == NULL || sourcing_lnum == 0)
10817 {
10818 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10819 return NULL;
10820 }
10821 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10822 result = strbuf;
10823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824#if defined(FEAT_CLIENTSERVER)
10825 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010826 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10827 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 result = strbuf;
10829 break;
10830#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010831 default:
10832 result = (char_u *)""; /* avoid gcc warning */
10833 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 }
10835
10836 resultlen = (int)STRLEN(result); /* length of new string */
10837 if (src[*usedlen] == '<') /* remove the file name extension */
10838 {
10839 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 resultlen = (int)(s - result);
10842 }
10843#ifdef FEAT_MODIFY_FNAME
10844 else if (!skip_mod)
10845 {
10846 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10847 &resultlen);
10848 if (result == NULL)
10849 {
10850 *errormsg = (char_u *)"";
10851 return NULL;
10852 }
10853 }
10854#endif
10855 }
10856
10857 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10858 {
10859 if (valid != VALID_HEAD + VALID_PATH)
10860 /* xgettext:no-c-format */
10861 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10862 else
10863 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10864 result = NULL;
10865 }
10866 else
10867 result = vim_strnsave(result, resultlen);
10868 vim_free(resultbuf);
10869 return result;
10870}
10871
10872/*
10873 * Concatenate all files in the argument list, separated by spaces, and return
10874 * it in one allocated string.
10875 * Spaces and backslashes in the file names are escaped with a backslash.
10876 * Returns NULL when out of memory.
10877 */
10878 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010879arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880{
10881 int len;
10882 int idx;
10883 char_u *retval = NULL;
10884 char_u *p;
10885
10886 /*
10887 * Do this loop two times:
10888 * first time: compute the total length
10889 * second time: concatenate the names
10890 */
10891 for (;;)
10892 {
10893 len = 0;
10894 for (idx = 0; idx < ARGCOUNT; ++idx)
10895 {
10896 p = alist_name(&ARGLIST[idx]);
10897 if (p != NULL)
10898 {
10899 if (len > 0)
10900 {
10901 /* insert a space in between names */
10902 if (retval != NULL)
10903 retval[len] = ' ';
10904 ++len;
10905 }
10906 for ( ; *p != NUL; ++p)
10907 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010908 if (*p == ' '
10909#ifndef BACKSLASH_IN_FILENAME
10910 || *p == '\\'
10911#endif
10912 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 {
10914 /* insert a backslash */
10915 if (retval != NULL)
10916 retval[len] = '\\';
10917 ++len;
10918 }
10919 if (retval != NULL)
10920 retval[len] = *p;
10921 ++len;
10922 }
10923 }
10924 }
10925
10926 /* second time: break here */
10927 if (retval != NULL)
10928 {
10929 retval[len] = NUL;
10930 break;
10931 }
10932
10933 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010934 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 if (retval == NULL)
10936 break;
10937 }
10938
10939 return retval;
10940}
10941
10942#if defined(FEAT_AUTOCMD) || defined(PROTO)
10943/*
10944 * Expand the <sfile> string in "arg".
10945 *
10946 * Returns an allocated string, or NULL for any error.
10947 */
10948 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010949expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950{
10951 char_u *errormsg;
10952 int len;
10953 char_u *result;
10954 char_u *newres;
10955 char_u *repl;
10956 int srclen;
10957 char_u *p;
10958
10959 result = vim_strsave(arg);
10960 if (result == NULL)
10961 return NULL;
10962
10963 for (p = result; *p; )
10964 {
10965 if (STRNCMP(p, "<sfile>", 7) != 0)
10966 ++p;
10967 else
10968 {
10969 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010970 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 if (errormsg != NULL)
10972 {
10973 if (*errormsg)
10974 emsg(errormsg);
10975 vim_free(result);
10976 return NULL;
10977 }
10978 if (repl == NULL) /* no match (cannot happen) */
10979 {
10980 p += srclen;
10981 continue;
10982 }
10983 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10984 newres = alloc(len);
10985 if (newres == NULL)
10986 {
10987 vim_free(repl);
10988 vim_free(result);
10989 return NULL;
10990 }
10991 mch_memmove(newres, result, (size_t)(p - result));
10992 STRCPY(newres + (p - result), repl);
10993 len = (int)STRLEN(newres);
10994 STRCAT(newres, p + srclen);
10995 vim_free(repl);
10996 vim_free(result);
10997 result = newres;
10998 p = newres + len; /* continue after the match */
10999 }
11000 }
11001
11002 return result;
11003}
11004#endif
11005
11006#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011007static int ses_winsizes(FILE *fd, int restore_size,
11008 win_T *tab_firstwin);
11009static int ses_win_rec(FILE *fd, frame_T *fr);
11010static frame_T *ses_skipframe(frame_T *fr);
11011static int ses_do_frame(frame_T *fr);
11012static int ses_do_win(win_T *wp);
11013static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11014static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011015static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016
11017/*
11018 * Write openfile commands for the current buffers to an .exrc file.
11019 * Return FAIL on error, OK otherwise.
11020 */
11021 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011022makeopens(
11023 FILE *fd,
11024 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025{
11026 buf_T *buf;
11027 int only_save_windows = TRUE;
11028 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 int restore_size = TRUE;
11030 win_T *wp;
11031 char_u *sname;
11032 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011033 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011034 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011035 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011036 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011037 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011038 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039
11040 if (ssop_flags & SSOP_BUFFERS)
11041 only_save_windows = FALSE; /* Save ALL buffers */
11042
11043 /*
11044 * Begin by setting the this_session variable, and then other
11045 * sessionable variables.
11046 */
11047#ifdef FEAT_EVAL
11048 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11049 return FAIL;
11050 if (ssop_flags & SSOP_GLOBALS)
11051 if (store_session_globals(fd) == FAIL)
11052 return FAIL;
11053#endif
11054
11055 /*
11056 * Close all windows but one.
11057 */
11058 if (put_line(fd, "silent only") == FAIL)
11059 return FAIL;
11060
11061 /*
11062 * Now a :cd command to the session directory or the current directory
11063 */
11064 if (ssop_flags & SSOP_SESDIR)
11065 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011066 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11067 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 return FAIL;
11069 }
11070 else if (ssop_flags & SSOP_CURDIR)
11071 {
11072 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11073 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011074 || fputs("cd ", fd) < 0
11075 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11076 || put_eol(fd) == FAIL)
11077 {
11078 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 vim_free(sname);
11082 }
11083
11084 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011085 * If there is an empty, unnamed buffer we will wipe it out later.
11086 * Remember the buffer number.
11087 */
11088 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11089 return FAIL;
11090 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11091 return FAIL;
11092 if (put_line(fd, "endif") == FAIL)
11093 return FAIL;
11094
11095 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 * Now save the current files, current buffer first.
11097 */
11098 if (put_line(fd, "set shortmess=aoO") == FAIL)
11099 return FAIL;
11100
11101 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011102 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 {
11104 if (!(only_save_windows && buf->b_nwindows == 0)
11105 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11106 && buf->b_fname != NULL
11107 && buf->b_p_bl)
11108 {
11109 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11110 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011111 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 return FAIL;
11113 }
11114 }
11115
11116 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011117 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11119 return FAIL;
11120
11121 if (ssop_flags & SSOP_RESIZE)
11122 {
11123 /* Note: after the restore we still check it worked!*/
11124 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11125 || put_eol(fd) == FAIL)
11126 return FAIL;
11127 }
11128
11129#ifdef FEAT_GUI
11130 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11131 {
11132 int x, y;
11133
11134 if (gui_mch_get_winpos(&x, &y) == OK)
11135 {
11136 /* Note: after the restore we still check it worked!*/
11137 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11138 return FAIL;
11139 }
11140 }
11141#endif
11142
11143 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011144 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11145 * will be displayed when creating the next tab. That resizes the windows
11146 * in the first tab, which may cause problems. Set 'showtabline' to 2
11147 * temporarily to avoid that.
11148 */
11149 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11150 {
11151 if (put_line(fd, "set stal=2") == FAIL)
11152 return FAIL;
11153 restore_stal = TRUE;
11154 }
11155
11156 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011157 * May repeat putting Windows for each tab, when "tabpages" is in
11158 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011159 * Don't use goto_tabpage(), it may change directory and trigger
11160 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011162 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011163 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011164 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011166 int need_tabnew = FALSE;
11167 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011168
Bram Moolenaar18144c82006-04-12 21:52:12 +000011169 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011171 tabpage_T *tp = find_tabpage(tabnr);
11172
11173 if (tp == NULL)
11174 break; /* done all tab pages */
11175 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011176 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011177 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011178 tab_topframe = topframe;
11179 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011180 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011181 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011182 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011183 tab_topframe = tp->tp_topframe;
11184 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011185 if (tabnr > 1)
11186 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188
Bram Moolenaar18144c82006-04-12 21:52:12 +000011189 /*
11190 * Before creating the window layout, try loading one file. If this
11191 * is aborted we don't end up with a number of useless windows.
11192 * This may have side effects! (e.g., compressed or network file).
11193 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011194 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011195 {
11196 if (ses_do_win(wp)
11197 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011198 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011199#ifdef FEAT_QUICKFIX
11200 && !bt_nofile(wp->w_buffer)
11201#endif
11202 )
11203 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011204 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011205 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11206 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011207 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011208 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011209 if (!wp->w_arg_idx_invalid)
11210 edited_win = wp;
11211 break;
11212 }
11213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011215 /* If no file got edited create an empty tab page. */
11216 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11217 return FAIL;
11218
Bram Moolenaar18144c82006-04-12 21:52:12 +000011219 /*
11220 * Save current window layout.
11221 */
11222 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011224 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011226 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11227 return FAIL;
11228 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11229 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230
Bram Moolenaar18144c82006-04-12 21:52:12 +000011231 /*
11232 * Check if window sizes can be restored (no windows omitted).
11233 * Remember the window number of the current window after restoring.
11234 */
11235 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011236 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011237 {
11238 if (ses_do_win(wp))
11239 ++nr;
11240 else
11241 restore_size = FALSE;
11242 if (curwin == wp)
11243 cnr = nr;
11244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245
Bram Moolenaar18144c82006-04-12 21:52:12 +000011246 /* Go to the first window. */
11247 if (put_line(fd, "wincmd t") == FAIL)
11248 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011249
Bram Moolenaar18144c82006-04-12 21:52:12 +000011250 /*
11251 * If more than one window, see if sizes can be restored.
11252 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11253 * resized when moving between windows.
11254 * Do this before restoring the view, so that the topline and the
11255 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011256 * winminheight and winminwidth need to be set to avoid an error if the
11257 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011259 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011260 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011261 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263
Bram Moolenaar18144c82006-04-12 21:52:12 +000011264 /*
11265 * Restore the view of the window (options, file, cursor, etc.).
11266 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011267 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011268 {
11269 if (!ses_do_win(wp))
11270 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011271 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11272 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011273 return FAIL;
11274 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11275 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011276 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011277 }
11278
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011279 /* The argument index in the first tab page is zero, need to set it in
11280 * each window. For further tab pages it's the window where we do
11281 * "tabedit". */
11282 cur_arg_idx = next_arg_idx;
11283
Bram Moolenaar18144c82006-04-12 21:52:12 +000011284 /*
11285 * Restore cursor to the current window if it's not the first one.
11286 */
11287 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11288 || put_eol(fd) == FAIL))
11289 return FAIL;
11290
11291 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011292 * Restore window sizes again after jumping around in windows, because
11293 * the current window has a minimum size while others may not.
11294 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011295 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011296 return FAIL;
11297
Bram Moolenaar18144c82006-04-12 21:52:12 +000011298 /* Don't continue in another tab page when doing only the current one
11299 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011300 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011301 break;
11302 }
11303
11304 if (ssop_flags & SSOP_TABPAGES)
11305 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011306 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11307 || put_eol(fd) == FAIL)
11308 return FAIL;
11309 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011310 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11311 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011312
Bram Moolenaar9c102382006-05-03 21:26:49 +000011313 /*
11314 * Wipe out an empty unnamed buffer we started in.
11315 */
11316 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11317 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011318 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011319 return FAIL;
11320 if (put_line(fd, "endif") == FAIL)
11321 return FAIL;
11322 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11323 return FAIL;
11324
11325 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11326 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11327 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11328 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011329 /* Re-apply 'winminheight' and 'winminwidth'. */
11330 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11331 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333
11334 /*
11335 * Lastly, execute the x.vim file if it exists.
11336 */
11337 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11338 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011339 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 || put_line(fd, "endif") == FAIL)
11341 return FAIL;
11342
11343 return OK;
11344}
11345
11346 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011347ses_winsizes(
11348 FILE *fd,
11349 int restore_size,
11350 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351{
11352 int n = 0;
11353 win_T *wp;
11354
11355 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11356 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011357 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 {
11359 if (!ses_do_win(wp))
11360 continue;
11361 ++n;
11362
11363 /* restore height when not full height */
11364 if (wp->w_height + wp->w_status_height < topframe->fr_height
11365 && (fprintf(fd,
11366 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11367 n, (long)wp->w_height, Rows / 2, Rows) < 0
11368 || put_eol(fd) == FAIL))
11369 return FAIL;
11370
11371 /* restore width when not full width */
11372 if (wp->w_width < Columns && (fprintf(fd,
11373 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11374 n, (long)wp->w_width, Columns / 2, Columns) < 0
11375 || put_eol(fd) == FAIL))
11376 return FAIL;
11377 }
11378 }
11379 else
11380 {
11381 /* Just equalise window sizes */
11382 if (put_line(fd, "wincmd =") == FAIL)
11383 return FAIL;
11384 }
11385 return OK;
11386}
11387
11388/*
11389 * Write commands to "fd" to recursively create windows for frame "fr",
11390 * horizontally and vertically split.
11391 * After the commands the last window in the frame is the current window.
11392 * Returns FAIL when writing the commands to "fd" fails.
11393 */
11394 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011395ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396{
11397 frame_T *frc;
11398 int count = 0;
11399
11400 if (fr->fr_layout != FR_LEAF)
11401 {
11402 /* Find first frame that's not skipped and then create a window for
11403 * each following one (first frame is already there). */
11404 frc = ses_skipframe(fr->fr_child);
11405 if (frc != NULL)
11406 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11407 {
11408 /* Make window as big as possible so that we have lots of room
11409 * to split. */
11410 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11411 || put_line(fd, fr->fr_layout == FR_COL
11412 ? "split" : "vsplit") == FAIL)
11413 return FAIL;
11414 ++count;
11415 }
11416
11417 /* Go back to the first window. */
11418 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11419 ? "%dwincmd k" : "%dwincmd h", count) < 0
11420 || put_eol(fd) == FAIL))
11421 return FAIL;
11422
11423 /* Recursively create frames/windows in each window of this column or
11424 * row. */
11425 frc = ses_skipframe(fr->fr_child);
11426 while (frc != NULL)
11427 {
11428 ses_win_rec(fd, frc);
11429 frc = ses_skipframe(frc->fr_next);
11430 /* Go to next window. */
11431 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11432 return FAIL;
11433 }
11434 }
11435 return OK;
11436}
11437
11438/*
11439 * Skip frames that don't contain windows we want to save in the Session.
11440 * Returns NULL when there none.
11441 */
11442 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011443ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444{
11445 frame_T *frc;
11446
11447 for (frc = fr; frc != NULL; frc = frc->fr_next)
11448 if (ses_do_frame(frc))
11449 break;
11450 return frc;
11451}
11452
11453/*
11454 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11455 * the Session.
11456 */
11457 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011458ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
11460 frame_T *frc;
11461
11462 if (fr->fr_layout == FR_LEAF)
11463 return ses_do_win(fr->fr_win);
11464 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11465 if (ses_do_frame(frc))
11466 return TRUE;
11467 return FALSE;
11468}
11469
11470/*
11471 * Return non-zero if window "wp" is to be stored in the Session.
11472 */
11473 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011474ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475{
11476 if (wp->w_buffer->b_fname == NULL
11477#ifdef FEAT_QUICKFIX
11478 /* When 'buftype' is "nofile" can't restore the window contents. */
11479 || bt_nofile(wp->w_buffer)
11480#endif
11481 )
11482 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011483 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 return (ssop_flags & SSOP_HELP);
11485 return TRUE;
11486}
11487
11488/*
11489 * Write commands to "fd" to restore the view of a window.
11490 * Caller must make sure 'scrolloff' is zero.
11491 */
11492 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011493put_view(
11494 FILE *fd,
11495 win_T *wp,
11496 int add_edit, /* add ":edit" command to view */
11497 unsigned *flagp, /* vop_flags or ssop_flags */
11498 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011499 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500{
11501 win_T *save_curwin;
11502 int f;
11503 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011504 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505
11506 /* Always restore cursor position for ":mksession". For ":mkview" only
11507 * when 'viewoptions' contains "cursor". */
11508 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11509
11510 /*
11511 * Local argument list.
11512 */
11513 if (wp->w_alist == &global_alist)
11514 {
11515 if (put_line(fd, "argglobal") == FAIL)
11516 return FAIL;
11517 }
11518 else
11519 {
11520 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11521 flagp == &vop_flags
11522 || !(*flagp & SSOP_CURDIR)
11523 || wp->w_localdir != NULL, flagp) == FAIL)
11524 return FAIL;
11525 }
11526
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011527 /* Only when part of a session: restore the argument index. Some
11528 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011529 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011530 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011532 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 || put_eol(fd) == FAIL)
11534 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011535 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 }
11537
11538 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011539 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 {
11541 /*
11542 * Load the file.
11543 */
11544 if (wp->w_buffer->b_ffname != NULL
11545#ifdef FEAT_QUICKFIX
11546 && !bt_nofile(wp->w_buffer)
11547#endif
11548 )
11549 {
11550 /*
11551 * Editing a file in this buffer: use ":edit file".
11552 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011553 *
11554 * Note, if a buffer for that file already exists, use :badd to
11555 * edit that buffer, to not lose folding information (:edit resets
11556 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011558 if (fputs("if bufexists('", fd) < 0
11559 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11560 || fputs("') | buffer ", fd) < 0
11561 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11562 || fputs(" | else | edit ", fd) < 0
11563 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11564 || fputs(" | endif", fd) < 0
11565 ||
11566 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 return FAIL;
11568 }
11569 else
11570 {
11571 /* No file in this buffer, just make it empty. */
11572 if (put_line(fd, "enew") == FAIL)
11573 return FAIL;
11574#ifdef FEAT_QUICKFIX
11575 if (wp->w_buffer->b_ffname != NULL)
11576 {
11577 /* The buffer does have a name, but it's not a file name. */
11578 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011579 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 return FAIL;
11581 }
11582#endif
11583 do_cursor = FALSE;
11584 }
11585 }
11586
11587 /*
11588 * Local mappings and abbreviations.
11589 */
11590 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11591 && makemap(fd, wp->w_buffer) == FAIL)
11592 return FAIL;
11593
11594 /*
11595 * Local options. Need to go to the window temporarily.
11596 * Store only local values when using ":mkview" and when ":mksession" is
11597 * used and 'sessionoptions' doesn't include "options".
11598 * Some folding options are always stored when "folds" is included,
11599 * otherwise the folds would not be restored correctly.
11600 */
11601 save_curwin = curwin;
11602 curwin = wp;
11603 curbuf = curwin->w_buffer;
11604 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11605 f = makeset(fd, OPT_LOCAL,
11606 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11607#ifdef FEAT_FOLDING
11608 else if (*flagp & SSOP_FOLDS)
11609 f = makefoldset(fd);
11610#endif
11611 else
11612 f = OK;
11613 curwin = save_curwin;
11614 curbuf = curwin->w_buffer;
11615 if (f == FAIL)
11616 return FAIL;
11617
11618#ifdef FEAT_FOLDING
11619 /*
11620 * Save Folds when 'buftype' is empty and for help files.
11621 */
11622 if ((*flagp & SSOP_FOLDS)
11623 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011624 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 {
11626 if (put_folds(fd, wp) == FAIL)
11627 return FAIL;
11628 }
11629#endif
11630
11631 /*
11632 * Set the cursor after creating folds, since that moves the cursor.
11633 */
11634 if (do_cursor)
11635 {
11636
11637 /* Restore the cursor line in the file and relatively in the
11638 * window. Don't use "G", it changes the jumplist. */
11639 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11640 (long)wp->w_cursor.lnum,
11641 (long)(wp->w_cursor.lnum - wp->w_topline),
11642 (long)wp->w_height / 2, (long)wp->w_height) < 0
11643 || put_eol(fd) == FAIL
11644 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11645 || put_line(fd, "exe s:l") == FAIL
11646 || put_line(fd, "normal! zt") == FAIL
11647 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11648 || put_eol(fd) == FAIL)
11649 return FAIL;
11650 /* Restore the cursor column and left offset when not wrapping. */
11651 if (wp->w_cursor.col == 0)
11652 {
11653 if (put_line(fd, "normal! 0") == FAIL)
11654 return FAIL;
11655 }
11656 else
11657 {
11658 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11659 {
11660 if (fprintf(fd,
11661 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011662 (long)wp->w_virtcol + 1,
11663 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 (long)wp->w_width / 2, (long)wp->w_width) < 0
11665 || put_eol(fd) == FAIL
11666 || put_line(fd, "if s:c > 0") == FAIL
11667 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011668 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11669 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 || put_eol(fd) == FAIL
11671 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011672 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 || put_eol(fd) == FAIL
11674 || put_line(fd, "endif") == FAIL)
11675 return FAIL;
11676 }
11677 else
11678 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011679 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 || put_eol(fd) == FAIL)
11681 return FAIL;
11682 }
11683 }
11684 }
11685
11686 /*
11687 * Local directory.
11688 */
11689 if (wp->w_localdir != NULL)
11690 {
11691 if (fputs("lcd ", fd) < 0
11692 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11693 || put_eol(fd) == FAIL)
11694 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011695 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 }
11697
11698 return OK;
11699}
11700
11701/*
11702 * Write an argument list to the session file.
11703 * Returns FAIL if writing fails.
11704 */
11705 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011706ses_arglist(
11707 FILE *fd,
11708 char *cmd,
11709 garray_T *gap,
11710 int fullname, /* TRUE: use full path name */
11711 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712{
11713 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011714 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 char_u *s;
11716
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011717 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11718 return FAIL;
11719 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 return FAIL;
11721 for (i = 0; i < gap->ga_len; ++i)
11722 {
11723 /* NULL file names are skipped (only happens when out of memory). */
11724 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11725 if (s != NULL)
11726 {
11727 if (fullname)
11728 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011729 buf = alloc(MAXPATHL);
11730 if (buf != NULL)
11731 {
11732 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11733 s = buf;
11734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011736 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011737 || ses_put_fname(fd, s, flagp) == FAIL
11738 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011739 {
11740 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011742 }
11743 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 }
11745 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011746 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747}
11748
11749/*
11750 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011751 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 * Returns FAIL if writing fails.
11753 */
11754 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011755ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756{
11757 char_u *name;
11758
11759 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011760 * the session file will be sourced.
11761 * Don't do this for ":mkview", we don't know the current directory.
11762 * Don't do this after ":lcd", we don't keep track of what the current
11763 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 if (buf->b_sfname != NULL
11765 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011766 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011767#ifdef FEAT_AUTOCHDIR
11768 && !p_acd
11769#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011770 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 name = buf->b_sfname;
11772 else
11773 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011774 if (ses_put_fname(fd, name, flagp) == FAIL
11775 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 return FAIL;
11777 return OK;
11778}
11779
11780/*
11781 * Write a file name to the session file.
11782 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11783 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011784 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 */
11786 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011787ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788{
11789 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011790 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792
11793 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011794 if (sname == NULL)
11795 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011797 if (*flagp & SSOP_SLASH)
11798 {
11799 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011800 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011801 if (*p == '\\')
11802 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011804
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011805 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011806 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011808 if (p == NULL)
11809 return FAIL;
11810
11811 /* write the result */
11812 if (fputs((char *)p, fd) < 0)
11813 retval = FAIL;
11814
11815 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 return retval;
11817}
11818
11819/*
11820 * ":loadview [nr]"
11821 */
11822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011823ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824{
11825 char_u *fname;
11826
11827 fname = get_view_file(*eap->arg);
11828 if (fname != NULL)
11829 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011830 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 vim_free(fname);
11832 }
11833}
11834
11835/*
11836 * Get the name of the view file for the current buffer.
11837 */
11838 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011839get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840{
11841 int len = 0;
11842 char_u *p, *s;
11843 char_u *retval;
11844 char_u *sname;
11845
11846 if (curbuf->b_ffname == NULL)
11847 {
11848 EMSG(_(e_noname));
11849 return NULL;
11850 }
11851 sname = home_replace_save(NULL, curbuf->b_ffname);
11852 if (sname == NULL)
11853 return NULL;
11854
11855 /*
11856 * We want a file name without separators, because we're not going to make
11857 * a directory.
11858 * "normal" path separator -> "=+"
11859 * "=" -> "=="
11860 * ":" path separator -> "=-"
11861 */
11862 for (p = sname; *p; ++p)
11863 if (*p == '=' || vim_ispathsep(*p))
11864 ++len;
11865 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11866 if (retval != NULL)
11867 {
11868 STRCPY(retval, p_vdir);
11869 add_pathsep(retval);
11870 s = retval + STRLEN(retval);
11871 for (p = sname; *p; ++p)
11872 {
11873 if (*p == '=')
11874 {
11875 *s++ = '=';
11876 *s++ = '=';
11877 }
11878 else if (vim_ispathsep(*p))
11879 {
11880 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011881#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 if (*p == ':')
11883 *s++ = '-';
11884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011886 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 }
11888 else
11889 *s++ = *p;
11890 }
11891 *s++ = '=';
11892 *s++ = c;
11893 STRCPY(s, ".vim");
11894 }
11895
11896 vim_free(sname);
11897 return retval;
11898}
11899
11900#endif /* FEAT_SESSION */
11901
11902/*
11903 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11904 * Return FAIL for a write error.
11905 */
11906 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011907put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908{
11909 if (
11910#ifdef USE_CRNL
11911 (
11912# ifdef MKSESSION_NL
11913 !mksession_nl &&
11914# endif
11915 (putc('\r', fd) < 0)) ||
11916#endif
11917 (putc('\n', fd) < 0))
11918 return FAIL;
11919 return OK;
11920}
11921
11922/*
11923 * Write a line to "fd".
11924 * Return FAIL for a write error.
11925 */
11926 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011927put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928{
11929 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11930 return FAIL;
11931 return OK;
11932}
11933
11934#ifdef FEAT_VIMINFO
11935/*
11936 * ":rviminfo" and ":wviminfo".
11937 */
11938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011939ex_viminfo(
11940 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941{
11942 char_u *save_viminfo;
11943
11944 save_viminfo = p_viminfo;
11945 if (*p_viminfo == NUL)
11946 p_viminfo = (char_u *)"'100";
11947 if (eap->cmdidx == CMD_rviminfo)
11948 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011949 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11950 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 EMSG(_("E195: Cannot open viminfo file for reading"));
11952 }
11953 else
11954 write_viminfo(eap->arg, eap->forceit);
11955 p_viminfo = save_viminfo;
11956}
11957#endif
11958
11959#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011960/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011961 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011962 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011965dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 if (fname == NULL)
11968 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011969 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970}
11971#endif
11972
11973/*
11974 * ":behave {mswin,xterm}"
11975 */
11976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011977ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
11979 if (STRCMP(eap->arg, "mswin") == 0)
11980 {
11981 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11982 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11983 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11984 set_option_value((char_u *)"keymodel", 0L,
11985 (char_u *)"startsel,stopsel", 0);
11986 }
11987 else if (STRCMP(eap->arg, "xterm") == 0)
11988 {
11989 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11990 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11991 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11992 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11993 }
11994 else
11995 EMSG2(_(e_invarg2), eap->arg);
11996}
11997
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011998#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11999/*
12000 * Function given to ExpandGeneric() to obtain the possible arguments of the
12001 * ":behave {mswin,xterm}" command.
12002 */
12003 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012004get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012005{
12006 if (idx == 0)
12007 return (char_u *)"mswin";
12008 if (idx == 1)
12009 return (char_u *)"xterm";
12010 return NULL;
12011}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012012
12013/*
12014 * Function given to ExpandGeneric() to obtain the possible arguments of the
12015 * ":messages {clear}" command.
12016 */
12017 char_u *
12018get_messages_arg(expand_T *xp UNUSED, int idx)
12019{
12020 if (idx == 0)
12021 return (char_u *)"clear";
12022 return NULL;
12023}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012024#endif
12025
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012026 char_u *
12027get_mapclear_arg(expand_T *xp UNUSED, int idx)
12028{
12029 if (idx == 0)
12030 return (char_u *)"<buffer>";
12031 return NULL;
12032}
12033
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034#ifdef FEAT_AUTOCMD
12035static int filetype_detect = FALSE;
12036static int filetype_plugin = FALSE;
12037static int filetype_indent = FALSE;
12038
12039/*
12040 * ":filetype [plugin] [indent] {on,off,detect}"
12041 * on: Load the filetype.vim file to install autocommands for file types.
12042 * off: Load the ftoff.vim file to remove all autocommands for file types.
12043 * plugin on: load filetype.vim and ftplugin.vim
12044 * plugin off: load ftplugof.vim
12045 * indent on: load filetype.vim and indent.vim
12046 * indent off: load indoff.vim
12047 */
12048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012049ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050{
12051 char_u *arg = eap->arg;
12052 int plugin = FALSE;
12053 int indent = FALSE;
12054
12055 if (*eap->arg == NUL)
12056 {
12057 /* Print current status. */
12058 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12059 filetype_detect ? "ON" : "OFF",
12060 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12061 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12062 return;
12063 }
12064
12065 /* Accept "plugin" and "indent" in any order. */
12066 for (;;)
12067 {
12068 if (STRNCMP(arg, "plugin", 6) == 0)
12069 {
12070 plugin = TRUE;
12071 arg = skipwhite(arg + 6);
12072 continue;
12073 }
12074 if (STRNCMP(arg, "indent", 6) == 0)
12075 {
12076 indent = TRUE;
12077 arg = skipwhite(arg + 6);
12078 continue;
12079 }
12080 break;
12081 }
12082 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12083 {
12084 if (*arg == 'o' || !filetype_detect)
12085 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012086 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 filetype_detect = TRUE;
12088 if (plugin)
12089 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012090 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 filetype_plugin = TRUE;
12092 }
12093 if (indent)
12094 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012095 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 filetype_indent = TRUE;
12097 }
12098 }
12099 if (*arg == 'd')
12100 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012101 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012102 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103 }
12104 }
12105 else if (STRCMP(arg, "off") == 0)
12106 {
12107 if (plugin || indent)
12108 {
12109 if (plugin)
12110 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012111 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 filetype_plugin = FALSE;
12113 }
12114 if (indent)
12115 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012116 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 filetype_indent = FALSE;
12118 }
12119 }
12120 else
12121 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012122 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 filetype_detect = FALSE;
12124 }
12125 }
12126 else
12127 EMSG2(_(e_invarg2), arg);
12128}
12129
12130/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012131 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 */
12133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012134ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135{
12136 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012137 {
12138 char_u *arg = eap->arg;
12139
12140 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12141 arg += 9;
12142
12143 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12144 if (arg != eap->arg)
12145 did_filetype = FALSE;
12146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147}
12148#endif
12149
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012151ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152{
12153#ifdef FEAT_DIGRAPHS
12154 if (*eap->arg != NUL)
12155 putdigraph(eap->arg);
12156 else
12157 listdigraphs();
12158#else
12159 EMSG(_("E196: No digraphs in this version"));
12160#endif
12161}
12162
12163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012164ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165{
12166 int flags = 0;
12167
12168 if (eap->cmdidx == CMD_setlocal)
12169 flags = OPT_LOCAL;
12170 else if (eap->cmdidx == CMD_setglobal)
12171 flags = OPT_GLOBAL;
12172#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12173 if (cmdmod.browse && flags == 0)
12174 ex_options(eap);
12175 else
12176#endif
12177 (void)do_set(eap->arg, flags);
12178}
12179
12180#ifdef FEAT_SEARCH_EXTRA
12181/*
12182 * ":nohlsearch"
12183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012185ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012187 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012188 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189}
12190
12191/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012192 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 * Sets nextcmd to the start of the next command, if any. Also called when
12194 * skipping commands to find the next command.
12195 */
12196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012197ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012200 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 char_u *end;
12202 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012203 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012204
12205 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012206 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012207 else
12208 {
12209 EMSG(e_invcmd);
12210 return;
12211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212
12213 /* First clear any old pattern. */
12214 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012215 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216
12217 if (ends_excmd(*eap->arg))
12218 end = eap->arg;
12219 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012220 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 end = eap->arg + 4;
12222 else
12223 {
12224 p = skiptowhite(eap->arg);
12225 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012226 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 p = skipwhite(p);
12228 if (*p == NUL)
12229 {
12230 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012231 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 EMSG2(_(e_invarg2), eap->arg);
12233 return;
12234 }
12235 end = skip_regexp(p + 1, *p, TRUE, NULL);
12236 if (!eap->skip)
12237 {
12238 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12239 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012240 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 eap->errmsg = e_trailing;
12242 return;
12243 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012244 if (*end != *p)
12245 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012246 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012247 EMSG2(_(e_invarg2), p);
12248 return;
12249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250
12251 c = *end;
12252 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012253 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012254 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012255 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 }
12257 }
12258 eap->nextcmd = find_nextcmd(end);
12259}
12260#endif
12261
12262#ifdef FEAT_CRYPT
12263/*
12264 * ":X": Get crypt key
12265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012267ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012269 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012270 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271}
12272#endif
12273
12274#ifdef FEAT_FOLDING
12275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012276ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277{
12278 if (foldManualAllowed(TRUE))
12279 foldCreate(eap->line1, eap->line2);
12280}
12281
12282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012283ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284{
12285 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12286 eap->forceit, FALSE);
12287}
12288
12289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012290ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291{
12292 linenr_T lnum;
12293
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012294#ifdef FEAT_CLIPBOARD
12295 start_global_changes();
12296#endif
12297
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 /* First set the marks for all lines closed/open. */
12299 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12300 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12301 ml_setmarked(lnum);
12302
12303 /* Execute the command on the marked lines. */
12304 global_exe(eap->arg);
12305 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012306#ifdef FEAT_CLIPBOARD
12307 end_global_changes();
12308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309}
12310#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012311
12312# if defined(FEAT_TIMERS) || defined(PROTO)
12313 int
12314get_pressedreturn(void)
12315{
12316 return ex_pressedreturn;
12317}
12318
12319 void
12320set_pressedreturn(int val)
12321{
12322 ex_pressedreturn = val;
12323}
12324#endif