blob: 4082636767984232d32587e5fa3847b5e6a24e47 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200111#if !defined(FEAT_LISTCMDS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200129#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ex_close(exarg_T *eap);
163static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
164static void ex_only(exarg_T *eap);
165static void ex_resize(exarg_T *eap);
166static void ex_stag(exarg_T *eap);
167static void ex_tabclose(exarg_T *eap);
168static void ex_tabonly(exarg_T *eap);
169static void ex_tabnext(exarg_T *eap);
170static void ex_tabmove(exarg_T *eap);
171static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200172#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100173static void ex_pclose(exarg_T *eap);
174static void ex_ptag(exarg_T *eap);
175static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_hide(exarg_T *eap);
182static void ex_stop(exarg_T *eap);
183static void ex_exit(exarg_T *eap);
184static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#else
188# define ex_goto ex_ni
189#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static void ex_shell(exarg_T *eap);
191static void ex_preserve(exarg_T *eap);
192static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void ex_mode(exarg_T *eap);
200static void ex_wrongmodifier(exarg_T *eap);
201static void ex_find(exarg_T *eap);
202static void ex_open(exarg_T *eap);
203static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
205# define ex_drop ex_ni
206#endif
207#ifndef FEAT_GUI
208# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#endif
211#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100212static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#else
214# define ex_tearoff ex_ni
215#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000216#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#else
219# define ex_popup ex_ni
220#endif
221#ifndef FEAT_GUI_MSWIN
222# define ex_simalt ex_ni
223#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000224#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define gui_mch_find_dialog ex_ni
226# define gui_mch_replace_dialog ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define ex_helpfind ex_ni
230#endif
231#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100232# define ex_cscope ex_ni
233# define ex_scscope ex_ni
234# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
236#ifndef FEAT_SYN_HL
237# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200238# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200240#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200241# define ex_syntime ex_ni
242#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000244# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000245# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000246# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000247# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000248# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000249#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200250#ifndef FEAT_PERSISTENT_UNDO
251# define ex_rundo ex_ni
252# define ex_wundo ex_ni
253#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200254#ifndef FEAT_LUA
255# define ex_lua ex_script_ni
256# define ex_luado ex_ni
257# define ex_luafile ex_ni
258#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000259#ifndef FEAT_MZSCHEME
260# define ex_mzscheme ex_script_ni
261# define ex_mzfile ex_ni
262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifndef FEAT_PERL
264# define ex_perl ex_script_ni
265# define ex_perldo ex_ni
266#endif
267#ifndef FEAT_PYTHON
268# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200269# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200274# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275# define ex_py3file ex_ni
276#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100277#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
278# define ex_pyx ex_script_ni
279# define ex_pyxdo ex_ni
280# define ex_pyxfile ex_ni
281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifndef FEAT_TCL
283# define ex_tcl ex_script_ni
284# define ex_tcldo ex_ni
285# define ex_tclfile ex_ni
286#endif
287#ifndef FEAT_RUBY
288# define ex_ruby ex_script_ni
289# define ex_rubydo ex_ni
290# define ex_rubyfile ex_ni
291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifndef FEAT_KEYMAP
293# define ex_loadkeymap ex_ni
294#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100295static void ex_swapname(exarg_T *eap);
296static void ex_syncbind(exarg_T *eap);
297static void ex_read(exarg_T *eap);
298static void ex_pwd(exarg_T *eap);
299static void ex_equal(exarg_T *eap);
300static void ex_sleep(exarg_T *eap);
301static void do_exmap(exarg_T *eap, int isabbrev);
302static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100303static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000304#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#else
307# define ex_winpos ex_ni
308#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_operators(exarg_T *eap);
310static void ex_put(exarg_T *eap);
311static void ex_copymove(exarg_T *eap);
312static void ex_submagic(exarg_T *eap);
313static void ex_join(exarg_T *eap);
314static void ex_at(exarg_T *eap);
315static void ex_bang(exarg_T *eap);
316static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200317#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_wundo(exarg_T *eap);
319static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_redo(exarg_T *eap);
322static void ex_later(exarg_T *eap);
323static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_redrawstatus(exarg_T *eap);
325static void close_redir(void);
326static void ex_mkrc(exarg_T *eap);
327static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static char_u *uc_fun_cmd(void);
330static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_startinsert(exarg_T *eap);
333static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_checkpath(exarg_T *eap);
336static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_findpat ex_ni
339# define ex_checkpath ex_ni
340#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200341#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_psearch ex_ni
345#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_tag(exarg_T *eap);
347static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifndef FEAT_EVAL
349# define ex_scriptnames ex_ni
350# define ex_finish ex_ni
351# define ex_echo ex_ni
352# define ex_echohl ex_ni
353# define ex_execute ex_ni
354# define ex_call ex_ni
355# define ex_if ex_ni
356# define ex_endif ex_ni
357# define ex_else ex_ni
358# define ex_while ex_ni
359# define ex_continue ex_ni
360# define ex_break ex_ni
361# define ex_endwhile ex_ni
362# define ex_throw ex_ni
363# define ex_try ex_ni
364# define ex_catch ex_ni
365# define ex_finally ex_ni
366# define ex_endtry ex_ni
367# define ex_endfunction ex_ni
368# define ex_let ex_ni
369# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000370# define ex_lockvar ex_ni
371# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372# define ex_function ex_ni
373# define ex_delfunction ex_ni
374# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000375# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100377static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100379static int makeopens(FILE *fd, char_u *dirnow);
380static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
381static void ex_loadview(exarg_T *eap);
382static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000383static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#else
385# define ex_loadview ex_ni
386#endif
387#ifndef FEAT_EVAL
388# define ex_compiler ex_ni
389#endif
390#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100391static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#else
393# define ex_viminfo ex_ni
394#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100397static void ex_filetype(exarg_T *eap);
398static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_filetype ex_ni
401# define ex_setfiletype ex_ni
402#endif
403#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000404# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# define ex_diffpatch ex_ni
406# define ex_diffgetput ex_ni
407# define ex_diffsplit ex_ni
408# define ex_diffthis ex_ni
409# define ex_diffupdate ex_ni
410#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static void ex_digraphs(exarg_T *eap);
412static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
414# define ex_options ex_ni
415#endif
416#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100417static void ex_nohlsearch(exarg_T *eap);
418static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#else
420# define ex_nohlsearch ex_ni
421# define ex_match ex_ni
422#endif
423#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100424static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#else
426# define ex_X ex_ni
427#endif
428#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_fold(exarg_T *eap);
430static void ex_foldopen(exarg_T *eap);
431static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#else
433# define ex_fold ex_ni
434# define ex_foldopen ex_ni
435# define ex_folddo ex_ni
436#endif
437#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
438 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
439# define ex_language ex_ni
440#endif
441#ifndef FEAT_SIGNS
442# define ex_sign ex_ni
443#endif
444#ifndef FEAT_SUN_WORKSHOP
445# define ex_wsverb ex_ni
446#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000447#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200448# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000449# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200450# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453#ifndef FEAT_EVAL
454# define ex_debug ex_ni
455# define ex_breakadd ex_ni
456# define ex_debuggreedy ex_ni
457# define ex_breakdel ex_ni
458# define ex_breaklist ex_ni
459#endif
460
461#ifndef FEAT_CMDHIST
462# define ex_history ex_ni
463#endif
464#ifndef FEAT_JUMPLIST
465# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200466# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# define ex_changes ex_ni
468#endif
469
Bram Moolenaar05159a02005-02-26 23:04:13 +0000470#ifndef FEAT_PROFILE
471# define ex_profile ex_ni
472#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200473#ifndef FEAT_TERMINAL
474# define ex_terminal ex_ni
475#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477/*
478 * Declare cmdnames[].
479 */
480#define DO_DECLARE_EXCMD
481#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200482#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484static char_u dollar_command[2] = {'$', 0};
485
486
487#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000488/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489typedef struct
490{
491 char_u *line; /* command line */
492 linenr_T lnum; /* sourcing_lnum of the line */
493} wcmd_T;
494
495/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000496 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000498 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000500struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 garray_T *lines_gap; /* growarray with line info */
503 int current_line; /* last read line from growarray */
504 int repeating; /* TRUE when looping a second time */
505 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100506 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 void *cookie;
508};
509
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100510static char_u *get_loop_line(int c, void *cookie, int indent);
511static int store_loop_line(garray_T *gap, char_u *line);
512static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000513
514/* Struct to save a few things while debugging. Used in do_cmdline() only. */
515struct dbg_stuff
516{
517 int trylevel;
518 int force_abort;
519 except_T *caught_stack;
520 char_u *vv_exception;
521 char_u *vv_throwpoint;
522 int did_emsg;
523 int got_int;
524 int did_throw;
525 int need_rethrow;
526 int check_cstack;
527 except_T *current_exception;
528};
529
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100530static void save_dbg_stuff(struct dbg_stuff *dsp);
531static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000532
533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100534save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000535{
536 dsp->trylevel = trylevel; trylevel = 0;
537 dsp->force_abort = force_abort; force_abort = FALSE;
538 dsp->caught_stack = caught_stack; caught_stack = NULL;
539 dsp->vv_exception = v_exception(NULL);
540 dsp->vv_throwpoint = v_throwpoint(NULL);
541
542 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
543 dsp->did_emsg = did_emsg; did_emsg = FALSE;
544 dsp->got_int = got_int; got_int = FALSE;
545 dsp->did_throw = did_throw; did_throw = FALSE;
546 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
547 dsp->check_cstack = check_cstack; check_cstack = FALSE;
548 dsp->current_exception = current_exception; current_exception = NULL;
549}
550
551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100552restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000553{
554 suppress_errthrow = FALSE;
555 trylevel = dsp->trylevel;
556 force_abort = dsp->force_abort;
557 caught_stack = dsp->caught_stack;
558 (void)v_exception(dsp->vv_exception);
559 (void)v_throwpoint(dsp->vv_throwpoint);
560 did_emsg = dsp->did_emsg;
561 got_int = dsp->got_int;
562 did_throw = dsp->did_throw;
563 need_rethrow = dsp->need_rethrow;
564 check_cstack = dsp->check_cstack;
565 current_exception = dsp->current_exception;
566}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569/*
570 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
571 * command is given.
572 */
573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100574do_exmode(
575 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
577 int save_msg_scroll;
578 int prev_msg_row;
579 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100580 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000581
582 if (improved)
583 exmode_active = EXMODE_VIM;
584 else
585 exmode_active = EXMODE_NORMAL;
586 State = NORMAL;
587
588 /* When using ":global /pat/ visual" and then "Q" we return to continue
589 * the :global command. */
590 if (global_busy)
591 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 save_msg_scroll = msg_scroll;
594 ++RedrawingDisabled; /* don't redisplay the window */
595 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#ifdef FEAT_GUI
597 /* Ignore scrollbar and mouse events in Ex mode */
598 ++hold_gui_events;
599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
602 while (exmode_active)
603 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000604 /* Check for a ":normal" command and no more characters left. */
605 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
606 {
607 exmode_active = FALSE;
608 break;
609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 msg_scroll = TRUE;
611 need_wait_return = FALSE;
612 ex_pressedreturn = FALSE;
613 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100614 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 prev_msg_row = msg_row;
616 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (improved)
618 {
619 cmdline_row = msg_row;
620 do_cmdline(NULL, getexline, NULL, 0);
621 }
622 else
623 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
624 lines_left = Rows - 1;
625
Bram Moolenaardf177f62005-02-22 08:39:57 +0000626 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100627 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 if (curbuf->b_ml.ml_flags & ML_EMPTY)
630 EMSG(_(e_emptybuf));
631 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000633 if (ex_pressedreturn)
634 {
635 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000636 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000637 msg_row = prev_msg_row;
638 if (prev_msg_row == Rows - 1)
639 msg_row--;
640 }
641 msg_col = 0;
642 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
643 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000646 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
647 {
648 if (curbuf->b_ml.ml_flags & ML_EMPTY)
649 EMSG(_(e_emptybuf));
650 else
651 EMSG(_("E501: At end-of-file"));
652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654
655#ifdef FEAT_GUI
656 --hold_gui_events;
657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 --RedrawingDisabled;
659 --no_wait_return;
660 update_screen(CLEAR);
661 need_wait_return = FALSE;
662 msg_scroll = save_msg_scroll;
663}
664
665/*
666 * Execute a simple command line. Used for translated commands like "*".
667 */
668 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100669do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 return do_cmdline(cmd, NULL, NULL,
672 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
673}
674
675/*
676 * do_cmdline(): execute one Ex command line
677 *
678 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100679 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * 2. Split up in parts separated with '|'.
681 *
682 * This function can be called recursively!
683 *
684 * flags:
685 * DOCMD_VERBOSE - The command will be included in the error message.
686 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100687 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * DOCMD_KEYTYPED - Don't reset KeyTyped.
689 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
690 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
691 *
692 * return FAIL if cmdline could not be executed, OK otherwise
693 */
694 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100695do_cmdline(
696 char_u *cmdline,
697 char_u *(*fgetline)(int, void *, int),
698 void *cookie, /* argument for fgetline() */
699 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 char_u *next_cmdline; /* next cmd to execute */
702 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 static int recursive = 0; /* recursive depth */
705 int msg_didout_before_start = 0;
706 int count = 0; /* line number count */
707 int did_inc = FALSE; /* incremented RedrawingDisabled */
708 int retval = OK;
709#ifdef FEAT_EVAL
710 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000711 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 int current_line = 0; /* active line in lines_ga */
713 char_u *fname = NULL; /* function or script name */
714 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
715 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000716 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 int initial_trylevel;
718 struct msglist **saved_msg_list = NULL;
719 struct msglist *private_msg_list;
720
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100722 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000724 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000726 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729# define cmd_cookie cookie
730#endif
731 static int call_depth = 0; /* recursiveness */
732
733#ifdef FEAT_EVAL
734 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
735 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000736 * This ensures that the do_errthrow() call in do_one_cmd() does not
737 * combine the messages stored by an earlier invocation of do_one_cmd()
738 * with the command name of the later one. This would happen when
739 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 saved_msg_list = msg_list;
741 msg_list = &private_msg_list;
742 private_msg_list = NULL;
743#endif
744
745 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100746 * here. The value of 200 allows nested function calls, ":source", etc.
747 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100748 if (call_depth >= 200
749#ifdef FEAT_EVAL
750 && call_depth >= p_mfd
751#endif
752 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 EMSG(_("E169: Command too recursive"));
755#ifdef FEAT_EVAL
756 /* When converting to an exception, we do not include the command name
757 * since this is not an error of the specific command. */
758 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
759 msg_list = saved_msg_list;
760#endif
761 return FAIL;
762 }
763 ++call_depth;
764
765#ifdef FEAT_EVAL
766 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 cstack.cs_trylevel = 0;
769 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000770 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
772
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100776 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 ++ex_nesting_level;
779
780 /* Get the function or script name and the address where the next breakpoint
781 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
784 fname = func_name(real_cookie);
785 breakpoint = func_breakpoint(real_cookie);
786 dbg_tick = func_dbg_tick(real_cookie);
787 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
790 fname = sourcing_name;
791 breakpoint = source_breakpoint(real_cookie);
792 dbg_tick = source_dbg_tick(real_cookie);
793 }
794
795 /*
796 * Initialize "force_abort" and "suppress_errthrow" at the top level.
797 */
798 if (!recursive)
799 {
800 force_abort = FALSE;
801 suppress_errthrow = FALSE;
802 }
803
804 /*
805 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000806 * exception handling (used when debugging). Otherwise clear it to avoid
807 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000809 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000810 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000811 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200812 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 initial_trylevel = trylevel;
815
816 /*
817 * "did_throw" will be set to TRUE when an exception is being thrown.
818 */
819 did_throw = FALSE;
820#endif
821 /*
822 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000823 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * If force_abort is set, we cancel everything.
825 */
826 did_emsg = FALSE;
827
828 /*
829 * KeyTyped is only set when calling vgetc(). Reset it here when not
830 * calling vgetc() (sourced command lines).
831 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (!(flags & DOCMD_KEYTYPED)
833 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 KeyTyped = FALSE;
835
836 /*
837 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * - for multiple commands on one line, separated with '|'
840 * - when repeating until there are no more lines (for ":source")
841 */
842 next_cmdline = cmdline;
843 do
844 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847#endif
848
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000849 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (next_cmdline == NULL
851#ifdef FEAT_EVAL
852 && !force_abort
853 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
856 )
857 did_emsg = FALSE;
858
859 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * 3. If a line is given: Make a copy, so we can mess with it.
863 */
864
865#ifdef FEAT_EVAL
866 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
869 /* Each '|' separated command is stored separately in lines_ga, to
870 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100871 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 /* Check if a function has returned or, unless it has an unclosed
874 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000875 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000878 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000879 func_line_end(real_cookie);
880# endif
881 if (func_has_ended(real_cookie))
882 {
883 retval = FAIL;
884 break;
885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000888 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100889 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890 script_line_end();
891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100894 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
896 retval = FAIL;
897 break;
898 }
899
900 /* If breakpoints have been added/deleted need to check for it. */
901 if (breakpoint != NULL && dbg_tick != NULL
902 && *dbg_tick != debug_tick)
903 {
904 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100905 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 fname, sourcing_lnum);
907 *dbg_tick = debug_tick;
908 }
909
910 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
911 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
912
913 /* Did we encounter a breakpoint? */
914 if (breakpoint != NULL && *breakpoint != 0
915 && *breakpoint <= sourcing_lnum)
916 {
917 dbg_breakpoint(fname, sourcing_lnum);
918 /* Find next breakpoint. */
919 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100920 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 fname, sourcing_lnum);
922 *dbg_tick = debug_tick;
923 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000924# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000925 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000926 {
927 if (getline_is_func)
928 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930 script_line_start();
931 }
932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100938 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * below, so that it stores lines in or reads them from
940 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000941 * while/for loop. */
942 cmd_getline = get_loop_line;
943 cmd_cookie = (void *)&cmd_loop_cookie;
944 cmd_loop_cookie.lines_gap = &lines_ga;
945 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100946 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000947 cmd_loop_cookie.cookie = cookie;
948 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950 else
951 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100952 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 cmd_cookie = cookie;
954 }
955#endif
956
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (next_cmdline == NULL)
959 {
960 /*
961 * Need to set msg_didout for the first line after an ":if",
962 * otherwise the ":if" will be overwritten.
963 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100964 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100966 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_EVAL
968 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
969#else
970 0
971#endif
972 )) == NULL)
973 {
974 /* Don't call wait_return for aborted command line. The NULL
975 * returned for the end of a sourced file or executed function
976 * doesn't do this. */
977 if (KeyTyped && !(flags & DOCMD_REPEAT))
978 need_wait_return = FALSE;
979 retval = FAIL;
980 break;
981 }
982 used_getline = TRUE;
983
984 /*
985 * Keep the first typed line. Clear it when more lines are typed.
986 */
987 if (flags & DOCMD_KEEPLINE)
988 {
989 vim_free(repeat_cmdline);
990 if (count == 0)
991 repeat_cmdline = vim_strsave(next_cmdline);
992 else
993 repeat_cmdline = NULL;
994 }
995 }
996
997 /* 3. Make a copy of the command so we can mess with it. */
998 else if (cmdline_copy == NULL)
999 {
1000 next_cmdline = vim_strsave(next_cmdline);
1001 if (next_cmdline == NULL)
1002 {
1003 EMSG(_(e_outofmem));
1004 retval = FAIL;
1005 break;
1006 }
1007 }
1008 cmdline_copy = next_cmdline;
1009
1010#ifdef FEAT_EVAL
1011 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001012 * Save the current line when inside a ":while" or ":for", and when
1013 * the command looks like a ":while" or ":for", because we may need it
1014 * later. When there is a '|' and another command, it is stored
1015 * separately, because we need to be able to jump back to it from an
1016 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001018 if (current_line == lines_ga.ga_len
1019 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001021 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 retval = FAIL;
1024 break;
1025 }
1026 }
1027 did_endif = FALSE;
1028#endif
1029
1030 if (count++ == 0)
1031 {
1032 /*
1033 * All output from the commands is put below each other, without
1034 * waiting for a return. Don't do this when executing commands
1035 * from a script or when being called recursive (e.g. for ":e
1036 * +command file").
1037 */
1038 if (!(flags & DOCMD_NOWAIT) && !recursive)
1039 {
1040 msg_didout_before_start = msg_didout;
1041 msg_didany = FALSE; /* no output yet */
1042 msg_start();
1043 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001044 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 ++RedrawingDisabled;
1046 did_inc = TRUE;
1047 }
1048 }
1049
1050 if (p_verbose >= 15 && sourcing_name != NULL)
1051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001053 verbose_enter_scroll();
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 smsg((char_u *)_("line %ld: %s"),
1056 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001057 if (msg_silent == 0)
1058 msg_puts((char_u *)"\n"); /* don't overwrite this */
1059
1060 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 --no_wait_return;
1062 }
1063
1064 /*
1065 * 2. Execute one '|' separated command.
1066 * do_one_cmd() will return NULL if there is no trailing '|'.
1067 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1068 */
1069 ++recursive;
1070 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1071#ifdef FEAT_EVAL
1072 &cstack,
1073#endif
1074 cmd_getline, cmd_cookie);
1075 --recursive;
1076
1077#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 if (cmd_cookie == (void *)&cmd_loop_cookie)
1079 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082#endif
1083
1084 if (next_cmdline == NULL)
1085 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001086 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef FEAT_CMDHIST
1088 /*
1089 * If the command was typed, remember it for the ':' register.
1090 * Do this AFTER executing the command to make :@: work.
1091 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001092 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 && new_last_cmdline != NULL)
1094 {
1095 vim_free(last_cmdline);
1096 last_cmdline = new_last_cmdline;
1097 new_last_cmdline = NULL;
1098 }
1099#endif
1100 }
1101 else
1102 {
1103 /* need to copy the command after the '|' to cmdline_copy, for the
1104 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001105 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 next_cmdline = cmdline_copy;
1107 }
1108
1109
1110#ifdef FEAT_EVAL
1111 /* reset did_emsg for a function that is not aborted by an error */
1112 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001113 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 && !func_has_abort(real_cookie))
1115 did_emsg = FALSE;
1116
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 ++current_line;
1120
1121 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 * An ":endwhile", ":endfor" and ":continue" is handled here.
1123 * If we were executing commands, jump back to the ":while" or
1124 * ":for".
1125 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 /* Jump back to the matching ":while" or ":for". Be careful
1132 * not to use a cs_line[] from an entry that isn't a ":while"
1133 * or ":for": It would make "current_line" invalid and can
1134 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 if (!did_emsg && !got_int && !did_throw
1136 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 && (cstack.cs_flags[cstack.cs_idx]
1138 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 && cstack.cs_line[cstack.cs_idx] >= 0
1140 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1141 {
1142 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 /* remember we jumped there */
1144 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 line_breakcheck(); /* check if CTRL-C typed */
1146
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 /* Check for the next breakpoint at or after the ":while"
1148 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (breakpoint != NULL)
1150 {
1151 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001152 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 fname,
1154 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1155 *dbg_tick = debug_tick;
1156 }
1157 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001162 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1163 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 }
1166
1167 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1174 }
1175 }
1176
1177 /*
1178 * When not inside any ":while" loop, clear remembered lines.
1179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182 if (lines_ga.ga_len > 0)
1183 {
1184 sourcing_lnum =
1185 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1186 free_cmdlines(&lines_ga);
1187 }
1188 current_line = 0;
1189 }
1190
1191 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1193 * being restored at the ":endtry". Reset them here and set the
1194 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1195 * This includes the case where a missing ":endif", ":endwhile" or
1196 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 cstack.cs_lflags &= ~CSL_HAD_FINA;
1201 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1202 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 did_throw ? (void *)current_exception : NULL);
1204 did_emsg = got_int = did_throw = FALSE;
1205 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1206 }
1207
1208 /* Update global "trylevel" for recursive calls to do_cmdline() from
1209 * within this loop. */
1210 trylevel = initial_trylevel + cstack.cs_trylevel;
1211
1212 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001213 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 * files) is aborted because of an error, an interrupt, or an uncaught
1215 * exception, cancel everything. If it is left normally, reset
1216 * force_abort to get the non-EH compatible abortion behavior for
1217 * the rest of the script.
1218 */
1219 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1220 force_abort = FALSE;
1221
1222 /* Convert an interrupt to an exception if appropriate. */
1223 (void)do_intthrow(&cstack);
1224#endif /* FEAT_EVAL */
1225
1226 }
1227 /*
1228 * Continue executing command lines when:
1229 * - no CTRL-C typed, no aborting error, no exception thrown or try
1230 * conditionals need to be checked for executing finally clauses or
1231 * catching an interrupt exception
1232 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * looping for ":source" command or function call.
1235 */
1236 while (!((got_int
1237#ifdef FEAT_EVAL
1238 || (did_emsg && force_abort) || did_throw
1239#endif
1240 )
1241#ifdef FEAT_EVAL
1242 && cstack.cs_trylevel == 0
1243#endif
1244 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001245 && !(did_emsg
1246#ifdef FEAT_EVAL
1247 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001248 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001249 * the :endtry to be missed. */
1250 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1251#endif
1252 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001253 && (getline_equal(fgetline, cookie, getexmodeline)
1254 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 && (next_cmdline != NULL
1256#ifdef FEAT_EVAL
1257 || cstack.cs_idx >= 0
1258#endif
1259 || (flags & DOCMD_REPEAT)));
1260
1261 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001262 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#ifdef FEAT_EVAL
1264 free_cmdlines(&lines_ga);
1265 ga_clear(&lines_ga);
1266
1267 if (cstack.cs_idx >= 0)
1268 {
1269 /*
1270 * If a sourced file or executed function ran to its end, report the
1271 * unclosed conditional.
1272 */
1273 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001274 && ((getline_equal(fgetline, cookie, getsourceline)
1275 && !source_finished(fgetline, cookie))
1276 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 && !func_has_ended(real_cookie))))
1278 {
1279 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1280 EMSG(_(e_endtry));
1281 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1282 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001283 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1284 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 else
1286 EMSG(_(e_endif));
1287 }
1288
1289 /*
1290 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1291 * ":endtry" in a sourced file or executed function. If the try
1292 * conditional is in its finally clause, ignore anything pending.
1293 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001294 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 */
1296 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001297 {
1298 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1299
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001300 if (idx >= 0)
1301 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001302 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1303 &cstack.cs_looplevel);
1304 }
1305 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 trylevel = initial_trylevel;
1307 }
1308
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001309 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1310 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001312 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 ? (char_u *)"endfunction" : (char_u *)NULL);
1314
1315 if (trylevel == 0)
1316 {
1317 /*
1318 * When an exception is being thrown out of the outermost try
1319 * conditional, discard the uncaught exception, disable the conversion
1320 * of interrupts or errors to exceptions, and ensure that no more
1321 * commands are executed.
1322 */
1323 if (did_throw)
1324 {
1325 void *p = NULL;
1326 char_u *saved_sourcing_name;
1327 int saved_sourcing_lnum;
1328 struct msglist *messages = NULL, *next;
1329
1330 /*
1331 * If the uncaught exception is a user exception, report it as an
1332 * error. If it is an error exception, display the saved error
1333 * message now. For an interrupt exception, do nothing; the
1334 * interrupt message is given elsewhere.
1335 */
1336 switch (current_exception->type)
1337 {
1338 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001339 vim_snprintf((char *)IObuff, IOSIZE,
1340 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 current_exception->value);
1342 p = vim_strsave(IObuff);
1343 break;
1344 case ET_ERROR:
1345 messages = current_exception->messages;
1346 current_exception->messages = NULL;
1347 break;
1348 case ET_INTERRUPT:
1349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351
1352 saved_sourcing_name = sourcing_name;
1353 saved_sourcing_lnum = sourcing_lnum;
1354 sourcing_name = current_exception->throw_name;
1355 sourcing_lnum = current_exception->throw_lnum;
1356 current_exception->throw_name = NULL;
1357
1358 discard_current_exception(); /* uses IObuff if 'verbose' */
1359 suppress_errthrow = TRUE;
1360 force_abort = TRUE;
1361
1362 if (messages != NULL)
1363 {
1364 do
1365 {
1366 next = messages->next;
1367 emsg(messages->msg);
1368 vim_free(messages->msg);
1369 vim_free(messages);
1370 messages = next;
1371 }
1372 while (messages != NULL);
1373 }
1374 else if (p != NULL)
1375 {
1376 emsg(p);
1377 vim_free(p);
1378 }
1379 vim_free(sourcing_name);
1380 sourcing_name = saved_sourcing_name;
1381 sourcing_lnum = saved_sourcing_lnum;
1382 }
1383
1384 /*
1385 * On an interrupt or an aborting error not converted to an exception,
1386 * disable the conversion of errors to exceptions. (Interrupts are not
1387 * converted any more, here.) This enables also the interrupt message
1388 * when force_abort is set and did_emsg unset in case of an interrupt
1389 * from a finally clause after an error.
1390 */
1391 else if (got_int || (did_emsg && force_abort))
1392 suppress_errthrow = TRUE;
1393 }
1394
1395 /*
1396 * The current cstack will be freed when do_cmdline() returns. An uncaught
1397 * exception will have to be rethrown in the previous cstack. If a function
1398 * has just returned or a script file was just finished and the previous
1399 * cstack belongs to the same function or, respectively, script file, it
1400 * will have to be checked for finally clauses to be executed due to the
1401 * ":return" or ":finish". This is done in do_one_cmd().
1402 */
1403 if (did_throw)
1404 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001405 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001407 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 && ex_nesting_level > func_level(real_cookie) + 1))
1409 {
1410 if (!did_throw)
1411 check_cstack = TRUE;
1412 }
1413 else
1414 {
1415 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001416 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 --ex_nesting_level;
1418 /*
1419 * Go to debug mode when returning from a function in which we are
1420 * single-stepping.
1421 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001422 if ((getline_equal(fgetline, cookie, getsourceline)
1423 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001425 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 ? (char_u *)_("End of sourced file")
1427 : (char_u *)_("End of function"));
1428 }
1429
1430 /*
1431 * Restore the exception environment (done after returning from the
1432 * debugger).
1433 */
1434 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001435 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 msg_list = saved_msg_list;
1438#endif /* FEAT_EVAL */
1439
1440 /*
1441 * If there was too much output to fit on the command line, ask the user to
1442 * hit return before redrawing the screen. With the ":global" command we do
1443 * this only once after the command is finished.
1444 */
1445 if (did_inc)
1446 {
1447 --RedrawingDisabled;
1448 --no_wait_return;
1449 msg_scroll = FALSE;
1450
1451 /*
1452 * When just finished an ":if"-":else" which was typed, no need to
1453 * wait for hit-return. Also for an error situation.
1454 */
1455 if (retval == FAIL
1456#ifdef FEAT_EVAL
1457 || (did_endif && KeyTyped && !did_emsg)
1458#endif
1459 )
1460 {
1461 need_wait_return = FALSE;
1462 msg_didany = FALSE; /* don't wait when restarting edit */
1463 }
1464 else if (need_wait_return)
1465 {
1466 /*
1467 * The msg_start() above clears msg_didout. The wait_return we do
1468 * here should not overwrite the command that may be shown before
1469 * doing that.
1470 */
1471 msg_didout |= msg_didout_before_start;
1472 wait_return(FALSE);
1473 }
1474 }
1475
Bram Moolenaar2a942252012-11-28 23:03:07 +01001476#ifdef FEAT_EVAL
1477 did_endif = FALSE; /* in case do_cmdline used recursively */
1478#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 /*
1480 * Reset if_level, in case a sourced script file contains more ":if" than
1481 * ":endif" (could be ":if x | foo | endif").
1482 */
1483 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001484#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 --call_depth;
1487 return retval;
1488}
1489
1490#ifdef FEAT_EVAL
1491/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001492 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 */
1494 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001495get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 wcmd_T *wp;
1499 char_u *line;
1500
1501 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1502 {
1503 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (cp->getline == NULL)
1508 line = getcmdline(c, 0L, indent);
1509 else
1510 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001511 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 ++cp->current_line;
1513
1514 return line;
1515 }
1516
1517 KeyTyped = FALSE;
1518 ++cp->current_line;
1519 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1520 sourcing_lnum = wp->lnum;
1521 return vim_strsave(wp->line);
1522}
1523
1524/*
1525 * Store a line in "gap" so that a ":while" loop can execute it again.
1526 */
1527 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001528store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 if (ga_grow(gap, 1) == FAIL)
1531 return FAIL;
1532 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1533 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1534 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 return OK;
1536}
1537
1538/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001539 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 */
1541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001542free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 while (gap->ga_len > 0)
1545 {
1546 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1547 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549}
1550#endif
1551
1552/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001553 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1554 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001557getline_equal(
1558 char_u *(*fgetline)(int, void *, int),
1559 void *cookie UNUSED, /* argument for fgetline() */
1560 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001563 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001564 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaar89d40322006-08-29 15:30:07 +00001566 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001567 * function that's originally used to obtain the lines. This may be
1568 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001569 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001570 cp = (struct loop_cookie *)cookie;
1571 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
1573 gp = cp->getline;
1574 cp = cp->cookie;
1575 }
1576 return gp == func;
1577#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001578 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579#endif
1580}
1581
1582#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1583/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001584 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 * getline function. Otherwise return "cookie".
1586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588getline_cookie(
1589 char_u *(*fgetline)(int, void *, int) UNUSED,
1590 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
1592# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001593 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001594 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar89d40322006-08-29 15:30:07 +00001596 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001597 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001599 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001600 cp = (struct loop_cookie *)cookie;
1601 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 gp = cp->getline;
1604 cp = cp->cookie;
1605 }
1606 return cp;
1607# else
1608 return cookie;
1609# endif
1610}
1611#endif
1612
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613
1614/*
1615 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1616 * ":bwipeout", etc.
1617 * Returns the buffer number.
1618 */
1619 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001620compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001621{
1622 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001623 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001624 int count = offset;
1625
1626 buf = firstbuf;
1627 while (buf->b_next != NULL && buf->b_fnum < lnum)
1628 buf = buf->b_next;
1629 while (count != 0)
1630 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001631 count += (offset < 0) ? 1 : -1;
1632 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1633 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001636 if (addr_type == ADDR_LOADED_BUFFERS)
1637 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001638 while (buf->b_ml.ml_mfp == NULL)
1639 {
1640 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1641 if (nextbuf == NULL)
1642 break;
1643 buf = nextbuf;
1644 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001645 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001646 /* we might have gone too far, last buffer is not loadedd */
1647 if (addr_type == ADDR_LOADED_BUFFERS)
1648 while (buf->b_ml.ml_mfp == NULL)
1649 {
1650 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1651 if (nextbuf == NULL)
1652 break;
1653 buf = nextbuf;
1654 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001655 return buf->b_fnum;
1656}
1657
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001658static int current_win_nr(win_T *win);
1659static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001660
1661 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001662current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663{
1664 win_T *wp;
1665 int nr = 0;
1666
Bram Moolenaar29323592016-07-24 22:04:11 +02001667 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001668 {
1669 ++nr;
1670 if (wp == win)
1671 break;
1672 }
1673 return nr;
1674}
1675
1676 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001677current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678{
1679 tabpage_T *tp;
1680 int nr = 0;
1681
Bram Moolenaar29323592016-07-24 22:04:11 +02001682 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001683 {
1684 ++nr;
1685 if (tp == tab)
1686 break;
1687 }
1688 return nr;
1689}
1690
1691# define CURRENT_WIN_NR current_win_nr(curwin)
1692# define LAST_WIN_NR current_win_nr(NULL)
1693# define CURRENT_TAB_NR current_tab_nr(curtab)
1694# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * Execute one Ex command.
1698 *
1699 * If 'sourcing' is TRUE, the command will be included in the error message.
1700 *
1701 * 1. skip comment lines and leading space
1702 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001703 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001705 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 6. parse arguments
1707 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001709 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 *
1711 * This function may be called recursively!
1712 */
1713#if (_MSC_VER == 1200)
1714/*
Bram Moolenaared203462004-06-16 11:19:22 +00001715 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001717 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718#endif
1719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720do_one_cmd(
1721 char_u **cmdlinep,
1722 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001724 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001726 char_u *(*fgetline)(int, void *, int),
1727 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 char_u *p;
1730 linenr_T lnum;
1731 long n;
1732 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001733 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 exarg_T ea; /* Ex command arguments */
1735 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001736 int save_msg_scroll = msg_scroll;
1737 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001739#ifdef HAVE_SANDBOX
1740 int did_sandbox = FALSE;
1741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 cmdmod_T save_cmdmod;
1743 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001744 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001745 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
1747 vim_memset(&ea, 0, sizeof(ea));
1748 ea.line1 = 1;
1749 ea.line2 = 1;
1750#ifdef FEAT_EVAL
1751 ++ex_nesting_level;
1752#endif
1753
Bram Moolenaar21691f82012-12-05 19:13:18 +01001754 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (quitmore
1756#ifdef FEAT_EVAL
1757 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759#endif
1760#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001761 /* avoid that an autocommand, e.g. QuitPre, does this */
1762 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#endif
1764 )
1765 --quitmore;
1766
1767 /*
1768 * Reset browse, confirm, etc.. They are restored when returning, for
1769 * recursive calls.
1770 */
1771 save_cmdmod = cmdmod;
1772 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1773
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001774 /* "#!anything" is handled like a comment. */
1775 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1776 goto doend;
1777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 /*
1779 * Repeat until no more command modifiers are found.
1780 */
1781 ea.cmd = *cmdlinep;
1782 for (;;)
1783 {
1784/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001785 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 */
1787 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1788 ++ea.cmd;
1789
1790 /* in ex mode, an empty line works like :+ */
1791 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001792 && (getline_equal(fgetline, cookie, getexmodeline)
1793 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001794 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796 ea.cmd = (char_u *)"+";
1797 ex_pressedreturn = TRUE;
1798 }
1799
1800 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001801 if (*ea.cmd == '"')
1802 goto doend;
1803 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001804 {
1805 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001812 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 switch (*p)
1814 {
1815 /* When adding an entry, also modify cmd_exists(). */
1816 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 continue;
1820
1821 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1822 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 continue;
1825 }
1826 if (checkforcmd(&ea.cmd, "browse", 3))
1827 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001828#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 cmdmod.browse = TRUE;
1830#endif
1831 continue;
1832 }
1833 if (!checkforcmd(&ea.cmd, "botright", 2))
1834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 continue;
1837
1838 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1839 break;
1840#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1841 cmdmod.confirm = TRUE;
1842#endif
1843 continue;
1844
1845 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1846 {
1847 cmdmod.keepmarks = TRUE;
1848 continue;
1849 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001850 if (checkforcmd(&ea.cmd, "keepalt", 5))
1851 {
1852 cmdmod.keepalt = TRUE;
1853 continue;
1854 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001855 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1856 {
1857 cmdmod.keeppatterns = TRUE;
1858 continue;
1859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1861 break;
1862 cmdmod.keepjumps = TRUE;
1863 continue;
1864
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001865 case 'f': /* only accept ":filter {pat} cmd" */
1866 {
1867 char_u *reg_pat;
1868
1869 if (!checkforcmd(&p, "filter", 4)
1870 || *p == NUL || ends_excmd(*p))
1871 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001872 if (*p == '!')
1873 {
1874 cmdmod.filter_force = TRUE;
1875 p = skipwhite(p + 1);
1876 if (*p == NUL || ends_excmd(*p))
1877 break;
1878 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001879 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1880 if (p == NULL || *p == NUL)
1881 break;
1882 cmdmod.filter_regmatch.regprog =
1883 vim_regcomp(reg_pat, RE_MAGIC);
1884 if (cmdmod.filter_regmatch.regprog == NULL)
1885 break;
1886 ea.cmd = p;
1887 continue;
1888 }
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 /* ":hide" and ":hide | cmd" are not modifiers */
1891 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1892 || *p == NUL || ends_excmd(*p))
1893 break;
1894 ea.cmd = p;
1895 cmdmod.hide = TRUE;
1896 continue;
1897
1898 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1899 {
1900 cmdmod.lockmarks = TRUE;
1901 continue;
1902 }
1903
1904 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 continue;
1908
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001909 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001910 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001911#ifdef FEAT_AUTOCMD
1912 if (cmdmod.save_ei == NULL)
1913 {
1914 /* Set 'eventignore' to "all". Restore the
1915 * existing option value later. */
1916 cmdmod.save_ei = vim_strsave(p_ei);
1917 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001918 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001919 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001920#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001921 continue;
1922 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001923 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001924 break;
1925 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001926 continue;
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 continue;
1932
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001933 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1934 {
1935#ifdef HAVE_SANDBOX
1936 if (!did_sandbox)
1937 ++sandbox;
1938 did_sandbox = TRUE;
1939#endif
1940 continue;
1941 }
1942 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001944 if (save_msg_silent == -1)
1945 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001947 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
1949 /* ":silent!", but not "silent !cmd" */
1950 ea.cmd = skipwhite(ea.cmd + 1);
1951 ++emsg_silent;
1952 ++did_esilent;
1953 }
1954 continue;
1955
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001956 case 't': if (checkforcmd(&p, "tab", 3))
1957 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001958 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001959 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001960 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001961 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001962 else
1963 {
1964 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1965 {
1966 errormsg = (char_u *)_(e_invrange);
1967 goto doend;
1968 }
1969 cmdmod.tab = tabnr + 1;
1970 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001971 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001972 continue;
1973 }
1974 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 continue;
1978
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001979 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1980 break;
1981 if (save_msg_silent == -1)
1982 save_msg_silent = msg_silent;
1983 msg_silent = 0;
1984 continue;
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 continue;
1990 }
1991 if (!checkforcmd(&p, "verbose", 4))
1992 break;
1993 if (verbose_save < 0)
1994 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001995 if (vim_isdigit(*ea.cmd))
1996 p_verbose = atoi((char *)ea.cmd);
1997 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 p_verbose = 1;
1999 ea.cmd = p;
2000 continue;
2001 }
2002 break;
2003 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002004 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
2006#ifdef FEAT_EVAL
2007 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2008 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2009#else
2010 ea.skip = (if_level > 0);
2011#endif
2012
2013#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002014# ifdef FEAT_PROFILE
2015 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002016 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002017 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002018 if (getline_equal(fgetline, cookie, get_func_line))
2019 func_line_exec(getline_cookie(fgetline, cookie));
2020 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021 script_line_exec();
2022 }
2023#endif
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 /* May go to debug mode. If this happens and the ">quit" debug command is
2026 * used, throw an interrupt exception and skip the next command. */
2027 dbg_check_breakpoint(&ea);
2028 if (!ea.skip && got_int)
2029 {
2030 ea.skip = TRUE;
2031 (void)do_intthrow(cstack);
2032 }
2033#endif
2034
2035/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002036 * 3. Skip over the range to find the command. Let "p" point to after it.
2037 *
2038 * We need the command to know what kind of range it uses.
2039 */
2040 cmd = ea.cmd;
2041 ea.cmd = skip_range(ea.cmd, NULL);
2042 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2043 ea.cmd = skipwhite(ea.cmd + 1);
2044 p = find_command(&ea, NULL);
2045
2046/*
2047 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 *
2049 * where 'addr' is:
2050 *
2051 * % (entire file)
2052 * $ [+-NUM]
2053 * 'x [+-NUM] (where x denotes a currently defined mark)
2054 * . [+-NUM]
2055 * [+-NUM]..
2056 * NUM
2057 *
2058 * The ea.cmd pointer is updated to point to the first character following the
2059 * range spec. If an initial address is found, but no second, the upper bound
2060 * is equal to the lower.
2061 */
2062
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002063 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002064 if (!IS_USER_CMDIDX(ea.cmdidx))
2065 {
2066 if (ea.cmdidx != CMD_SIZE)
2067 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2068 else
2069 ea.addr_type = ADDR_LINES;
2070
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002071 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002072 if (ea.cmdidx == CMD_wincmd && p != NULL)
2073 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002074 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002077 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 for (;;)
2079 {
2080 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002081 switch (ea.addr_type)
2082 {
2083 case ADDR_LINES:
2084 /* default is current line number */
2085 ea.line2 = curwin->w_cursor.lnum;
2086 break;
2087 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002088 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002089 break;
2090 case ADDR_ARGUMENTS:
2091 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002092 if (ea.line2 > ARGCOUNT)
2093 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002094 break;
2095 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002096 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002097 ea.line2 = curbuf->b_fnum;
2098 break;
2099 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002100 ea.line2 = CURRENT_TAB_NR;
2101 break;
2102 case ADDR_TABS_RELATIVE:
2103 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002104 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002105#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002106 case ADDR_QUICKFIX:
2107 ea.line2 = qf_get_cur_valid_idx(&ea);
2108 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002109#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002112 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002113 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (ea.cmd == NULL) /* error detected */
2115 goto doend;
2116 if (lnum == MAXLNUM)
2117 {
2118 if (*ea.cmd == '%') /* '%' - all lines */
2119 {
2120 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002121 switch (ea.addr_type)
2122 {
2123 case ADDR_LINES:
2124 ea.line1 = 1;
2125 ea.line2 = curbuf->b_ml.ml_line_count;
2126 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002127 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002128 {
2129 buf_T *buf = firstbuf;
2130
2131 while (buf->b_next != NULL
2132 && buf->b_ml.ml_mfp == NULL)
2133 buf = buf->b_next;
2134 ea.line1 = buf->b_fnum;
2135 buf = lastbuf;
2136 while (buf->b_prev != NULL
2137 && buf->b_ml.ml_mfp == NULL)
2138 buf = buf->b_prev;
2139 ea.line2 = buf->b_fnum;
2140 break;
2141 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002142 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002143 ea.line1 = firstbuf->b_fnum;
2144 ea.line2 = lastbuf->b_fnum;
2145 break;
2146 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002147 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002148 if (IS_USER_CMDIDX(ea.cmdidx))
2149 {
2150 ea.line1 = 1;
2151 ea.line2 = ea.addr_type == ADDR_WINDOWS
2152 ? LAST_WIN_NR : LAST_TAB_NR;
2153 }
2154 else
2155 {
2156 /* there is no Vim command which uses '%' and
2157 * ADDR_WINDOWS or ADDR_TABS */
2158 errormsg = (char_u *)_(e_invrange);
2159 goto doend;
2160 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002161 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002162 case ADDR_TABS_RELATIVE:
2163 errormsg = (char_u *)_(e_invrange);
2164 goto doend;
2165 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002167 if (ARGCOUNT == 0)
2168 ea.line1 = ea.line2 = 0;
2169 else
2170 {
2171 ea.line1 = 1;
2172 ea.line2 = ARGCOUNT;
2173 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002174 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002175#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002176 case ADDR_QUICKFIX:
2177 ea.line1 = 1;
2178 ea.line2 = qf_get_size(&ea);
2179 if (ea.line2 == 0)
2180 ea.line2 = 1;
2181 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002182#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 ++ea.addr_count;
2185 }
2186 /* '*' - visual area */
2187 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2188 {
2189 pos_T *fp;
2190
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002191 if (ea.addr_type != ADDR_LINES)
2192 {
2193 errormsg = (char_u *)_(e_invrange);
2194 goto doend;
2195 }
2196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 ++ea.cmd;
2198 if (!ea.skip)
2199 {
2200 fp = getmark('<', FALSE);
2201 if (check_mark(fp) == FAIL)
2202 goto doend;
2203 ea.line1 = fp->lnum;
2204 fp = getmark('>', FALSE);
2205 if (check_mark(fp) == FAIL)
2206 goto doend;
2207 ea.line2 = fp->lnum;
2208 ++ea.addr_count;
2209 }
2210 }
2211 }
2212 else
2213 ea.line2 = lnum;
2214 ea.addr_count++;
2215
2216 if (*ea.cmd == ';')
2217 {
2218 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002221 /* don't leave the cursor on an illegal line or column */
2222 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 else if (*ea.cmd != ',')
2226 break;
2227 ++ea.cmd;
2228 }
2229
2230 /* One address given: set start and end lines */
2231 if (ea.addr_count == 1)
2232 {
2233 ea.line1 = ea.line2;
2234 /* ... but only implicit: really no address given */
2235 if (lnum == MAXLNUM)
2236 ea.addr_count = 0;
2237 }
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002240 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 */
2242
2243 /*
2244 * Skip ':' and any white space
2245 */
2246 ea.cmd = skipwhite(ea.cmd);
2247 while (*ea.cmd == ':')
2248 ea.cmd = skipwhite(ea.cmd + 1);
2249
2250 /*
2251 * If we got a line, but no command, then go to the line.
2252 * If we find a '|' or '\n' we set ea.nextcmd.
2253 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002254 if (*ea.cmd == NUL || *ea.cmd == '"'
2255 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
2257 /*
2258 * strange vi behaviour:
2259 * ":3" jumps to line 3
2260 * ":3|..." prints line 3
2261 * ":|" prints current line
2262 */
2263 if (ea.skip) /* skip this if inside :if */
2264 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002265 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 ea.cmdidx = CMD_print;
2268 ea.argt = RANGE+COUNT+TRLBAR;
2269 if ((errormsg = invalid_range(&ea)) == NULL)
2270 {
2271 correct_range(&ea);
2272 ex_print(&ea);
2273 }
2274 }
2275 else if (ea.addr_count != 0)
2276 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002277 if (ea.line2 > curbuf->b_ml.ml_line_count)
2278 {
2279 /* With '-' in 'cpoptions' a line number past the file is an
2280 * error, otherwise put it at the end of the file. */
2281 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2282 ea.line2 = -1;
2283 else
2284 ea.line2 = curbuf->b_ml.ml_line_count;
2285 }
2286
2287 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002288 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 else
2290 {
2291 if (ea.line2 == 0)
2292 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 else
2294 curwin->w_cursor.lnum = ea.line2;
2295 beginline(BL_SOL | BL_FIX);
2296 }
2297 }
2298 goto doend;
2299 }
2300
Bram Moolenaard5005162014-08-22 23:05:54 +02002301#ifdef FEAT_AUTOCMD
2302 /* If this looks like an undefined user command and there are CmdUndefined
2303 * autocommands defined, trigger the matching autocommands. */
2304 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2305 && ASCII_ISUPPER(*ea.cmd)
2306 && has_cmdundefined())
2307 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002308 int ret;
2309
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002310 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002311 while (ASCII_ISALNUM(*p))
2312 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002313 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002314 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2315 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002316 /* If the autocommands did something and didn't cause an error, try
2317 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002318 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002319 }
2320#endif
2321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#ifdef FEAT_USR_CMDS
2323 if (p == NULL)
2324 {
2325 if (!ea.skip)
2326 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2327 goto doend;
2328 }
2329 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002330 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2331 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 errormsg = uc_fun_cmd();
2334 goto doend;
2335 }
2336#endif
2337 if (ea.cmdidx == CMD_SIZE)
2338 {
2339 if (!ea.skip)
2340 {
2341 STRCPY(IObuff, _("E492: Not an editor command"));
2342 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002343 {
2344 /* If the modifier was parsed OK the error must be in the
2345 * following command */
2346 if (after_modifier != NULL)
2347 append_command(after_modifier);
2348 else
2349 append_command(*cmdlinep);
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002352 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354 goto doend;
2355 }
2356
Bram Moolenaar958636c2014-10-21 20:01:58 +02002357 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2358 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002359#ifdef HAVE_EX_SCRIPT_NI
2360 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2361#endif
2362 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364#ifndef FEAT_EVAL
2365 /*
2366 * When the expression evaluation is disabled, recognize the ":if" and
2367 * ":endif" commands and ignore everything in between it.
2368 */
2369 if (ea.cmdidx == CMD_if)
2370 ++if_level;
2371 if (if_level)
2372 {
2373 if (ea.cmdidx == CMD_endif)
2374 --if_level;
2375 goto doend;
2376 }
2377
2378#endif
2379
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002380 /* forced commands */
2381 if (*p == '!' && ea.cmdidx != CMD_substitute
2382 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 ++p;
2385 ea.forceit = TRUE;
2386 }
2387 else
2388 ea.forceit = FALSE;
2389
2390/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002391 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002393 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002394 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 if (!ea.skip)
2397 {
2398#ifdef HAVE_SANDBOX
2399 if (sandbox != 0 && !(ea.argt & SBOXOK))
2400 {
2401 /* Command not allowed in sandbox. */
2402 errormsg = (char_u *)_(e_sandbox);
2403 goto doend;
2404 }
2405#endif
2406 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2407 {
2408 /* Command not allowed in non-'modifiable' buffer */
2409 errormsg = (char_u *)_(e_modifiable);
2410 goto doend;
2411 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002412
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002413 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002414 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002416 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002417 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 goto doend;
2419 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002420#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002421 /* Disallow editing another buffer when "curbuf_lock" is set.
2422 * Do allow ":edit" (check for argument later).
2423 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002424 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002425 && ea.cmdidx != CMD_edit
2426 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002427 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002428 && curbuf_locked())
2429 goto doend;
2430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
2432 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2433 {
2434 /* no range allowed */
2435 errormsg = (char_u *)_(e_norange);
2436 goto doend;
2437 }
2438 }
2439
2440 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2441 {
2442 errormsg = (char_u *)_(e_nobang);
2443 goto doend;
2444 }
2445
2446 /*
2447 * Don't complain about the range if it is not used
2448 * (could happen if line_count is accidentally set to 0).
2449 */
2450 if (!ea.skip && !ni)
2451 {
2452 /*
2453 * If the range is backwards, ask for confirmation and, if given, swap
2454 * ea.line1 & ea.line2 so it's forwards again.
2455 * When global command is busy, don't ask, will fail below.
2456 */
2457 if (!global_busy && ea.line1 > ea.line2)
2458 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002459 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002461 if (sourcing || exmode_active)
2462 {
2463 errormsg = (char_u *)_("E493: Backwards range given");
2464 goto doend;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (ask_yesno((char_u *)
2467 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002468 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470 lnum = ea.line1;
2471 ea.line1 = ea.line2;
2472 ea.line2 = lnum;
2473 }
2474 if ((errormsg = invalid_range(&ea)) != NULL)
2475 goto doend;
2476 }
2477
2478 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2479 ea.line2 = 1;
2480
2481 correct_range(&ea);
2482
2483#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002484 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2485 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /* Put the first line at the start of a closed fold, put the last line
2488 * at the end of a closed fold. */
2489 (void)hasFolding(ea.line1, &ea.line1, NULL);
2490 (void)hasFolding(ea.line2, NULL, &ea.line2);
2491 }
2492#endif
2493
2494#ifdef FEAT_QUICKFIX
2495 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002496 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 * option here, so things like % get expanded.
2498 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002499 p = replace_makeprg(&ea, p, cmdlinep);
2500 if (p == NULL)
2501 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502#endif
2503
2504 /*
2505 * Skip to start of argument.
2506 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2507 */
2508 if (ea.cmdidx == CMD_bang)
2509 ea.arg = p;
2510 else
2511 ea.arg = skipwhite(p);
2512
2513 /*
2514 * Check for "++opt=val" argument.
2515 * Must be first, allow ":w ++enc=utf8 !cmd"
2516 */
2517 if (ea.argt & ARGOPT)
2518 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2519 if (getargopt(&ea) == FAIL && !ni)
2520 {
2521 errormsg = (char_u *)_(e_invarg);
2522 goto doend;
2523 }
2524
2525 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2526 {
2527 if (*ea.arg == '>') /* append */
2528 {
2529 if (*++ea.arg != '>') /* typed wrong */
2530 {
2531 errormsg = (char_u *)_("E494: Use w or w>>");
2532 goto doend;
2533 }
2534 ea.arg = skipwhite(ea.arg + 1);
2535 ea.append = TRUE;
2536 }
2537 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2538 {
2539 ++ea.arg;
2540 ea.usefilter = TRUE;
2541 }
2542 }
2543
2544 if (ea.cmdidx == CMD_read)
2545 {
2546 if (ea.forceit)
2547 {
2548 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2549 ea.forceit = FALSE;
2550 }
2551 else if (*ea.arg == '!') /* :r !filter */
2552 {
2553 ++ea.arg;
2554 ea.usefilter = TRUE;
2555 }
2556 }
2557
2558 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2559 {
2560 ea.amount = 1;
2561 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2562 {
2563 ++ea.arg;
2564 ++ea.amount;
2565 }
2566 ea.arg = skipwhite(ea.arg);
2567 }
2568
2569 /*
2570 * Check for "+command" argument, before checking for next command.
2571 * Don't do this for ":read !cmd" and ":write !cmd".
2572 */
2573 if ((ea.argt & EDITCMD) && !ea.usefilter)
2574 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2575
2576 /*
2577 * Check for '|' to separate commands and '"' to start comments.
2578 * Don't do this for ":read !cmd" and ":write !cmd".
2579 */
2580 if ((ea.argt & TRLBAR) && !ea.usefilter)
2581 separate_nextcmd(&ea);
2582
2583 /*
2584 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002585 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2586 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002588 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002589 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002590 || ea.cmdidx == CMD_global
2591 || ea.cmdidx == CMD_vglobal
2592 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 for (p = ea.arg; *p; ++p)
2595 {
2596 /* Remove one backslash before a newline, so that it's possible to
2597 * pass a newline to the shell and also a newline that is preceded
2598 * with a backslash. This makes it impossible to end a shell
2599 * command in a backslash, but that doesn't appear useful.
2600 * Halving the number of backslashes is incompatible with previous
2601 * versions. */
2602 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002603 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 else if (*p == '\n')
2605 {
2606 ea.nextcmd = p + 1;
2607 *p = NUL;
2608 break;
2609 }
2610 }
2611 }
2612
2613 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2614 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002615 buf_T *buf;
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002618 switch (ea.addr_type)
2619 {
2620 case ADDR_LINES:
2621 ea.line2 = curbuf->b_ml.ml_line_count;
2622 break;
2623 case ADDR_LOADED_BUFFERS:
2624 buf = firstbuf;
2625 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2626 buf = buf->b_next;
2627 ea.line1 = buf->b_fnum;
2628 buf = lastbuf;
2629 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2630 buf = buf->b_prev;
2631 ea.line2 = buf->b_fnum;
2632 break;
2633 case ADDR_BUFFERS:
2634 ea.line1 = firstbuf->b_fnum;
2635 ea.line2 = lastbuf->b_fnum;
2636 break;
2637 case ADDR_WINDOWS:
2638 ea.line2 = LAST_WIN_NR;
2639 break;
2640 case ADDR_TABS:
2641 ea.line2 = LAST_TAB_NR;
2642 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002643 case ADDR_TABS_RELATIVE:
2644 ea.line2 = 1;
2645 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002646 case ADDR_ARGUMENTS:
2647 if (ARGCOUNT == 0)
2648 ea.line1 = ea.line2 = 0;
2649 else
2650 ea.line2 = ARGCOUNT;
2651 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002652#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002653 case ADDR_QUICKFIX:
2654 ea.line2 = qf_get_size(&ea);
2655 if (ea.line2 == 0)
2656 ea.line2 = 1;
2657 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002658#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661
2662 /* accept numbered register only when no count allowed (:put) */
2663 if ( (ea.argt & REGSTR)
2664 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002665 /* Do not allow register = for user commands */
2666 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2668 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002669#ifndef FEAT_CLIPBOARD
2670 /* check these explicitly for a more specific error message */
2671 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002673 errormsg = (char_u *)_(e_invalidreg);
2674 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
2676#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002677 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2678 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002679 {
2680 ea.regname = *ea.arg++;
2681#ifdef FEAT_EVAL
2682 /* for '=' register: accept the rest of the line as an expression */
2683 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2684 {
2685 set_expr_line(vim_strsave(ea.arg));
2686 ea.arg += STRLEN(ea.arg);
2687 }
2688#endif
2689 ea.arg = skipwhite(ea.arg);
2690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 /*
2694 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2695 * count, it's a buffer name.
2696 */
2697 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2698 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002699 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 n = getdigits(&ea.arg);
2702 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002703 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
2705 errormsg = (char_u *)_(e_zerocount);
2706 goto doend;
2707 }
2708 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2709 {
2710 ea.line2 = n;
2711 if (ea.addr_count == 0)
2712 ea.addr_count = 1;
2713 }
2714 else
2715 {
2716 ea.line1 = ea.line2;
2717 ea.line2 += n - 1;
2718 ++ea.addr_count;
2719 /*
2720 * Be vi compatible: no error message for out of range.
2721 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002722 if (ea.addr_type == ADDR_LINES
2723 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 ea.line2 = curbuf->b_ml.ml_line_count;
2725 }
2726 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002727
2728 /*
2729 * Check for flags: 'l', 'p' and '#'.
2730 */
2731 if (ea.argt & EXFLAGS)
2732 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002734 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002735 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
2737 errormsg = (char_u *)_(e_trailing);
2738 goto doend;
2739 }
2740
2741 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2742 {
2743 errormsg = (char_u *)_(e_argreq);
2744 goto doend;
2745 }
2746
2747#ifdef FEAT_EVAL
2748 /*
2749 * Skip the command when it's not going to be executed.
2750 * The commands like :if, :endif, etc. always need to be executed.
2751 * Also make an exception for commands that handle a trailing command
2752 * themselves.
2753 */
2754 if (ea.skip)
2755 {
2756 switch (ea.cmdidx)
2757 {
2758 /* commands that need evaluation */
2759 case CMD_while:
2760 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002761 case CMD_for:
2762 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 case CMD_if:
2764 case CMD_elseif:
2765 case CMD_else:
2766 case CMD_endif:
2767 case CMD_try:
2768 case CMD_catch:
2769 case CMD_finally:
2770 case CMD_endtry:
2771 case CMD_function:
2772 break;
2773
2774 /* Commands that handle '|' themselves. Check: A command should
2775 * either have the TRLBAR flag, appear in this list or appear in
2776 * the list at ":help :bar". */
2777 case CMD_aboveleft:
2778 case CMD_and:
2779 case CMD_belowright:
2780 case CMD_botright:
2781 case CMD_browse:
2782 case CMD_call:
2783 case CMD_confirm:
2784 case CMD_delfunction:
2785 case CMD_djump:
2786 case CMD_dlist:
2787 case CMD_dsearch:
2788 case CMD_dsplit:
2789 case CMD_echo:
2790 case CMD_echoerr:
2791 case CMD_echomsg:
2792 case CMD_echon:
2793 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002794 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 case CMD_help:
2796 case CMD_hide:
2797 case CMD_ijump:
2798 case CMD_ilist:
2799 case CMD_isearch:
2800 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002801 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case CMD_keepjumps:
2803 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002804 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 case CMD_leftabove:
2806 case CMD_let:
2807 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002808 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002810 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002811 case CMD_noautocmd:
2812 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 case CMD_perl:
2814 case CMD_psearch:
2815 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002816 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002817 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 case CMD_return:
2819 case CMD_rightbelow:
2820 case CMD_ruby:
2821 case CMD_silent:
2822 case CMD_smagic:
2823 case CMD_snomagic:
2824 case CMD_substitute:
2825 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002826 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 case CMD_tcl:
2828 case CMD_throw:
2829 case CMD_tilde:
2830 case CMD_topleft:
2831 case CMD_unlet:
2832 case CMD_verbose:
2833 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002834 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 break;
2836
2837 default: goto doend;
2838 }
2839 }
2840#endif
2841
2842 if (ea.argt & XFILE)
2843 {
2844 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2845 goto doend;
2846 }
2847
2848#ifdef FEAT_LISTCMDS
2849 /*
2850 * Accept buffer name. Cannot be used at the same time with a buffer
2851 * number. Don't do this for a user command.
2852 */
2853 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002854 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
2856 /*
2857 * :bdelete, :bwipeout and :bunload take several arguments, separated
2858 * by spaces: find next space (skipping over escaped characters).
2859 * The others take one argument: ignore trailing spaces.
2860 */
2861 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2862 || ea.cmdidx == CMD_bunload)
2863 p = skiptowhite_esc(ea.arg);
2864 else
2865 {
2866 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002867 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 --p;
2869 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002870 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2871 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (ea.line2 < 0) /* failed */
2873 goto doend;
2874 ea.addr_count = 1;
2875 ea.arg = skipwhite(p);
2876 }
2877#endif
2878
2879/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002880 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 *
2882 * The "ea" structure holds the arguments that can be used.
2883 */
2884 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002885 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 ea.cookie = cookie;
2887#ifdef FEAT_EVAL
2888 ea.cstack = cstack;
2889#endif
2890
2891#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002892 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
2894 /*
2895 * Execute a user-defined command.
2896 */
2897 do_ucmd(&ea);
2898 }
2899 else
2900#endif
2901 {
2902 /*
2903 * Call the function to execute the command.
2904 */
2905 ea.errmsg = NULL;
2906 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2907 if (ea.errmsg != NULL)
2908 errormsg = (char_u *)_(ea.errmsg);
2909 }
2910
2911#ifdef FEAT_EVAL
2912 /*
2913 * If the command just executed called do_cmdline(), any throw or ":return"
2914 * or ":finish" encountered there must also check the cstack of the still
2915 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2916 * exception, or reanimate a returned function or finished script file and
2917 * return or finish it again.
2918 */
2919 if (need_rethrow)
2920 do_throw(cstack);
2921 else if (check_cstack)
2922 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002923 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002925 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 && current_func_returned())
2927 do_return(&ea, TRUE, FALSE, NULL);
2928 }
2929 need_rethrow = check_cstack = FALSE;
2930#endif
2931
2932doend:
2933 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002936 curwin->w_cursor.col = 0;
2937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2940 {
2941 if (sourcing)
2942 {
2943 if (errormsg != IObuff)
2944 {
2945 STRCPY(IObuff, errormsg);
2946 errormsg = IObuff;
2947 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002948 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 emsg(errormsg);
2951 }
2952#ifdef FEAT_EVAL
2953 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002954 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2955 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956#endif
2957
2958 if (verbose_save >= 0)
2959 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002960#ifdef FEAT_AUTOCMD
2961 if (cmdmod.save_ei != NULL)
2962 {
2963 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002964 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2965 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002966 free_string_option(cmdmod.save_ei);
2967 }
2968#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002969 if (cmdmod.filter_regmatch.regprog != NULL)
2970 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 cmdmod = save_cmdmod;
2973
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002974 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
2976 /* messages could be enabled for a serious error, need to check if the
2977 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002978 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002979 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 emsg_silent -= did_esilent;
2981 if (emsg_silent < 0)
2982 emsg_silent = 0;
2983 /* Restore msg_scroll, it's set by file I/O commands, even when no
2984 * message is actually displayed. */
2985 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002986
2987 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2988 * somewhere in the line. Put it back in the first column. */
2989 if (redirecting())
2990 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 }
2992
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002993#ifdef HAVE_SANDBOX
2994 if (did_sandbox)
2995 --sandbox;
2996#endif
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2999 ea.nextcmd = NULL;
3000
3001#ifdef FEAT_EVAL
3002 --ex_nesting_level;
3003#endif
3004
3005 return ea.nextcmd;
3006}
3007#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003008 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#endif
3010
3011/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003012 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 * If there is a match advance "pp" to the argument and return TRUE.
3014 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003015 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003016checkforcmd(
3017 char_u **pp, /* start of command */
3018 char *cmd, /* name of command */
3019 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
3021 int i;
3022
3023 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003024 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 break;
3026 if (i >= len && !isalpha((*pp)[i]))
3027 {
3028 *pp = skipwhite(*pp + i);
3029 return TRUE;
3030 }
3031 return FALSE;
3032}
3033
3034/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003035 * Append "cmd" to the error message in IObuff.
3036 * Takes care of limiting the length and handling 0xa0, which would be
3037 * invisible otherwise.
3038 */
3039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003040append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003041{
3042 char_u *s = cmd;
3043 char_u *d;
3044
3045 STRCAT(IObuff, ": ");
3046 d = IObuff + STRLEN(IObuff);
3047 while (*s != NUL && d - IObuff < IOSIZE - 7)
3048 {
3049 if (
3050#ifdef FEAT_MBYTE
3051 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3052#endif
3053 *s == 0xa0)
3054 {
3055 s +=
3056#ifdef FEAT_MBYTE
3057 enc_utf8 ? 2 :
3058#endif
3059 1;
3060 STRCPY(d, "<a0>");
3061 d += 4;
3062 }
3063 else
3064 MB_COPY_CHAR(s, d);
3065 }
3066 *d = NUL;
3067}
3068
3069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003071 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003073 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * Returns NULL for an ambiguous user command.
3075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003077find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 int len;
3080 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003081 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083 /*
3084 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003085 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * - the 'k' command can directly be followed by any character.
3087 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003088 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003090 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 */
3092 p = eap->cmd;
3093 if (*p == 'k')
3094 {
3095 eap->cmdidx = CMD_k;
3096 ++p;
3097 }
3098 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003099 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3100 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 || p[1] == 'g'
3102 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3103 || p[1] == 'I'
3104 || (p[1] == 'r' && p[2] != 'e')))
3105 {
3106 eap->cmdidx = CMD_substitute;
3107 ++p;
3108 }
3109 else
3110 {
3111 while (ASCII_ISALPHA(*p))
3112 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003113 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003114 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003115 while (ASCII_ISALNUM(*p))
3116 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 /* check for non-alpha command */
3119 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3120 ++p;
3121 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003122 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3123 {
3124 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3125 * :delete with the 'l' flag. Same for 'p'. */
3126 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003127 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003128 break;
3129 if (i == len - 1)
3130 {
3131 --len;
3132 if (p[-1] == 'l')
3133 eap->flags |= EXFLAG_LIST;
3134 else
3135 eap->flags |= EXFLAG_PRINT;
3136 }
3137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003139 if (ASCII_ISLOWER(eap->cmd[0]))
3140 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003141 int c1 = eap->cmd[0];
3142 int c2 = eap->cmd[1];
3143
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003144 if (command_count != (int)CMD_SIZE)
3145 {
3146 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3147 getout(1);
3148 }
3149
3150 /* Use a precomputed index for fast look-up in cmdnames[]
3151 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003152 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3153 if (ASCII_ISLOWER(c2))
3154 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003157 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3160 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3161 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3162 (size_t)len) == 0)
3163 {
3164#ifdef FEAT_EVAL
3165 if (full != NULL
3166 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3167 *full = TRUE;
3168#endif
3169 break;
3170 }
3171
3172#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003173 /* Look for a user defined command as a last resort. Let ":Print" be
3174 * overruled by a user defined command. */
3175 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3176 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003178 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 while (ASCII_ISALNUM(*p))
3180 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003184 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 eap->cmdidx = CMD_SIZE;
3186 }
3187
3188 return p;
3189}
3190
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003191#ifdef FEAT_USR_CMDS
3192/*
3193 * Search for a user command that matches "eap->cmd".
3194 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3195 * Return a pointer to just after the command.
3196 * Return NULL if there is no matching command.
3197 */
3198 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003199find_ucmd(
3200 exarg_T *eap,
3201 char_u *p, /* end of the command (possibly including count) */
3202 int *full, /* set to TRUE for a full match */
3203 expand_T *xp, /* used for completion, NULL otherwise */
3204 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205{
3206 int len = (int)(p - eap->cmd);
3207 int j, k, matchlen = 0;
3208 ucmd_T *uc;
3209 int found = FALSE;
3210 int possible = FALSE;
3211 char_u *cp, *np; /* Point into typed cmd and test name */
3212 garray_T *gap;
3213 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3214 only full match global is accepted. */
3215
3216 /*
3217 * Look for buffer-local user commands first, then global ones.
3218 */
3219 gap = &curbuf->b_ucmds;
3220 for (;;)
3221 {
3222 for (j = 0; j < gap->ga_len; ++j)
3223 {
3224 uc = USER_CMD_GA(gap, j);
3225 cp = eap->cmd;
3226 np = uc->uc_name;
3227 k = 0;
3228 while (k < len && *np != NUL && *cp++ == *np++)
3229 k++;
3230 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3231 {
3232 /* If finding a second match, the command is ambiguous. But
3233 * not if a buffer-local command wasn't a full match and a
3234 * global command is a full match. */
3235 if (k == len && found && *np != NUL)
3236 {
3237 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239 amb_local = TRUE;
3240 }
3241
3242 if (!found || (k == len && *np == NUL))
3243 {
3244 /* If we matched up to a digit, then there could
3245 * be another command including the digit that we
3246 * should use instead.
3247 */
3248 if (k == len)
3249 found = TRUE;
3250 else
3251 possible = TRUE;
3252
3253 if (gap == &ucmds)
3254 eap->cmdidx = CMD_USER;
3255 else
3256 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003257 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003259 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260
3261# ifdef FEAT_CMDL_COMPL
3262 if (compl != NULL)
3263 *compl = uc->uc_compl;
3264# ifdef FEAT_EVAL
3265 if (xp != NULL)
3266 {
3267 xp->xp_arg = uc->uc_compl_arg;
3268 xp->xp_scriptID = uc->uc_scriptID;
3269 }
3270# endif
3271# endif
3272 /* Do not search for further abbreviations
3273 * if this is an exact match. */
3274 matchlen = k;
3275 if (k == len && *np == NUL)
3276 {
3277 if (full != NULL)
3278 *full = TRUE;
3279 amb_local = FALSE;
3280 break;
3281 }
3282 }
3283 }
3284 }
3285
3286 /* Stop if we found a full match or searched all. */
3287 if (j < gap->ga_len || gap == &ucmds)
3288 break;
3289 gap = &ucmds;
3290 }
3291
3292 /* Only found ambiguous matches. */
3293 if (amb_local)
3294 {
3295 if (xp != NULL)
3296 xp->xp_context = EXPAND_UNSUCCESSFUL;
3297 return NULL;
3298 }
3299
3300 /* The match we found may be followed immediately by a number. Move "p"
3301 * back to point to it. */
3302 if (found || possible)
3303 return p + (matchlen - len);
3304 return p;
3305}
3306#endif
3307
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003309static struct cmdmod
3310{
3311 char *name;
3312 int minlen;
3313 int has_count; /* :123verbose :3tab */
3314} cmdmods[] = {
3315 {"aboveleft", 3, FALSE},
3316 {"belowright", 3, FALSE},
3317 {"botright", 2, FALSE},
3318 {"browse", 3, FALSE},
3319 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003320 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003321 {"hide", 3, FALSE},
3322 {"keepalt", 5, FALSE},
3323 {"keepjumps", 5, FALSE},
3324 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003325 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003326 {"leftabove", 5, FALSE},
3327 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003328 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003329 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003330 {"rightbelow", 6, FALSE},
3331 {"sandbox", 3, FALSE},
3332 {"silent", 3, FALSE},
3333 {"tab", 3, TRUE},
3334 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003335 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003336 {"verbose", 4, TRUE},
3337 {"vertical", 4, FALSE},
3338};
3339
3340/*
3341 * Return length of a command modifier (including optional count).
3342 * Return zero when it's not a modifier.
3343 */
3344 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003345modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346{
3347 int i, j;
3348 char_u *p = cmd;
3349
3350 if (VIM_ISDIGIT(*cmd))
3351 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003352 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003353 {
3354 for (j = 0; p[j] != NUL; ++j)
3355 if (p[j] != cmdmods[i].name[j])
3356 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003357 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003358 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003359 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360 }
3361 return 0;
3362}
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364/*
3365 * Return > 0 if an Ex command "name" exists.
3366 * Return 2 if there is an exact match.
3367 * Return 3 if there is an ambiguous match.
3368 */
3369 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003370cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 exarg_T ea;
3373 int full = FALSE;
3374 int i;
3375 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003376 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
3378 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003379 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
3381 for (j = 0; name[j] != NUL; ++j)
3382 if (name[j] != cmdmods[i].name[j])
3383 break;
3384 if (name[j] == NUL && j >= cmdmods[i].minlen)
3385 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3386 }
3387
Bram Moolenaara9587612006-05-04 21:47:50 +00003388 /* Check built-in commands and user defined commands.
3389 * For ":2match" and ":3match" we need to skip the number. */
3390 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003392 p = find_command(&ea, &full);
3393 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003395 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3396 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003397 if (*skipwhite(p) != NUL)
3398 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3400}
3401#endif
3402
3403/*
3404 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3405 * we don't need/want deleted. Maybe this could be done better if we didn't
3406 * repeat all this stuff. The only problem is that they may not stay
3407 * perfectly compatible with each other, but then the command line syntax
3408 * probably won't change that much -- webb.
3409 */
3410 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003411set_one_cmd_context(
3412 expand_T *xp,
3413 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
3415 char_u *p;
3416 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003417 int len = 0;
3418 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3420 int compl = EXPAND_NOTHING;
3421#endif
3422#ifdef FEAT_CMDL_COMPL
3423 int delim;
3424#endif
3425 int forceit = FALSE;
3426 int usefilter = FALSE; /* filter instead of file name */
3427
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003428 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 xp->xp_pattern = buff;
3430 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003434 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 */
3436 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3437 ;
3438 xp->xp_pattern = cmd;
3439
3440 if (*cmd == NUL)
3441 return NULL;
3442 if (*cmd == '"') /* ignore comment lines */
3443 {
3444 xp->xp_context = EXPAND_NOTHING;
3445 return NULL;
3446 }
3447
3448/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003449 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
3451 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 xp->xp_pattern = cmd;
3453 if (*cmd == NUL)
3454 return NULL;
3455 if (*cmd == '"')
3456 {
3457 xp->xp_context = EXPAND_NOTHING;
3458 return NULL;
3459 }
3460
3461 if (*cmd == '|' || *cmd == '\n')
3462 return cmd + 1; /* There's another command */
3463
3464 /*
3465 * Isolate the command and search for it in the command table.
3466 * Exceptions:
3467 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003468 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3470 */
3471 if (*cmd == 'k' && cmd[1] != 'e')
3472 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 p = cmd + 1;
3475 }
3476 else
3477 {
3478 p = cmd;
3479 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3480 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003481 /* a user command may contain digits */
3482 if (ASCII_ISUPPER(cmd[0]))
3483 while (ASCII_ISALNUM(*p) || *p == '*')
3484 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003485 /* for python 3.x: ":py3*" commands completion */
3486 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003487 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003488 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003489 while (ASCII_ISALPHA(*p) || *p == '*')
3490 ++p;
3491 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003492 /* check for non-alpha command */
3493 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3494 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003495 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 xp->xp_context = EXPAND_UNSUCCESSFUL;
3500 return NULL;
3501 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003502 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003503 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3504 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3505 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 break;
3507
3508#ifdef FEAT_USR_CMDS
3509 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3511 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512#endif
3513 }
3514
3515 /*
3516 * If the cursor is touching the command, and it ends in an alpha-numeric
3517 * character, complete the command name.
3518 */
3519 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3520 return NULL;
3521
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003522 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3525 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003526 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 p = cmd + 1;
3528 }
3529#ifdef FEAT_USR_CMDS
3530 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3531 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003532 ea.cmd = cmd;
3533 p = find_ucmd(&ea, p, NULL, xp,
3534# if defined(FEAT_CMDL_COMPL)
3535 &compl
3536# else
3537 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003540 if (p == NULL)
3541 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543#endif
3544 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003545 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
3547 /* Not still touching the command and it was an illegal one */
3548 xp->xp_context = EXPAND_UNSUCCESSFUL;
3549 return NULL;
3550 }
3551
3552 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3553
3554 if (*p == '!') /* forced commands */
3555 {
3556 forceit = TRUE;
3557 ++p;
3558 }
3559
3560/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003561 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003563 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003564 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566 arg = skipwhite(p);
3567
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 if (*arg == '>') /* append */
3571 {
3572 if (*++arg == '>')
3573 ++arg;
3574 arg = skipwhite(arg);
3575 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
3578 ++arg;
3579 usefilter = TRUE;
3580 }
3581 }
3582
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 usefilter = forceit; /* :r! filter if forced */
3586 if (*arg == '!') /* :r !filter */
3587 {
3588 ++arg;
3589 usefilter = TRUE;
3590 }
3591 }
3592
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 while (*arg == *cmd) /* allow any number of '>' or '<' */
3596 ++arg;
3597 arg = skipwhite(arg);
3598 }
3599
3600 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003601 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
3603 /* Check if we're in the +command */
3604 p = arg + 1;
3605 arg = skip_cmd_arg(arg, FALSE);
3606
3607 /* Still touching the command after '+'? */
3608 if (*arg == NUL)
3609 return p;
3610
3611 /* Skip space(s) after +command to get to the real argument */
3612 arg = skipwhite(arg);
3613 }
3614
3615 /*
3616 * Check for '|' to separate commands and '"' to start comments.
3617 * Don't do this for ":read !cmd" and ":write !cmd".
3618 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 p = arg;
3622 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003623 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 p += 2;
3625 while (*p)
3626 {
3627 if (*p == Ctrl_V)
3628 {
3629 if (p[1] != NUL)
3630 ++p;
3631 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003632 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 || *p == '|' || *p == '\n')
3634 {
3635 if (*(p - 1) != '\\')
3636 {
3637 if (*p == '|' || *p == '\n')
3638 return p + 1;
3639 return NULL; /* It's a comment */
3640 }
3641 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003642 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 }
3645
3646 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003647 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 vim_strchr((char_u *)"|\"", *arg) == NULL)
3649 return NULL;
3650
3651 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003652 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003654 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003655 while (*p && p < buff + len)
3656 {
3657 if (*p == ' ' || *p == TAB)
3658 {
3659 /* argument starts after a space */
3660 xp->xp_pattern = ++p;
3661 }
3662 else
3663 {
3664 if (*p == '\\' && *(p + 1) != NUL)
3665 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003666 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003667 }
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003670 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003672 int c;
3673 int in_quote = FALSE;
3674 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
3676 /*
3677 * Allow spaces within back-quotes to count as part of the argument
3678 * being expanded.
3679 */
3680 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003681 p = xp->xp_pattern;
3682 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003684#ifdef FEAT_MBYTE
3685 if (has_mbyte)
3686 c = mb_ptr2char(p);
3687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003689 c = *p;
3690 if (c == '\\' && p[1] != NUL)
3691 ++p;
3692 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 if (!in_quote)
3695 {
3696 xp->xp_pattern = p;
3697 bow = p + 1;
3698 }
3699 in_quote = !in_quote;
3700 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003701 /* An argument can contain just about everything, except
3702 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003703 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003704#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003705 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003706#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003707 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003709 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003710 while (*p != NUL)
3711 {
3712#ifdef FEAT_MBYTE
3713 if (has_mbyte)
3714 c = mb_ptr2char(p);
3715 else
3716#endif
3717 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003718 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003719 break;
3720#ifdef FEAT_MBYTE
3721 if (has_mbyte)
3722 len = (*mb_ptr2len)(p);
3723 else
3724#endif
3725 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003726 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003727 }
3728 if (in_quote)
3729 bow = p;
3730 else
3731 xp->xp_pattern = p;
3732 p -= len;
3733 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003734 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736
3737 /*
3738 * If we are still inside the quotes, and we passed a space, just
3739 * expand from there.
3740 */
3741 if (bow != NULL && in_quote)
3742 xp->xp_pattern = bow;
3743 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003744
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003745 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003746 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003747 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003748#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003749 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003750#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751 /* When still after the command name expand executables. */
3752 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003753 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
3756 /* Check for environment variable */
3757 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003758#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 || *xp->xp_pattern == '%'
3760#endif
3761 )
3762 {
3763 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3764 if (!vim_isIDc(*p))
3765 break;
3766 if (*p == NUL)
3767 {
3768 xp->xp_context = EXPAND_ENV_VARS;
3769 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003770#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3771 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003772 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003773 compl = EXPAND_ENV_VARS;
3774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003777#if defined(FEAT_CMDL_COMPL)
3778 /* Check for user names */
3779 if (*xp->xp_pattern == '~')
3780 {
3781 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3782 ;
3783 /* Complete ~user only if it partially matches a user name.
3784 * A full match ~user<Tab> will be replaced by user's home
3785 * directory i.e. something like ~user<Tab> -> /home/user/ */
3786 if (*p == NUL && p > xp->xp_pattern + 1
3787 && match_user(xp->xp_pattern + 1) == 1)
3788 {
3789 xp->xp_context = EXPAND_USER;
3790 ++xp->xp_pattern;
3791 }
3792 }
3793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795
3796/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003797 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003799 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003801 case CMD_find:
3802 case CMD_sfind:
3803 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003804 if (xp->xp_context == EXPAND_FILES)
3805 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003806 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 case CMD_cd:
3808 case CMD_chdir:
3809 case CMD_lcd:
3810 case CMD_lchdir:
3811 if (xp->xp_context == EXPAND_FILES)
3812 xp->xp_context = EXPAND_DIRECTORIES;
3813 break;
3814 case CMD_help:
3815 xp->xp_context = EXPAND_HELP;
3816 xp->xp_pattern = arg;
3817 break;
3818
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003819 /* Command modifiers: return the argument.
3820 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003822 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_belowright:
3824 case CMD_botright:
3825 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003826 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003827 case CMD_cdo:
3828 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003830 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 case CMD_folddoclosed:
3832 case CMD_folddoopen:
3833 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003834 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_keepjumps:
3836 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003837 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003838 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003840 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003842 case CMD_noautocmd:
3843 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003845 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003847 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003848 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 case CMD_topleft:
3850 case CMD_verbose:
3851 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003852 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 return arg;
3854
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003855 case CMD_filter:
3856 if (*arg != NUL)
3857 arg = skip_vimgrep_pat(arg, NULL, NULL);
3858 if (arg == NULL || *arg == NUL)
3859 {
3860 xp->xp_context = EXPAND_NOTHING;
3861 return NULL;
3862 }
3863 return skipwhite(arg);
3864
Bram Moolenaar4f688582007-07-24 12:34:30 +00003865#ifdef FEAT_CMDL_COMPL
3866# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 case CMD_match:
3868 if (*arg == NUL || !ends_excmd(*arg))
3869 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003870 /* also complete "None" */
3871 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 arg = skipwhite(skiptowhite(arg));
3873 if (*arg != NUL)
3874 {
3875 xp->xp_context = EXPAND_NOTHING;
3876 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3877 }
3878 }
3879 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882/*
3883 * All completion for the +cmdline_compl feature goes here.
3884 */
3885
3886# ifdef FEAT_USR_CMDS
3887 case CMD_command:
3888 /* Check for attributes */
3889 while (*arg == '-')
3890 {
3891 arg++; /* Skip "-" */
3892 p = skiptowhite(arg);
3893 if (*p == NUL)
3894 {
3895 /* Cursor is still in the attribute */
3896 p = vim_strchr(arg, '=');
3897 if (p == NULL)
3898 {
3899 /* No "=", so complete attribute names */
3900 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3901 xp->xp_pattern = arg;
3902 return NULL;
3903 }
3904
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003905 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 * their arguments as well.
3907 */
3908 if (STRNICMP(arg, "complete", p - arg) == 0)
3909 {
3910 xp->xp_context = EXPAND_USER_COMPLETE;
3911 xp->xp_pattern = p + 1;
3912 return NULL;
3913 }
3914 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3915 {
3916 xp->xp_context = EXPAND_USER_NARGS;
3917 xp->xp_pattern = p + 1;
3918 return NULL;
3919 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003920 else if (STRNICMP(arg, "addr", p - arg) == 0)
3921 {
3922 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3923 xp->xp_pattern = p + 1;
3924 return NULL;
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return NULL;
3927 }
3928 arg = skipwhite(p);
3929 }
3930
3931 /* After the attributes comes the new command name */
3932 p = skiptowhite(arg);
3933 if (*p == NUL)
3934 {
3935 xp->xp_context = EXPAND_USER_COMMANDS;
3936 xp->xp_pattern = arg;
3937 break;
3938 }
3939
3940 /* And finally comes a normal command */
3941 return skipwhite(p);
3942
3943 case CMD_delcommand:
3944 xp->xp_context = EXPAND_USER_COMMANDS;
3945 xp->xp_pattern = arg;
3946 break;
3947# endif
3948
3949 case CMD_global:
3950 case CMD_vglobal:
3951 delim = *arg; /* get the delimiter */
3952 if (delim)
3953 ++arg; /* skip delimiter if there is one */
3954
3955 while (arg[0] != NUL && arg[0] != delim)
3956 {
3957 if (arg[0] == '\\' && arg[1] != NUL)
3958 ++arg;
3959 ++arg;
3960 }
3961 if (arg[0] != NUL)
3962 return arg + 1;
3963 break;
3964 case CMD_and:
3965 case CMD_substitute:
3966 delim = *arg;
3967 if (delim)
3968 {
3969 /* skip "from" part */
3970 ++arg;
3971 arg = skip_regexp(arg, delim, p_magic, NULL);
3972 }
3973 /* skip "to" part */
3974 while (arg[0] != NUL && arg[0] != delim)
3975 {
3976 if (arg[0] == '\\' && arg[1] != NUL)
3977 ++arg;
3978 ++arg;
3979 }
3980 if (arg[0] != NUL) /* skip delimiter */
3981 ++arg;
3982 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3983 ++arg;
3984 if (arg[0] != NUL)
3985 return arg;
3986 break;
3987 case CMD_isearch:
3988 case CMD_dsearch:
3989 case CMD_ilist:
3990 case CMD_dlist:
3991 case CMD_ijump:
3992 case CMD_psearch:
3993 case CMD_djump:
3994 case CMD_isplit:
3995 case CMD_dsplit:
3996 arg = skipwhite(skipdigits(arg)); /* skip count */
3997 if (*arg == '/') /* Match regexp, not just whole words */
3998 {
3999 for (++arg; *arg && *arg != '/'; arg++)
4000 if (*arg == '\\' && arg[1] != NUL)
4001 arg++;
4002 if (*arg)
4003 {
4004 arg = skipwhite(arg + 1);
4005
4006 /* Check for trailing illegal characters */
4007 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4008 xp->xp_context = EXPAND_NOTHING;
4009 else
4010 return arg;
4011 }
4012 }
4013 break;
4014#ifdef FEAT_AUTOCMD
4015 case CMD_autocmd:
4016 return set_context_in_autocmd(xp, arg, FALSE);
4017
4018 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004019 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 return set_context_in_autocmd(xp, arg, TRUE);
4021#endif
4022 case CMD_set:
4023 set_context_in_set_cmd(xp, arg, 0);
4024 break;
4025 case CMD_setglobal:
4026 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4027 break;
4028 case CMD_setlocal:
4029 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4030 break;
4031 case CMD_tag:
4032 case CMD_stag:
4033 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004034 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 case CMD_tselect:
4036 case CMD_stselect:
4037 case CMD_ptselect:
4038 case CMD_tjump:
4039 case CMD_stjump:
4040 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004041 if (*p_wop != NUL)
4042 xp->xp_context = EXPAND_TAGS_LISTFILES;
4043 else
4044 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 xp->xp_pattern = arg;
4046 break;
4047 case CMD_augroup:
4048 xp->xp_context = EXPAND_AUGROUP;
4049 xp->xp_pattern = arg;
4050 break;
4051#ifdef FEAT_SYN_HL
4052 case CMD_syntax:
4053 set_context_in_syntax_cmd(xp, arg);
4054 break;
4055#endif
4056#ifdef FEAT_EVAL
4057 case CMD_let:
4058 case CMD_if:
4059 case CMD_elseif:
4060 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004061 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 case CMD_echo:
4063 case CMD_echon:
4064 case CMD_execute:
4065 case CMD_echomsg:
4066 case CMD_echoerr:
4067 case CMD_call:
4068 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004069 case CMD_cexpr:
4070 case CMD_caddexpr:
4071 case CMD_cgetexpr:
4072 case CMD_lexpr:
4073 case CMD_laddexpr:
4074 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004075 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 break;
4077
4078 case CMD_unlet:
4079 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4080 arg = xp->xp_pattern + 1;
4081 xp->xp_context = EXPAND_USER_VARS;
4082 xp->xp_pattern = arg;
4083 break;
4084
4085 case CMD_function:
4086 case CMD_delfunction:
4087 xp->xp_context = EXPAND_USER_FUNC;
4088 xp->xp_pattern = arg;
4089 break;
4090
4091 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004092 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 break;
4094#endif
4095 case CMD_highlight:
4096 set_context_in_highlight_cmd(xp, arg);
4097 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004098#ifdef FEAT_CSCOPE
4099 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004100 case CMD_lcscope:
4101 case CMD_scscope:
4102 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004103 break;
4104#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004105#ifdef FEAT_SIGNS
4106 case CMD_sign:
4107 set_context_in_sign_cmd(xp, arg);
4108 break;
4109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#ifdef FEAT_LISTCMDS
4111 case CMD_bdelete:
4112 case CMD_bwipeout:
4113 case CMD_bunload:
4114 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4115 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004116 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 case CMD_buffer:
4118 case CMD_sbuffer:
4119 case CMD_checktime:
4120 xp->xp_context = EXPAND_BUFFERS;
4121 xp->xp_pattern = arg;
4122 break;
4123#endif
4124#ifdef FEAT_USR_CMDS
4125 case CMD_USER:
4126 case CMD_USER_BUF:
4127 if (compl != EXPAND_NOTHING)
4128 {
4129 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004130 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132# ifdef FEAT_MENU
4133 if (compl == EXPAND_MENUS)
4134 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4135# endif
4136 if (compl == EXPAND_COMMANDS)
4137 return arg;
4138 if (compl == EXPAND_MAPPINGS)
4139 return set_context_in_map_cmd(xp, (char_u *)"map",
4140 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004141 /* Find start of last argument. */
4142 p = arg;
4143 while (*p)
4144 {
4145 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004146 /* argument starts after a space */
4147 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004148 else if (*p == '\\' && *(p + 1) != NUL)
4149 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004150 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 xp->xp_pattern = arg;
4153 }
4154 xp->xp_context = compl;
4155 }
4156 break;
4157#endif
4158 case CMD_map: case CMD_noremap:
4159 case CMD_nmap: case CMD_nnoremap:
4160 case CMD_vmap: case CMD_vnoremap:
4161 case CMD_omap: case CMD_onoremap:
4162 case CMD_imap: case CMD_inoremap:
4163 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004164 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004165 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004166 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004167 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004169 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 case CMD_unmap:
4171 case CMD_nunmap:
4172 case CMD_vunmap:
4173 case CMD_ounmap:
4174 case CMD_iunmap:
4175 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004176 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004177 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004178 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004179 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004181 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004182 case CMD_mapclear:
4183 case CMD_nmapclear:
4184 case CMD_vmapclear:
4185 case CMD_omapclear:
4186 case CMD_imapclear:
4187 case CMD_cmapclear:
4188 case CMD_lmapclear:
4189 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004190 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004191 case CMD_xmapclear:
4192 xp->xp_context = EXPAND_MAPCLEAR;
4193 xp->xp_pattern = arg;
4194 break;
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 case CMD_abbreviate: case CMD_noreabbrev:
4197 case CMD_cabbrev: case CMD_cnoreabbrev:
4198 case CMD_iabbrev: case CMD_inoreabbrev:
4199 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004200 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 case CMD_unabbreviate:
4202 case CMD_cunabbrev:
4203 case CMD_iunabbrev:
4204 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004205 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#ifdef FEAT_MENU
4207 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4208 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4209 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4210 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4211 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4212 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4213 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4214 case CMD_tmenu: case CMD_tunmenu:
4215 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4216 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4217#endif
4218
4219 case CMD_colorscheme:
4220 xp->xp_context = EXPAND_COLORS;
4221 xp->xp_pattern = arg;
4222 break;
4223
4224 case CMD_compiler:
4225 xp->xp_context = EXPAND_COMPILER;
4226 xp->xp_pattern = arg;
4227 break;
4228
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004229 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004230 xp->xp_context = EXPAND_OWNSYNTAX;
4231 xp->xp_pattern = arg;
4232 break;
4233
4234 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004235 xp->xp_context = EXPAND_FILETYPE;
4236 xp->xp_pattern = arg;
4237 break;
4238
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004239 case CMD_packadd:
4240 xp->xp_context = EXPAND_PACKADD;
4241 xp->xp_pattern = arg;
4242 break;
4243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4245 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4246 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004247 p = skiptowhite(arg);
4248 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 xp->xp_context = EXPAND_LANGUAGE;
4251 xp->xp_pattern = arg;
4252 }
4253 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004254 {
4255 if ( STRNCMP(arg, "messages", p - arg) == 0
4256 || STRNCMP(arg, "ctype", p - arg) == 0
4257 || STRNCMP(arg, "time", p - arg) == 0)
4258 {
4259 xp->xp_context = EXPAND_LOCALES;
4260 xp->xp_pattern = skipwhite(p);
4261 }
4262 else
4263 xp->xp_context = EXPAND_NOTHING;
4264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 break;
4266#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004267#if defined(FEAT_PROFILE)
4268 case CMD_profile:
4269 set_context_in_profile_cmd(xp, arg);
4270 break;
4271#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004272 case CMD_behave:
4273 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004274 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004277 case CMD_messages:
4278 xp->xp_context = EXPAND_MESSAGES;
4279 xp->xp_pattern = arg;
4280 break;
4281
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004282#if defined(FEAT_CMDHIST)
4283 case CMD_history:
4284 xp->xp_context = EXPAND_HISTORY;
4285 xp->xp_pattern = arg;
4286 break;
4287#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004288#if defined(FEAT_PROFILE)
4289 case CMD_syntime:
4290 xp->xp_context = EXPAND_SYNTIME;
4291 xp->xp_pattern = arg;
4292 break;
4293#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#endif /* FEAT_CMDL_COMPL */
4296
4297 default:
4298 break;
4299 }
4300 return NULL;
4301}
4302
4303/*
4304 * skip a range specifier of the form: addr [,addr] [;addr] ..
4305 *
4306 * Backslashed delimiters after / or ? will be skipped, and commands will
4307 * not be expanded between /'s and ?'s or after "'".
4308 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004309 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * Returns the "cmd" pointer advanced to beyond the range.
4311 */
4312 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004313skip_range(
4314 char_u *cmd,
4315 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004317 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004319 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004321 if (*cmd == '\\')
4322 {
4323 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4324 ++cmd;
4325 else
4326 break;
4327 }
4328 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 if (*++cmd == NUL && ctx != NULL)
4331 *ctx = EXPAND_NOTHING;
4332 }
4333 else if (*cmd == '/' || *cmd == '?')
4334 {
4335 delim = *cmd++;
4336 while (*cmd != NUL && *cmd != delim)
4337 if (*cmd++ == '\\' && *cmd != NUL)
4338 ++cmd;
4339 if (*cmd == NUL && ctx != NULL)
4340 *ctx = EXPAND_NOTHING;
4341 }
4342 if (*cmd != NUL)
4343 ++cmd;
4344 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004345
4346 /* Skip ":" and white space. */
4347 while (*cmd == ':')
4348 cmd = skipwhite(cmd + 1);
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 return cmd;
4351}
4352
4353/*
4354 * get a single EX address
4355 *
4356 * Set ptr to the next character after the part that was interpreted.
4357 * Set ptr to NULL when an error is encountered.
4358 *
4359 * Return MAXLNUM when no Ex address was found.
4360 */
4361 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004362get_address(
4363 exarg_T *eap UNUSED,
4364 char_u **ptr,
4365 int addr_type, /* flag: one of ADDR_LINES, ... */
4366 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004367 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004368 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 int c;
4371 int i;
4372 long n;
4373 char_u *cmd;
4374 pos_T pos;
4375 pos_T *fp;
4376 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004377 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 cmd = skipwhite(*ptr);
4380 lnum = MAXLNUM;
4381 do
4382 {
4383 switch (*cmd)
4384 {
4385 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 ++cmd;
4387 switch (addr_type)
4388 {
4389 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 lnum = curwin->w_cursor.lnum;
4391 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004393 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 break;
4395 case ADDR_ARGUMENTS:
4396 lnum = curwin->w_arg_idx + 1;
4397 break;
4398 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004399 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 lnum = curbuf->b_fnum;
4401 break;
4402 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004403 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004405 case ADDR_TABS_RELATIVE:
4406 EMSG(_(e_invrange));
4407 cmd = NULL;
4408 goto error;
4409 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004410#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004411 case ADDR_QUICKFIX:
4412 lnum = qf_get_cur_valid_idx(eap);
4413 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004414#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 }
4416 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 ++cmd;
4420 switch (addr_type)
4421 {
4422 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 lnum = curbuf->b_ml.ml_line_count;
4424 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004425 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004426 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 break;
4428 case ADDR_ARGUMENTS:
4429 lnum = ARGCOUNT;
4430 break;
4431 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004432 buf = lastbuf;
4433 while (buf->b_ml.ml_mfp == NULL)
4434 {
4435 if (buf->b_prev == NULL)
4436 break;
4437 buf = buf->b_prev;
4438 }
4439 lnum = buf->b_fnum;
4440 break;
4441 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 lnum = lastbuf->b_fnum;
4443 break;
4444 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004445 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004447 case ADDR_TABS_RELATIVE:
4448 EMSG(_(e_invrange));
4449 cmd = NULL;
4450 goto error;
4451 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004452#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004453 case ADDR_QUICKFIX:
4454 lnum = qf_get_size(eap);
4455 if (lnum == 0)
4456 lnum = 1;
4457 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004458#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 }
4460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 if (*++cmd == NUL)
4464 {
4465 cmd = NULL;
4466 goto error;
4467 }
4468 if (addr_type != ADDR_LINES)
4469 {
4470 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004471 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 goto error;
4473 }
4474 if (skip)
4475 ++cmd;
4476 else
4477 {
4478 /* Only accept a mark in another file when it is
4479 * used by itself: ":'M". */
4480 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4481 ++cmd;
4482 if (fp == (pos_T *)-1)
4483 /* Jumped to another file. */
4484 lnum = curwin->w_cursor.lnum;
4485 else
4486 {
4487 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
4489 cmd = NULL;
4490 goto error;
4491 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 lnum = fp->lnum;
4493 }
4494 }
4495 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 case '/':
4498 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 c = *cmd++;
4500 if (addr_type != ADDR_LINES)
4501 {
4502 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004503 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 goto error;
4505 }
4506 if (skip) /* skip "/pat/" */
4507 {
4508 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4509 if (*cmd == c)
4510 ++cmd;
4511 }
4512 else
4513 {
4514 pos = curwin->w_cursor; /* save curwin->w_cursor */
4515 /*
4516 * When '/' or '?' follows another address, start
4517 * from there.
4518 */
4519 if (lnum != MAXLNUM)
4520 curwin->w_cursor.lnum = lnum;
4521 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004522 * Start a forward search at the end of the line (unless
4523 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004524 * Start a backward search at the start of the line.
4525 * This makes sure we never match in the current
4526 * line, and can match anywhere in the
4527 * next/previous line.
4528 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004529 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 curwin->w_cursor.col = MAXCOL;
4531 else
4532 curwin->w_cursor.col = 0;
4533 searchcmdlen = 0;
4534 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004535 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004536 {
4537 curwin->w_cursor = pos;
4538 cmd = NULL;
4539 goto error;
4540 }
4541 lnum = curwin->w_cursor.lnum;
4542 curwin->w_cursor = pos;
4543 /* adjust command string pointer */
4544 cmd += searchcmdlen;
4545 }
4546 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004549 ++cmd;
4550 if (addr_type != ADDR_LINES)
4551 {
4552 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004553 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 goto error;
4555 }
4556 if (*cmd == '&')
4557 i = RE_SUBST;
4558 else if (*cmd == '?' || *cmd == '/')
4559 i = RE_SEARCH;
4560 else
4561 {
4562 EMSG(_(e_backslash));
4563 cmd = NULL;
4564 goto error;
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 if (!skip)
4568 {
4569 /*
4570 * When search follows another address, start from
4571 * there.
4572 */
4573 if (lnum != MAXLNUM)
4574 pos.lnum = lnum;
4575 else
4576 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 /*
4579 * Start the search just like for the above
4580 * do_search().
4581 */
4582 if (*cmd != '?')
4583 pos.col = MAXCOL;
4584 else
4585 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004586#ifdef FEAT_VIRTUALEDIT
4587 pos.coladd = 0;
4588#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004589 if (searchit(curwin, curbuf, &pos,
4590 *cmd == '?' ? BACKWARD : FORWARD,
4591 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004592 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 lnum = pos.lnum;
4594 else
4595 {
4596 cmd = NULL;
4597 goto error;
4598 }
4599 }
4600 ++cmd;
4601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4605 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607
4608 for (;;)
4609 {
4610 cmd = skipwhite(cmd);
4611 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4612 break;
4613
4614 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 {
4616 switch (addr_type)
4617 {
4618 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004619 /* "+1" is same as ".+1" */
4620 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 break;
4622 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004623 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004624 break;
4625 case ADDR_ARGUMENTS:
4626 lnum = curwin->w_arg_idx + 1;
4627 break;
4628 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004629 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 lnum = curbuf->b_fnum;
4631 break;
4632 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004633 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004635 case ADDR_TABS_RELATIVE:
4636 lnum = 1;
4637 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004638#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004639 case ADDR_QUICKFIX:
4640 lnum = qf_get_cur_valid_idx(eap);
4641 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004642#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 }
4644 }
4645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (VIM_ISDIGIT(*cmd))
4647 i = '+'; /* "number" is same as "+number" */
4648 else
4649 i = *cmd++;
4650 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4651 n = 1;
4652 else
4653 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004654
4655 if (addr_type == ADDR_TABS_RELATIVE)
4656 {
4657 EMSG(_(e_invrange));
4658 cmd = NULL;
4659 goto error;
4660 }
4661 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004662 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004663 lnum = compute_buffer_local_count(
4664 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004666 {
4667#ifdef FEAT_FOLDING
4668 /* Relative line addressing, need to adjust for folded lines
4669 * now, but only do it after the first address. */
4670 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4671 && address_count >= 2)
4672 (void)hasFolding(lnum, NULL, &lnum);
4673#endif
4674 if (i == '-')
4675 lnum -= n;
4676 else
4677 lnum += n;
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680 } while (*cmd == '/' || *cmd == '?');
4681
4682error:
4683 *ptr = cmd;
4684 return lnum;
4685}
4686
4687/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004688 * Get flags from an Ex command argument.
4689 */
4690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004691get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004692{
4693 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4694 {
4695 if (*eap->arg == 'l')
4696 eap->flags |= EXFLAG_LIST;
4697 else if (*eap->arg == 'p')
4698 eap->flags |= EXFLAG_PRINT;
4699 else
4700 eap->flags |= EXFLAG_NR;
4701 eap->arg = skipwhite(eap->arg + 1);
4702 }
4703}
4704
4705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 * Function called for command which is Not Implemented. NI!
4707 */
4708 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004709ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 if (!eap->skip)
4712 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4713}
4714
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004715#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716/*
4717 * Function called for script command which is Not Implemented. NI!
4718 * Skips over ":perl <<EOF" constructs.
4719 */
4720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004721ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 if (!eap->skip)
4724 ex_ni(eap);
4725 else
4726 vim_free(script_get(eap, eap->arg));
4727}
4728#endif
4729
4730/*
4731 * Check range in Ex command for validity.
4732 * Return NULL when valid, error message when invalid.
4733 */
4734 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004737 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if ( eap->line1 < 0
4739 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004740 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004742
4743 if (eap->argt & RANGE)
4744 {
4745 switch(eap->addr_type)
4746 {
4747 case ADDR_LINES:
4748 if (!(eap->argt & NOTADR)
4749 && eap->line2 > curbuf->b_ml.ml_line_count
4750#ifdef FEAT_DIFF
4751 + (eap->cmdidx == CMD_diffget)
4752#endif
4753 )
4754 return (char_u *)_(e_invrange);
4755 break;
4756 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004757 /* add 1 if ARGCOUNT is 0 */
4758 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004759 return (char_u *)_(e_invrange);
4760 break;
4761 case ADDR_BUFFERS:
4762 if (eap->line1 < firstbuf->b_fnum
4763 || eap->line2 > lastbuf->b_fnum)
4764 return (char_u *)_(e_invrange);
4765 break;
4766 case ADDR_LOADED_BUFFERS:
4767 buf = firstbuf;
4768 while (buf->b_ml.ml_mfp == NULL)
4769 {
4770 if (buf->b_next == NULL)
4771 return (char_u *)_(e_invrange);
4772 buf = buf->b_next;
4773 }
4774 if (eap->line1 < buf->b_fnum)
4775 return (char_u *)_(e_invrange);
4776 buf = lastbuf;
4777 while (buf->b_ml.ml_mfp == NULL)
4778 {
4779 if (buf->b_prev == NULL)
4780 return (char_u *)_(e_invrange);
4781 buf = buf->b_prev;
4782 }
4783 if (eap->line2 > buf->b_fnum)
4784 return (char_u *)_(e_invrange);
4785 break;
4786 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004787 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004788 return (char_u *)_(e_invrange);
4789 break;
4790 case ADDR_TABS:
4791 if (eap->line2 > LAST_TAB_NR)
4792 return (char_u *)_(e_invrange);
4793 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004794 case ADDR_TABS_RELATIVE:
4795 /* Do nothing */
4796 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004797#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004798 case ADDR_QUICKFIX:
4799 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4800 return (char_u *)_(e_invrange);
4801 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004802#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004803 }
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return NULL;
4806}
4807
4808/*
4809 * Correct the range for zero line number, if required.
4810 */
4811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004812correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4815 {
4816 if (eap->line1 == 0)
4817 eap->line1 = 1;
4818 if (eap->line2 == 0)
4819 eap->line2 = 1;
4820 }
4821}
4822
Bram Moolenaar748bf032005-02-02 23:04:36 +00004823#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004824static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004825
4826/*
4827 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4828 * pattern. Otherwise return eap->arg.
4829 */
4830 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004831skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004832{
4833 char_u *p = eap->arg;
4834
Bram Moolenaara37420f2006-02-04 22:37:47 +00004835 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4836 || eap->cmdidx == CMD_vimgrepadd
4837 || eap->cmdidx == CMD_lvimgrepadd
4838 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004839 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004840 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004841 if (p == NULL)
4842 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004843 }
4844 return p;
4845}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004846
4847/*
4848 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4849 * in the command line, so that things like % get expanded.
4850 */
4851 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004853{
4854 char_u *new_cmdline;
4855 char_u *program;
4856 char_u *pos;
4857 char_u *ptr;
4858 int len;
4859 int i;
4860
4861 /*
4862 * Don't do it when ":vimgrep" is used for ":grep".
4863 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004864 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4865 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4866 || eap->cmdidx == CMD_grepadd
4867 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004868 && !grep_internal(eap->cmdidx))
4869 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004870 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4871 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004872 {
4873 if (*curbuf->b_p_gp == NUL)
4874 program = p_gp;
4875 else
4876 program = curbuf->b_p_gp;
4877 }
4878 else
4879 {
4880 if (*curbuf->b_p_mp == NUL)
4881 program = p_mp;
4882 else
4883 program = curbuf->b_p_mp;
4884 }
4885
4886 p = skipwhite(p);
4887
4888 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4889 {
4890 /* replace $* by given arguments */
4891 i = 1;
4892 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4893 ++i;
4894 len = (int)STRLEN(p);
4895 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4896 if (new_cmdline == NULL)
4897 return NULL; /* out of memory */
4898 ptr = new_cmdline;
4899 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4900 {
4901 i = (int)(pos - program);
4902 STRNCPY(ptr, program, i);
4903 STRCPY(ptr += i, p);
4904 ptr += len;
4905 program = pos + 2;
4906 }
4907 STRCPY(ptr, program);
4908 }
4909 else
4910 {
4911 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4912 if (new_cmdline == NULL)
4913 return NULL; /* out of memory */
4914 STRCPY(new_cmdline, program);
4915 STRCAT(new_cmdline, " ");
4916 STRCAT(new_cmdline, p);
4917 }
4918 msg_make(p);
4919
4920 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4921 vim_free(*cmdlinep);
4922 *cmdlinep = new_cmdline;
4923 p = new_cmdline;
4924 }
4925 return p;
4926}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004927#endif
4928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929/*
4930 * Expand file name in Ex command argument.
4931 * Return FAIL for failure, OK otherwise.
4932 */
4933 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934expand_filename(
4935 exarg_T *eap,
4936 char_u **cmdlinep,
4937 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 int has_wildcards; /* need to expand wildcards */
4940 char_u *repl;
4941 int srclen;
4942 char_u *p;
4943 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004944 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
Bram Moolenaar748bf032005-02-02 23:04:36 +00004946#ifdef FEAT_QUICKFIX
4947 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4948 p = skip_grep_pat(eap);
4949#else
4950 p = eap->arg;
4951#endif
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 /*
4954 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4955 * the file name contains a wildcard it should not cause expanding.
4956 * (it will be expanded anyway if there is a wildcard before replacing).
4957 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004958 has_wildcards = mch_has_wildcard(p);
4959 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004961#ifdef FEAT_EVAL
4962 /* Skip over `=expr`, wildcards in it are not expanded. */
4963 if (p[0] == '`' && p[1] == '=')
4964 {
4965 p += 2;
4966 (void)skip_expr(&p);
4967 if (*p == '`')
4968 ++p;
4969 continue;
4970 }
4971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /*
4973 * Quick check if this cannot be the start of a special string.
4974 * Also removes backslash before '%', '#' and '<'.
4975 */
4976 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4977 {
4978 ++p;
4979 continue;
4980 }
4981
4982 /*
4983 * Try to find a match at this position.
4984 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004985 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4986 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (*errormsgp != NULL) /* error detected */
4988 return FAIL;
4989 if (repl == NULL) /* no match found */
4990 {
4991 p += srclen;
4992 continue;
4993 }
4994
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004995 /* Wildcards won't be expanded below, the replacement is taken
4996 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4997 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4998 {
4999 char_u *l = repl;
5000
5001 repl = expand_env_save(repl);
5002 vim_free(l);
5003 }
5004
Bram Moolenaar63b92542007-03-27 14:57:09 +00005005 /* Need to escape white space et al. with a backslash.
5006 * Don't do this for:
5007 * - replacement that already has been escaped: "##"
5008 * - shell commands (may have to use quotes instead).
5009 * - non-unix systems when there is a single argument (spaces don't
5010 * separate arguments then).
5011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005013 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 && eap->cmdidx != CMD_grep
5016 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005017 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005018 && eap->cmdidx != CMD_lgrep
5019 && eap->cmdidx != CMD_lgrepadd
5020 && eap->cmdidx != CMD_lmake
5021 && eap->cmdidx != CMD_make
5022 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#ifndef UNIX
5024 && !(eap->argt & NOSPC)
5025#endif
5026 )
5027 {
5028 char_u *l;
5029#ifdef BACKSLASH_IN_FILENAME
5030 /* Don't escape a backslash here, because rem_backslash() doesn't
5031 * remove it later. */
5032 static char_u *nobslash = (char_u *)" \t\"|";
5033# define ESCAPE_CHARS nobslash
5034#else
5035# define ESCAPE_CHARS escape_chars
5036#endif
5037
5038 for (l = repl; *l; ++l)
5039 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5040 {
5041 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5042 if (l != NULL)
5043 {
5044 vim_free(repl);
5045 repl = l;
5046 }
5047 break;
5048 }
5049 }
5050
5051 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005052 if ((eap->usefilter || eap->cmdidx == CMD_bang
5053 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005054 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 char_u *l;
5057
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005058 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (l != NULL)
5060 {
5061 vim_free(repl);
5062 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 }
5065
5066 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5067 vim_free(repl);
5068 if (p == NULL)
5069 return FAIL;
5070 }
5071
5072 /*
5073 * One file argument: Expand wildcards.
5074 * Don't do this with ":r !command" or ":w !command".
5075 */
5076 if ((eap->argt & NOSPC) && !eap->usefilter)
5077 {
5078 /*
5079 * May do this twice:
5080 * 1. Replace environment variables.
5081 * 2. Replace any other wildcards, remove backslashes.
5082 */
5083 for (n = 1; n <= 2; ++n)
5084 {
5085 if (n == 2)
5086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /*
5088 * Halve the number of backslashes (this is Vi compatible).
5089 * For Unix and OS/2, when wildcards are expanded, this is
5090 * done by ExpandOne() below.
5091 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005092#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 if (!has_wildcards)
5094#endif
5095 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097
5098 if (has_wildcards)
5099 {
5100 if (n == 1)
5101 {
5102 /*
5103 * First loop: May expand environment variables. This
5104 * can be done much faster with expand_env() than with
5105 * something else (e.g., calling a shell).
5106 * After expanding environment variables, check again
5107 * if there are still wildcards present.
5108 */
5109 if (vim_strchr(eap->arg, '$') != NULL
5110 || vim_strchr(eap->arg, '~') != NULL)
5111 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005112 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005113 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 has_wildcards = mch_has_wildcard(NameBuff);
5115 p = NameBuff;
5116 }
5117 else
5118 p = NULL;
5119 }
5120 else /* n == 2 */
5121 {
5122 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005123 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
5125 ExpandInit(&xpc);
5126 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005127 if (p_wic)
5128 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005130 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (p == NULL)
5132 return FAIL;
5133 }
5134 if (p != NULL)
5135 {
5136 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5137 p, cmdlinep);
5138 if (n == 2) /* p came from ExpandOne() */
5139 vim_free(p);
5140 }
5141 }
5142 }
5143 }
5144 return OK;
5145}
5146
5147/*
5148 * Replace part of the command line, keeping eap->cmd, eap->arg and
5149 * eap->nextcmd correct.
5150 * "src" points to the part that is to be replaced, of length "srclen".
5151 * "repl" is the replacement string.
5152 * Returns a pointer to the character after the replaced string.
5153 * Returns NULL for failure.
5154 */
5155 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005156repl_cmdline(
5157 exarg_T *eap,
5158 char_u *src,
5159 int srclen,
5160 char_u *repl,
5161 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162{
5163 int len;
5164 int i;
5165 char_u *new_cmdline;
5166
5167 /*
5168 * The new command line is build in new_cmdline[].
5169 * First allocate it.
5170 * Careful: a "+cmd" argument may have been NUL terminated.
5171 */
5172 len = (int)STRLEN(repl);
5173 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005174 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5176 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5177 return NULL; /* out of memory! */
5178
5179 /*
5180 * Copy the stuff before the expanded part.
5181 * Copy the expanded stuff.
5182 * Copy what came after the expanded part.
5183 * Copy the next commands, if there are any.
5184 */
5185 i = (int)(src - *cmdlinep); /* length of part before match */
5186 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 mch_memmove(new_cmdline + i, repl, (size_t)len);
5189 i += len; /* remember the end of the string */
5190 STRCPY(new_cmdline + i, src + srclen);
5191 src = new_cmdline + i; /* remember where to continue */
5192
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005193 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 i = (int)STRLEN(new_cmdline) + 1;
5196 STRCPY(new_cmdline + i, eap->nextcmd);
5197 eap->nextcmd = new_cmdline + i;
5198 }
5199 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5200 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5201 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5202 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5203 vim_free(*cmdlinep);
5204 *cmdlinep = new_cmdline;
5205
5206 return src;
5207}
5208
5209/*
5210 * Check for '|' to separate commands and '"' to start comments.
5211 */
5212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005213separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
5215 char_u *p;
5216
Bram Moolenaar86b68352004-12-27 21:59:20 +00005217#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005218 p = skip_grep_pat(eap);
5219#else
5220 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221#endif
5222
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005223 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 if (*p == Ctrl_V)
5226 {
5227 if (eap->argt & (USECTRLV | XFILE))
5228 ++p; /* skip CTRL-V and next char */
5229 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005230 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005231 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (*p == NUL) /* stop at NUL after CTRL-V */
5233 break;
5234 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005235
5236#ifdef FEAT_EVAL
5237 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005238 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005239 {
5240 p += 2;
5241 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005242 }
5243#endif
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /* Check for '"': start of comment or '|': next command */
5246 /* :@" and :*" do not start a comment!
5247 * :redir @" doesn't either. */
5248 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5249 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5250 || p != eap->arg)
5251 && (eap->cmdidx != CMD_redir
5252 || p != eap->arg + 1 || p[-1] != '@'))
5253 || *p == '|' || *p == '\n')
5254 {
5255 /*
5256 * We remove the '\' before the '|', unless USECTRLV is used
5257 * AND 'b' is present in 'cpoptions'.
5258 */
5259 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5260 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5261 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005262 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 --p;
5264 }
5265 else
5266 {
5267 eap->nextcmd = check_nextcmd(p);
5268 *p = NUL;
5269 break;
5270 }
5271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5275 del_trailing_spaces(eap->arg);
5276}
5277
5278/*
5279 * get + command from ex argument
5280 */
5281 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005282getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
5284 char_u *arg = *argp;
5285 char_u *command = NULL;
5286
5287 if (*arg == '+') /* +[command] */
5288 {
5289 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005290 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 command = dollar_command;
5292 else
5293 {
5294 command = arg;
5295 arg = skip_cmd_arg(command, TRUE);
5296 if (*arg != NUL)
5297 *arg++ = NUL; /* terminate command with NUL */
5298 }
5299
5300 arg = skipwhite(arg); /* skip over spaces */
5301 *argp = arg;
5302 }
5303 return command;
5304}
5305
5306/*
5307 * Find end of "+command" argument. Skip over "\ " and "\\".
5308 */
5309 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005310skip_cmd_arg(
5311 char_u *p,
5312 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314 while (*p && !vim_isspace(*p))
5315 {
5316 if (*p == '\\' && p[1] != NUL)
5317 {
5318 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005319 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 else
5321 ++p;
5322 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005323 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 }
5325 return p;
5326}
5327
5328/*
5329 * Get "++opt=arg" argument.
5330 * Return FAIL or OK.
5331 */
5332 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005333getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
5335 char_u *arg = eap->arg + 2;
5336 int *pp = NULL;
5337#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005338 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u *p;
5340#endif
5341
5342 /* ":edit ++[no]bin[ary] file" */
5343 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5344 {
5345 if (*arg == 'n')
5346 {
5347 arg += 2;
5348 eap->force_bin = FORCE_NOBIN;
5349 }
5350 else
5351 eap->force_bin = FORCE_BIN;
5352 if (!checkforcmd(&arg, "binary", 3))
5353 return FAIL;
5354 eap->arg = skipwhite(arg);
5355 return OK;
5356 }
5357
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005358 /* ":read ++edit file" */
5359 if (STRNCMP(arg, "edit", 4) == 0)
5360 {
5361 eap->read_edit = TRUE;
5362 eap->arg = skipwhite(arg + 4);
5363 return OK;
5364 }
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (STRNCMP(arg, "ff", 2) == 0)
5367 {
5368 arg += 2;
5369 pp = &eap->force_ff;
5370 }
5371 else if (STRNCMP(arg, "fileformat", 10) == 0)
5372 {
5373 arg += 10;
5374 pp = &eap->force_ff;
5375 }
5376#ifdef FEAT_MBYTE
5377 else if (STRNCMP(arg, "enc", 3) == 0)
5378 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005379 if (STRNCMP(arg, "encoding", 8) == 0)
5380 arg += 8;
5381 else
5382 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 pp = &eap->force_enc;
5384 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005385 else if (STRNCMP(arg, "bad", 3) == 0)
5386 {
5387 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005388 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#endif
5391
5392 if (pp == NULL || *arg != '=')
5393 return FAIL;
5394
5395 ++arg;
5396 *pp = (int)(arg - eap->cmd);
5397 arg = skip_cmd_arg(arg, FALSE);
5398 eap->arg = skipwhite(arg);
5399 *arg = NUL;
5400
5401#ifdef FEAT_MBYTE
5402 if (pp == &eap->force_ff)
5403 {
5404#endif
5405 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5406 return FAIL;
5407#ifdef FEAT_MBYTE
5408 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005409 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
5411 /* Make 'fileencoding' lower case. */
5412 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5413 *p = TOLOWER_ASC(*p);
5414 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005415 else
5416 {
5417 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5418 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005419 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005420 if (STRICMP(p, "keep") == 0)
5421 eap->bad_char = BAD_KEEP;
5422 else if (STRICMP(p, "drop") == 0)
5423 eap->bad_char = BAD_DROP;
5424 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5425 eap->bad_char = *p;
5426 else
5427 return FAIL;
5428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#endif
5430
5431 return OK;
5432}
5433
5434/*
5435 * ":abbreviate" and friends.
5436 */
5437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005438ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 do_exmap(eap, TRUE); /* almost the same as mapping */
5441}
5442
5443/*
5444 * ":map" and friends.
5445 */
5446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 /*
5450 * If we are sourcing .exrc or .vimrc in current directory we
5451 * print the mappings for security reasons.
5452 */
5453 if (secure)
5454 {
5455 secure = 2;
5456 msg_outtrans(eap->cmd);
5457 msg_putchar('\n');
5458 }
5459 do_exmap(eap, FALSE);
5460}
5461
5462/*
5463 * ":unmap" and friends.
5464 */
5465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 do_exmap(eap, FALSE);
5469}
5470
5471/*
5472 * ":mapclear" and friends.
5473 */
5474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005475ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5478}
5479
5480/*
5481 * ":abclear" and friends.
5482 */
5483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5487}
5488
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005489#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 /*
5494 * Disallow auto commands from .exrc and .vimrc in current
5495 * directory for security reasons.
5496 */
5497 if (secure)
5498 {
5499 secure = 2;
5500 eap->errmsg = e_curdir;
5501 }
5502 else if (eap->cmdidx == CMD_autocmd)
5503 do_autocmd(eap->arg, eap->forceit);
5504 else
5505 do_augroup(eap->arg, eap->forceit);
5506}
5507
5508/*
5509 * ":doautocmd": Apply the automatic commands to the current buffer.
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005514 char_u *arg = eap->arg;
5515 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005516 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005517
Bram Moolenaar1610d052016-06-09 22:53:01 +02005518 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5519 /* Only when there is no <nomodeline>. */
5520 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005521 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
5523#endif
5524
5525#ifdef FEAT_LISTCMDS
5526/*
5527 * :[N]bunload[!] [N] [bufname] unload buffer
5528 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5529 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5530 */
5531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005532ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 eap->errmsg = do_bufdel(
5535 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5536 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5537 : DOBUF_UNLOAD, eap->arg,
5538 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5539}
5540
5541/*
5542 * :[N]buffer [N] to buffer N
5543 * :[N]sbuffer [N] to buffer N
5544 */
5545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 if (*eap->arg)
5549 eap->errmsg = e_trailing;
5550 else
5551 {
5552 if (eap->addr_count == 0) /* default is current buffer */
5553 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5554 else
5555 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005556 if (eap->do_ecmd_cmd != NULL)
5557 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
5559}
5560
5561/*
5562 * :[N]bmodified [N] to next mod. buffer
5563 * :[N]sbmodified [N] to next mod. buffer
5564 */
5565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005566ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005569 if (eap->do_ecmd_cmd != NULL)
5570 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571}
5572
5573/*
5574 * :[N]bnext [N] to next buffer
5575 * :[N]sbnext [N] split and to next buffer
5576 */
5577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005581 if (eap->do_ecmd_cmd != NULL)
5582 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583}
5584
5585/*
5586 * :[N]bNext [N] to previous buffer
5587 * :[N]bprevious [N] to previous buffer
5588 * :[N]sbNext [N] split and to previous buffer
5589 * :[N]sbprevious [N] split and to previous buffer
5590 */
5591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005595 if (eap->do_ecmd_cmd != NULL)
5596 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
5599/*
5600 * :brewind to first buffer
5601 * :bfirst to first buffer
5602 * :sbrewind split and to first buffer
5603 * :sbfirst split and to first buffer
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005609 if (eap->do_ecmd_cmd != NULL)
5610 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611}
5612
5613/*
5614 * :blast to last buffer
5615 * :sblast split and to last buffer
5616 */
5617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
5620 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005621 if (eap->do_ecmd_cmd != NULL)
5622 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623}
5624#endif
5625
5626 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005627ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 return (c == NUL || c == '|' || c == '"' || c == '\n');
5630}
5631
5632#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5633 || defined(PROTO)
5634/*
5635 * Return the next command, after the first '|' or '\n'.
5636 * Return NULL if not found.
5637 */
5638 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 while (*p != '|' && *p != '\n')
5642 {
5643 if (*p == NUL)
5644 return NULL;
5645 ++p;
5646 }
5647 return (p + 1);
5648}
5649#endif
5650
5651/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005652 * Check if *p is a separator between Ex commands, skipping over white space.
5653 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 */
5655 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005658 char_u *s = skipwhite(p);
5659
5660 if (*s == '|' || *s == '\n')
5661 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 else
5663 return NULL;
5664}
5665
5666/*
5667 * - if there are more files to edit
5668 * - and this is the last window
5669 * - and forceit not used
5670 * - and not repeated twice on a row
5671 * return FAIL and give error message if 'message' TRUE
5672 * return OK otherwise
5673 */
5674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675check_more(
5676 int message, /* when FALSE check only, no messages */
5677 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
5679 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5680
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005681 if (!forceit && only_one_window()
5682 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
5684 if (message)
5685 {
5686#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5687 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5688 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005689 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005692 vim_strncpy(buff,
5693 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005694 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005696 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 _("%d more files to edit. Quit anyway?"), n);
5698 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5699 return OK;
5700 return FAIL;
5701 }
5702#endif
5703 if (n == 1)
5704 EMSG(_("E173: 1 more file to edit"));
5705 else
5706 EMSGN(_("E173: %ld more files to edit"), n);
5707 quitmore = 2; /* next try to quit is allowed */
5708 }
5709 return FAIL;
5710 }
5711 return OK;
5712}
5713
5714#ifdef FEAT_CMDL_COMPL
5715/*
5716 * Function given to ExpandGeneric() to obtain the list of command names.
5717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005719get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 if (idx >= (int)CMD_SIZE)
5722# ifdef FEAT_USR_CMDS
5723 return get_user_command_name(idx);
5724# else
5725 return NULL;
5726# endif
5727 return cmdnames[idx].cmd_name;
5728}
5729#endif
5730
5731#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005732static 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);
5733static void uc_list(char_u *name, size_t name_len);
5734static 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);
5735static char_u *uc_split_args(char_u *arg, size_t *lenp);
5736static 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 +00005737
5738 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739uc_add_command(
5740 char_u *name,
5741 size_t name_len,
5742 char_u *rep,
5743 long argt,
5744 long def,
5745 int flags,
5746 int compl,
5747 char_u *compl_arg,
5748 int addr_type,
5749 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 ucmd_T *cmd = NULL;
5752 char_u *p;
5753 int i;
5754 int cmp = 1;
5755 char_u *rep_buf = NULL;
5756 garray_T *gap;
5757
Bram Moolenaar9c102382006-05-03 21:26:49 +00005758 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 if (rep_buf == NULL)
5760 {
5761 /* Can't replace termcodes - try using the string as is */
5762 rep_buf = vim_strsave(rep);
5763
5764 /* Give up if out of memory */
5765 if (rep_buf == NULL)
5766 return FAIL;
5767 }
5768
5769 /* get address of growarray: global or in curbuf */
5770 if (flags & UC_BUFFER)
5771 {
5772 gap = &curbuf->b_ucmds;
5773 if (gap->ga_itemsize == 0)
5774 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5775 }
5776 else
5777 gap = &ucmds;
5778
5779 /* Search for the command in the already defined commands. */
5780 for (i = 0; i < gap->ga_len; ++i)
5781 {
5782 size_t len;
5783
5784 cmd = USER_CMD_GA(gap, i);
5785 len = STRLEN(cmd->uc_name);
5786 cmp = STRNCMP(name, cmd->uc_name, name_len);
5787 if (cmp == 0)
5788 {
5789 if (name_len < len)
5790 cmp = -1;
5791 else if (name_len > len)
5792 cmp = 1;
5793 }
5794
5795 if (cmp == 0)
5796 {
5797 if (!force)
5798 {
5799 EMSG(_("E174: Command already exists: add ! to replace it"));
5800 goto fail;
5801 }
5802
Bram Moolenaard23a8232018-02-10 18:45:26 +01005803 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005804#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005805 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 break;
5808 }
5809
5810 /* Stop as soon as we pass the name to add */
5811 if (cmp < 0)
5812 break;
5813 }
5814
5815 /* Extend the array unless we're replacing an existing command */
5816 if (cmp != 0)
5817 {
5818 if (ga_grow(gap, 1) != OK)
5819 goto fail;
5820 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5821 goto fail;
5822
5823 cmd = USER_CMD_GA(gap, i);
5824 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5825
5826 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
5828 cmd->uc_name = p;
5829 }
5830
5831 cmd->uc_rep = rep_buf;
5832 cmd->uc_argt = argt;
5833 cmd->uc_def = def;
5834 cmd->uc_compl = compl;
5835#ifdef FEAT_EVAL
5836 cmd->uc_scriptID = current_SID;
5837# ifdef FEAT_CMDL_COMPL
5838 cmd->uc_compl_arg = compl_arg;
5839# endif
5840#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005841 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
5843 return OK;
5844
5845fail:
5846 vim_free(rep_buf);
5847#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5848 vim_free(compl_arg);
5849#endif
5850 return FAIL;
5851}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005854#if defined(FEAT_USR_CMDS)
5855static struct
5856{
5857 int expand;
5858 char *name;
5859} addr_type_complete[] =
5860{
5861 {ADDR_ARGUMENTS, "arguments"},
5862 {ADDR_LINES, "lines"},
5863 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5864 {ADDR_TABS, "tabs"},
5865 {ADDR_BUFFERS, "buffers"},
5866 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005867 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005868 {-1, NULL}
5869};
5870#endif
5871
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005872#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873/*
5874 * List of names for completion for ":command" with the EXPAND_ flag.
5875 * Must be alphabetical for completion.
5876 */
5877static struct
5878{
5879 int expand;
5880 char *name;
5881} command_complete[] =
5882{
5883 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005884 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005886 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005888 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005889#if defined(FEAT_CSCOPE)
5890 {EXPAND_CSCOPE, "cscope"},
5891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5893 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005894 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#endif
5896 {EXPAND_DIRECTORIES, "dir"},
5897 {EXPAND_ENV_VARS, "environment"},
5898 {EXPAND_EVENTS, "event"},
5899 {EXPAND_EXPRESSION, "expression"},
5900 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005901 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005902 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {EXPAND_FUNCTIONS, "function"},
5904 {EXPAND_HELP, "help"},
5905 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005906#if defined(FEAT_CMDHIST)
5907 {EXPAND_HISTORY, "history"},
5908#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005909#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005910 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005911 {EXPAND_LOCALES, "locale"},
5912#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005913 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {EXPAND_MAPPINGS, "mapping"},
5915 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005916 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005917 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005918#if defined(FEAT_PROFILE)
5919 {EXPAND_SYNTIME, "syntime"},
5920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005922 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005923 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005924#if defined(FEAT_SIGNS)
5925 {EXPAND_SIGN, "sign"},
5926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {EXPAND_TAGS, "tag"},
5928 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005929 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {EXPAND_USER_VARS, "var"},
5931 {0, NULL}
5932};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005935#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005937uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 int i, j;
5940 int found = FALSE;
5941 ucmd_T *cmd;
5942 int len;
5943 long a;
5944 garray_T *gap;
5945
5946 gap = &curbuf->b_ucmds;
5947 for (;;)
5948 {
5949 for (i = 0; i < gap->ga_len; ++i)
5950 {
5951 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005952 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaard29459b2016-08-26 22:29:11 +02005954 /* Skip commands which don't match the requested prefix and
5955 * commands filtered out. */
5956 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5957 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 continue;
5959
5960 /* Put out the title first time */
5961 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005962 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 found = TRUE;
5964 msg_putchar('\n');
5965 if (got_int)
5966 break;
5967
5968 /* Special cases */
5969 msg_putchar(a & BANG ? '!' : ' ');
5970 msg_putchar(a & REGSTR ? '"' : ' ');
5971 msg_putchar(gap != &ucmds ? 'b' : ' ');
5972 msg_putchar(' ');
5973
Bram Moolenaar8820b482017-03-16 17:23:31 +01005974 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 len = (int)STRLEN(cmd->uc_name) + 4;
5976
5977 do {
5978 msg_putchar(' ');
5979 ++len;
5980 } while (len < 16);
5981
5982 len = 0;
5983
5984 /* Arguments */
5985 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5986 {
5987 case 0: IObuff[len++] = '0'; break;
5988 case (EXTRA): IObuff[len++] = '*'; break;
5989 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5990 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5991 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5992 }
5993
5994 do {
5995 IObuff[len++] = ' ';
5996 } while (len < 5);
5997
5998 /* Range */
5999 if (a & (RANGE|COUNT))
6000 {
6001 if (a & COUNT)
6002 {
6003 /* -count=N */
6004 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6005 len += (int)STRLEN(IObuff + len);
6006 }
6007 else if (a & DFLALL)
6008 IObuff[len++] = '%';
6009 else if (cmd->uc_def >= 0)
6010 {
6011 /* -range=N */
6012 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6013 len += (int)STRLEN(IObuff + len);
6014 }
6015 else
6016 IObuff[len++] = '.';
6017 }
6018
6019 do {
6020 IObuff[len++] = ' ';
6021 } while (len < 11);
6022
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006023 /* Address Type */
6024 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6025 if (addr_type_complete[j].expand != ADDR_LINES
6026 && addr_type_complete[j].expand == cmd->uc_addr_type)
6027 {
6028 STRCPY(IObuff + len, addr_type_complete[j].name);
6029 len += (int)STRLEN(IObuff + len);
6030 break;
6031 }
6032
6033 do {
6034 IObuff[len++] = ' ';
6035 } while (len < 21);
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 /* Completion */
6038 for (j = 0; command_complete[j].expand != 0; ++j)
6039 if (command_complete[j].expand == cmd->uc_compl)
6040 {
6041 STRCPY(IObuff + len, command_complete[j].name);
6042 len += (int)STRLEN(IObuff + len);
6043 break;
6044 }
6045
6046 do {
6047 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006048 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 IObuff[len] = '\0';
6051 msg_outtrans(IObuff);
6052
6053 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006054#ifdef FEAT_EVAL
6055 if (p_verbose > 0)
6056 last_set_msg(cmd->uc_scriptID);
6057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 out_flush();
6059 ui_breakcheck();
6060 if (got_int)
6061 break;
6062 }
6063 if (gap == &ucmds || i < gap->ga_len)
6064 break;
6065 gap = &ucmds;
6066 }
6067
6068 if (!found)
6069 MSG(_("No user-defined commands found"));
6070}
6071
6072 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
6075 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6076 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6077 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6078 0xb9, 0x7f, 0};
6079 int i;
6080
6081 for (i = 0; fcmd[i]; ++i)
6082 IObuff[i] = fcmd[i] - 0x40;
6083 IObuff[i] = 0;
6084 return IObuff;
6085}
6086
6087 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006088uc_scan_attr(
6089 char_u *attr,
6090 size_t len,
6091 long *argt,
6092 long *def,
6093 int *flags,
6094 int *compl,
6095 char_u **compl_arg,
6096 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 char_u *p;
6099
6100 if (len == 0)
6101 {
6102 EMSG(_("E175: No attribute specified"));
6103 return FAIL;
6104 }
6105
6106 /* First, try the simple attributes (no arguments) */
6107 if (STRNICMP(attr, "bang", len) == 0)
6108 *argt |= BANG;
6109 else if (STRNICMP(attr, "buffer", len) == 0)
6110 *flags |= UC_BUFFER;
6111 else if (STRNICMP(attr, "register", len) == 0)
6112 *argt |= REGSTR;
6113 else if (STRNICMP(attr, "bar", len) == 0)
6114 *argt |= TRLBAR;
6115 else
6116 {
6117 int i;
6118 char_u *val = NULL;
6119 size_t vallen = 0;
6120 size_t attrlen = len;
6121
6122 /* Look for the attribute name - which is the part before any '=' */
6123 for (i = 0; i < (int)len; ++i)
6124 {
6125 if (attr[i] == '=')
6126 {
6127 val = &attr[i + 1];
6128 vallen = len - i - 1;
6129 attrlen = i;
6130 break;
6131 }
6132 }
6133
6134 if (STRNICMP(attr, "nargs", attrlen) == 0)
6135 {
6136 if (vallen == 1)
6137 {
6138 if (*val == '0')
6139 /* Do nothing - this is the default */;
6140 else if (*val == '1')
6141 *argt |= (EXTRA | NOSPC | NEEDARG);
6142 else if (*val == '*')
6143 *argt |= EXTRA;
6144 else if (*val == '?')
6145 *argt |= (EXTRA | NOSPC);
6146 else if (*val == '+')
6147 *argt |= (EXTRA | NEEDARG);
6148 else
6149 goto wrong_nargs;
6150 }
6151 else
6152 {
6153wrong_nargs:
6154 EMSG(_("E176: Invalid number of arguments"));
6155 return FAIL;
6156 }
6157 }
6158 else if (STRNICMP(attr, "range", attrlen) == 0)
6159 {
6160 *argt |= RANGE;
6161 if (vallen == 1 && *val == '%')
6162 *argt |= DFLALL;
6163 else if (val != NULL)
6164 {
6165 p = val;
6166 if (*def >= 0)
6167 {
6168two_count:
6169 EMSG(_("E177: Count cannot be specified twice"));
6170 return FAIL;
6171 }
6172
6173 *def = getdigits(&p);
6174 *argt |= (ZEROR | NOTADR);
6175
6176 if (p != val + vallen || vallen == 0)
6177 {
6178invalid_count:
6179 EMSG(_("E178: Invalid default value for count"));
6180 return FAIL;
6181 }
6182 }
6183 }
6184 else if (STRNICMP(attr, "count", attrlen) == 0)
6185 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006186 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 if (val != NULL)
6189 {
6190 p = val;
6191 if (*def >= 0)
6192 goto two_count;
6193
6194 *def = getdigits(&p);
6195
6196 if (p != val + vallen)
6197 goto invalid_count;
6198 }
6199
6200 if (*def < 0)
6201 *def = 0;
6202 }
6203 else if (STRNICMP(attr, "complete", attrlen) == 0)
6204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 if (val == NULL)
6206 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006207 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 return FAIL;
6209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006211 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6212 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006215 else if (STRNICMP(attr, "addr", attrlen) == 0)
6216 {
6217 *argt |= RANGE;
6218 if (val == NULL)
6219 {
6220 EMSG(_("E179: argument required for -addr"));
6221 return FAIL;
6222 }
6223 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6224 == FAIL)
6225 return FAIL;
6226 if (addr_type_arg != ADDR_LINES)
6227 *argt |= (ZEROR | NOTADR) ;
6228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 else
6230 {
6231 char_u ch = attr[len];
6232 attr[len] = '\0';
6233 EMSG2(_("E181: Invalid attribute: %s"), attr);
6234 attr[len] = ch;
6235 return FAIL;
6236 }
6237 }
6238
6239 return OK;
6240}
6241
Bram Moolenaara850a712009-01-28 14:42:59 +00006242/*
6243 * ":command ..."
6244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006246ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 char_u *name;
6249 char_u *end;
6250 char_u *p;
6251 long argt = 0;
6252 long def = -1;
6253 int flags = 0;
6254 int compl = EXPAND_NOTHING;
6255 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006256 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006258 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
6260 p = eap->arg;
6261
6262 /* Check for attributes */
6263 while (*p == '-')
6264 {
6265 ++p;
6266 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006267 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6268 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 == FAIL)
6270 return;
6271 p = skipwhite(end);
6272 }
6273
6274 /* Get the name (if any) and skip to the following argument */
6275 name = p;
6276 if (ASCII_ISALPHA(*p))
6277 while (ASCII_ISALNUM(*p))
6278 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006279 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
6281 EMSG(_("E182: Invalid command name"));
6282 return;
6283 }
6284 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006285 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
6287 /* If there is nothing after the name, and no attributes were specified,
6288 * we are listing commands
6289 */
6290 p = skipwhite(end);
6291 if (!has_attr && ends_excmd(*p))
6292 {
6293 uc_list(name, end - name);
6294 }
6295 else if (!ASCII_ISUPPER(*name))
6296 {
6297 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6298 return;
6299 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006300 else if ((name_len == 1 && *name == 'X')
6301 || (name_len <= 4
6302 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6303 {
6304 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6305 return;
6306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 else
6308 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006309 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310}
6311
6312/*
6313 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 * Clear all user commands, global and for current buffer.
6315 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006316 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006317ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
6319 uc_clear(&ucmds);
6320 uc_clear(&curbuf->b_ucmds);
6321}
6322
6323/*
6324 * Clear all user commands for "gap".
6325 */
6326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006327uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328{
6329 int i;
6330 ucmd_T *cmd;
6331
6332 for (i = 0; i < gap->ga_len; ++i)
6333 {
6334 cmd = USER_CMD_GA(gap, i);
6335 vim_free(cmd->uc_name);
6336 vim_free(cmd->uc_rep);
6337# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6338 vim_free(cmd->uc_compl_arg);
6339# endif
6340 }
6341 ga_clear(gap);
6342}
6343
6344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006345ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 int i = 0;
6348 ucmd_T *cmd = NULL;
6349 int cmp = -1;
6350 garray_T *gap;
6351
6352 gap = &curbuf->b_ucmds;
6353 for (;;)
6354 {
6355 for (i = 0; i < gap->ga_len; ++i)
6356 {
6357 cmd = USER_CMD_GA(gap, i);
6358 cmp = STRCMP(eap->arg, cmd->uc_name);
6359 if (cmp <= 0)
6360 break;
6361 }
6362 if (gap == &ucmds || cmp == 0)
6363 break;
6364 gap = &ucmds;
6365 }
6366
6367 if (cmp != 0)
6368 {
6369 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6370 return;
6371 }
6372
6373 vim_free(cmd->uc_name);
6374 vim_free(cmd->uc_rep);
6375# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6376 vim_free(cmd->uc_compl_arg);
6377# endif
6378
6379 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
6381 if (i < gap->ga_len)
6382 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6383}
6384
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006385/*
6386 * split and quote args for <f-args>
6387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006389uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391 char_u *buf;
6392 char_u *p;
6393 char_u *q;
6394 int len;
6395
6396 /* Precalculate length */
6397 p = arg;
6398 len = 2; /* Initial and final quotes */
6399
6400 while (*p)
6401 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006402 if (p[0] == '\\' && p[1] == '\\')
6403 {
6404 len += 2;
6405 p += 2;
6406 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006407 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 {
6409 len += 1;
6410 p += 2;
6411 }
6412 else if (*p == '\\' || *p == '"')
6413 {
6414 len += 2;
6415 p += 1;
6416 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006417 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 {
6419 p = skipwhite(p);
6420 if (*p == NUL)
6421 break;
6422 len += 3; /* "," */
6423 }
6424 else
6425 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006426#ifdef FEAT_MBYTE
6427 int charlen = (*mb_ptr2len)(p);
6428 len += charlen;
6429 p += charlen;
6430#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 ++len;
6432 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 }
6435 }
6436
6437 buf = alloc(len + 1);
6438 if (buf == NULL)
6439 {
6440 *lenp = 0;
6441 return buf;
6442 }
6443
6444 p = arg;
6445 q = buf;
6446 *q++ = '"';
6447 while (*p)
6448 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006449 if (p[0] == '\\' && p[1] == '\\')
6450 {
6451 *q++ = '\\';
6452 *q++ = '\\';
6453 p += 2;
6454 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006455 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 {
6457 *q++ = p[1];
6458 p += 2;
6459 }
6460 else if (*p == '\\' || *p == '"')
6461 {
6462 *q++ = '\\';
6463 *q++ = *p++;
6464 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006465 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
6467 p = skipwhite(p);
6468 if (*p == NUL)
6469 break;
6470 *q++ = '"';
6471 *q++ = ',';
6472 *q++ = '"';
6473 }
6474 else
6475 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006476 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
6478 }
6479 *q++ = '"';
6480 *q = 0;
6481
6482 *lenp = len;
6483 return buf;
6484}
6485
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006486 static size_t
6487add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6488{
6489 size_t result;
6490
6491 result = STRLEN(mod_str);
6492 if (*multi_mods)
6493 result += 1;
6494 if (buf != NULL)
6495 {
6496 if (*multi_mods)
6497 STRCAT(buf, " ");
6498 STRCAT(buf, mod_str);
6499 }
6500
6501 *multi_mods = 1;
6502
6503 return result;
6504}
6505
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506/*
6507 * Check for a <> code in a user command.
6508 * "code" points to the '<'. "len" the length of the <> (inclusive).
6509 * "buf" is where the result is to be added.
6510 * "split_buf" points to a buffer used for splitting, caller should free it.
6511 * "split_len" is the length of what "split_buf" contains.
6512 * Returns the length of the replacement, which has been added to "buf".
6513 * Returns -1 if there was no match, and only the "<" has been copied.
6514 */
6515 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006516uc_check_code(
6517 char_u *code,
6518 size_t len,
6519 char_u *buf,
6520 ucmd_T *cmd, /* the user command we're expanding */
6521 exarg_T *eap, /* ex arguments */
6522 char_u **split_buf,
6523 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 size_t result = 0;
6526 char_u *p = code + 1;
6527 size_t l = len - 2;
6528 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006529 enum {
6530 ct_ARGS,
6531 ct_BANG,
6532 ct_COUNT,
6533 ct_LINE1,
6534 ct_LINE2,
6535 ct_RANGE,
6536 ct_MODS,
6537 ct_REGISTER,
6538 ct_LT,
6539 ct_NONE
6540 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6543 {
6544 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6545 p += 2;
6546 l -= 2;
6547 }
6548
Bram Moolenaar371d5402006-03-20 21:47:49 +00006549 ++l;
6550 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006552 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006554 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006556 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006558 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006560 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006562 else if (STRNICMP(p, "range>", l) == 0)
6563 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006564 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006566 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006568 else if (STRNICMP(p, "mods>", l) == 0)
6569 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
6571 switch (type)
6572 {
6573 case ct_ARGS:
6574 /* Simple case first */
6575 if (*eap->arg == NUL)
6576 {
6577 if (quote == 1)
6578 {
6579 result = 2;
6580 if (buf != NULL)
6581 STRCPY(buf, "''");
6582 }
6583 else
6584 result = 0;
6585 break;
6586 }
6587
6588 /* When specified there is a single argument don't split it.
6589 * Works for ":Cmd %" when % is "a b c". */
6590 if ((eap->argt & NOSPC) && quote == 2)
6591 quote = 1;
6592
6593 switch (quote)
6594 {
6595 case 0: /* No quoting, no splitting */
6596 result = STRLEN(eap->arg);
6597 if (buf != NULL)
6598 STRCPY(buf, eap->arg);
6599 break;
6600 case 1: /* Quote, but don't split */
6601 result = STRLEN(eap->arg) + 2;
6602 for (p = eap->arg; *p; ++p)
6603 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006604#ifdef FEAT_MBYTE
6605 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6606 /* DBCS can contain \ in a trail byte, skip the
6607 * double-byte character. */
6608 ++p;
6609 else
6610#endif
6611 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 ++result;
6613 }
6614
6615 if (buf != NULL)
6616 {
6617 *buf++ = '"';
6618 for (p = eap->arg; *p; ++p)
6619 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006620#ifdef FEAT_MBYTE
6621 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6622 /* DBCS can contain \ in a trail byte, copy the
6623 * double-byte character to avoid escaping. */
6624 *buf++ = *p++;
6625 else
6626#endif
6627 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 *buf++ = '\\';
6629 *buf++ = *p;
6630 }
6631 *buf = '"';
6632 }
6633
6634 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006635 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 /* This is hard, so only do it once, and cache the result */
6637 if (*split_buf == NULL)
6638 *split_buf = uc_split_args(eap->arg, split_len);
6639
6640 result = *split_len;
6641 if (buf != NULL && result != 0)
6642 STRCPY(buf, *split_buf);
6643
6644 break;
6645 }
6646 break;
6647
6648 case ct_BANG:
6649 result = eap->forceit ? 1 : 0;
6650 if (quote)
6651 result += 2;
6652 if (buf != NULL)
6653 {
6654 if (quote)
6655 *buf++ = '"';
6656 if (eap->forceit)
6657 *buf++ = '!';
6658 if (quote)
6659 *buf = '"';
6660 }
6661 break;
6662
6663 case ct_LINE1:
6664 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006665 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 case ct_COUNT:
6667 {
6668 char num_buf[20];
6669 long num = (type == ct_LINE1) ? eap->line1 :
6670 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006671 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6673 size_t num_len;
6674
6675 sprintf(num_buf, "%ld", num);
6676 num_len = STRLEN(num_buf);
6677 result = num_len;
6678
6679 if (quote)
6680 result += 2;
6681
6682 if (buf != NULL)
6683 {
6684 if (quote)
6685 *buf++ = '"';
6686 STRCPY(buf, num_buf);
6687 buf += num_len;
6688 if (quote)
6689 *buf = '"';
6690 }
6691
6692 break;
6693 }
6694
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006695 case ct_MODS:
6696 {
6697 int multi_mods = 0;
6698 typedef struct {
6699 int *varp;
6700 char *name;
6701 } mod_entry_T;
6702 static mod_entry_T mod_entries[] = {
6703#ifdef FEAT_BROWSE_CMD
6704 {&cmdmod.browse, "browse"},
6705#endif
6706#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6707 {&cmdmod.confirm, "confirm"},
6708#endif
6709 {&cmdmod.hide, "hide"},
6710 {&cmdmod.keepalt, "keepalt"},
6711 {&cmdmod.keepjumps, "keepjumps"},
6712 {&cmdmod.keepmarks, "keepmarks"},
6713 {&cmdmod.keeppatterns, "keeppatterns"},
6714 {&cmdmod.lockmarks, "lockmarks"},
6715 {&cmdmod.noswapfile, "noswapfile"},
6716 {NULL, NULL}
6717 };
6718 int i;
6719
6720 result = quote ? 2 : 0;
6721 if (buf != NULL)
6722 {
6723 if (quote)
6724 *buf++ = '"';
6725 *buf = '\0';
6726 }
6727
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006728 /* :aboveleft and :leftabove */
6729 if (cmdmod.split & WSP_ABOVE)
6730 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6731 /* :belowright and :rightbelow */
6732 if (cmdmod.split & WSP_BELOW)
6733 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6734 /* :botright */
6735 if (cmdmod.split & WSP_BOT)
6736 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006737
6738 /* the modifiers that are simple flags */
6739 for (i = 0; mod_entries[i].varp != NULL; ++i)
6740 if (*mod_entries[i].varp)
6741 result += add_cmd_modifier(buf, mod_entries[i].name,
6742 &multi_mods);
6743
6744 /* TODO: How to support :noautocmd? */
6745#ifdef HAVE_SANDBOX
6746 /* TODO: How to support :sandbox?*/
6747#endif
6748 /* :silent */
6749 if (msg_silent > 0)
6750 result += add_cmd_modifier(buf,
6751 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006752 /* :tab */
6753 if (cmdmod.tab > 0)
6754 result += add_cmd_modifier(buf, "tab", &multi_mods);
6755 /* :topleft */
6756 if (cmdmod.split & WSP_TOP)
6757 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006758 /* TODO: How to support :unsilent?*/
6759 /* :verbose */
6760 if (p_verbose > 0)
6761 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006762 /* :vertical */
6763 if (cmdmod.split & WSP_VERT)
6764 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006765 if (quote && buf != NULL)
6766 {
6767 buf += result - 2;
6768 *buf = '"';
6769 }
6770 break;
6771 }
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 case ct_REGISTER:
6774 result = eap->regname ? 1 : 0;
6775 if (quote)
6776 result += 2;
6777 if (buf != NULL)
6778 {
6779 if (quote)
6780 *buf++ = '\'';
6781 if (eap->regname)
6782 *buf++ = eap->regname;
6783 if (quote)
6784 *buf = '\'';
6785 }
6786 break;
6787
6788 case ct_LT:
6789 result = 1;
6790 if (buf != NULL)
6791 *buf = '<';
6792 break;
6793
6794 default:
6795 /* Not recognized: just copy the '<' and return -1. */
6796 result = (size_t)-1;
6797 if (buf != NULL)
6798 *buf = '<';
6799 break;
6800 }
6801
6802 return result;
6803}
6804
6805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006806do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
6808 char_u *buf;
6809 char_u *p;
6810 char_u *q;
6811
6812 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006813 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006814 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 size_t len, totlen;
6816
6817 size_t split_len = 0;
6818 char_u *split_buf = NULL;
6819 ucmd_T *cmd;
6820#ifdef FEAT_EVAL
6821 scid_T save_current_SID = current_SID;
6822#endif
6823
6824 if (eap->cmdidx == CMD_USER)
6825 cmd = USER_CMD(eap->useridx);
6826 else
6827 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6828
6829 /*
6830 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006831 * First round: "buf" is NULL, compute length, allocate "buf".
6832 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 */
6834 buf = NULL;
6835 for (;;)
6836 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006837 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006838 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006840
6841 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006843 start = vim_strchr(p, '<');
6844 if (start != NULL)
6845 end = vim_strchr(start + 1, '>');
6846 if (buf != NULL)
6847 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006848 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6849 ;
6850 if (*ksp == K_SPECIAL
6851 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006852 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6853# ifdef FEAT_GUI
6854 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6855# endif
6856 ))
6857 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006858 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006859 * KS_SPECIAL KE_FILLER, like for mappings, but
6860 * do_cmdline() doesn't handle that, so convert it back.
6861 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6862 len = ksp - p;
6863 if (len > 0)
6864 {
6865 mch_memmove(q, p, len);
6866 q += len;
6867 }
6868 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6869 p = ksp + 3;
6870 continue;
6871 }
6872 }
6873
6874 /* break if there no <item> is found */
6875 if (start == NULL || end == NULL)
6876 break;
6877
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 /* Include the '>' */
6879 ++end;
6880
6881 /* Take everything up to the '<' */
6882 len = start - p;
6883 if (buf == NULL)
6884 totlen += len;
6885 else
6886 {
6887 mch_memmove(q, p, len);
6888 q += len;
6889 }
6890
6891 len = uc_check_code(start, end - start, q, cmd, eap,
6892 &split_buf, &split_len);
6893 if (len == (size_t)-1)
6894 {
6895 /* no match, continue after '<' */
6896 p = start + 1;
6897 len = 1;
6898 }
6899 else
6900 p = end;
6901 if (buf == NULL)
6902 totlen += len;
6903 else
6904 q += len;
6905 }
6906 if (buf != NULL) /* second time here, finished */
6907 {
6908 STRCPY(q, p);
6909 break;
6910 }
6911
6912 totlen += STRLEN(p); /* Add on the trailing characters */
6913 buf = alloc((unsigned)(totlen + 1));
6914 if (buf == NULL)
6915 {
6916 vim_free(split_buf);
6917 return;
6918 }
6919 }
6920
6921#ifdef FEAT_EVAL
6922 current_SID = cmd->uc_scriptID;
6923#endif
6924 (void)do_cmdline(buf, eap->getline, eap->cookie,
6925 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6926#ifdef FEAT_EVAL
6927 current_SID = save_current_SID;
6928#endif
6929 vim_free(buf);
6930 vim_free(split_buf);
6931}
6932
6933# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6934 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006935get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6938}
6939
6940/*
6941 * Function given to ExpandGeneric() to obtain the list of user command names.
6942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006944get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945{
6946 if (idx < curbuf->b_ucmds.ga_len)
6947 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6948 idx -= curbuf->b_ucmds.ga_len;
6949 if (idx < ucmds.ga_len)
6950 return USER_CMD(idx)->uc_name;
6951 return NULL;
6952}
6953
6954/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006955 * Function given to ExpandGeneric() to obtain the list of user address type names.
6956 */
6957 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006958get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006959{
6960 return (char_u *)addr_type_complete[idx].name;
6961}
6962
6963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 * Function given to ExpandGeneric() to obtain the list of user command
6965 * attributes.
6966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006968get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
6970 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006971 {"addr", "bang", "bar", "buffer", "complete",
6972 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006974 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 return NULL;
6976 return (char_u *)user_cmd_flags[idx];
6977}
6978
6979/*
6980 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006983get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984{
6985 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6986
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006987 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 return NULL;
6989 return (char_u *)user_cmd_nargs[idx];
6990}
6991
6992/*
6993 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006996get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 return (char_u *)command_complete[idx].name;
6999}
7000# endif /* FEAT_CMDL_COMPL */
7001
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007002/*
7003 * Parse address type argument
7004 */
7005 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007006parse_addr_type_arg(
7007 char_u *value,
7008 int vallen,
7009 long *argt,
7010 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007011{
7012 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007013
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007014 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7015 {
7016 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7017 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7018 if (a && b)
7019 {
7020 *addr_type_arg = addr_type_complete[i].expand;
7021 break;
7022 }
7023 }
7024
7025 if (addr_type_complete[i].expand == -1)
7026 {
7027 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007028
Bram Moolenaar1c465442017-03-12 20:10:05 +01007029 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007030 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007031 err[i] = NUL;
7032 EMSG2(_("E180: Invalid address type value: %s"), err);
7033 return FAIL;
7034 }
7035
7036 if (*addr_type_arg != ADDR_LINES)
7037 *argt |= NOTADR;
7038
7039 return OK;
7040}
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042#endif /* FEAT_USR_CMDS */
7043
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007044#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7045/*
7046 * Parse a completion argument "value[vallen]".
7047 * The detected completion goes in "*complp", argument type in "*argt".
7048 * When there is an argument, for function and user defined completion, it's
7049 * copied to allocated memory and stored in "*compl_arg".
7050 * Returns FAIL if something is wrong.
7051 */
7052 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007053parse_compl_arg(
7054 char_u *value,
7055 int vallen,
7056 int *complp,
7057 long *argt,
7058 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007059{
7060 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007061# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007062 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007063# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007064 int i;
7065 int valend = vallen;
7066
7067 /* Look for any argument part - which is the part after any ',' */
7068 for (i = 0; i < vallen; ++i)
7069 {
7070 if (value[i] == ',')
7071 {
7072 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007073# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007074 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007075# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007076 valend = i;
7077 break;
7078 }
7079 }
7080
7081 for (i = 0; command_complete[i].expand != 0; ++i)
7082 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007083 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007084 && STRNCMP(value, command_complete[i].name, valend) == 0)
7085 {
7086 *complp = command_complete[i].expand;
7087 if (command_complete[i].expand == EXPAND_BUFFERS)
7088 *argt |= BUFNAME;
7089 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7090 || command_complete[i].expand == EXPAND_FILES)
7091 *argt |= XFILE;
7092 break;
7093 }
7094 }
7095
7096 if (command_complete[i].expand == 0)
7097 {
7098 EMSG2(_("E180: Invalid complete value: %s"), value);
7099 return FAIL;
7100 }
7101
7102# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7103 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7104 && arg != NULL)
7105# else
7106 if (arg != NULL)
7107# endif
7108 {
7109 EMSG(_("E468: Completion argument only allowed for custom completion"));
7110 return FAIL;
7111 }
7112
7113# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7114 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7115 && arg == NULL)
7116 {
7117 EMSG(_("E467: Custom completion requires a function argument"));
7118 return FAIL;
7119 }
7120
7121 if (arg != NULL)
7122 *compl_arg = vim_strnsave(arg, (int)arglen);
7123# endif
7124 return OK;
7125}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007126
7127 int
7128cmdcomplete_str_to_type(char_u *complete_str)
7129{
7130 int i;
7131
7132 for (i = 0; command_complete[i].expand != 0; ++i)
7133 if (STRCMP(complete_str, command_complete[i].name) == 0)
7134 return command_complete[i].expand;
7135
7136 return EXPAND_NOTHING;
7137}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007138#endif
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
Bram Moolenaare6850792010-05-14 15:28:44 +02007143 if (*eap->arg == NUL)
7144 {
7145#ifdef FEAT_EVAL
7146 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7147 char_u *p = NULL;
7148
7149 if (expr != NULL)
7150 {
7151 ++emsg_off;
7152 p = eval_to_string(expr, NULL, FALSE);
7153 --emsg_off;
7154 vim_free(expr);
7155 }
7156 if (p != NULL)
7157 {
7158 MSG(p);
7159 vim_free(p);
7160 }
7161 else
7162 MSG("default");
7163#else
7164 MSG(_("unknown"));
7165#endif
7166 }
7167 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007168 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169}
7170
7171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007172ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 if (*eap->arg == NUL && eap->cmd[2] == '!')
7175 MSG(_("Greetings, Vim user!"));
7176 do_highlight(eap->arg, eap->forceit, FALSE);
7177}
7178
7179
7180/*
7181 * Call this function if we thought we were going to exit, but we won't
7182 * (because of an error). May need to restore the terminal mode.
7183 */
7184 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007185not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 exiting = FALSE;
7188 settmode(TMODE_RAW);
7189}
7190
7191/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007192 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 */
7194 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007195ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007197 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007198
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199#ifdef FEAT_CMDWIN
7200 if (cmdwin_type != 0)
7201 {
7202 cmdwin_result = Ctrl_C;
7203 return;
7204 }
7205#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007206 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007207 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007208 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007209 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007210 return;
7211 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007212 if (eap->addr_count > 0)
7213 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007214 int wnr = eap->line2;
7215
7216 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7217 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007218 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007219 }
7220 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007221 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007222
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007223#ifdef FEAT_AUTOCMD
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007224 /* Refuse to quit when locked. */
7225 if (curbuf_locked())
7226 return;
7227 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7228 /* Bail out when autocommands closed the window.
7229 * Refuse to quit when the buffer in the last window is being closed (can
7230 * only happen in autocommands). */
7231 if (!win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007232 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007233 return;
7234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
7236#ifdef FEAT_NETBEANS_INTG
7237 netbeansForcedQuit = eap->forceit;
7238#endif
7239
7240 /*
7241 * If there are more files or windows we won't exit.
7242 */
7243 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7244 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007245 if ((!buf_hide(wp->w_buffer)
7246 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007247 | (eap->forceit ? CCGD_FORCEIT : 0)
7248 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007250 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
7252 not_exiting();
7253 }
7254 else
7255 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007256 /* quit last window
7257 * Note: only_one_window() returns true, even so a help window is
7258 * still open. In that case only quit, if no address has been
7259 * specified. Example:
7260 * :h|wincmd w|1q - don't quit
7261 * :h|wincmd w|q - quit
7262 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007263 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007265 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007266#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007270 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
7272}
7273
7274/*
7275 * ":cquit".
7276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007278ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
7280 getout(1); /* this does not always pass on the exit code to the Manx
7281 compiler. why? */
7282}
7283
7284/*
7285 * ":qall": try to quit all windows
7286 */
7287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007288ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290# ifdef FEAT_CMDWIN
7291 if (cmdwin_type != 0)
7292 {
7293 if (eap->forceit)
7294 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7295 else
7296 cmdwin_result = K_XF2;
7297 return;
7298 }
7299# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007300
7301 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007302 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007303 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007304 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007305 return;
7306 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007307#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007308 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7309 /* Refuse to quit when locked or when the buffer in the last window is
7310 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007311 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007312 return;
7313#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007316 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 getout(0);
7318 not_exiting();
7319}
7320
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321/*
7322 * ":close": close current window, unless it is the last one
7323 */
7324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007325ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007327 win_T *win;
7328 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007329#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007331 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007333#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007334 if (!text_locked()
7335#ifdef FEAT_AUTOCMD
7336 && !curbuf_locked()
7337#endif
7338 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007339 {
7340 if (eap->addr_count == 0)
7341 ex_win_close(eap->forceit, curwin, NULL);
7342 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007343 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007344 {
7345 winnr++;
7346 if (winnr == eap->line2)
7347 break;
7348 }
7349 if (win == NULL)
7350 win = lastwin;
7351 ex_win_close(eap->forceit, win, NULL);
7352 }
7353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354}
7355
Bram Moolenaar4033c552017-09-16 20:54:51 +02007356#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007357/*
7358 * ":pclose": Close any preview window.
7359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007361ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007362{
7363 win_T *win;
7364
Bram Moolenaar29323592016-07-24 22:04:11 +02007365 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007366 if (win->w_p_pvw)
7367 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007368 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007369 break;
7370 }
7371}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007372#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373
Bram Moolenaarf740b292006-02-16 22:11:02 +00007374/*
7375 * Close window "win" and take care of handling closing the last window for a
7376 * modified buffer.
7377 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007379ex_win_close(
7380 int forceit,
7381 win_T *win,
7382 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384 int need_hide;
7385 buf_T *buf = win->w_buffer;
7386
7387 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007388 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007390#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if ((p_confirm || cmdmod.confirm) && p_write)
7392 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007393 bufref_T bufref;
7394
7395 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007397 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 return;
7399 need_hide = FALSE;
7400 }
7401 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007404 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 return;
7406 }
7407 }
7408
Bram Moolenaar4033c552017-09-16 20:54:51 +02007409#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007411#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007414 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007415 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007416 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007417 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007421 * Handle the argument for a tabpage related ex command.
7422 * Returns a tabpage number.
7423 * When an error is encountered then eap->errmsg is set.
7424 */
7425 static int
7426get_tabpage_arg(exarg_T *eap)
7427{
7428 int tab_number;
7429 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7430
7431 if (eap->arg && *eap->arg != NUL)
7432 {
7433 char_u *p = eap->arg;
7434 char_u *p_save;
7435 int relative = 0; /* argument +N/-N means: go to N places to the
7436 * right/left relative to the current position. */
7437
7438 if (*p == '-')
7439 {
7440 relative = -1;
7441 p++;
7442 }
7443 else if (*p == '+')
7444 {
7445 relative = 1;
7446 p++;
7447 }
7448
7449 p_save = p;
7450 tab_number = getdigits(&p);
7451
7452 if (relative == 0)
7453 {
7454 if (STRCMP(p, "$") == 0)
7455 tab_number = LAST_TAB_NR;
7456 else if (p == p_save || *p_save == '-' || *p != NUL
7457 || tab_number > LAST_TAB_NR)
7458 {
7459 /* No numbers as argument. */
7460 eap->errmsg = e_invarg;
7461 goto theend;
7462 }
7463 }
7464 else
7465 {
7466 if (*p_save == NUL)
7467 tab_number = 1;
7468 else if (p == p_save || *p_save == '-' || *p != NUL
7469 || tab_number == 0)
7470 {
7471 /* No numbers as argument. */
7472 eap->errmsg = e_invarg;
7473 goto theend;
7474 }
7475 tab_number = tab_number * relative + tabpage_index(curtab);
7476 if (!unaccept_arg0 && relative == -1)
7477 --tab_number;
7478 }
7479 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7480 eap->errmsg = e_invarg;
7481 }
7482 else if (eap->addr_count > 0)
7483 {
7484 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007485 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007486 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007487 tab_number = 0;
7488 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007489 else
7490 {
7491 tab_number = eap->line2;
7492 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7493 {
7494 --tab_number;
7495 if (tab_number < unaccept_arg0)
7496 eap->errmsg = e_invarg;
7497 }
7498 }
7499 }
7500 else
7501 {
7502 switch (eap->cmdidx)
7503 {
7504 case CMD_tabnext:
7505 tab_number = tabpage_index(curtab) + 1;
7506 if (tab_number > LAST_TAB_NR)
7507 tab_number = 1;
7508 break;
7509 case CMD_tabmove:
7510 tab_number = LAST_TAB_NR;
7511 break;
7512 default:
7513 tab_number = tabpage_index(curtab);
7514 }
7515 }
7516
7517theend:
7518 return tab_number;
7519}
7520
7521/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007522 * ":tabclose": close current tab page, unless it is the last one.
7523 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 */
7525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007526ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007528 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007529 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007530
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007531# ifdef FEAT_CMDWIN
7532 if (cmdwin_type != 0)
7533 cmdwin_result = K_IGNORE;
7534 else
7535# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007536 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007537 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007540 tab_number = get_tabpage_arg(eap);
7541 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007542 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007543 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007544 if (tp == NULL)
7545 {
7546 beep_flush();
7547 return;
7548 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007549 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007550 {
7551 tabpage_close_other(tp, eap->forceit);
7552 return;
7553 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007554 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007555#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007556 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007557#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007558 )
7559 tabpage_close(eap->forceit);
7560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007562}
7563
7564/*
7565 * ":tabonly": close all tab pages except the current one
7566 */
7567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007568ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007569{
7570 tabpage_T *tp;
7571 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007572 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007573
7574# ifdef FEAT_CMDWIN
7575 if (cmdwin_type != 0)
7576 cmdwin_result = K_IGNORE;
7577 else
7578# endif
7579 if (first_tabpage->tp_next == NULL)
7580 MSG(_("Already only one tab page"));
7581 else
7582 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007583 tab_number = get_tabpage_arg(eap);
7584 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007585 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007586 goto_tabpage(tab_number);
7587 /* Repeat this up to a 1000 times, because autocommands may
7588 * mess up the lists. */
7589 for (done = 0; done < 1000; ++done)
7590 {
7591 FOR_ALL_TABPAGES(tp)
7592 if (tp->tp_topframe != topframe)
7593 {
7594 tabpage_close_other(tp, eap->forceit);
7595 /* if we failed to close it quit */
7596 if (valid_tabpage(tp))
7597 done = 1000;
7598 /* start over, "tp" is now invalid */
7599 break;
7600 }
7601 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007602 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007603 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007604 }
7605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
7608/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007609 * Close the current tab page.
7610 */
7611 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007612tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007613{
7614 /* First close all the windows but the current one. If that worked then
7615 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007616 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007617 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007618 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007619 ex_win_close(forceit, curwin, NULL);
7620# ifdef FEAT_GUI
7621 need_mouse_correct = TRUE;
7622# endif
7623}
7624
7625/*
7626 * Close tab page "tp", which is not the current tab page.
7627 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007628 * Also takes care of the tab pages line disappearing when closing the
7629 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007630 */
7631 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007632tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007633{
7634 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007635 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007636 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637
7638 /* Limit to 1000 windows, autocommands may add a window while we close
7639 * one. OK, so I'm paranoid... */
7640 while (++done < 1000)
7641 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007642 wp = tp->tp_firstwin;
7643 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007644
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007645 /* Autocommands may delete the tab page under our fingers and we may
7646 * fail to close a window with a modified buffer. */
7647 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007648 break;
7649 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007650
Bram Moolenaar29323592016-07-24 22:04:11 +02007651#ifdef FEAT_AUTOCMD
7652 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7653#endif
7654
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007655 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007656 if (h != tabline_height())
7657 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007658}
7659
7660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 * ":only".
7662 */
7663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007664ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007666 win_T *wp;
7667 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668# ifdef FEAT_GUI
7669 need_mouse_correct = TRUE;
7670# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007671 if (eap->addr_count > 0)
7672 {
7673 wnr = eap->line2;
7674 for (wp = firstwin; --wnr > 0; )
7675 {
7676 if (wp->w_next == NULL)
7677 break;
7678 else
7679 wp = wp->w_next;
7680 }
7681 win_goto(wp);
7682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 close_others(TRUE, eap->forceit);
7684}
7685
7686/*
7687 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007688 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007690 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007691ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 if (eap->addr_count == 0)
7694 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007695 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
7698 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007699ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007701 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007702 if (!eap->skip)
7703 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007704#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007705 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007706#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007707 if (eap->addr_count == 0)
7708 win_close(curwin, FALSE); /* don't free buffer */
7709 else
7710 {
7711 int winnr = 0;
7712 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007713
Bram Moolenaar2256c992016-11-15 21:17:07 +01007714 FOR_ALL_WINDOWS(win)
7715 {
7716 winnr++;
7717 if (winnr == eap->line2)
7718 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007719 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007720 if (win == NULL)
7721 win = lastwin;
7722 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725}
7726
7727/*
7728 * ":stop" and ":suspend": Suspend Vim.
7729 */
7730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007731ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 /*
7734 * Disallow suspending for "rvim".
7735 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007736 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
7738 if (!eap->forceit)
7739 autowrite_all();
7740 windgoto((int)Rows - 1, 0);
7741 out_char('\n');
7742 out_flush();
7743 stoptermcap();
7744 out_flush(); /* needed for SUN to restore xterm buffer */
7745#ifdef FEAT_TITLE
7746 mch_restore_title(3); /* restore window titles */
7747#endif
7748 ui_suspend(); /* call machine specific function */
7749#ifdef FEAT_TITLE
7750 maketitle();
7751 resettitle(); /* force updating the title */
7752#endif
7753 starttermcap();
7754 scroll_start(); /* scroll screen before redrawing */
7755 redraw_later_clear();
7756 shell_resized(); /* may have resized window */
7757 }
7758}
7759
7760/*
7761 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7762 */
7763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007764ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766#ifdef FEAT_CMDWIN
7767 if (cmdwin_type != 0)
7768 {
7769 cmdwin_result = Ctrl_C;
7770 return;
7771 }
7772#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007773 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007774 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007775 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007776 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007777 return;
7778 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007779#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007780 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7781 /* Refuse to quit when locked or when the buffer in the last window is
7782 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007783 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007784 return;
7785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
7787 /*
7788 * if more files or windows we won't exit
7789 */
7790 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7791 exiting = TRUE;
7792 if ( ((eap->cmdidx == CMD_wq
7793 || curbufIsChanged())
7794 && do_write(eap) == FAIL)
7795 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007796 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 {
7798 not_exiting();
7799 }
7800 else
7801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007804 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805# ifdef FEAT_GUI
7806 need_mouse_correct = TRUE;
7807# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007808 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007809 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
7811}
7812
7813/*
7814 * ":print", ":list", ":number".
7815 */
7816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007817ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007819 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7820 EMSG(_(e_emptybuf));
7821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007823 for ( ;!got_int; ui_breakcheck())
7824 {
7825 print_line(eap->line1,
7826 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7827 || (eap->flags & EXFLAG_NR)),
7828 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7829 if (++eap->line1 > eap->line2)
7830 break;
7831 out_flush(); /* show one line at a time */
7832 }
7833 setpcmark();
7834 /* put cursor at last line */
7835 curwin->w_cursor.lnum = eap->line2;
7836 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 }
7838
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840}
7841
7842#ifdef FEAT_BYTEOFF
7843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007844ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 goto_byte(eap->line2);
7847}
7848#endif
7849
7850/*
7851 * ":shell".
7852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007854ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 do_shell(NULL, 0);
7857}
7858
Bram Moolenaar4033c552017-09-16 20:54:51 +02007859#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007861 || defined(FEAT_GUI_MSWIN) \
7862 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 || defined(PROTO)
7864
7865/*
7866 * Handle a file drop. The code is here because a drop is *nearly* like an
7867 * :args command, but not quite (we have a list of exact filenames, so we
7868 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7869 * code is very similar to :args and hence needs access to a lot of the static
7870 * functions in this file.
7871 *
7872 * The list should be allocated using alloc(), as should each item in the
7873 * list. This function takes over responsibility for freeing the list.
7874 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007875 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007876 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 */
7878 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007879handle_drop(
7880 int filec, /* the number of files dropped */
7881 char_u **filev, /* the list of files dropped */
7882 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 exarg_T ea;
7885 int save_msg_scroll = msg_scroll;
7886
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007887 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007888 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007890#ifdef FEAT_AUTOCMD
7891 if (curbuf_locked())
7892 return;
7893#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007894 /* When the screen is being updated we should not change buffers and
7895 * windows structures, it may cause freed memory to be used. */
7896 if (updating_screen)
7897 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
7899 /* Check whether the current buffer is changed. If so, we will need
7900 * to split the current window or data could be lost.
7901 * We don't need to check if the 'hidden' option is set, as in this
7902 * case the buffer won't be lost.
7903 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007904 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
7906 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007907 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 --emsg_off;
7909 }
7910 if (split)
7911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 if (win_split(0, 0) == FAIL)
7913 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007914 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915
7916 /* When splitting the window, create a new alist. Otherwise the
7917 * existing one is overwritten. */
7918 alist_unlink(curwin->w_alist);
7919 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
7921
7922 /*
7923 * Set up the new argument list.
7924 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007925 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
7927 /*
7928 * Move to the first file.
7929 */
7930 /* Fake up a minimal "next" command for do_argfile() */
7931 vim_memset(&ea, 0, sizeof(ea));
7932 ea.cmd = (char_u *)"next";
7933 do_argfile(&ea, 0);
7934
7935 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7936 * mode that is not needed here. */
7937 need_start_insertmode = FALSE;
7938
7939 /* Restore msg_scroll, otherwise a following command may cause scrolling
7940 * unexpectedly. The screen will be redrawn by the caller, thus
7941 * msg_scroll being set by displaying a message is irrelevant. */
7942 msg_scroll = save_msg_scroll;
7943}
7944#endif
7945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946/*
7947 * Clear an argument list: free all file names and reset it to zero entries.
7948 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007949 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 while (--al->al_ga.ga_len >= 0)
7953 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7954 ga_clear(&al->al_ga);
7955}
7956
7957/*
7958 * Init an argument list.
7959 */
7960 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007961alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7964}
7965
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966/*
7967 * Remove a reference from an argument list.
7968 * Ignored when the argument list is the global one.
7969 * If the argument list is no longer used by any window, free it.
7970 */
7971 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007972alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 if (al != &global_alist && --al->al_refcount <= 0)
7975 {
7976 alist_clear(al);
7977 vim_free(al);
7978 }
7979}
7980
Bram Moolenaar4033c552017-09-16 20:54:51 +02007981#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982/*
7983 * Create a new argument list and use it for the current window.
7984 */
7985 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7989 if (curwin->w_alist == NULL)
7990 {
7991 curwin->w_alist = &global_alist;
7992 ++global_alist.al_refcount;
7993 }
7994 else
7995 {
7996 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007997 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 alist_init(curwin->w_alist);
7999 }
8000}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001#endif
8002
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008003#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004/*
8005 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008006 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8007 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 */
8009 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008010alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008013 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 char_u **new_arg_files;
8015 int new_arg_file_count;
8016 char_u *save_p_su = p_su;
8017 int i;
8018
8019 /* Don't use 'suffixes' here. This should work like the shell did the
8020 * expansion. Also, the vimrc file isn't read yet, thus the user
8021 * can't set the options. */
8022 p_su = empty_option;
8023 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8024 if (old_arg_files != NULL)
8025 {
8026 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008027 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8028 old_arg_count = GARGCOUNT;
8029 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008031 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 && new_arg_file_count > 0)
8033 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008034 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8035 TRUE, fnum_list, fnum_len);
8036 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 }
8039 p_su = save_p_su;
8040}
8041#endif
8042
8043/*
8044 * Set the argument list for the current window.
8045 * Takes over the allocated files[] and the allocated fnames in it.
8046 */
8047 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008048alist_set(
8049 alist_T *al,
8050 int count,
8051 char_u **files,
8052 int use_curbuf,
8053 int *fnum_list,
8054 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
8056 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008057 static int recursive = 0;
8058
8059 if (recursive)
8060 {
8061#ifdef FEAT_AUTOCMD
8062 EMSG(_(e_au_recursive));
8063#endif
8064 return;
8065 }
8066 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067
8068 alist_clear(al);
8069 if (ga_grow(&al->al_ga, count) == OK)
8070 {
8071 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008072 {
8073 if (got_int)
8074 {
8075 /* When adding many buffers this can take a long time. Allow
8076 * interrupting here. */
8077 while (i < count)
8078 vim_free(files[i++]);
8079 break;
8080 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008081
8082 /* May set buffer name of a buffer previously used for the
8083 * argument list, so that it's re-used by alist_add. */
8084 if (fnum_list != NULL && i < fnum_len)
8085 buf_set_name(fnum_list[i], files[i]);
8086
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008088 ui_breakcheck();
8089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 vim_free(files);
8091 }
8092 else
8093 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008096
8097 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
8099
8100/*
8101 * Add file "fname" to argument list "al".
8102 * "fname" must have been allocated and "al" must have been checked for room.
8103 */
8104 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008105alist_add(
8106 alist_T *al,
8107 char_u *fname,
8108 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110 if (fname == NULL) /* don't add NULL file names */
8111 return;
8112#ifdef BACKSLASH_IN_FILENAME
8113 slash_adjust(fname);
8114#endif
8115 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8116 if (set_fnum > 0)
8117 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8118 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8119 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120}
8121
8122#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8123/*
8124 * Adjust slashes in file names. Called after 'shellslash' was set.
8125 */
8126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008127alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008131 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132
8133 for (i = 0; i < GARGCOUNT; ++i)
8134 if (GARGLIST[i].ae_fname != NULL)
8135 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008136 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (wp->w_alist != &global_alist)
8138 for (i = 0; i < WARGCOUNT(wp); ++i)
8139 if (WARGLIST(wp)[i].ae_fname != NULL)
8140 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141}
8142#endif
8143
8144/*
8145 * ":preserve".
8146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008148ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008150 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 ml_preserve(curbuf, TRUE);
8152}
8153
8154/*
8155 * ":recover".
8156 */
8157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8161 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008162 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8163 | CCGD_MULTWIN
8164 | (eap->forceit ? CCGD_FORCEIT : 0)
8165 | CCGD_EXCMD)
8166
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 && (*eap->arg == NUL
8168 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8169 ml_recover();
8170 recoverymode = FALSE;
8171}
8172
8173/*
8174 * Command modifier used in a wrong way.
8175 */
8176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008177ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
8179 eap->errmsg = e_invcmd;
8180}
8181
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182/*
8183 * :sview [+command] file split window with new file, read-only
8184 * :split [[+command] file] split window with current or new file
8185 * :vsplit [[+command] file] split window vertically with current or new file
8186 * :new [[+command] file] split window with no or new file
8187 * :vnew [[+command] file] split vertically window with no or new file
8188 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008189 *
8190 * :tabedit open new Tab page with empty window
8191 * :tabedit [+command] file open new Tab page and edit "file"
8192 * :tabnew [[+command] file] just like :tabedit
8193 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 */
8195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008196ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008198 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008199#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008201#endif
8202#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
Bram Moolenaar4033c552017-09-16 20:54:51 +02008206#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
Bram Moolenaar4033c552017-09-16 20:54:51 +02008210#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008212 * quickfix windows. But it's OK when doing ":tab split". */
8213 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {
8215 if (eap->cmdidx == CMD_split)
8216 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 if (eap->cmdidx == CMD_vsplit)
8218 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
Bram Moolenaar4033c552017-09-16 20:54:51 +02008222#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008223 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {
8225 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8226 FNAME_MESS, TRUE, curbuf->b_ffname);
8227 if (fname == NULL)
8228 goto theend;
8229 eap->arg = fname;
8230 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008231# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008232 else
8233# endif
8234#endif
8235#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 && eap->cmdidx != CMD_new)
8239 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008240# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008241 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008242# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008243 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008244# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008245 au_has_group((char_u *)"FileExplorer"))
8246 {
8247 /* No browsing supported but we do have the file explorer:
8248 * Edit the directory. */
8249 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8250 eap->arg = (char_u *)".";
8251 }
8252 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008253# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008254 {
8255 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008257 if (fname == NULL)
8258 goto theend;
8259 eap->arg = fname;
8260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 }
8262 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008265 /*
8266 * Either open new tab page or split the window.
8267 */
8268 if (eap->cmdidx == CMD_tabedit
8269 || eap->cmdidx == CMD_tabfind
8270 || eap->cmdidx == CMD_tabnew)
8271 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008272 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8273 : eap->addr_count == 0 ? 0
8274 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008275 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008276 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008277
8278 /* set the alternate buffer for the window we came from */
8279 if (curwin != old_curwin
8280 && win_valid(old_curwin)
8281 && old_curwin->w_buffer != curbuf
8282 && !cmdmod.keepalt)
8283 old_curwin->w_alt_fnum = curbuf->b_fnum;
8284 }
8285 }
8286 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8288 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008289# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 /* Reset 'scrollbind' when editing another file, but keep it when
8291 * doing ":split" without arguments. */
8292 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008293# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008297 {
8298 RESET_BINDING(curwin);
8299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 else
8301 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 do_exedit(eap, old_curwin);
8304 }
8305
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008306# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311theend:
8312 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008315
8316/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008317 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008318 */
8319 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008320tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008321{
8322 exarg_T ea;
8323
8324 vim_memset(&ea, 0, sizeof(ea));
8325 ea.cmdidx = CMD_tabnew;
8326 ea.cmd = (char_u *)"tabn";
8327 ea.arg = (char_u *)"";
8328 ex_splitview(&ea);
8329}
8330
8331/*
8332 * :tabnext command
8333 */
8334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008335ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008336{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008337 int tab_number;
8338
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008339 switch (eap->cmdidx)
8340 {
8341 case CMD_tabfirst:
8342 case CMD_tabrewind:
8343 goto_tabpage(1);
8344 break;
8345 case CMD_tablast:
8346 goto_tabpage(9999);
8347 break;
8348 case CMD_tabprevious:
8349 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008350 if (eap->arg && *eap->arg != NUL)
8351 {
8352 char_u *p = eap->arg;
8353 char_u *p_save = p;
8354
8355 tab_number = getdigits(&p);
8356 if (p == p_save || *p_save == '-' || *p != NUL
8357 || tab_number == 0)
8358 {
8359 /* No numbers as argument. */
8360 eap->errmsg = e_invarg;
8361 return;
8362 }
8363 }
8364 else
8365 {
8366 if (eap->addr_count == 0)
8367 tab_number = 1;
8368 else
8369 {
8370 tab_number = eap->line2;
8371 if (tab_number < 1)
8372 {
8373 eap->errmsg = e_invrange;
8374 return;
8375 }
8376 }
8377 }
8378 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008379 break;
8380 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008381 tab_number = get_tabpage_arg(eap);
8382 if (eap->errmsg == NULL)
8383 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008384 break;
8385 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008386}
8387
8388/*
8389 * :tabmove command
8390 */
8391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008392ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008393{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008394 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008395
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008396 tab_number = get_tabpage_arg(eap);
8397 if (eap->errmsg == NULL)
8398 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008399}
8400
8401/*
8402 * :tabs command: List tabs and their contents.
8403 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008405ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008406{
8407 tabpage_T *tp;
8408 win_T *wp;
8409 int tabcount = 1;
8410
8411 msg_start();
8412 msg_scroll = TRUE;
8413 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8414 {
8415 msg_putchar('\n');
8416 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008417 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008418 out_flush(); /* output one line at a time */
8419 ui_breakcheck();
8420
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008421 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008422 wp = firstwin;
8423 else
8424 wp = tp->tp_firstwin;
8425 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8426 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008427 msg_putchar('\n');
8428 msg_putchar(wp == curwin ? '>' : ' ');
8429 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008430 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8431 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008432 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008433 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008434 else
8435 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8436 IObuff, IOSIZE, TRUE);
8437 msg_outtrans(IObuff);
8438 out_flush(); /* output one line at a time */
8439 ui_breakcheck();
8440 }
8441 }
8442}
8443
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444/*
8445 * ":mode": Set screen mode.
8446 * If no argument given, just get the screen size and redraw.
8447 */
8448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008449ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 if (*eap->arg == NUL)
8452 shell_resized();
8453 else
8454 mch_screenmode(eap->arg);
8455}
8456
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457/*
8458 * ":resize".
8459 * set, increment or decrement current window height
8460 */
8461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008462ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
8464 int n;
8465 win_T *wp = curwin;
8466
8467 if (eap->addr_count > 0)
8468 {
8469 n = eap->line2;
8470 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8471 ;
8472 }
8473
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008474# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 if (cmdmod.split & WSP_VERT)
8479 {
8480 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008481 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8483 n = 9999;
8484 win_setwidth_win((int)n, wp);
8485 }
8486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {
8488 if (*eap->arg == '-' || *eap->arg == '+')
8489 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008490 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 n = 9999;
8492 win_setheight_win((int)n, wp);
8493 }
8494}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
8496/*
8497 * ":find [+command] <file>" command.
8498 */
8499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008500ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502#ifdef FEAT_SEARCHPATH
8503 char_u *fname;
8504 int count;
8505
8506 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8507 TRUE, curbuf->b_ffname);
8508 if (eap->addr_count > 0)
8509 {
8510 /* Repeat finding the file "count" times. This matters when it
8511 * appears several times in the path. */
8512 count = eap->line2;
8513 while (fname != NULL && --count > 0)
8514 {
8515 vim_free(fname);
8516 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8517 FALSE, curbuf->b_ffname);
8518 }
8519 }
8520
8521 if (fname != NULL)
8522 {
8523 eap->arg = fname;
8524#endif
8525 do_exedit(eap, NULL);
8526#ifdef FEAT_SEARCHPATH
8527 vim_free(fname);
8528 }
8529#endif
8530}
8531
8532/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008533 * ":open" simulation: for now just work like ":visual".
8534 */
8535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008536ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008537{
8538 regmatch_T regmatch;
8539 char_u *p;
8540
8541 curwin->w_cursor.lnum = eap->line2;
8542 beginline(BL_SOL | BL_FIX);
8543 if (*eap->arg == '/')
8544 {
8545 /* ":open /pattern/": put cursor in column found with pattern */
8546 ++eap->arg;
8547 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8548 *p = NUL;
8549 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8550 if (regmatch.regprog != NULL)
8551 {
8552 regmatch.rm_ic = p_ic;
8553 p = ml_get_curline();
8554 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008555 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008556 else
8557 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008558 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008559 }
8560 /* Move to the NUL, ignore any other arguments. */
8561 eap->arg += STRLEN(eap->arg);
8562 }
8563 check_cursor();
8564
8565 eap->cmdidx = CMD_visual;
8566 do_exedit(eap, NULL);
8567}
8568
8569/*
8570 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 */
8572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008573ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 do_exedit(eap, NULL);
8576}
8577
8578/*
8579 * ":edit <file>" command and alikes.
8580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008582do_exedit(
8583 exarg_T *eap,
8584 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
8586 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008588 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 /*
8591 * ":vi" command ends Ex mode.
8592 */
8593 if (exmode_active && (eap->cmdidx == CMD_visual
8594 || eap->cmdidx == CMD_view))
8595 {
8596 exmode_active = FALSE;
8597 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008598 {
8599 /* Special case: ":global/pat/visual\NLvi-commands" */
8600 if (global_busy)
8601 {
8602 int rd = RedrawingDisabled;
8603 int nwr = no_wait_return;
8604 int ms = msg_scroll;
8605#ifdef FEAT_GUI
8606 int he = hold_gui_events;
8607#endif
8608
8609 if (eap->nextcmd != NULL)
8610 {
8611 stuffReadbuff(eap->nextcmd);
8612 eap->nextcmd = NULL;
8613 }
8614
8615 if (exmode_was != EXMODE_VIM)
8616 settmode(TMODE_RAW);
8617 RedrawingDisabled = 0;
8618 no_wait_return = 0;
8619 need_wait_return = FALSE;
8620 msg_scroll = 0;
8621#ifdef FEAT_GUI
8622 hold_gui_events = 0;
8623#endif
8624 must_redraw = CLEAR;
8625
8626 main_loop(FALSE, TRUE);
8627
8628 RedrawingDisabled = rd;
8629 no_wait_return = nwr;
8630 msg_scroll = ms;
8631#ifdef FEAT_GUI
8632 hold_gui_events = he;
8633#endif
8634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638
8639 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008640 || eap->cmdidx == CMD_tabnew
8641 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008642 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008644 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 setpcmark();
8646 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008647 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8648 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008650 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 || *eap->arg != NUL
8652#ifdef FEAT_BROWSE
8653 || cmdmod.browse
8654#endif
8655 )
8656 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008657#ifdef FEAT_AUTOCMD
8658 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8659 * can bring us here, others are stopped earlier. */
8660 if (*eap->arg != NUL && curbuf_locked())
8661 return;
8662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 n = readonlymode;
8664 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8665 readonlymode = TRUE;
8666 else if (eap->cmdidx == CMD_enew)
8667 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8668 empty buffer */
8669 setpcmark();
8670 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8671 NULL, eap,
8672 /* ":edit" goes to first line if Vi compatible */
8673 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8674 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8675 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008676 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008678 /* after a split we can use an existing buffer */
8679 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680#ifdef FEAT_LISTCMDS
8681 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8682#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008683 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 {
8685 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 if (old_curwin != NULL)
8687 {
8688 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008689 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02008691#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008692 cleanup_T cs;
8693
8694 /* Reset the error/interrupt/exception state here so that
8695 * aborting() returns FALSE when closing a window. */
8696 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008697#endif
8698#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008700#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008701 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008702
Bram Moolenaar4033c552017-09-16 20:54:51 +02008703#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008704 /* Restore the error/interrupt/exception state if not
8705 * discarded by a new aborting error, interrupt, or
8706 * uncaught exception. */
8707 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 }
8712 else if (readonlymode && curbuf->b_nwindows == 1)
8713 {
8714 /* When editing an already visited buffer, 'readonly' won't be set
8715 * but the previous value is kept. With ":view" and ":sview" we
8716 * want the file to be readonly, except when another window is
8717 * editing the same buffer. */
8718 curbuf->b_p_ro = TRUE;
8719 }
8720 readonlymode = n;
8721 }
8722 else
8723 {
8724 if (eap->do_ecmd_cmd != NULL)
8725 do_cmdline_cmd(eap->do_ecmd_cmd);
8726#ifdef FEAT_TITLE
8727 n = curwin->w_arg_idx_invalid;
8728#endif
8729 check_arg_idx(curwin);
8730#ifdef FEAT_TITLE
8731 if (n != curwin->w_arg_idx_invalid)
8732 maketitle();
8733#endif
8734 }
8735
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 /*
8737 * if ":split file" worked, set alternate file name in old window to new
8738 * file
8739 */
8740 if (old_curwin != NULL
8741 && *eap->arg != NUL
8742 && curwin != old_curwin
8743 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008744 && old_curwin->w_buffer != curbuf
8745 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
8748 ex_no_reprint = TRUE;
8749}
8750
8751#ifndef FEAT_GUI
8752/*
8753 * ":gui" and ":gvim" when there is no GUI.
8754 */
8755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008756ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757{
8758 eap->errmsg = e_nogvim;
8759}
8760#endif
8761
8762#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008764ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 gui_make_tearoff(eap->arg);
8767}
8768#endif
8769
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008770#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008772ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008774 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776#endif
8777
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008779ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8782 MSG(_("No swap file"));
8783 else
8784 msg(curbuf->b_ml.ml_mfp->mf_fname);
8785}
8786
8787/*
8788 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8789 * offset.
8790 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008793ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795#ifdef FEAT_SCROLLBIND
8796 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008797 win_T *save_curwin = curwin;
8798 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 long topline;
8800 long y;
8801 linenr_T old_linenr = curwin->w_cursor.lnum;
8802
8803 setpcmark();
8804
8805 /*
8806 * determine max topline
8807 */
8808 if (curwin->w_p_scb)
8809 {
8810 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008811 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 {
8813 if (wp->w_p_scb && wp->w_buffer)
8814 {
8815 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8816 if (topline > y)
8817 topline = y;
8818 }
8819 }
8820 if (topline < 1)
8821 topline = 1;
8822 }
8823 else
8824 {
8825 topline = 1;
8826 }
8827
8828
8829 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008830 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008832 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 {
8834 if (curwin->w_p_scb)
8835 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008836 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 y = topline - curwin->w_topline;
8838 if (y > 0)
8839 scrollup(y, TRUE);
8840 else
8841 scrolldown(-y, TRUE);
8842 curwin->w_scbind_pos = topline;
8843 redraw_later(VALID);
8844 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008848 curwin = save_curwin;
8849 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (curwin->w_p_scb)
8851 {
8852 did_syncbind = TRUE;
8853 checkpcmark();
8854 if (old_linenr != curwin->w_cursor.lnum)
8855 {
8856 char_u ctrl_o[2];
8857
8858 ctrl_o[0] = Ctrl_O;
8859 ctrl_o[1] = 0;
8860 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8861 }
8862 }
8863#endif
8864}
8865
8866
8867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008868ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008870 int i;
8871 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8872 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
8874 if (eap->usefilter) /* :r!cmd */
8875 do_bang(1, eap, FALSE, FALSE, TRUE);
8876 else
8877 {
8878 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8879 return;
8880
8881#ifdef FEAT_BROWSE
8882 if (cmdmod.browse)
8883 {
8884 char_u *browseFile;
8885
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008886 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 NULL, NULL, NULL, curbuf);
8888 if (browseFile != NULL)
8889 {
8890 i = readfile(browseFile, NULL,
8891 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8892 vim_free(browseFile);
8893 }
8894 else
8895 i = OK;
8896 }
8897 else
8898#endif
8899 if (*eap->arg == NUL)
8900 {
8901 if (check_fname() == FAIL) /* check for no file name */
8902 return;
8903 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8904 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8905 }
8906 else
8907 {
8908 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8909 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8910 i = readfile(eap->arg, NULL,
8911 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8912
8913 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008914 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 {
8916#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8917 if (!aborting())
8918#endif
8919 EMSG2(_(e_notopen), eap->arg);
8920 }
8921 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008922 {
8923 if (empty && exmode_active)
8924 {
8925 /* Delete the empty line that remains. Historically ex does
8926 * this but vi doesn't. */
8927 if (eap->line2 == 0)
8928 lnum = curbuf->b_ml.ml_line_count;
8929 else
8930 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008931 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008932 {
8933 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008934 if (curwin->w_cursor.lnum > 1
8935 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008936 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008937 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008938 }
8939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 }
8943}
8944
Bram Moolenaarea408852005-06-25 22:49:46 +00008945static char_u *prev_dir = NULL;
8946
8947#if defined(EXITFREE) || defined(PROTO)
8948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008949free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008950{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008951 VIM_CLEAR(prev_dir);
8952 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008953}
8954#endif
8955
Bram Moolenaarf4258302013-06-02 18:20:17 +02008956/*
8957 * Deal with the side effects of changing the current directory.
8958 * When "local" is TRUE then this was after an ":lcd" command.
8959 */
8960 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008961post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008962{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008963 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008964 if (local)
8965 {
8966 /* If still in global directory, need to remember current
8967 * directory as global directory. */
8968 if (globaldir == NULL && prev_dir != NULL)
8969 globaldir = vim_strsave(prev_dir);
8970 /* Remember this local directory for the window. */
8971 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8972 curwin->w_localdir = vim_strsave(NameBuff);
8973 }
8974 else
8975 {
8976 /* We are now in the global directory, no need to remember its
8977 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008978 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008979 }
8980
8981 shorten_fnames(TRUE);
8982}
8983
Bram Moolenaarea408852005-06-25 22:49:46 +00008984
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985/*
8986 * ":cd", ":lcd", ":chdir" and ":lchdir".
8987 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008988 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008989ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 char_u *new_dir;
8992 char_u *tofree;
8993
8994 new_dir = eap->arg;
8995#if !defined(UNIX) && !defined(VMS)
8996 /* for non-UNIX ":cd" means: print current directory */
8997 if (*new_dir == NUL)
8998 ex_pwd(NULL);
8999 else
9000#endif
9001 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009002#ifdef FEAT_AUTOCMD
9003 if (allbuf_locked())
9004 return;
9005#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009006 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9007 && !eap->forceit)
9008 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009009 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009010 return;
9011 }
9012
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 /* ":cd -": Change to previous directory */
9014 if (STRCMP(new_dir, "-") == 0)
9015 {
9016 if (prev_dir == NULL)
9017 {
9018 EMSG(_("E186: No previous directory"));
9019 return;
9020 }
9021 new_dir = prev_dir;
9022 }
9023
9024 /* Save current directory for next ":cd -" */
9025 tofree = prev_dir;
9026 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9027 prev_dir = vim_strsave(NameBuff);
9028 else
9029 prev_dir = NULL;
9030
9031#if defined(UNIX) || defined(VMS)
9032 /* for UNIX ":cd" means: go to home directory */
9033 if (*new_dir == NUL)
9034 {
9035 /* use NameBuff for home directory name */
9036# ifdef VMS
9037 char_u *p;
9038
9039 p = mch_getenv((char_u *)"SYS$LOGIN");
9040 if (p == NULL || *p == NUL) /* empty is the same as not set */
9041 NameBuff[0] = NUL;
9042 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009043 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044# else
9045 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9046# endif
9047 new_dir = NameBuff;
9048 }
9049#endif
9050 if (new_dir == NULL || vim_chdir(new_dir))
9051 EMSG(_(e_failed));
9052 else
9053 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009054 int is_local_chdir = eap->cmdidx == CMD_lcd
9055 || eap->cmdidx == CMD_lchdir;
9056
9057 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009060 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009062#ifdef FEAT_AUTOCMD
9063 apply_autocmds(EVENT_DIRCHANGED,
9064 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9065 new_dir, FALSE, curbuf);
9066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 }
9068 vim_free(tofree);
9069 }
9070}
9071
9072/*
9073 * ":pwd".
9074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
9078 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9079 {
9080#ifdef BACKSLASH_IN_FILENAME
9081 slash_adjust(NameBuff);
9082#endif
9083 msg(NameBuff);
9084 }
9085 else
9086 EMSG(_("E187: Unknown"));
9087}
9088
9089/*
9090 * ":=".
9091 */
9092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009093ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
9095 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009096 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097}
9098
9099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009100ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009102 int n;
9103 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 if (cursor_valid())
9106 {
9107 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9108 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009109 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009111
9112 len = eap->line2;
9113 switch (*eap->arg)
9114 {
9115 case 'm': break;
9116 case NUL: len *= 1000L; break;
9117 default: EMSG2(_(e_invarg2), eap->arg); return;
9118 }
9119 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
9122/*
9123 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9124 */
9125 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009126do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009129 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130
9131 cursor_on();
9132 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009133 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009135 wait_now = msec - done > 1000L ? 1000L : msec - done;
9136#ifdef FEAT_TIMERS
9137 {
9138 long due_time = check_due_timer();
9139
9140 if (due_time > 0 && due_time < wait_now)
9141 wait_now = due_time;
9142 }
9143#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009144#ifdef FEAT_JOB_CHANNEL
9145 if (has_any_channel() && wait_now > 100L)
9146 wait_now = 100L;
9147#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009148 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009149#ifdef FEAT_JOB_CHANNEL
9150 if (has_any_channel())
9151 ui_breakcheck_force(TRUE);
9152 else
9153#endif
9154 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009155#ifdef MESSAGE_QUEUE
9156 /* Process the netbeans and clientserver messages that may have been
9157 * received in the call to ui_breakcheck() when the GUI is in use. This
9158 * may occur when running a test case. */
9159 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 }
9162}
9163
9164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009165do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167 int mode;
9168 char_u *cmdp;
9169
9170 cmdp = eap->cmd;
9171 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9172
9173 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9174 eap->arg, mode, isabbrev))
9175 {
9176 case 1: EMSG(_(e_invarg));
9177 break;
9178 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9179 break;
9180 }
9181}
9182
9183/*
9184 * ":winsize" command (obsolete).
9185 */
9186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009187ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189 int w, h;
9190 char_u *arg = eap->arg;
9191 char_u *p;
9192
9193 w = getdigits(&arg);
9194 arg = skipwhite(arg);
9195 p = arg;
9196 h = getdigits(&arg);
9197 if (*p != NUL && *arg == NUL)
9198 set_shellsize(w, h, TRUE);
9199 else
9200 EMSG(_("E465: :winsize requires two number arguments"));
9201}
9202
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009204ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
9206 int xchar = NUL;
9207 char_u *p;
9208
9209 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9210 {
9211 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9212 if (eap->arg[1] == NUL)
9213 {
9214 EMSG(_(e_invarg));
9215 return;
9216 }
9217 xchar = eap->arg[1];
9218 p = eap->arg + 2;
9219 }
9220 else
9221 p = eap->arg + 1;
9222
9223 eap->nextcmd = check_nextcmd(p);
9224 p = skipwhite(p);
9225 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9226 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009227 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 {
9229 /* Pass flags on for ":vertical wincmd ]". */
9230 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009231 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9233 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009234 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 }
9236}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237
Bram Moolenaar843ee412004-06-30 16:16:41 +00009238#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239/*
9240 * ":winpos".
9241 */
9242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009243ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244{
9245 int x, y;
9246 char_u *arg = eap->arg;
9247 char_u *p;
9248
9249 if (*arg == NUL)
9250 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009251# if defined(FEAT_GUI) || defined(MSWIN)
9252# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009254# else
9255 if (mch_get_winpos(&x, &y) != FAIL)
9256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 {
9258 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9259 msg(IObuff);
9260 }
9261 else
9262# endif
9263 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9264 }
9265 else
9266 {
9267 x = getdigits(&arg);
9268 arg = skipwhite(arg);
9269 p = arg;
9270 y = getdigits(&arg);
9271 if (*p == NUL || *arg != NUL)
9272 {
9273 EMSG(_("E466: :winpos requires two number arguments"));
9274 return;
9275 }
9276# ifdef FEAT_GUI
9277 if (gui.in_use)
9278 gui_mch_set_winpos(x, y);
9279 else if (gui.starting)
9280 {
9281 /* Remember the coordinates for when the window is opened. */
9282 gui_win_x = x;
9283 gui_win_y = y;
9284 }
9285# ifdef HAVE_TGETENT
9286 else
9287# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009288# else
9289# ifdef MSWIN
9290 mch_set_winpos(x, y);
9291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292# endif
9293# ifdef HAVE_TGETENT
9294 if (*T_CWP)
9295 term_set_winpos(x, y);
9296# endif
9297 }
9298}
9299#endif
9300
9301/*
9302 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9303 */
9304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009305ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307 oparg_T oa;
9308
9309 clear_oparg(&oa);
9310 oa.regname = eap->regname;
9311 oa.start.lnum = eap->line1;
9312 oa.end.lnum = eap->line2;
9313 oa.line_count = eap->line2 - eap->line1 + 1;
9314 oa.motion_type = MLINE;
9315#ifdef FEAT_VIRTUALEDIT
9316 virtual_op = FALSE;
9317#endif
9318 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9319 {
9320 setpcmark();
9321 curwin->w_cursor.lnum = eap->line1;
9322 beginline(BL_SOL | BL_FIX);
9323 }
9324
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009325 if (VIsual_active)
9326 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009327
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 switch (eap->cmdidx)
9329 {
9330 case CMD_delete:
9331 oa.op_type = OP_DELETE;
9332 op_delete(&oa);
9333 break;
9334
9335 case CMD_yank:
9336 oa.op_type = OP_YANK;
9337 (void)op_yank(&oa, FALSE, TRUE);
9338 break;
9339
9340 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009341 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009343 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9344#else
9345 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009347 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 oa.op_type = OP_RSHIFT;
9349 else
9350 oa.op_type = OP_LSHIFT;
9351 op_shift(&oa, FALSE, eap->amount);
9352 break;
9353 }
9354#ifdef FEAT_VIRTUALEDIT
9355 virtual_op = MAYBE;
9356#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009357 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358}
9359
9360/*
9361 * ":put".
9362 */
9363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009364ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 /* ":0put" works like ":1put!". */
9367 if (eap->line2 == 0)
9368 {
9369 eap->line2 = 1;
9370 eap->forceit = TRUE;
9371 }
9372 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009373 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9374 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375}
9376
9377/*
9378 * Handle ":copy" and ":move".
9379 */
9380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009381ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 long n;
9384
Bram Moolenaarded27822017-01-02 14:27:34 +01009385 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 if (eap->arg == NULL) /* error detected */
9387 {
9388 eap->nextcmd = NULL;
9389 return;
9390 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009391 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392
9393 /*
9394 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9395 */
9396 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9397 {
9398 EMSG(_(e_invaddr));
9399 return;
9400 }
9401
9402 if (eap->cmdidx == CMD_move)
9403 {
9404 if (do_move(eap->line1, eap->line2, n) == FAIL)
9405 return;
9406 }
9407 else
9408 ex_copy(eap->line1, eap->line2, n);
9409 u_clearline();
9410 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009411 ex_may_print(eap);
9412}
9413
9414/*
9415 * Print the current line if flags were given to the Ex command.
9416 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009418ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009419{
9420 if (eap->flags != 0)
9421 {
9422 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9423 (eap->flags & EXFLAG_LIST));
9424 ex_no_reprint = TRUE;
9425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426}
9427
9428/*
9429 * ":smagic" and ":snomagic".
9430 */
9431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009432ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 int magic_save = p_magic;
9435
9436 p_magic = (eap->cmdidx == CMD_smagic);
9437 do_sub(eap);
9438 p_magic = magic_save;
9439}
9440
9441/*
9442 * ":join".
9443 */
9444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009445ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 curwin->w_cursor.lnum = eap->line1;
9448 if (eap->line1 == eap->line2)
9449 {
9450 if (eap->addr_count >= 2) /* :2,2join does nothing */
9451 return;
9452 if (eap->line2 == curbuf->b_ml.ml_line_count)
9453 {
9454 beep_flush();
9455 return;
9456 }
9457 ++eap->line2;
9458 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009459 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009461 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463
9464/*
9465 * ":[addr]@r" or ":[addr]*r": execute register
9466 */
9467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009468ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009471 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472
9473 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009474 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475
9476#ifdef USE_ON_FLY_SCROLL
9477 dont_scroll = TRUE; /* disallow scrolling here */
9478#endif
9479
9480 /* get the register name. No name means to use the previous one */
9481 c = *eap->arg;
9482 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9483 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009484 /* Put the register in the typeahead buffer with the "silent" flag. */
9485 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9486 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 else
9491 {
9492 int save_efr = exec_from_reg;
9493
9494 exec_from_reg = TRUE;
9495
9496 /*
9497 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009498 * Continue until the stuff buffer is empty and all added characters
9499 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009501 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9503
9504 exec_from_reg = save_efr;
9505 }
9506}
9507
9508/*
9509 * ":!".
9510 */
9511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009512ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9515}
9516
9517/*
9518 * ":undo".
9519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009521ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009523 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009524 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009525 else
9526 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527}
9528
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009529#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009531ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009532{
9533 char_u hash[UNDO_HASH_SIZE];
9534
9535 u_compute_hash(hash);
9536 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9537}
9538
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009540ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009541{
9542 char_u hash[UNDO_HASH_SIZE];
9543
9544 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009545 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009546}
9547#endif
9548
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549/*
9550 * ":redo".
9551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009553ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 u_redo(1);
9556}
9557
9558/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009559 * ":earlier" and ":later".
9560 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009562ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009563{
9564 long count = 0;
9565 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009566 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009567 char_u *p = eap->arg;
9568
9569 if (*p == NUL)
9570 count = 1;
9571 else if (isdigit(*p))
9572 {
9573 count = getdigits(&p);
9574 switch (*p)
9575 {
9576 case 's': ++p; sec = TRUE; break;
9577 case 'm': ++p; sec = TRUE; count *= 60; break;
9578 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009579 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9580 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009581 }
9582 }
9583
9584 if (*p != NUL)
9585 EMSG2(_(e_invarg2), eap->arg);
9586 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009587 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9588 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009589}
9590
9591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 * ":redir": start/stop redirection.
9593 */
9594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009595ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 char *mode;
9598 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009599 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600
Bram Moolenaarba768492016-07-08 15:32:54 +02009601#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009602 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009603 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009604 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009605 return;
9606 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009607#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009608
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 if (STRICMP(eap->arg, "END") == 0)
9610 close_redir();
9611 else
9612 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009613 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009615 ++arg;
9616 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009618 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 mode = "a";
9620 }
9621 else
9622 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009623 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
9625 close_redir();
9626
9627 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009628 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 if (fname == NULL)
9630 return;
9631#ifdef FEAT_BROWSE
9632 if (cmdmod.browse)
9633 {
9634 char_u *browseFile;
9635
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009636 browseFile = do_browse(BROWSE_SAVE,
9637 (char_u *)_("Save Redirection"),
9638 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 if (browseFile == NULL)
9640 return; /* operation cancelled */
9641 vim_free(fname);
9642 fname = browseFile;
9643 eap->forceit = TRUE; /* since dialog already asked */
9644 }
9645#endif
9646
9647 redir_fd = open_exfile(fname, eap->forceit, mode);
9648 vim_free(fname);
9649 }
9650#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009651 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 {
9653 /* redirect to a register a-z (resp. A-Z for appending) */
9654 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009655 ++arg;
9656 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009658 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009659 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009661 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009663 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009664 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009665 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009668 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009669 if (*arg == '>')
9670 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009671 /* Make register empty when not using @A-@Z and the
9672 * command is valid. */
9673 if (*arg == NUL && !isupper(redir_reg))
9674 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009676 }
9677 if (*arg != NUL)
9678 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009679 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009680 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009681 }
9682 }
9683 else if (*arg == '=' && arg[1] == '>')
9684 {
9685 int append;
9686
9687 /* redirect to a variable */
9688 close_redir();
9689 arg += 2;
9690
9691 if (*arg == '>')
9692 {
9693 ++arg;
9694 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 }
9696 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009697 append = FALSE;
9698
9699 if (var_redir_start(skipwhite(arg), append) == OK)
9700 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 }
9702#endif
9703
9704 /* TODO: redirect to a buffer */
9705
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009707 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009709
9710 /* Make sure redirection is not off. Can happen for cmdline completion
9711 * that indirectly invokes a command to catch its output. */
9712 if (redir_fd != NULL
9713#ifdef FEAT_EVAL
9714 || redir_reg || redir_vname
9715#endif
9716 )
9717 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720/*
9721 * ":redraw": force redraw
9722 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009723 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009724ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726 int r = RedrawingDisabled;
9727 int p = p_lz;
9728
9729 RedrawingDisabled = 0;
9730 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009731 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009733 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734#ifdef FEAT_TITLE
9735 if (need_maketitle)
9736 maketitle();
9737#endif
9738 RedrawingDisabled = r;
9739 p_lz = p;
9740
9741 /* Reset msg_didout, so that a message that's there is overwritten. */
9742 msg_didout = FALSE;
9743 msg_col = 0;
9744
Bram Moolenaar56667a52013-07-17 11:54:28 +02009745 /* No need to wait after an intentional redraw. */
9746 need_wait_return = FALSE;
9747
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 out_flush();
9749}
9750
9751/*
9752 * ":redrawstatus": force redraw of status line(s)
9753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009755ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 int r = RedrawingDisabled;
9758 int p = p_lz;
9759
9760 RedrawingDisabled = 0;
9761 p_lz = FALSE;
9762 if (eap->forceit)
9763 status_redraw_all();
9764 else
9765 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009766 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 RedrawingDisabled = r;
9768 p_lz = p;
9769 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770}
9771
9772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009773close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
9775 if (redir_fd != NULL)
9776 {
9777 fclose(redir_fd);
9778 redir_fd = NULL;
9779 }
9780#ifdef FEAT_EVAL
9781 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009782 if (redir_vname)
9783 {
9784 var_redir_stop();
9785 redir_vname = 0;
9786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787#endif
9788}
9789
9790#if defined(FEAT_SESSION) && defined(USE_CRNL)
9791# define MKSESSION_NL
9792static int mksession_nl = FALSE; /* use NL only in put_eol() */
9793#endif
9794
9795/*
9796 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9797 */
9798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009799ex_mkrc(
9800 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802 FILE *fd;
9803 int failed = FALSE;
9804 char_u *fname;
9805#ifdef FEAT_BROWSE
9806 char_u *browseFile = NULL;
9807#endif
9808#ifdef FEAT_SESSION
9809 int view_session = FALSE;
9810 int using_vdir = FALSE; /* using 'viewdir'? */
9811 char_u *viewFile = NULL;
9812 unsigned *flagp;
9813#endif
9814
9815 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9816 {
9817#ifdef FEAT_SESSION
9818 view_session = TRUE;
9819#else
9820 ex_ni(eap);
9821 return;
9822#endif
9823 }
9824
9825#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009826 /* Use the short file name until ":lcd" is used. We also don't use the
9827 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009828 did_lcd = FALSE;
9829
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9831 if (eap->cmdidx == CMD_mkview
9832 && (*eap->arg == NUL
9833 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9834 {
9835 eap->forceit = TRUE;
9836 fname = get_view_file(*eap->arg);
9837 if (fname == NULL)
9838 return;
9839 viewFile = fname;
9840 using_vdir = TRUE;
9841 }
9842 else
9843#endif
9844 if (*eap->arg != NUL)
9845 fname = eap->arg;
9846 else if (eap->cmdidx == CMD_mkvimrc)
9847 fname = (char_u *)VIMRC_FILE;
9848#ifdef FEAT_SESSION
9849 else if (eap->cmdidx == CMD_mksession)
9850 fname = (char_u *)SESSION_FILE;
9851#endif
9852 else
9853 fname = (char_u *)EXRC_FILE;
9854
9855#ifdef FEAT_BROWSE
9856 if (cmdmod.browse)
9857 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009858 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859# ifdef FEAT_SESSION
9860 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9861 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9862# endif
9863 (char_u *)_("Save Setup"),
9864 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9865 if (browseFile == NULL)
9866 goto theend;
9867 fname = browseFile;
9868 eap->forceit = TRUE; /* since dialog already asked */
9869 }
9870#endif
9871
9872#if defined(FEAT_SESSION) && defined(vim_mkdir)
9873 /* When using 'viewdir' may have to create the directory. */
9874 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009875 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876#endif
9877
9878 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9879 if (fd != NULL)
9880 {
9881#ifdef FEAT_SESSION
9882 if (eap->cmdidx == CMD_mkview)
9883 flagp = &vop_flags;
9884 else
9885 flagp = &ssop_flags;
9886#endif
9887
9888#ifdef MKSESSION_NL
9889 /* "unix" in 'sessionoptions': use NL line separator */
9890 if (view_session && (*flagp & SSOP_UNIX))
9891 mksession_nl = TRUE;
9892#endif
9893
9894 /* Write the version command for :mkvimrc */
9895 if (eap->cmdidx == CMD_mkvimrc)
9896 (void)put_line(fd, "version 6.0");
9897
9898#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009899 if (eap->cmdidx == CMD_mksession)
9900 {
9901 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9902 failed = TRUE;
9903 }
9904
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (eap->cmdidx != CMD_mkview)
9906#endif
9907 {
9908 /* Write setting 'compatible' first, because it has side effects.
9909 * For that same reason only do it when needed. */
9910 if (p_cp)
9911 (void)put_line(fd, "if !&cp | set cp | endif");
9912 else
9913 (void)put_line(fd, "if &cp | set nocp | endif");
9914 }
9915
9916#ifdef FEAT_SESSION
9917 if (!view_session
9918 || (eap->cmdidx == CMD_mksession
9919 && (*flagp & SSOP_OPTIONS)))
9920#endif
9921 failed |= (makemap(fd, NULL) == FAIL
9922 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9923
9924#ifdef FEAT_SESSION
9925 if (!failed && view_session)
9926 {
9927 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9928 failed = TRUE;
9929 if (eap->cmdidx == CMD_mksession)
9930 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009931 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932
Bram Moolenaard9462e32011-04-11 21:35:11 +02009933 dirnow = alloc(MAXPATHL);
9934 if (dirnow == NULL)
9935 failed = TRUE;
9936 else
9937 {
9938 /*
9939 * Change to session file's dir.
9940 */
9941 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009943 *dirnow = NUL;
9944 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9945 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009946 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009947 shorten_fnames(TRUE);
9948 }
9949 else if (*dirnow != NUL
9950 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9951 {
9952 if (mch_chdir((char *)globaldir) == 0)
9953 shorten_fnames(TRUE);
9954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
Bram Moolenaard9462e32011-04-11 21:35:11 +02009956 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957
Bram Moolenaard9462e32011-04-11 21:35:11 +02009958 /* restore original dir */
9959 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009961 {
9962 if (mch_chdir((char *)dirnow) != 0)
9963 EMSG(_(e_prev_dir));
9964 shorten_fnames(TRUE);
9965 }
9966 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 }
9968 }
9969 else
9970 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009971 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9972 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 }
9974 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9975 == FAIL)
9976 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009977 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9978 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009979 if (eap->cmdidx == CMD_mksession)
9980 {
9981 if (put_line(fd, "unlet SessionLoad") == FAIL)
9982 failed = TRUE;
9983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 }
9985#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009986 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9987 failed = TRUE;
9988
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 failed |= fclose(fd);
9990
9991 if (failed)
9992 EMSG(_(e_write));
9993#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9994 else if (eap->cmdidx == CMD_mksession)
9995 {
9996 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009997 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998
Bram Moolenaard9462e32011-04-11 21:35:11 +02009999 tbuf = alloc(MAXPATHL);
10000 if (tbuf != NULL)
10001 {
10002 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10003 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10004 vim_free(tbuf);
10005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 }
10007#endif
10008#ifdef MKSESSION_NL
10009 mksession_nl = FALSE;
10010#endif
10011 }
10012
10013#ifdef FEAT_BROWSE
10014theend:
10015 vim_free(browseFile);
10016#endif
10017#ifdef FEAT_SESSION
10018 vim_free(viewFile);
10019#endif
10020}
10021
Bram Moolenaardf177f62005-02-22 08:39:57 +000010022#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10023 || defined(PROTO)
10024 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010025vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010026{
10027 if (vim_mkdir(name, prot) != 0)
10028 {
10029 EMSG2(_("E739: Cannot create directory: %s"), name);
10030 return FAIL;
10031 }
10032 return OK;
10033}
10034#endif
10035
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036/*
10037 * Open a file for writing for an Ex command, with some checks.
10038 * Return file descriptor, or NULL on failure.
10039 */
10040 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010041open_exfile(
10042 char_u *fname,
10043 int forceit,
10044 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
10046 FILE *fd;
10047
10048#ifdef UNIX
10049 /* with Unix it is possible to open a directory */
10050 if (mch_isdir(fname))
10051 {
10052 EMSG2(_(e_isadir2), fname);
10053 return NULL;
10054 }
10055#endif
10056 if (!forceit && *mode != 'a' && vim_fexists(fname))
10057 {
10058 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10059 return NULL;
10060 }
10061
10062 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10063 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10064
10065 return fd;
10066}
10067
10068/*
10069 * ":mark" and ":k".
10070 */
10071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010072ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074 pos_T pos;
10075
10076 if (*eap->arg == NUL) /* No argument? */
10077 EMSG(_(e_argreq));
10078 else if (eap->arg[1] != NUL) /* more than one character? */
10079 EMSG(_(e_trailing));
10080 else
10081 {
10082 pos = curwin->w_cursor; /* save curwin->w_cursor */
10083 curwin->w_cursor.lnum = eap->line2;
10084 beginline(BL_WHITE | BL_FIX);
10085 if (setmark(*eap->arg) == FAIL) /* set mark */
10086 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10087 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10088 }
10089}
10090
10091/*
10092 * Update w_topline, w_leftcol and the cursor position.
10093 */
10094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010095update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097 check_cursor(); /* put cursor on valid line */
10098 update_topline();
10099 if (!curwin->w_p_wrap)
10100 validate_cursor();
10101 update_curswant();
10102}
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010105 * Save the current State and go to Normal mode.
10106 * Return TRUE if the typeahead could be saved.
10107 */
10108 int
10109save_current_state(save_state_T *sst)
10110{
10111 sst->save_msg_scroll = msg_scroll;
10112 sst->save_restart_edit = restart_edit;
10113 sst->save_msg_didout = msg_didout;
10114 sst->save_State = State;
10115 sst->save_insertmode = p_im;
10116 sst->save_finish_op = finish_op;
10117 sst->save_opcount = opcount;
10118
10119 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10120 restart_edit = 0; /* don't go to Insert mode */
10121 p_im = FALSE; /* don't use 'insertmode' */
10122
10123 /*
10124 * Save the current typeahead. This is required to allow using ":normal"
10125 * from an event handler and makes sure we don't hang when the argument
10126 * ends with half a command.
10127 */
10128 save_typeahead(&sst->tabuf);
10129 return sst->tabuf.typebuf_valid;
10130}
10131
10132 void
10133restore_current_state(save_state_T *sst)
10134{
10135 /* Restore the previous typeahead. */
10136 restore_typeahead(&sst->tabuf);
10137
10138 msg_scroll = sst->save_msg_scroll;
10139 restart_edit = sst->save_restart_edit;
10140 p_im = sst->save_insertmode;
10141 finish_op = sst->save_finish_op;
10142 opcount = sst->save_opcount;
10143 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10144
10145 /* Restore the state (needed when called from a function executed for
10146 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10147 State = sst->save_State;
10148#ifdef CURSOR_SHAPE
10149 ui_cursor_shape(); /* may show different cursor shape */
10150#endif
10151}
10152
10153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 * ":normal[!] {commands}": Execute normal mode commands.
10155 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010156 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010157ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010159 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#ifdef FEAT_MBYTE
10161 char_u *arg = NULL;
10162 int l;
10163 char_u *p;
10164#endif
10165
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010166 if (ex_normal_lock > 0)
10167 {
10168 EMSG(_(e_secure));
10169 return;
10170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 if (ex_normal_busy >= p_mmd)
10172 {
10173 EMSG(_("E192: Recursive use of :normal too deep"));
10174 return;
10175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176
10177#ifdef FEAT_MBYTE
10178 /*
10179 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10180 * this for the K_SPECIAL leading byte, otherwise special keys will not
10181 * work.
10182 */
10183 if (has_mbyte)
10184 {
10185 int len = 0;
10186
10187 /* Count the number of characters to be escaped. */
10188 for (p = eap->arg; *p != NUL; ++p)
10189 {
10190# ifdef FEAT_GUI
10191 if (*p == CSI) /* leadbyte CSI */
10192 len += 2;
10193# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010194 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10196# ifdef FEAT_GUI
10197 || *p == CSI
10198# endif
10199 )
10200 len += 2;
10201 }
10202 if (len > 0)
10203 {
10204 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10205 if (arg != NULL)
10206 {
10207 len = 0;
10208 for (p = eap->arg; *p != NUL; ++p)
10209 {
10210 arg[len++] = *p;
10211# ifdef FEAT_GUI
10212 if (*p == CSI)
10213 {
10214 arg[len++] = KS_EXTRA;
10215 arg[len++] = (int)KE_CSI;
10216 }
10217# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010218 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 {
10220 arg[len++] = *++p;
10221 if (*p == K_SPECIAL)
10222 {
10223 arg[len++] = KS_SPECIAL;
10224 arg[len++] = KE_FILLER;
10225 }
10226# ifdef FEAT_GUI
10227 else if (*p == CSI)
10228 {
10229 arg[len++] = KS_EXTRA;
10230 arg[len++] = (int)KE_CSI;
10231 }
10232# endif
10233 }
10234 arg[len] = NUL;
10235 }
10236 }
10237 }
10238 }
10239#endif
10240
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010241 ++ex_normal_busy;
10242 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 {
10244 /*
10245 * Repeat the :normal command for each line in the range. When no
10246 * range given, execute it just once, without positioning the cursor
10247 * first.
10248 */
10249 do
10250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 if (eap->addr_count != 0)
10252 {
10253 curwin->w_cursor.lnum = eap->line1++;
10254 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010255 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010258 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259#ifdef FEAT_MBYTE
10260 arg != NULL ? arg :
10261#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010262 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 }
10264 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10265 }
10266
10267 /* Might not return to the main loop when in an event handler. */
10268 update_topline_cursor();
10269
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010270 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010272#ifdef FEAT_MOUSE
10273 setmouse();
10274#endif
10275#ifdef CURSOR_SHAPE
10276 ui_cursor_shape(); /* may show different cursor shape */
10277#endif
10278
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279#ifdef FEAT_MBYTE
10280 vim_free(arg);
10281#endif
10282}
10283
10284/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010285 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 */
10287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010288ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010290 if (eap->forceit)
10291 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010292 /* cursor line can be zero on startup */
10293 if (!curwin->w_cursor.lnum)
10294 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010295 coladvance((colnr_T)MAXCOL);
10296 curwin->w_curswant = MAXCOL;
10297 curwin->w_set_curswant = FALSE;
10298 }
10299
Bram Moolenaara40c5002005-01-09 21:16:21 +000010300 /* Ignore the command when already in Insert mode. Inserting an
10301 * expression register that invokes a function can do this. */
10302 if (State & INSERT)
10303 return;
10304
Bram Moolenaar64969662005-12-14 21:59:55 +000010305 if (eap->cmdidx == CMD_startinsert)
10306 restart_edit = 'a';
10307 else if (eap->cmdidx == CMD_startreplace)
10308 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010310 restart_edit = 'V';
10311
10312 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010314 if (eap->cmdidx == CMD_startinsert)
10315 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 curwin->w_curswant = 0; /* avoid MAXCOL */
10317 }
10318}
10319
10320/*
10321 * ":stopinsert"
10322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010324ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325{
10326 restart_edit = 0;
10327 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010328 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010331/*
10332 * Execute normal mode command "cmd".
10333 * "remap" can be REMAP_NONE or REMAP_YES.
10334 */
10335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010336exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010337{
Bram Moolenaar25281632016-01-21 23:32:32 +010010338 /* Stuff the argument into the typeahead buffer. */
10339 ins_typebuf(cmd, remap, 0, TRUE, silent);
10340 exec_normal(FALSE);
10341}
Bram Moolenaar25281632016-01-21 23:32:32 +010010342
Bram Moolenaar25281632016-01-21 23:32:32 +010010343/*
10344 * Execute normal_cmd() until there is no typeahead left.
10345 */
10346 void
10347exec_normal(int was_typed)
10348{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010349 oparg_T oa;
10350
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010351 clear_oparg(&oa);
10352 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010353 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10354 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010355 {
10356 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010357 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010358 }
10359}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010360
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361#ifdef FEAT_FIND_ID
10362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010363ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
10365 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10366 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10367 (linenr_T)1, (linenr_T)MAXLNUM);
10368}
10369
Bram Moolenaar4033c552017-09-16 20:54:51 +020010370#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371/*
10372 * ":psearch"
10373 */
10374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010375ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376{
10377 g_do_tagpreview = p_pvh;
10378 ex_findpat(eap);
10379 g_do_tagpreview = 0;
10380}
10381#endif
10382
10383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010384ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385{
10386 int whole = TRUE;
10387 long n;
10388 char_u *p;
10389 int action;
10390
10391 switch (cmdnames[eap->cmdidx].cmd_name[2])
10392 {
10393 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10394 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10395 action = ACTION_GOTO;
10396 else
10397 action = ACTION_SHOW;
10398 break;
10399 case 'i': /* ":ilist" and ":dlist" */
10400 action = ACTION_SHOW_ALL;
10401 break;
10402 case 'u': /* ":ijump" and ":djump" */
10403 action = ACTION_GOTO;
10404 break;
10405 default: /* ":isplit" and ":dsplit" */
10406 action = ACTION_SPLIT;
10407 break;
10408 }
10409
10410 n = 1;
10411 if (vim_isdigit(*eap->arg)) /* get count */
10412 {
10413 n = getdigits(&eap->arg);
10414 eap->arg = skipwhite(eap->arg);
10415 }
10416 if (*eap->arg == '/') /* Match regexp, not just whole words */
10417 {
10418 whole = FALSE;
10419 ++eap->arg;
10420 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10421 if (*p)
10422 {
10423 *p++ = NUL;
10424 p = skipwhite(p);
10425
10426 /* Check for trailing illegal characters */
10427 if (!ends_excmd(*p))
10428 eap->errmsg = e_trailing;
10429 else
10430 eap->nextcmd = check_nextcmd(p);
10431 }
10432 }
10433 if (!eap->skip)
10434 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10435 whole, !eap->forceit,
10436 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10437 n, action, eap->line1, eap->line2);
10438}
10439#endif
10440
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441
Bram Moolenaar4033c552017-09-16 20:54:51 +020010442#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443/*
10444 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10445 */
10446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010447ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010449 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10451}
10452
10453/*
10454 * ":pedit"
10455 */
10456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010457ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
10459 win_T *curwin_save = curwin;
10460
10461 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010462 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010463 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 do_exedit(eap, NULL);
10465 keep_help_flag = FALSE;
10466 if (curwin != curwin_save && win_valid(curwin_save))
10467 {
10468 /* Return cursor to where we were */
10469 validate_cursor();
10470 redraw_later(VALID);
10471 win_enter(curwin_save, TRUE);
10472 }
10473 g_do_tagpreview = 0;
10474}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476
10477/*
10478 * ":stag", ":stselect" and ":stjump".
10479 */
10480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010481ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
10483 postponed_split = -1;
10484 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010485 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10487 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010488 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490
10491/*
10492 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10493 */
10494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010495ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10498}
10499
10500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010501ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503 int cmd;
10504
10505 switch (name[1])
10506 {
10507 case 'j': cmd = DT_JUMP; /* ":tjump" */
10508 break;
10509 case 's': cmd = DT_SELECT; /* ":tselect" */
10510 break;
10511 case 'p': cmd = DT_PREV; /* ":tprevious" */
10512 break;
10513 case 'N': cmd = DT_PREV; /* ":tNext" */
10514 break;
10515 case 'n': cmd = DT_NEXT; /* ":tnext" */
10516 break;
10517 case 'o': cmd = DT_POP; /* ":pop" */
10518 break;
10519 case 'f': /* ":tfirst" */
10520 case 'r': cmd = DT_FIRST; /* ":trewind" */
10521 break;
10522 case 'l': cmd = DT_LAST; /* ":tlast" */
10523 break;
10524 default: /* ":tag" */
10525#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010526 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010528 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 return;
10530 }
10531#endif
10532 cmd = DT_TAG;
10533 break;
10534 }
10535
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010536 if (name[0] == 'l')
10537 {
10538#ifndef FEAT_QUICKFIX
10539 ex_ni(eap);
10540 return;
10541#else
10542 cmd = DT_LTAG;
10543#endif
10544 }
10545
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10547 eap->forceit, TRUE);
10548}
10549
10550/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010551 * Check "str" for starting with a special cmdline variable.
10552 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10553 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10554 */
10555 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010556find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010557{
10558 int len;
10559 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010560 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010561 "%",
10562#define SPEC_PERC 0
10563 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010564#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010565 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010566#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010567 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010568#define SPEC_CCWORD (SPEC_CWORD + 1)
10569 "<cexpr>", /* expr under cursor */
10570#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010571 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010572#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010573 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010574#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010575 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010576#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010577#ifdef FEAT_AUTOCMD
10578 "<afile>", /* autocommand file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010579# define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010580 "<abuf>", /* autocommand buffer number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010581# define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010582 "<amatch>", /* autocommand match name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010583# define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010584#endif
10585#ifdef FEAT_CLIENTSERVER
10586 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010587# ifdef FEAT_AUTOCMD
Bram Moolenaar65f08472017-09-10 18:16:20 +020010588# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010589# else
Bram Moolenaar65f08472017-09-10 18:16:20 +020010590# define SPEC_CLIENT (SPEC_SLNUM + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010591# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010592#endif
10593 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010594
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010595 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010596 {
10597 len = (int)STRLEN(spec_str[i]);
10598 if (STRNCMP(src, spec_str[i], len) == 0)
10599 {
10600 *usedlen = len;
10601 return i;
10602 }
10603 }
10604 return -1;
10605}
10606
10607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 * Evaluate cmdline variables.
10609 *
10610 * change '%' to curbuf->b_ffname
10611 * '#' to curwin->w_altfile
10612 * '<cword>' to word under the cursor
10613 * '<cWORD>' to WORD under the cursor
10614 * '<cfile>' to path name under the cursor
10615 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010616 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 * '<afile>' to file name for autocommand
10618 * '<abuf>' to buffer number for autocommand
10619 * '<amatch>' to matching name for autocommand
10620 *
10621 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10622 * "" for error without a message) and NULL is returned.
10623 * Returns an allocated string if a valid match was found.
10624 * Returns NULL if no match was found. "usedlen" then still contains the
10625 * number of characters to skip.
10626 */
10627 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010628eval_vars(
10629 char_u *src, /* pointer into commandline */
10630 char_u *srcstart, /* beginning of valid memory for src */
10631 int *usedlen, /* characters after src that are used */
10632 linenr_T *lnump, /* line number for :e command, or NULL */
10633 char_u **errormsg, /* pointer to error message */
10634 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010635 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636{
10637 int i;
10638 char_u *s;
10639 char_u *result;
10640 char_u *resultbuf = NULL;
10641 int resultlen;
10642 buf_T *buf;
10643 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10644 int spec_idx;
10645#ifdef FEAT_MODIFY_FNAME
10646 int skip_mod = FALSE;
10647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649
10650 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010651 if (escaped != NULL)
10652 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653
10654 /*
10655 * Check if there is something to do.
10656 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010657 spec_idx = find_cmdline_var(src, usedlen);
10658 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 {
10660 *usedlen = 1;
10661 return NULL;
10662 }
10663
10664 /*
10665 * Skip when preceded with a backslash "\%" and "\#".
10666 * Note: In "\\%" the % is also not recognized!
10667 */
10668 if (src > srcstart && src[-1] == '\\')
10669 {
10670 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010671 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 return NULL;
10673 }
10674
10675 /*
10676 * word or WORD under cursor
10677 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010678 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10679 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010681 resultlen = find_ident_under_cursor(&result,
10682 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10683 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10684 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 if (resultlen == 0)
10686 {
10687 *errormsg = (char_u *)"";
10688 return NULL;
10689 }
10690 }
10691
10692 /*
10693 * '#': Alternate file name
10694 * '%': Current file name
10695 * File name under the cursor
10696 * File name for autocommand
10697 * and following modifiers
10698 */
10699 else
10700 {
10701 switch (spec_idx)
10702 {
10703 case SPEC_PERC: /* '%': current file */
10704 if (curbuf->b_fname == NULL)
10705 {
10706 result = (char_u *)"";
10707 valid = 0; /* Must have ":p:h" to be valid */
10708 }
10709 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 break;
10712
10713 case SPEC_HASH: /* '#' or "#99": alternate file */
10714 if (src[1] == '#') /* "##": the argument list */
10715 {
10716 result = arg_all();
10717 resultbuf = result;
10718 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010719 if (escaped != NULL)
10720 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721#ifdef FEAT_MODIFY_FNAME
10722 skip_mod = TRUE;
10723#endif
10724 break;
10725 }
10726 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010727 if (*s == '<') /* "#<99" uses v:oldfiles */
10728 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010730 if (s == src + 2 && src[1] == '-')
10731 /* just a minus sign, don't skip over it */
10732 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 *usedlen = (int)(s - src); /* length of what we expand */
10734
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010735 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010737 if (*usedlen < 2)
10738 {
10739 /* Should we give an error message for #<text? */
10740 *usedlen = 1;
10741 return NULL;
10742 }
10743#ifdef FEAT_EVAL
10744 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10745 (long)i);
10746 if (result == NULL)
10747 {
10748 *errormsg = (char_u *)"";
10749 return NULL;
10750 }
10751#else
10752 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 }
10756 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010757 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010758 if (i == 0 && src[1] == '<' && *usedlen > 1)
10759 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010760 buf = buflist_findnr(i);
10761 if (buf == NULL)
10762 {
10763 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10764 return NULL;
10765 }
10766 if (lnump != NULL)
10767 *lnump = ECMD_LAST;
10768 if (buf->b_fname == NULL)
10769 {
10770 result = (char_u *)"";
10771 valid = 0; /* Must have ":p:h" to be valid */
10772 }
10773 else
10774 result = buf->b_fname;
10775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 break;
10777
10778#ifdef FEAT_SEARCHPATH
10779 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010780 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 if (result == NULL)
10782 {
10783 *errormsg = (char_u *)"";
10784 return NULL;
10785 }
10786 resultbuf = result; /* remember allocated string */
10787 break;
10788#endif
10789
10790#ifdef FEAT_AUTOCMD
10791 case SPEC_AFILE: /* file name for autocommand */
10792 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010793 if (result != NULL && !autocmd_fname_full)
10794 {
10795 /* Still need to turn the fname into a full path. It is
10796 * postponed to avoid a delay when <afile> is not used. */
10797 autocmd_fname_full = TRUE;
10798 result = FullName_save(autocmd_fname, FALSE);
10799 vim_free(autocmd_fname);
10800 autocmd_fname = result;
10801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 if (result == NULL)
10803 {
10804 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10805 return NULL;
10806 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010807 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 break;
10809
10810 case SPEC_ABUF: /* buffer number for autocommand */
10811 if (autocmd_bufnr <= 0)
10812 {
10813 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10814 return NULL;
10815 }
10816 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10817 result = strbuf;
10818 break;
10819
10820 case SPEC_AMATCH: /* match name for autocommand */
10821 result = autocmd_match;
10822 if (result == NULL)
10823 {
10824 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10825 return NULL;
10826 }
10827 break;
10828
10829#endif
10830 case SPEC_SFILE: /* file name for ":so" command */
10831 result = sourcing_name;
10832 if (result == NULL)
10833 {
10834 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10835 return NULL;
10836 }
10837 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010838 case SPEC_SLNUM: /* line in file for ":so" command */
10839 if (sourcing_name == NULL || sourcing_lnum == 0)
10840 {
10841 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10842 return NULL;
10843 }
10844 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10845 result = strbuf;
10846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847#if defined(FEAT_CLIENTSERVER)
10848 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010849 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10850 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 result = strbuf;
10852 break;
10853#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010854 default:
10855 result = (char_u *)""; /* avoid gcc warning */
10856 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 }
10858
10859 resultlen = (int)STRLEN(result); /* length of new string */
10860 if (src[*usedlen] == '<') /* remove the file name extension */
10861 {
10862 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 resultlen = (int)(s - result);
10865 }
10866#ifdef FEAT_MODIFY_FNAME
10867 else if (!skip_mod)
10868 {
10869 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10870 &resultlen);
10871 if (result == NULL)
10872 {
10873 *errormsg = (char_u *)"";
10874 return NULL;
10875 }
10876 }
10877#endif
10878 }
10879
10880 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10881 {
10882 if (valid != VALID_HEAD + VALID_PATH)
10883 /* xgettext:no-c-format */
10884 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10885 else
10886 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10887 result = NULL;
10888 }
10889 else
10890 result = vim_strnsave(result, resultlen);
10891 vim_free(resultbuf);
10892 return result;
10893}
10894
10895/*
10896 * Concatenate all files in the argument list, separated by spaces, and return
10897 * it in one allocated string.
10898 * Spaces and backslashes in the file names are escaped with a backslash.
10899 * Returns NULL when out of memory.
10900 */
10901 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010902arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903{
10904 int len;
10905 int idx;
10906 char_u *retval = NULL;
10907 char_u *p;
10908
10909 /*
10910 * Do this loop two times:
10911 * first time: compute the total length
10912 * second time: concatenate the names
10913 */
10914 for (;;)
10915 {
10916 len = 0;
10917 for (idx = 0; idx < ARGCOUNT; ++idx)
10918 {
10919 p = alist_name(&ARGLIST[idx]);
10920 if (p != NULL)
10921 {
10922 if (len > 0)
10923 {
10924 /* insert a space in between names */
10925 if (retval != NULL)
10926 retval[len] = ' ';
10927 ++len;
10928 }
10929 for ( ; *p != NUL; ++p)
10930 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010931 if (*p == ' '
10932#ifndef BACKSLASH_IN_FILENAME
10933 || *p == '\\'
10934#endif
10935 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 {
10937 /* insert a backslash */
10938 if (retval != NULL)
10939 retval[len] = '\\';
10940 ++len;
10941 }
10942 if (retval != NULL)
10943 retval[len] = *p;
10944 ++len;
10945 }
10946 }
10947 }
10948
10949 /* second time: break here */
10950 if (retval != NULL)
10951 {
10952 retval[len] = NUL;
10953 break;
10954 }
10955
10956 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010957 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 if (retval == NULL)
10959 break;
10960 }
10961
10962 return retval;
10963}
10964
10965#if defined(FEAT_AUTOCMD) || defined(PROTO)
10966/*
10967 * Expand the <sfile> string in "arg".
10968 *
10969 * Returns an allocated string, or NULL for any error.
10970 */
10971 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010972expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973{
10974 char_u *errormsg;
10975 int len;
10976 char_u *result;
10977 char_u *newres;
10978 char_u *repl;
10979 int srclen;
10980 char_u *p;
10981
10982 result = vim_strsave(arg);
10983 if (result == NULL)
10984 return NULL;
10985
10986 for (p = result; *p; )
10987 {
10988 if (STRNCMP(p, "<sfile>", 7) != 0)
10989 ++p;
10990 else
10991 {
10992 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010993 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 if (errormsg != NULL)
10995 {
10996 if (*errormsg)
10997 emsg(errormsg);
10998 vim_free(result);
10999 return NULL;
11000 }
11001 if (repl == NULL) /* no match (cannot happen) */
11002 {
11003 p += srclen;
11004 continue;
11005 }
11006 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11007 newres = alloc(len);
11008 if (newres == NULL)
11009 {
11010 vim_free(repl);
11011 vim_free(result);
11012 return NULL;
11013 }
11014 mch_memmove(newres, result, (size_t)(p - result));
11015 STRCPY(newres + (p - result), repl);
11016 len = (int)STRLEN(newres);
11017 STRCAT(newres, p + srclen);
11018 vim_free(repl);
11019 vim_free(result);
11020 result = newres;
11021 p = newres + len; /* continue after the match */
11022 }
11023 }
11024
11025 return result;
11026}
11027#endif
11028
11029#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011030static int ses_winsizes(FILE *fd, int restore_size,
11031 win_T *tab_firstwin);
11032static int ses_win_rec(FILE *fd, frame_T *fr);
11033static frame_T *ses_skipframe(frame_T *fr);
11034static int ses_do_frame(frame_T *fr);
11035static int ses_do_win(win_T *wp);
11036static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11037static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011038static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039
11040/*
11041 * Write openfile commands for the current buffers to an .exrc file.
11042 * Return FAIL on error, OK otherwise.
11043 */
11044 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011045makeopens(
11046 FILE *fd,
11047 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048{
11049 buf_T *buf;
11050 int only_save_windows = TRUE;
11051 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 int restore_size = TRUE;
11053 win_T *wp;
11054 char_u *sname;
11055 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011056 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011057 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011058 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011059 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011060 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011061 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062
11063 if (ssop_flags & SSOP_BUFFERS)
11064 only_save_windows = FALSE; /* Save ALL buffers */
11065
11066 /*
11067 * Begin by setting the this_session variable, and then other
11068 * sessionable variables.
11069 */
11070#ifdef FEAT_EVAL
11071 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11072 return FAIL;
11073 if (ssop_flags & SSOP_GLOBALS)
11074 if (store_session_globals(fd) == FAIL)
11075 return FAIL;
11076#endif
11077
11078 /*
11079 * Close all windows but one.
11080 */
11081 if (put_line(fd, "silent only") == FAIL)
11082 return FAIL;
11083
11084 /*
11085 * Now a :cd command to the session directory or the current directory
11086 */
11087 if (ssop_flags & SSOP_SESDIR)
11088 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011089 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11090 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 return FAIL;
11092 }
11093 else if (ssop_flags & SSOP_CURDIR)
11094 {
11095 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11096 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011097 || fputs("cd ", fd) < 0
11098 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11099 || put_eol(fd) == FAIL)
11100 {
11101 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 vim_free(sname);
11105 }
11106
11107 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011108 * If there is an empty, unnamed buffer we will wipe it out later.
11109 * Remember the buffer number.
11110 */
11111 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11112 return FAIL;
11113 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11114 return FAIL;
11115 if (put_line(fd, "endif") == FAIL)
11116 return FAIL;
11117
11118 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 * Now save the current files, current buffer first.
11120 */
11121 if (put_line(fd, "set shortmess=aoO") == FAIL)
11122 return FAIL;
11123
11124 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011125 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 {
11127 if (!(only_save_windows && buf->b_nwindows == 0)
11128 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11129 && buf->b_fname != NULL
11130 && buf->b_p_bl)
11131 {
11132 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11133 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011134 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 return FAIL;
11136 }
11137 }
11138
11139 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011140 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11142 return FAIL;
11143
11144 if (ssop_flags & SSOP_RESIZE)
11145 {
11146 /* Note: after the restore we still check it worked!*/
11147 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11148 || put_eol(fd) == FAIL)
11149 return FAIL;
11150 }
11151
11152#ifdef FEAT_GUI
11153 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11154 {
11155 int x, y;
11156
11157 if (gui_mch_get_winpos(&x, &y) == OK)
11158 {
11159 /* Note: after the restore we still check it worked!*/
11160 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11161 return FAIL;
11162 }
11163 }
11164#endif
11165
11166 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011167 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11168 * will be displayed when creating the next tab. That resizes the windows
11169 * in the first tab, which may cause problems. Set 'showtabline' to 2
11170 * temporarily to avoid that.
11171 */
11172 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11173 {
11174 if (put_line(fd, "set stal=2") == FAIL)
11175 return FAIL;
11176 restore_stal = TRUE;
11177 }
11178
11179 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011180 * May repeat putting Windows for each tab, when "tabpages" is in
11181 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011182 * Don't use goto_tabpage(), it may change directory and trigger
11183 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011185 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011186 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011187 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011189 int need_tabnew = FALSE;
11190 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011191
Bram Moolenaar18144c82006-04-12 21:52:12 +000011192 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011194 tabpage_T *tp = find_tabpage(tabnr);
11195
11196 if (tp == NULL)
11197 break; /* done all tab pages */
11198 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011199 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011200 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011201 tab_topframe = topframe;
11202 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011203 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011204 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011205 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011206 tab_topframe = tp->tp_topframe;
11207 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011208 if (tabnr > 1)
11209 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211
Bram Moolenaar18144c82006-04-12 21:52:12 +000011212 /*
11213 * Before creating the window layout, try loading one file. If this
11214 * is aborted we don't end up with a number of useless windows.
11215 * This may have side effects! (e.g., compressed or network file).
11216 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011217 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011218 {
11219 if (ses_do_win(wp)
11220 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011221 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011222#ifdef FEAT_QUICKFIX
11223 && !bt_nofile(wp->w_buffer)
11224#endif
11225 )
11226 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011227 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011228 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11229 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011230 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011231 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011232 if (!wp->w_arg_idx_invalid)
11233 edited_win = wp;
11234 break;
11235 }
11236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011238 /* If no file got edited create an empty tab page. */
11239 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11240 return FAIL;
11241
Bram Moolenaar18144c82006-04-12 21:52:12 +000011242 /*
11243 * Save current window layout.
11244 */
11245 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011247 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011249 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11250 return FAIL;
11251 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11252 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253
Bram Moolenaar18144c82006-04-12 21:52:12 +000011254 /*
11255 * Check if window sizes can be restored (no windows omitted).
11256 * Remember the window number of the current window after restoring.
11257 */
11258 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011259 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011260 {
11261 if (ses_do_win(wp))
11262 ++nr;
11263 else
11264 restore_size = FALSE;
11265 if (curwin == wp)
11266 cnr = nr;
11267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268
Bram Moolenaar18144c82006-04-12 21:52:12 +000011269 /* Go to the first window. */
11270 if (put_line(fd, "wincmd t") == FAIL)
11271 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011272
Bram Moolenaar18144c82006-04-12 21:52:12 +000011273 /*
11274 * If more than one window, see if sizes can be restored.
11275 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11276 * resized when moving between windows.
11277 * Do this before restoring the view, so that the topline and the
11278 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011279 * winminheight and winminwidth need to be set to avoid an error if the
11280 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011281 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011282 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011283 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011284 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286
Bram Moolenaar18144c82006-04-12 21:52:12 +000011287 /*
11288 * Restore the view of the window (options, file, cursor, etc.).
11289 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011290 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011291 {
11292 if (!ses_do_win(wp))
11293 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011294 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11295 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011296 return FAIL;
11297 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11298 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011299 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011300 }
11301
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011302 /* The argument index in the first tab page is zero, need to set it in
11303 * each window. For further tab pages it's the window where we do
11304 * "tabedit". */
11305 cur_arg_idx = next_arg_idx;
11306
Bram Moolenaar18144c82006-04-12 21:52:12 +000011307 /*
11308 * Restore cursor to the current window if it's not the first one.
11309 */
11310 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11311 || put_eol(fd) == FAIL))
11312 return FAIL;
11313
11314 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011315 * Restore window sizes again after jumping around in windows, because
11316 * the current window has a minimum size while others may not.
11317 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011318 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011319 return FAIL;
11320
Bram Moolenaar18144c82006-04-12 21:52:12 +000011321 /* Don't continue in another tab page when doing only the current one
11322 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011323 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011324 break;
11325 }
11326
11327 if (ssop_flags & SSOP_TABPAGES)
11328 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011329 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11330 || put_eol(fd) == FAIL)
11331 return FAIL;
11332 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011333 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11334 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011335
Bram Moolenaar9c102382006-05-03 21:26:49 +000011336 /*
11337 * Wipe out an empty unnamed buffer we started in.
11338 */
11339 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11340 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011341 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011342 return FAIL;
11343 if (put_line(fd, "endif") == FAIL)
11344 return FAIL;
11345 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11346 return FAIL;
11347
11348 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11349 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11350 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11351 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011352 /* Re-apply 'winminheight' and 'winminwidth'. */
11353 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11354 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356
11357 /*
11358 * Lastly, execute the x.vim file if it exists.
11359 */
11360 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11361 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011362 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 || put_line(fd, "endif") == FAIL)
11364 return FAIL;
11365
11366 return OK;
11367}
11368
11369 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011370ses_winsizes(
11371 FILE *fd,
11372 int restore_size,
11373 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
11375 int n = 0;
11376 win_T *wp;
11377
11378 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11379 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011380 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 {
11382 if (!ses_do_win(wp))
11383 continue;
11384 ++n;
11385
11386 /* restore height when not full height */
11387 if (wp->w_height + wp->w_status_height < topframe->fr_height
11388 && (fprintf(fd,
11389 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11390 n, (long)wp->w_height, Rows / 2, Rows) < 0
11391 || put_eol(fd) == FAIL))
11392 return FAIL;
11393
11394 /* restore width when not full width */
11395 if (wp->w_width < Columns && (fprintf(fd,
11396 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11397 n, (long)wp->w_width, Columns / 2, Columns) < 0
11398 || put_eol(fd) == FAIL))
11399 return FAIL;
11400 }
11401 }
11402 else
11403 {
11404 /* Just equalise window sizes */
11405 if (put_line(fd, "wincmd =") == FAIL)
11406 return FAIL;
11407 }
11408 return OK;
11409}
11410
11411/*
11412 * Write commands to "fd" to recursively create windows for frame "fr",
11413 * horizontally and vertically split.
11414 * After the commands the last window in the frame is the current window.
11415 * Returns FAIL when writing the commands to "fd" fails.
11416 */
11417 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011418ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419{
11420 frame_T *frc;
11421 int count = 0;
11422
11423 if (fr->fr_layout != FR_LEAF)
11424 {
11425 /* Find first frame that's not skipped and then create a window for
11426 * each following one (first frame is already there). */
11427 frc = ses_skipframe(fr->fr_child);
11428 if (frc != NULL)
11429 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11430 {
11431 /* Make window as big as possible so that we have lots of room
11432 * to split. */
11433 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11434 || put_line(fd, fr->fr_layout == FR_COL
11435 ? "split" : "vsplit") == FAIL)
11436 return FAIL;
11437 ++count;
11438 }
11439
11440 /* Go back to the first window. */
11441 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11442 ? "%dwincmd k" : "%dwincmd h", count) < 0
11443 || put_eol(fd) == FAIL))
11444 return FAIL;
11445
11446 /* Recursively create frames/windows in each window of this column or
11447 * row. */
11448 frc = ses_skipframe(fr->fr_child);
11449 while (frc != NULL)
11450 {
11451 ses_win_rec(fd, frc);
11452 frc = ses_skipframe(frc->fr_next);
11453 /* Go to next window. */
11454 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11455 return FAIL;
11456 }
11457 }
11458 return OK;
11459}
11460
11461/*
11462 * Skip frames that don't contain windows we want to save in the Session.
11463 * Returns NULL when there none.
11464 */
11465 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011466ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467{
11468 frame_T *frc;
11469
11470 for (frc = fr; frc != NULL; frc = frc->fr_next)
11471 if (ses_do_frame(frc))
11472 break;
11473 return frc;
11474}
11475
11476/*
11477 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11478 * the Session.
11479 */
11480 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011481ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482{
11483 frame_T *frc;
11484
11485 if (fr->fr_layout == FR_LEAF)
11486 return ses_do_win(fr->fr_win);
11487 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11488 if (ses_do_frame(frc))
11489 return TRUE;
11490 return FALSE;
11491}
11492
11493/*
11494 * Return non-zero if window "wp" is to be stored in the Session.
11495 */
11496 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011497ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498{
11499 if (wp->w_buffer->b_fname == NULL
11500#ifdef FEAT_QUICKFIX
11501 /* When 'buftype' is "nofile" can't restore the window contents. */
11502 || bt_nofile(wp->w_buffer)
11503#endif
11504 )
11505 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011506 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 return (ssop_flags & SSOP_HELP);
11508 return TRUE;
11509}
11510
11511/*
11512 * Write commands to "fd" to restore the view of a window.
11513 * Caller must make sure 'scrolloff' is zero.
11514 */
11515 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011516put_view(
11517 FILE *fd,
11518 win_T *wp,
11519 int add_edit, /* add ":edit" command to view */
11520 unsigned *flagp, /* vop_flags or ssop_flags */
11521 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011522 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523{
11524 win_T *save_curwin;
11525 int f;
11526 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011527 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528
11529 /* Always restore cursor position for ":mksession". For ":mkview" only
11530 * when 'viewoptions' contains "cursor". */
11531 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11532
11533 /*
11534 * Local argument list.
11535 */
11536 if (wp->w_alist == &global_alist)
11537 {
11538 if (put_line(fd, "argglobal") == FAIL)
11539 return FAIL;
11540 }
11541 else
11542 {
11543 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11544 flagp == &vop_flags
11545 || !(*flagp & SSOP_CURDIR)
11546 || wp->w_localdir != NULL, flagp) == FAIL)
11547 return FAIL;
11548 }
11549
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011550 /* Only when part of a session: restore the argument index. Some
11551 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011552 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011553 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011555 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 || put_eol(fd) == FAIL)
11557 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011558 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 }
11560
11561 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011562 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 {
11564 /*
11565 * Load the file.
11566 */
11567 if (wp->w_buffer->b_ffname != NULL
11568#ifdef FEAT_QUICKFIX
11569 && !bt_nofile(wp->w_buffer)
11570#endif
11571 )
11572 {
11573 /*
11574 * Editing a file in this buffer: use ":edit file".
11575 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011576 *
11577 * Note, if a buffer for that file already exists, use :badd to
11578 * edit that buffer, to not lose folding information (:edit resets
11579 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011581 if (fputs("if bufexists('", fd) < 0
11582 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11583 || fputs("') | buffer ", fd) < 0
11584 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11585 || fputs(" | else | edit ", fd) < 0
11586 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11587 || fputs(" | endif", fd) < 0
11588 ||
11589 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 return FAIL;
11591 }
11592 else
11593 {
11594 /* No file in this buffer, just make it empty. */
11595 if (put_line(fd, "enew") == FAIL)
11596 return FAIL;
11597#ifdef FEAT_QUICKFIX
11598 if (wp->w_buffer->b_ffname != NULL)
11599 {
11600 /* The buffer does have a name, but it's not a file name. */
11601 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011602 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 return FAIL;
11604 }
11605#endif
11606 do_cursor = FALSE;
11607 }
11608 }
11609
11610 /*
11611 * Local mappings and abbreviations.
11612 */
11613 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11614 && makemap(fd, wp->w_buffer) == FAIL)
11615 return FAIL;
11616
11617 /*
11618 * Local options. Need to go to the window temporarily.
11619 * Store only local values when using ":mkview" and when ":mksession" is
11620 * used and 'sessionoptions' doesn't include "options".
11621 * Some folding options are always stored when "folds" is included,
11622 * otherwise the folds would not be restored correctly.
11623 */
11624 save_curwin = curwin;
11625 curwin = wp;
11626 curbuf = curwin->w_buffer;
11627 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11628 f = makeset(fd, OPT_LOCAL,
11629 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11630#ifdef FEAT_FOLDING
11631 else if (*flagp & SSOP_FOLDS)
11632 f = makefoldset(fd);
11633#endif
11634 else
11635 f = OK;
11636 curwin = save_curwin;
11637 curbuf = curwin->w_buffer;
11638 if (f == FAIL)
11639 return FAIL;
11640
11641#ifdef FEAT_FOLDING
11642 /*
11643 * Save Folds when 'buftype' is empty and for help files.
11644 */
11645 if ((*flagp & SSOP_FOLDS)
11646 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011647 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 {
11649 if (put_folds(fd, wp) == FAIL)
11650 return FAIL;
11651 }
11652#endif
11653
11654 /*
11655 * Set the cursor after creating folds, since that moves the cursor.
11656 */
11657 if (do_cursor)
11658 {
11659
11660 /* Restore the cursor line in the file and relatively in the
11661 * window. Don't use "G", it changes the jumplist. */
11662 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11663 (long)wp->w_cursor.lnum,
11664 (long)(wp->w_cursor.lnum - wp->w_topline),
11665 (long)wp->w_height / 2, (long)wp->w_height) < 0
11666 || put_eol(fd) == FAIL
11667 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11668 || put_line(fd, "exe s:l") == FAIL
11669 || put_line(fd, "normal! zt") == FAIL
11670 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11671 || put_eol(fd) == FAIL)
11672 return FAIL;
11673 /* Restore the cursor column and left offset when not wrapping. */
11674 if (wp->w_cursor.col == 0)
11675 {
11676 if (put_line(fd, "normal! 0") == FAIL)
11677 return FAIL;
11678 }
11679 else
11680 {
11681 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11682 {
11683 if (fprintf(fd,
11684 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011685 (long)wp->w_virtcol + 1,
11686 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 (long)wp->w_width / 2, (long)wp->w_width) < 0
11688 || put_eol(fd) == FAIL
11689 || put_line(fd, "if s:c > 0") == FAIL
11690 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011691 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11692 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 || put_eol(fd) == FAIL
11694 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011695 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 || put_eol(fd) == FAIL
11697 || put_line(fd, "endif") == FAIL)
11698 return FAIL;
11699 }
11700 else
11701 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011702 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 || put_eol(fd) == FAIL)
11704 return FAIL;
11705 }
11706 }
11707 }
11708
11709 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011710 * Local directory, if the current flag is not view options or the "curdir"
11711 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011713 if (wp->w_localdir != NULL
11714 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 {
11716 if (fputs("lcd ", fd) < 0
11717 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11718 || put_eol(fd) == FAIL)
11719 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011720 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 }
11722
11723 return OK;
11724}
11725
11726/*
11727 * Write an argument list to the session file.
11728 * Returns FAIL if writing fails.
11729 */
11730 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011731ses_arglist(
11732 FILE *fd,
11733 char *cmd,
11734 garray_T *gap,
11735 int fullname, /* TRUE: use full path name */
11736 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737{
11738 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011739 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 char_u *s;
11741
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011742 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11743 return FAIL;
11744 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 return FAIL;
11746 for (i = 0; i < gap->ga_len; ++i)
11747 {
11748 /* NULL file names are skipped (only happens when out of memory). */
11749 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11750 if (s != NULL)
11751 {
11752 if (fullname)
11753 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011754 buf = alloc(MAXPATHL);
11755 if (buf != NULL)
11756 {
11757 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11758 s = buf;
11759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011761 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011762 || ses_put_fname(fd, s, flagp) == FAIL
11763 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011764 {
11765 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011767 }
11768 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 }
11770 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011771 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772}
11773
11774/*
11775 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011776 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 * Returns FAIL if writing fails.
11778 */
11779 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011780ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781{
11782 char_u *name;
11783
11784 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011785 * the session file will be sourced.
11786 * Don't do this for ":mkview", we don't know the current directory.
11787 * Don't do this after ":lcd", we don't keep track of what the current
11788 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 if (buf->b_sfname != NULL
11790 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011791 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011792#ifdef FEAT_AUTOCHDIR
11793 && !p_acd
11794#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011795 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 name = buf->b_sfname;
11797 else
11798 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011799 if (ses_put_fname(fd, name, flagp) == FAIL
11800 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 return FAIL;
11802 return OK;
11803}
11804
11805/*
11806 * Write a file name to the session file.
11807 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11808 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011809 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 */
11811 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011812ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813{
11814 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011815 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817
11818 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011819 if (sname == NULL)
11820 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011822 if (*flagp & SSOP_SLASH)
11823 {
11824 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011825 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011826 if (*p == '\\')
11827 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011829
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011830 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011831 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011833 if (p == NULL)
11834 return FAIL;
11835
11836 /* write the result */
11837 if (fputs((char *)p, fd) < 0)
11838 retval = FAIL;
11839
11840 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 return retval;
11842}
11843
11844/*
11845 * ":loadview [nr]"
11846 */
11847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011848ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
11850 char_u *fname;
11851
11852 fname = get_view_file(*eap->arg);
11853 if (fname != NULL)
11854 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011855 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 vim_free(fname);
11857 }
11858}
11859
11860/*
11861 * Get the name of the view file for the current buffer.
11862 */
11863 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011864get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865{
11866 int len = 0;
11867 char_u *p, *s;
11868 char_u *retval;
11869 char_u *sname;
11870
11871 if (curbuf->b_ffname == NULL)
11872 {
11873 EMSG(_(e_noname));
11874 return NULL;
11875 }
11876 sname = home_replace_save(NULL, curbuf->b_ffname);
11877 if (sname == NULL)
11878 return NULL;
11879
11880 /*
11881 * We want a file name without separators, because we're not going to make
11882 * a directory.
11883 * "normal" path separator -> "=+"
11884 * "=" -> "=="
11885 * ":" path separator -> "=-"
11886 */
11887 for (p = sname; *p; ++p)
11888 if (*p == '=' || vim_ispathsep(*p))
11889 ++len;
11890 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11891 if (retval != NULL)
11892 {
11893 STRCPY(retval, p_vdir);
11894 add_pathsep(retval);
11895 s = retval + STRLEN(retval);
11896 for (p = sname; *p; ++p)
11897 {
11898 if (*p == '=')
11899 {
11900 *s++ = '=';
11901 *s++ = '=';
11902 }
11903 else if (vim_ispathsep(*p))
11904 {
11905 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011906#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 if (*p == ':')
11908 *s++ = '-';
11909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011911 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 }
11913 else
11914 *s++ = *p;
11915 }
11916 *s++ = '=';
11917 *s++ = c;
11918 STRCPY(s, ".vim");
11919 }
11920
11921 vim_free(sname);
11922 return retval;
11923}
11924
11925#endif /* FEAT_SESSION */
11926
11927/*
11928 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11929 * Return FAIL for a write error.
11930 */
11931 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011932put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933{
11934 if (
11935#ifdef USE_CRNL
11936 (
11937# ifdef MKSESSION_NL
11938 !mksession_nl &&
11939# endif
11940 (putc('\r', fd) < 0)) ||
11941#endif
11942 (putc('\n', fd) < 0))
11943 return FAIL;
11944 return OK;
11945}
11946
11947/*
11948 * Write a line to "fd".
11949 * Return FAIL for a write error.
11950 */
11951 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011952put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953{
11954 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11955 return FAIL;
11956 return OK;
11957}
11958
11959#ifdef FEAT_VIMINFO
11960/*
11961 * ":rviminfo" and ":wviminfo".
11962 */
11963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011964ex_viminfo(
11965 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
11967 char_u *save_viminfo;
11968
11969 save_viminfo = p_viminfo;
11970 if (*p_viminfo == NUL)
11971 p_viminfo = (char_u *)"'100";
11972 if (eap->cmdidx == CMD_rviminfo)
11973 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011974 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11975 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 EMSG(_("E195: Cannot open viminfo file for reading"));
11977 }
11978 else
11979 write_viminfo(eap->arg, eap->forceit);
11980 p_viminfo = save_viminfo;
11981}
11982#endif
11983
11984#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011985/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011986 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011987 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011990dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 if (fname == NULL)
11993 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011994 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995}
11996#endif
11997
11998/*
11999 * ":behave {mswin,xterm}"
12000 */
12001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012002ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
12004 if (STRCMP(eap->arg, "mswin") == 0)
12005 {
12006 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12007 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12008 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12009 set_option_value((char_u *)"keymodel", 0L,
12010 (char_u *)"startsel,stopsel", 0);
12011 }
12012 else if (STRCMP(eap->arg, "xterm") == 0)
12013 {
12014 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12015 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12016 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12017 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12018 }
12019 else
12020 EMSG2(_(e_invarg2), eap->arg);
12021}
12022
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012023#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12024/*
12025 * Function given to ExpandGeneric() to obtain the possible arguments of the
12026 * ":behave {mswin,xterm}" command.
12027 */
12028 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012029get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012030{
12031 if (idx == 0)
12032 return (char_u *)"mswin";
12033 if (idx == 1)
12034 return (char_u *)"xterm";
12035 return NULL;
12036}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012037
12038/*
12039 * Function given to ExpandGeneric() to obtain the possible arguments of the
12040 * ":messages {clear}" command.
12041 */
12042 char_u *
12043get_messages_arg(expand_T *xp UNUSED, int idx)
12044{
12045 if (idx == 0)
12046 return (char_u *)"clear";
12047 return NULL;
12048}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012049#endif
12050
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012051 char_u *
12052get_mapclear_arg(expand_T *xp UNUSED, int idx)
12053{
12054 if (idx == 0)
12055 return (char_u *)"<buffer>";
12056 return NULL;
12057}
12058
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059#ifdef FEAT_AUTOCMD
12060static int filetype_detect = FALSE;
12061static int filetype_plugin = FALSE;
12062static int filetype_indent = FALSE;
12063
12064/*
12065 * ":filetype [plugin] [indent] {on,off,detect}"
12066 * on: Load the filetype.vim file to install autocommands for file types.
12067 * off: Load the ftoff.vim file to remove all autocommands for file types.
12068 * plugin on: load filetype.vim and ftplugin.vim
12069 * plugin off: load ftplugof.vim
12070 * indent on: load filetype.vim and indent.vim
12071 * indent off: load indoff.vim
12072 */
12073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012074ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
12076 char_u *arg = eap->arg;
12077 int plugin = FALSE;
12078 int indent = FALSE;
12079
12080 if (*eap->arg == NUL)
12081 {
12082 /* Print current status. */
12083 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12084 filetype_detect ? "ON" : "OFF",
12085 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12086 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12087 return;
12088 }
12089
12090 /* Accept "plugin" and "indent" in any order. */
12091 for (;;)
12092 {
12093 if (STRNCMP(arg, "plugin", 6) == 0)
12094 {
12095 plugin = TRUE;
12096 arg = skipwhite(arg + 6);
12097 continue;
12098 }
12099 if (STRNCMP(arg, "indent", 6) == 0)
12100 {
12101 indent = TRUE;
12102 arg = skipwhite(arg + 6);
12103 continue;
12104 }
12105 break;
12106 }
12107 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12108 {
12109 if (*arg == 'o' || !filetype_detect)
12110 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012111 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 filetype_detect = TRUE;
12113 if (plugin)
12114 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012115 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 filetype_plugin = TRUE;
12117 }
12118 if (indent)
12119 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012120 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 filetype_indent = TRUE;
12122 }
12123 }
12124 if (*arg == 'd')
12125 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012126 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012127 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 }
12129 }
12130 else if (STRCMP(arg, "off") == 0)
12131 {
12132 if (plugin || indent)
12133 {
12134 if (plugin)
12135 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012136 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 filetype_plugin = FALSE;
12138 }
12139 if (indent)
12140 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012141 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 filetype_indent = FALSE;
12143 }
12144 }
12145 else
12146 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012147 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 filetype_detect = FALSE;
12149 }
12150 }
12151 else
12152 EMSG2(_(e_invarg2), arg);
12153}
12154
12155/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012156 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 */
12158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012159ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160{
12161 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012162 {
12163 char_u *arg = eap->arg;
12164
12165 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12166 arg += 9;
12167
12168 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12169 if (arg != eap->arg)
12170 did_filetype = FALSE;
12171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172}
12173#endif
12174
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012176ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177{
12178#ifdef FEAT_DIGRAPHS
12179 if (*eap->arg != NUL)
12180 putdigraph(eap->arg);
12181 else
12182 listdigraphs();
12183#else
12184 EMSG(_("E196: No digraphs in this version"));
12185#endif
12186}
12187
12188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012189ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
12191 int flags = 0;
12192
12193 if (eap->cmdidx == CMD_setlocal)
12194 flags = OPT_LOCAL;
12195 else if (eap->cmdidx == CMD_setglobal)
12196 flags = OPT_GLOBAL;
12197#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12198 if (cmdmod.browse && flags == 0)
12199 ex_options(eap);
12200 else
12201#endif
12202 (void)do_set(eap->arg, flags);
12203}
12204
12205#ifdef FEAT_SEARCH_EXTRA
12206/*
12207 * ":nohlsearch"
12208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012210ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012212 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012213 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214}
12215
12216/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012217 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 * Sets nextcmd to the start of the next command, if any. Also called when
12219 * skipping commands to find the next command.
12220 */
12221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012222ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223{
12224 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012225 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 char_u *end;
12227 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012228 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012229
12230 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012231 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012232 else
12233 {
12234 EMSG(e_invcmd);
12235 return;
12236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237
12238 /* First clear any old pattern. */
12239 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012240 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241
12242 if (ends_excmd(*eap->arg))
12243 end = eap->arg;
12244 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012245 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 end = eap->arg + 4;
12247 else
12248 {
12249 p = skiptowhite(eap->arg);
12250 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012251 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 p = skipwhite(p);
12253 if (*p == NUL)
12254 {
12255 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012256 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 EMSG2(_(e_invarg2), eap->arg);
12258 return;
12259 }
12260 end = skip_regexp(p + 1, *p, TRUE, NULL);
12261 if (!eap->skip)
12262 {
12263 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12264 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012265 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 eap->errmsg = e_trailing;
12267 return;
12268 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012269 if (*end != *p)
12270 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012271 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012272 EMSG2(_(e_invarg2), p);
12273 return;
12274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275
12276 c = *end;
12277 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012278 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012279 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012280 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 }
12282 }
12283 eap->nextcmd = find_nextcmd(end);
12284}
12285#endif
12286
12287#ifdef FEAT_CRYPT
12288/*
12289 * ":X": Get crypt key
12290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012292ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012294 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012295 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296}
12297#endif
12298
12299#ifdef FEAT_FOLDING
12300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012301ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302{
12303 if (foldManualAllowed(TRUE))
12304 foldCreate(eap->line1, eap->line2);
12305}
12306
12307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012308ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309{
12310 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12311 eap->forceit, FALSE);
12312}
12313
12314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012315ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316{
12317 linenr_T lnum;
12318
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012319#ifdef FEAT_CLIPBOARD
12320 start_global_changes();
12321#endif
12322
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 /* First set the marks for all lines closed/open. */
12324 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12325 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12326 ml_setmarked(lnum);
12327
12328 /* Execute the command on the marked lines. */
12329 global_exe(eap->arg);
12330 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012331#ifdef FEAT_CLIPBOARD
12332 end_global_changes();
12333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334}
12335#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012336
12337# if defined(FEAT_TIMERS) || defined(PROTO)
12338 int
12339get_pressedreturn(void)
12340{
12341 return ex_pressedreturn;
12342}
12343
12344 void
12345set_pressedreturn(int val)
12346{
12347 ex_pressedreturn = val;
12348}
12349#endif