blob: f913fb527816ee0e64f863b0306bf5efce001076 [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
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void ex_autocmd(exarg_T *eap);
85static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static void ex_bunload(exarg_T *eap);
88static void ex_buffer(exarg_T *eap);
89static void ex_bmodified(exarg_T *eap);
90static void ex_bnext(exarg_T *eap);
91static void ex_bprevious(exarg_T *eap);
92static void ex_brewind(exarg_T *eap);
93static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#else
95# define ex_bunload ex_ni
96# define ex_buffer ex_ni
97# define ex_bmodified ex_ni
98# define ex_bnext ex_ni
99# define ex_bprevious ex_ni
100# define ex_brewind ex_ni
101# define ex_blast ex_ni
102# define buflist_list ex_ni
103# define ex_checktime ex_ni
104#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200105#if !defined(FEAT_LISTCMDS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106# define ex_buffer_all ex_ni
107#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100108static char_u *getargcmd(char_u **);
109static char_u *skip_cmd_arg(char_u *p, int rembs);
110static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#ifndef FEAT_QUICKFIX
112# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000113# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# define ex_cc ex_ni
115# define ex_cnext ex_ni
116# define ex_cfile ex_ni
117# define qf_list ex_ni
118# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200119# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000121# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200123#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# define ex_cclose ex_ni
125# define ex_copen ex_ni
126# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200127# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
130# define ex_cexpr ex_ni
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100134static 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 +0100135static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200136#if !defined(FEAT_PERL) \
137 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
138 || !defined(FEAT_TCL) \
139 || !defined(FEAT_RUBY) \
140 || !defined(FEAT_LUA) \
141 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000142# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100143static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100145static char_u *invalid_range(exarg_T *eap);
146static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000147#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000149#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100150static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
151static void ex_highlight(exarg_T *eap);
152static void ex_colorscheme(exarg_T *eap);
153static void ex_quit(exarg_T *eap);
154static void ex_cquit(exarg_T *eap);
155static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static void ex_close(exarg_T *eap);
157static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
158static void ex_only(exarg_T *eap);
159static void ex_resize(exarg_T *eap);
160static void ex_stag(exarg_T *eap);
161static void ex_tabclose(exarg_T *eap);
162static void ex_tabonly(exarg_T *eap);
163static void ex_tabnext(exarg_T *eap);
164static void ex_tabmove(exarg_T *eap);
165static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200166#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100167static void ex_pclose(exarg_T *eap);
168static void ex_ptag(exarg_T *eap);
169static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#else
171# define ex_pclose ex_ni
172# define ex_ptag ex_ni
173# define ex_pedit ex_ni
174#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100175static void ex_hide(exarg_T *eap);
176static void ex_stop(exarg_T *eap);
177static void ex_exit(exarg_T *eap);
178static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#else
182# define ex_goto ex_ni
183#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100184static void ex_shell(exarg_T *eap);
185static void ex_preserve(exarg_T *eap);
186static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifndef FEAT_LISTCMDS
188# define ex_argedit ex_ni
189# define ex_argadd ex_ni
190# define ex_argdelete ex_ni
191# define ex_listdo ex_ni
192#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100193static void ex_mode(exarg_T *eap);
194static void ex_wrongmodifier(exarg_T *eap);
195static void ex_find(exarg_T *eap);
196static void ex_open(exarg_T *eap);
197static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#ifndef FEAT_GUI
199# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100203static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#else
205# define ex_tearoff ex_ni
206#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100207#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
208 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#else
211# define ex_popup ex_ni
212#endif
213#ifndef FEAT_GUI_MSWIN
214# define ex_simalt ex_ni
215#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000216#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# define gui_mch_find_dialog ex_ni
218# define gui_mch_replace_dialog ex_ni
219#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000220#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221# define ex_helpfind ex_ni
222#endif
223#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100224# define ex_cscope ex_ni
225# define ex_scscope ex_ni
226# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#endif
228#ifndef FEAT_SYN_HL
229# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200230# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000231#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100232#ifndef FEAT_EVAL
233# define ex_packadd ex_ni
234# define ex_packloadall ex_ni
235#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200236#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200237# define ex_syntime ex_ni
238#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000240# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000241# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000242# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000243# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000244# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000245#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200246#ifndef FEAT_PERSISTENT_UNDO
247# define ex_rundo ex_ni
248# define ex_wundo ex_ni
249#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200250#ifndef FEAT_LUA
251# define ex_lua ex_script_ni
252# define ex_luado ex_ni
253# define ex_luafile ex_ni
254#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000255#ifndef FEAT_MZSCHEME
256# define ex_mzscheme ex_script_ni
257# define ex_mzfile ex_ni
258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#ifndef FEAT_PERL
260# define ex_perl ex_script_ni
261# define ex_perldo ex_ni
262#endif
263#ifndef FEAT_PYTHON
264# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200265# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266# define ex_pyfile ex_ni
267#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200268#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200269# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200270# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200271# define ex_py3file ex_ni
272#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100273#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
274# define ex_pyx ex_script_ni
275# define ex_pyxdo ex_ni
276# define ex_pyxfile ex_ni
277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifndef FEAT_TCL
279# define ex_tcl ex_script_ni
280# define ex_tcldo ex_ni
281# define ex_tclfile ex_ni
282#endif
283#ifndef FEAT_RUBY
284# define ex_ruby ex_script_ni
285# define ex_rubydo ex_ni
286# define ex_rubyfile ex_ni
287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288#ifndef FEAT_KEYMAP
289# define ex_loadkeymap ex_ni
290#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100291static void ex_swapname(exarg_T *eap);
292static void ex_syncbind(exarg_T *eap);
293static void ex_read(exarg_T *eap);
294static void ex_pwd(exarg_T *eap);
295static void ex_equal(exarg_T *eap);
296static void ex_sleep(exarg_T *eap);
297static void do_exmap(exarg_T *eap, int isabbrev);
298static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100299static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000300#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100301static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302#else
303# define ex_winpos ex_ni
304#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static void ex_operators(exarg_T *eap);
306static void ex_put(exarg_T *eap);
307static void ex_copymove(exarg_T *eap);
308static void ex_submagic(exarg_T *eap);
309static void ex_join(exarg_T *eap);
310static void ex_at(exarg_T *eap);
311static void ex_bang(exarg_T *eap);
312static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200313#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_wundo(exarg_T *eap);
315static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_redo(exarg_T *eap);
318static void ex_later(exarg_T *eap);
319static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_redrawstatus(exarg_T *eap);
321static void close_redir(void);
322static void ex_mkrc(exarg_T *eap);
323static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static char_u *uc_fun_cmd(void);
326static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_startinsert(exarg_T *eap);
329static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_checkpath(exarg_T *eap);
332static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#else
334# define ex_findpat ex_ni
335# define ex_checkpath ex_ni
336#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200337#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#else
340# define ex_psearch ex_ni
341#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_tag(exarg_T *eap);
343static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#ifndef FEAT_EVAL
345# define ex_scriptnames ex_ni
346# define ex_finish ex_ni
347# define ex_echo ex_ni
348# define ex_echohl ex_ni
349# define ex_execute ex_ni
350# define ex_call ex_ni
351# define ex_if ex_ni
352# define ex_endif ex_ni
353# define ex_else ex_ni
354# define ex_while ex_ni
355# define ex_continue ex_ni
356# define ex_break ex_ni
357# define ex_endwhile ex_ni
358# define ex_throw ex_ni
359# define ex_try ex_ni
360# define ex_catch ex_ni
361# define ex_finally ex_ni
362# define ex_endtry ex_ni
363# define ex_endfunction ex_ni
364# define ex_let ex_ni
365# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000366# define ex_lockvar ex_ni
367# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368# define ex_function ex_ni
369# define ex_delfunction ex_ni
370# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000371# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100373static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100375static int makeopens(FILE *fd, char_u *dirnow);
376static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
377static void ex_loadview(exarg_T *eap);
378static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000379static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#else
381# define ex_loadview ex_ni
382#endif
383#ifndef FEAT_EVAL
384# define ex_compiler ex_ni
385#endif
386#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100387static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#else
389# define ex_viminfo ex_ni
390#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100391static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static void ex_filetype(exarg_T *eap);
393static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000395# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396# define ex_diffpatch ex_ni
397# define ex_diffgetput ex_ni
398# define ex_diffsplit ex_ni
399# define ex_diffthis ex_ni
400# define ex_diffupdate ex_ni
401#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100402static void ex_digraphs(exarg_T *eap);
403static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100404#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# define ex_options ex_ni
406#endif
407#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_nohlsearch(exarg_T *eap);
409static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_nohlsearch ex_ni
412# define ex_match ex_ni
413#endif
414#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#else
417# define ex_X ex_ni
418#endif
419#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100420static void ex_fold(exarg_T *eap);
421static void ex_foldopen(exarg_T *eap);
422static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#else
424# define ex_fold ex_ni
425# define ex_foldopen ex_ni
426# define ex_folddo ex_ni
427#endif
428#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
429 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
430# define ex_language ex_ni
431#endif
432#ifndef FEAT_SIGNS
433# define ex_sign ex_ni
434#endif
435#ifndef FEAT_SUN_WORKSHOP
436# define ex_wsverb ex_ni
437#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000438#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200439# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000440# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200441# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
444#ifndef FEAT_EVAL
445# define ex_debug ex_ni
446# define ex_breakadd ex_ni
447# define ex_debuggreedy ex_ni
448# define ex_breakdel ex_ni
449# define ex_breaklist ex_ni
450#endif
451
452#ifndef FEAT_CMDHIST
453# define ex_history ex_ni
454#endif
455#ifndef FEAT_JUMPLIST
456# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200457# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458# define ex_changes ex_ni
459#endif
460
Bram Moolenaar05159a02005-02-26 23:04:13 +0000461#ifndef FEAT_PROFILE
462# define ex_profile ex_ni
463#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200464#ifndef FEAT_TERMINAL
465# define ex_terminal ex_ni
466#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
469 * Declare cmdnames[].
470 */
471#define DO_DECLARE_EXCMD
472#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200473#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475static char_u dollar_command[2] = {'$', 0};
476
477
478#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000479/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480typedef struct
481{
482 char_u *line; /* command line */
483 linenr_T lnum; /* sourcing_lnum of the line */
484} wcmd_T;
485
486/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000487 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000489 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000491struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 garray_T *lines_gap; /* growarray with line info */
494 int current_line; /* last read line from growarray */
495 int repeating; /* TRUE when looping a second time */
496 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100497 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 void *cookie;
499};
500
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100501static char_u *get_loop_line(int c, void *cookie, int indent);
502static int store_loop_line(garray_T *gap, char_u *line);
503static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000504
505/* Struct to save a few things while debugging. Used in do_cmdline() only. */
506struct dbg_stuff
507{
508 int trylevel;
509 int force_abort;
510 except_T *caught_stack;
511 char_u *vv_exception;
512 char_u *vv_throwpoint;
513 int did_emsg;
514 int got_int;
515 int did_throw;
516 int need_rethrow;
517 int check_cstack;
518 except_T *current_exception;
519};
520
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100521static void save_dbg_stuff(struct dbg_stuff *dsp);
522static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000523
524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100525save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000526{
527 dsp->trylevel = trylevel; trylevel = 0;
528 dsp->force_abort = force_abort; force_abort = FALSE;
529 dsp->caught_stack = caught_stack; caught_stack = NULL;
530 dsp->vv_exception = v_exception(NULL);
531 dsp->vv_throwpoint = v_throwpoint(NULL);
532
533 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
534 dsp->did_emsg = did_emsg; did_emsg = FALSE;
535 dsp->got_int = got_int; got_int = FALSE;
536 dsp->did_throw = did_throw; did_throw = FALSE;
537 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
538 dsp->check_cstack = check_cstack; check_cstack = FALSE;
539 dsp->current_exception = current_exception; current_exception = NULL;
540}
541
542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100543restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000544{
545 suppress_errthrow = FALSE;
546 trylevel = dsp->trylevel;
547 force_abort = dsp->force_abort;
548 caught_stack = dsp->caught_stack;
549 (void)v_exception(dsp->vv_exception);
550 (void)v_throwpoint(dsp->vv_throwpoint);
551 did_emsg = dsp->did_emsg;
552 got_int = dsp->got_int;
553 did_throw = dsp->did_throw;
554 need_rethrow = dsp->need_rethrow;
555 check_cstack = dsp->check_cstack;
556 current_exception = dsp->current_exception;
557}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560/*
561 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
562 * command is given.
563 */
564 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100565do_exmode(
566 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568 int save_msg_scroll;
569 int prev_msg_row;
570 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100571 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000572
573 if (improved)
574 exmode_active = EXMODE_VIM;
575 else
576 exmode_active = EXMODE_NORMAL;
577 State = NORMAL;
578
579 /* When using ":global /pat/ visual" and then "Q" we return to continue
580 * the :global command. */
581 if (global_busy)
582 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
584 save_msg_scroll = msg_scroll;
585 ++RedrawingDisabled; /* don't redisplay the window */
586 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#ifdef FEAT_GUI
588 /* Ignore scrollbar and mouse events in Ex mode */
589 ++hold_gui_events;
590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
593 while (exmode_active)
594 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000595 /* Check for a ":normal" command and no more characters left. */
596 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
597 {
598 exmode_active = FALSE;
599 break;
600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 msg_scroll = TRUE;
602 need_wait_return = FALSE;
603 ex_pressedreturn = FALSE;
604 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100605 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 prev_msg_row = msg_row;
607 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (improved)
609 {
610 cmdline_row = msg_row;
611 do_cmdline(NULL, getexline, NULL, 0);
612 }
613 else
614 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
615 lines_left = Rows - 1;
616
Bram Moolenaardf177f62005-02-22 08:39:57 +0000617 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100618 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000620 if (curbuf->b_ml.ml_flags & ML_EMPTY)
621 EMSG(_(e_emptybuf));
622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000624 if (ex_pressedreturn)
625 {
626 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000627 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000628 msg_row = prev_msg_row;
629 if (prev_msg_row == Rows - 1)
630 msg_row--;
631 }
632 msg_col = 0;
633 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
634 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000637 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
638 {
639 if (curbuf->b_ml.ml_flags & ML_EMPTY)
640 EMSG(_(e_emptybuf));
641 else
642 EMSG(_("E501: At end-of-file"));
643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
645
646#ifdef FEAT_GUI
647 --hold_gui_events;
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 --RedrawingDisabled;
650 --no_wait_return;
651 update_screen(CLEAR);
652 need_wait_return = FALSE;
653 msg_scroll = save_msg_scroll;
654}
655
656/*
657 * Execute a simple command line. Used for translated commands like "*".
658 */
659 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100660do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
662 return do_cmdline(cmd, NULL, NULL,
663 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
664}
665
666/*
667 * do_cmdline(): execute one Ex command line
668 *
669 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100670 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 * 2. Split up in parts separated with '|'.
672 *
673 * This function can be called recursively!
674 *
675 * flags:
676 * DOCMD_VERBOSE - The command will be included in the error message.
677 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100678 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 * DOCMD_KEYTYPED - Don't reset KeyTyped.
680 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
681 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
682 *
683 * return FAIL if cmdline could not be executed, OK otherwise
684 */
685 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100686do_cmdline(
687 char_u *cmdline,
688 char_u *(*fgetline)(int, void *, int),
689 void *cookie, /* argument for fgetline() */
690 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
692 char_u *next_cmdline; /* next cmd to execute */
693 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 static int recursive = 0; /* recursive depth */
696 int msg_didout_before_start = 0;
697 int count = 0; /* line number count */
698 int did_inc = FALSE; /* incremented RedrawingDisabled */
699 int retval = OK;
700#ifdef FEAT_EVAL
701 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000702 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 int current_line = 0; /* active line in lines_ga */
704 char_u *fname = NULL; /* function or script name */
705 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
706 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000707 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 int initial_trylevel;
709 struct msglist **saved_msg_list = NULL;
710 struct msglist *private_msg_list;
711
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100713 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000715 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000717 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100719# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720# define cmd_cookie cookie
721#endif
722 static int call_depth = 0; /* recursiveness */
723
724#ifdef FEAT_EVAL
725 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
726 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000727 * This ensures that the do_errthrow() call in do_one_cmd() does not
728 * combine the messages stored by an earlier invocation of do_one_cmd()
729 * with the command name of the later one. This would happen when
730 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 saved_msg_list = msg_list;
732 msg_list = &private_msg_list;
733 private_msg_list = NULL;
734#endif
735
736 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100737 * here. The value of 200 allows nested function calls, ":source", etc.
738 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100739 if (call_depth >= 200
740#ifdef FEAT_EVAL
741 && call_depth >= p_mfd
742#endif
743 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
745 EMSG(_("E169: Command too recursive"));
746#ifdef FEAT_EVAL
747 /* When converting to an exception, we do not include the command name
748 * since this is not an error of the specific command. */
749 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
750 msg_list = saved_msg_list;
751#endif
752 return FAIL;
753 }
754 ++call_depth;
755
756#ifdef FEAT_EVAL
757 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 cstack.cs_trylevel = 0;
760 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
763
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100767 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 ++ex_nesting_level;
770
771 /* Get the function or script name and the address where the next breakpoint
772 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 {
775 fname = func_name(real_cookie);
776 breakpoint = func_breakpoint(real_cookie);
777 dbg_tick = func_dbg_tick(real_cookie);
778 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 {
781 fname = sourcing_name;
782 breakpoint = source_breakpoint(real_cookie);
783 dbg_tick = source_dbg_tick(real_cookie);
784 }
785
786 /*
787 * Initialize "force_abort" and "suppress_errthrow" at the top level.
788 */
789 if (!recursive)
790 {
791 force_abort = FALSE;
792 suppress_errthrow = FALSE;
793 }
794
795 /*
796 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000797 * exception handling (used when debugging). Otherwise clear it to avoid
798 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000800 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000801 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000802 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200803 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 initial_trylevel = trylevel;
806
807 /*
808 * "did_throw" will be set to TRUE when an exception is being thrown.
809 */
810 did_throw = FALSE;
811#endif
812 /*
813 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000814 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 * If force_abort is set, we cancel everything.
816 */
817 did_emsg = FALSE;
818
819 /*
820 * KeyTyped is only set when calling vgetc(). Reset it here when not
821 * calling vgetc() (sourced command lines).
822 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100823 if (!(flags & DOCMD_KEYTYPED)
824 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 KeyTyped = FALSE;
826
827 /*
828 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000829 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * - for multiple commands on one line, separated with '|'
831 * - when repeating until there are no more lines (for ":source")
832 */
833 next_cmdline = cmdline;
834 do
835 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000836#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838#endif
839
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000840 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (next_cmdline == NULL
842#ifdef FEAT_EVAL
843 && !force_abort
844 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#endif
847 )
848 did_emsg = FALSE;
849
850 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000851 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 * 3. If a line is given: Make a copy, so we can mess with it.
854 */
855
856#ifdef FEAT_EVAL
857 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000858 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 {
860 /* Each '|' separated command is stored separately in lines_ga, to
861 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100862 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864 /* Check if a function has returned or, unless it has an unclosed
865 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000869 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000870 func_line_end(real_cookie);
871# endif
872 if (func_has_ended(real_cookie))
873 {
874 retval = FAIL;
875 break;
876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000879 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100880 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000881 script_line_end();
882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
887 retval = FAIL;
888 break;
889 }
890
891 /* If breakpoints have been added/deleted need to check for it. */
892 if (breakpoint != NULL && dbg_tick != NULL
893 && *dbg_tick != debug_tick)
894 {
895 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100896 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 fname, sourcing_lnum);
898 *dbg_tick = debug_tick;
899 }
900
901 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
902 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
903
904 /* Did we encounter a breakpoint? */
905 if (breakpoint != NULL && *breakpoint != 0
906 && *breakpoint <= sourcing_lnum)
907 {
908 dbg_breakpoint(fname, sourcing_lnum);
909 /* Find next breakpoint. */
910 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100911 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 fname, sourcing_lnum);
913 *dbg_tick = debug_tick;
914 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000916 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917 {
918 if (getline_is_func)
919 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100920 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000921 script_line_start();
922 }
923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000926 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 * below, so that it stores lines in or reads them from
931 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000932 * while/for loop. */
933 cmd_getline = get_loop_line;
934 cmd_cookie = (void *)&cmd_loop_cookie;
935 cmd_loop_cookie.lines_gap = &lines_ga;
936 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100937 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 cmd_loop_cookie.cookie = cookie;
939 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941 else
942 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100943 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 cmd_cookie = cookie;
945 }
946#endif
947
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100948 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (next_cmdline == NULL)
950 {
951 /*
952 * Need to set msg_didout for the first line after an ":if",
953 * otherwise the ":if" will be overwritten.
954 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100955 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958#ifdef FEAT_EVAL
959 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
960#else
961 0
962#endif
963 )) == NULL)
964 {
965 /* Don't call wait_return for aborted command line. The NULL
966 * returned for the end of a sourced file or executed function
967 * doesn't do this. */
968 if (KeyTyped && !(flags & DOCMD_REPEAT))
969 need_wait_return = FALSE;
970 retval = FAIL;
971 break;
972 }
973 used_getline = TRUE;
974
975 /*
976 * Keep the first typed line. Clear it when more lines are typed.
977 */
978 if (flags & DOCMD_KEEPLINE)
979 {
980 vim_free(repeat_cmdline);
981 if (count == 0)
982 repeat_cmdline = vim_strsave(next_cmdline);
983 else
984 repeat_cmdline = NULL;
985 }
986 }
987
988 /* 3. Make a copy of the command so we can mess with it. */
989 else if (cmdline_copy == NULL)
990 {
991 next_cmdline = vim_strsave(next_cmdline);
992 if (next_cmdline == NULL)
993 {
994 EMSG(_(e_outofmem));
995 retval = FAIL;
996 break;
997 }
998 }
999 cmdline_copy = next_cmdline;
1000
1001#ifdef FEAT_EVAL
1002 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 * Save the current line when inside a ":while" or ":for", and when
1004 * the command looks like a ":while" or ":for", because we may need it
1005 * later. When there is a '|' and another command, it is stored
1006 * separately, because we need to be able to jump back to it from an
1007 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 if (current_line == lines_ga.ga_len
1010 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001012 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
1014 retval = FAIL;
1015 break;
1016 }
1017 }
1018 did_endif = FALSE;
1019#endif
1020
1021 if (count++ == 0)
1022 {
1023 /*
1024 * All output from the commands is put below each other, without
1025 * waiting for a return. Don't do this when executing commands
1026 * from a script or when being called recursive (e.g. for ":e
1027 * +command file").
1028 */
1029 if (!(flags & DOCMD_NOWAIT) && !recursive)
1030 {
1031 msg_didout_before_start = msg_didout;
1032 msg_didany = FALSE; /* no output yet */
1033 msg_start();
1034 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001035 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 ++RedrawingDisabled;
1037 did_inc = TRUE;
1038 }
1039 }
1040
1041 if (p_verbose >= 15 && sourcing_name != NULL)
1042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001044 verbose_enter_scroll();
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 smsg((char_u *)_("line %ld: %s"),
1047 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001048 if (msg_silent == 0)
1049 msg_puts((char_u *)"\n"); /* don't overwrite this */
1050
1051 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 --no_wait_return;
1053 }
1054
1055 /*
1056 * 2. Execute one '|' separated command.
1057 * do_one_cmd() will return NULL if there is no trailing '|'.
1058 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1059 */
1060 ++recursive;
1061 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1062#ifdef FEAT_EVAL
1063 &cstack,
1064#endif
1065 cmd_getline, cmd_cookie);
1066 --recursive;
1067
1068#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 if (cmd_cookie == (void *)&cmd_loop_cookie)
1070 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#endif
1074
1075 if (next_cmdline == NULL)
1076 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001077 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#ifdef FEAT_CMDHIST
1079 /*
1080 * If the command was typed, remember it for the ':' register.
1081 * Do this AFTER executing the command to make :@: work.
1082 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001083 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 && new_last_cmdline != NULL)
1085 {
1086 vim_free(last_cmdline);
1087 last_cmdline = new_last_cmdline;
1088 new_last_cmdline = NULL;
1089 }
1090#endif
1091 }
1092 else
1093 {
1094 /* need to copy the command after the '|' to cmdline_copy, for the
1095 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001096 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 next_cmdline = cmdline_copy;
1098 }
1099
1100
1101#ifdef FEAT_EVAL
1102 /* reset did_emsg for a function that is not aborted by an error */
1103 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001104 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 && !func_has_abort(real_cookie))
1106 did_emsg = FALSE;
1107
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 {
1110 ++current_line;
1111
1112 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 * An ":endwhile", ":endfor" and ":continue" is handled here.
1114 * If we were executing commands, jump back to the ":while" or
1115 * ":for".
1116 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 /* Jump back to the matching ":while" or ":for". Be careful
1123 * not to use a cs_line[] from an entry that isn't a ":while"
1124 * or ":for": It would make "current_line" invalid and can
1125 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (!did_emsg && !got_int && !did_throw
1127 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 && (cstack.cs_flags[cstack.cs_idx]
1129 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 && cstack.cs_line[cstack.cs_idx] >= 0
1131 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1132 {
1133 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 /* remember we jumped there */
1135 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 line_breakcheck(); /* check if CTRL-C typed */
1137
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 /* Check for the next breakpoint at or after the ":while"
1139 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (breakpoint != NULL)
1141 {
1142 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001143 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 fname,
1145 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1146 *dbg_tick = debug_tick;
1147 }
1148 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001153 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1154 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 }
1157
1158 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001159 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001161 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1165 }
1166 }
1167
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001168 /* Check for the next breakpoint after a watchexpression */
1169 if (breakpoint != NULL && has_watchexpr())
1170 {
1171 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1172 *dbg_tick = debug_tick;
1173 }
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 /*
1176 * When not inside any ":while" loop, clear remembered lines.
1177 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180 if (lines_ga.ga_len > 0)
1181 {
1182 sourcing_lnum =
1183 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1184 free_cmdlines(&lines_ga);
1185 }
1186 current_line = 0;
1187 }
1188
1189 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1191 * being restored at the ":endtry". Reset them here and set the
1192 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1193 * This includes the case where a missing ":endif", ":endwhile" or
1194 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 cstack.cs_lflags &= ~CSL_HAD_FINA;
1199 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1200 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 did_throw ? (void *)current_exception : NULL);
1202 did_emsg = got_int = did_throw = FALSE;
1203 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1204 }
1205
1206 /* Update global "trylevel" for recursive calls to do_cmdline() from
1207 * within this loop. */
1208 trylevel = initial_trylevel + cstack.cs_trylevel;
1209
1210 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001211 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 * files) is aborted because of an error, an interrupt, or an uncaught
1213 * exception, cancel everything. If it is left normally, reset
1214 * force_abort to get the non-EH compatible abortion behavior for
1215 * the rest of the script.
1216 */
1217 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1218 force_abort = FALSE;
1219
1220 /* Convert an interrupt to an exception if appropriate. */
1221 (void)do_intthrow(&cstack);
1222#endif /* FEAT_EVAL */
1223
1224 }
1225 /*
1226 * Continue executing command lines when:
1227 * - no CTRL-C typed, no aborting error, no exception thrown or try
1228 * conditionals need to be checked for executing finally clauses or
1229 * catching an interrupt exception
1230 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 * looping for ":source" command or function call.
1233 */
1234 while (!((got_int
1235#ifdef FEAT_EVAL
1236 || (did_emsg && force_abort) || did_throw
1237#endif
1238 )
1239#ifdef FEAT_EVAL
1240 && cstack.cs_trylevel == 0
1241#endif
1242 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001243 && !(did_emsg
1244#ifdef FEAT_EVAL
1245 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001246 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001247 * the :endtry to be missed. */
1248 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1249#endif
1250 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001251 && (getline_equal(fgetline, cookie, getexmodeline)
1252 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 && (next_cmdline != NULL
1254#ifdef FEAT_EVAL
1255 || cstack.cs_idx >= 0
1256#endif
1257 || (flags & DOCMD_REPEAT)));
1258
1259 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001260 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261#ifdef FEAT_EVAL
1262 free_cmdlines(&lines_ga);
1263 ga_clear(&lines_ga);
1264
1265 if (cstack.cs_idx >= 0)
1266 {
1267 /*
1268 * If a sourced file or executed function ran to its end, report the
1269 * unclosed conditional.
1270 */
1271 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001272 && ((getline_equal(fgetline, cookie, getsourceline)
1273 && !source_finished(fgetline, cookie))
1274 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 && !func_has_ended(real_cookie))))
1276 {
1277 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1278 EMSG(_(e_endtry));
1279 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1280 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001281 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1282 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 else
1284 EMSG(_(e_endif));
1285 }
1286
1287 /*
1288 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1289 * ":endtry" in a sourced file or executed function. If the try
1290 * conditional is in its finally clause, ignore anything pending.
1291 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001292 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 */
1294 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001295 {
1296 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1297
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001298 if (idx >= 0)
1299 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001300 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1301 &cstack.cs_looplevel);
1302 }
1303 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 trylevel = initial_trylevel;
1305 }
1306
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001307 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1308 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001310 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 ? (char_u *)"endfunction" : (char_u *)NULL);
1312
1313 if (trylevel == 0)
1314 {
1315 /*
1316 * When an exception is being thrown out of the outermost try
1317 * conditional, discard the uncaught exception, disable the conversion
1318 * of interrupts or errors to exceptions, and ensure that no more
1319 * commands are executed.
1320 */
1321 if (did_throw)
1322 {
1323 void *p = NULL;
1324 char_u *saved_sourcing_name;
1325 int saved_sourcing_lnum;
1326 struct msglist *messages = NULL, *next;
1327
1328 /*
1329 * If the uncaught exception is a user exception, report it as an
1330 * error. If it is an error exception, display the saved error
1331 * message now. For an interrupt exception, do nothing; the
1332 * interrupt message is given elsewhere.
1333 */
1334 switch (current_exception->type)
1335 {
1336 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001337 vim_snprintf((char *)IObuff, IOSIZE,
1338 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 current_exception->value);
1340 p = vim_strsave(IObuff);
1341 break;
1342 case ET_ERROR:
1343 messages = current_exception->messages;
1344 current_exception->messages = NULL;
1345 break;
1346 case ET_INTERRUPT:
1347 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349
1350 saved_sourcing_name = sourcing_name;
1351 saved_sourcing_lnum = sourcing_lnum;
1352 sourcing_name = current_exception->throw_name;
1353 sourcing_lnum = current_exception->throw_lnum;
1354 current_exception->throw_name = NULL;
1355
1356 discard_current_exception(); /* uses IObuff if 'verbose' */
1357 suppress_errthrow = TRUE;
1358 force_abort = TRUE;
1359
1360 if (messages != NULL)
1361 {
1362 do
1363 {
1364 next = messages->next;
1365 emsg(messages->msg);
1366 vim_free(messages->msg);
1367 vim_free(messages);
1368 messages = next;
1369 }
1370 while (messages != NULL);
1371 }
1372 else if (p != NULL)
1373 {
1374 emsg(p);
1375 vim_free(p);
1376 }
1377 vim_free(sourcing_name);
1378 sourcing_name = saved_sourcing_name;
1379 sourcing_lnum = saved_sourcing_lnum;
1380 }
1381
1382 /*
1383 * On an interrupt or an aborting error not converted to an exception,
1384 * disable the conversion of errors to exceptions. (Interrupts are not
1385 * converted any more, here.) This enables also the interrupt message
1386 * when force_abort is set and did_emsg unset in case of an interrupt
1387 * from a finally clause after an error.
1388 */
1389 else if (got_int || (did_emsg && force_abort))
1390 suppress_errthrow = TRUE;
1391 }
1392
1393 /*
1394 * The current cstack will be freed when do_cmdline() returns. An uncaught
1395 * exception will have to be rethrown in the previous cstack. If a function
1396 * has just returned or a script file was just finished and the previous
1397 * cstack belongs to the same function or, respectively, script file, it
1398 * will have to be checked for finally clauses to be executed due to the
1399 * ":return" or ":finish". This is done in do_one_cmd().
1400 */
1401 if (did_throw)
1402 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001403 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001405 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 && ex_nesting_level > func_level(real_cookie) + 1))
1407 {
1408 if (!did_throw)
1409 check_cstack = TRUE;
1410 }
1411 else
1412 {
1413 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001414 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 --ex_nesting_level;
1416 /*
1417 * Go to debug mode when returning from a function in which we are
1418 * single-stepping.
1419 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001420 if ((getline_equal(fgetline, cookie, getsourceline)
1421 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001423 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 ? (char_u *)_("End of sourced file")
1425 : (char_u *)_("End of function"));
1426 }
1427
1428 /*
1429 * Restore the exception environment (done after returning from the
1430 * debugger).
1431 */
1432 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001433 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
1435 msg_list = saved_msg_list;
1436#endif /* FEAT_EVAL */
1437
1438 /*
1439 * If there was too much output to fit on the command line, ask the user to
1440 * hit return before redrawing the screen. With the ":global" command we do
1441 * this only once after the command is finished.
1442 */
1443 if (did_inc)
1444 {
1445 --RedrawingDisabled;
1446 --no_wait_return;
1447 msg_scroll = FALSE;
1448
1449 /*
1450 * When just finished an ":if"-":else" which was typed, no need to
1451 * wait for hit-return. Also for an error situation.
1452 */
1453 if (retval == FAIL
1454#ifdef FEAT_EVAL
1455 || (did_endif && KeyTyped && !did_emsg)
1456#endif
1457 )
1458 {
1459 need_wait_return = FALSE;
1460 msg_didany = FALSE; /* don't wait when restarting edit */
1461 }
1462 else if (need_wait_return)
1463 {
1464 /*
1465 * The msg_start() above clears msg_didout. The wait_return we do
1466 * here should not overwrite the command that may be shown before
1467 * doing that.
1468 */
1469 msg_didout |= msg_didout_before_start;
1470 wait_return(FALSE);
1471 }
1472 }
1473
Bram Moolenaar2a942252012-11-28 23:03:07 +01001474#ifdef FEAT_EVAL
1475 did_endif = FALSE; /* in case do_cmdline used recursively */
1476#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /*
1478 * Reset if_level, in case a sourced script file contains more ":if" than
1479 * ":endif" (could be ":if x | foo | endif").
1480 */
1481 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001482#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 --call_depth;
1485 return retval;
1486}
1487
1488#ifdef FEAT_EVAL
1489/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001490 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 */
1492 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001493get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 wcmd_T *wp;
1497 char_u *line;
1498
1499 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1500 {
1501 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (cp->getline == NULL)
1506 line = getcmdline(c, 0L, indent);
1507 else
1508 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001509 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 ++cp->current_line;
1511
1512 return line;
1513 }
1514
1515 KeyTyped = FALSE;
1516 ++cp->current_line;
1517 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1518 sourcing_lnum = wp->lnum;
1519 return vim_strsave(wp->line);
1520}
1521
1522/*
1523 * Store a line in "gap" so that a ":while" loop can execute it again.
1524 */
1525 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001526store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 if (ga_grow(gap, 1) == FAIL)
1529 return FAIL;
1530 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1531 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1532 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 return OK;
1534}
1535
1536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 */
1539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001540free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 while (gap->ga_len > 0)
1543 {
1544 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1545 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547}
1548#endif
1549
1550/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1552 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001555getline_equal(
1556 char_u *(*fgetline)(int, void *, int),
1557 void *cookie UNUSED, /* argument for fgetline() */
1558 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001561 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaar89d40322006-08-29 15:30:07 +00001564 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001565 * function that's originally used to obtain the lines. This may be
1566 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001567 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 cp = (struct loop_cookie *)cookie;
1569 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
1571 gp = cp->getline;
1572 cp = cp->cookie;
1573 }
1574 return gp == func;
1575#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001576 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577#endif
1578}
1579
1580#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1581/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001582 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 * getline function. Otherwise return "cookie".
1584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001586getline_cookie(
1587 char_u *(*fgetline)(int, void *, int) UNUSED,
1588 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001591 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001592 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001595 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001597 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001598 cp = (struct loop_cookie *)cookie;
1599 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 gp = cp->getline;
1602 cp = cp->cookie;
1603 }
1604 return cp;
1605# else
1606 return cookie;
1607# endif
1608}
1609#endif
1610
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001611
1612/*
1613 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1614 * ":bwipeout", etc.
1615 * Returns the buffer number.
1616 */
1617 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001618compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619{
1620 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001621 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622 int count = offset;
1623
1624 buf = firstbuf;
1625 while (buf->b_next != NULL && buf->b_fnum < lnum)
1626 buf = buf->b_next;
1627 while (count != 0)
1628 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001629 count += (offset < 0) ? 1 : -1;
1630 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1631 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001632 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001633 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 if (addr_type == ADDR_LOADED_BUFFERS)
1635 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001636 while (buf->b_ml.ml_mfp == NULL)
1637 {
1638 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1639 if (nextbuf == NULL)
1640 break;
1641 buf = nextbuf;
1642 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001643 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001644 /* we might have gone too far, last buffer is not loadedd */
1645 if (addr_type == ADDR_LOADED_BUFFERS)
1646 while (buf->b_ml.ml_mfp == NULL)
1647 {
1648 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1649 if (nextbuf == NULL)
1650 break;
1651 buf = nextbuf;
1652 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001653 return buf->b_fnum;
1654}
1655
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001656static int current_win_nr(win_T *win);
1657static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658
1659 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001660current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001661{
1662 win_T *wp;
1663 int nr = 0;
1664
Bram Moolenaar29323592016-07-24 22:04:11 +02001665 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001666 {
1667 ++nr;
1668 if (wp == win)
1669 break;
1670 }
1671 return nr;
1672}
1673
1674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001675current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001676{
1677 tabpage_T *tp;
1678 int nr = 0;
1679
Bram Moolenaar29323592016-07-24 22:04:11 +02001680 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001681 {
1682 ++nr;
1683 if (tp == tab)
1684 break;
1685 }
1686 return nr;
1687}
1688
1689# define CURRENT_WIN_NR current_win_nr(curwin)
1690# define LAST_WIN_NR current_win_nr(NULL)
1691# define CURRENT_TAB_NR current_tab_nr(curtab)
1692# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694/*
1695 * Execute one Ex command.
1696 *
1697 * If 'sourcing' is TRUE, the command will be included in the error message.
1698 *
1699 * 1. skip comment lines and leading space
1700 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001701 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001702 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001703 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 * 6. parse arguments
1705 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001707 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 *
1709 * This function may be called recursively!
1710 */
1711#if (_MSC_VER == 1200)
1712/*
Bram Moolenaared203462004-06-16 11:19:22 +00001713 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001715 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716#endif
1717 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001718do_one_cmd(
1719 char_u **cmdlinep,
1720 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001722 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001724 char_u *(*fgetline)(int, void *, int),
1725 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727 char_u *p;
1728 linenr_T lnum;
1729 long n;
1730 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001731 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 exarg_T ea; /* Ex command arguments */
1733 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001734 int save_msg_scroll = msg_scroll;
1735 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001737#ifdef HAVE_SANDBOX
1738 int did_sandbox = FALSE;
1739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 cmdmod_T save_cmdmod;
1741 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001742 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001743 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
1745 vim_memset(&ea, 0, sizeof(ea));
1746 ea.line1 = 1;
1747 ea.line2 = 1;
1748#ifdef FEAT_EVAL
1749 ++ex_nesting_level;
1750#endif
1751
Bram Moolenaar21691f82012-12-05 19:13:18 +01001752 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if (quitmore
1754#ifdef FEAT_EVAL
1755 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001757#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001758 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001759 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 --quitmore;
1761
1762 /*
1763 * Reset browse, confirm, etc.. They are restored when returning, for
1764 * recursive calls.
1765 */
1766 save_cmdmod = cmdmod;
1767 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1768
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001769 /* "#!anything" is handled like a comment. */
1770 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1771 goto doend;
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 /*
1774 * Repeat until no more command modifiers are found.
1775 */
1776 ea.cmd = *cmdlinep;
1777 for (;;)
1778 {
1779/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001780 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 */
1782 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1783 ++ea.cmd;
1784
1785 /* in ex mode, an empty line works like :+ */
1786 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001787 && (getline_equal(fgetline, cookie, getexmodeline)
1788 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001789 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
1791 ea.cmd = (char_u *)"+";
1792 ex_pressedreturn = TRUE;
1793 }
1794
1795 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001796 if (*ea.cmd == '"')
1797 goto doend;
1798 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001799 {
1800 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
1804/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001805 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001807 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 switch (*p)
1809 {
1810 /* When adding an entry, also modify cmd_exists(). */
1811 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 continue;
1815
1816 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 continue;
1820 }
1821 if (checkforcmd(&ea.cmd, "browse", 3))
1822 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001823#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 cmdmod.browse = TRUE;
1825#endif
1826 continue;
1827 }
1828 if (!checkforcmd(&ea.cmd, "botright", 2))
1829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 continue;
1832
1833 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1834 break;
1835#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1836 cmdmod.confirm = TRUE;
1837#endif
1838 continue;
1839
1840 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1841 {
1842 cmdmod.keepmarks = TRUE;
1843 continue;
1844 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001845 if (checkforcmd(&ea.cmd, "keepalt", 5))
1846 {
1847 cmdmod.keepalt = TRUE;
1848 continue;
1849 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001850 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1851 {
1852 cmdmod.keeppatterns = TRUE;
1853 continue;
1854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1856 break;
1857 cmdmod.keepjumps = TRUE;
1858 continue;
1859
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001860 case 'f': /* only accept ":filter {pat} cmd" */
1861 {
1862 char_u *reg_pat;
1863
1864 if (!checkforcmd(&p, "filter", 4)
1865 || *p == NUL || ends_excmd(*p))
1866 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001867 if (*p == '!')
1868 {
1869 cmdmod.filter_force = TRUE;
1870 p = skipwhite(p + 1);
1871 if (*p == NUL || ends_excmd(*p))
1872 break;
1873 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001874 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1875 if (p == NULL || *p == NUL)
1876 break;
1877 cmdmod.filter_regmatch.regprog =
1878 vim_regcomp(reg_pat, RE_MAGIC);
1879 if (cmdmod.filter_regmatch.regprog == NULL)
1880 break;
1881 ea.cmd = p;
1882 continue;
1883 }
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 /* ":hide" and ":hide | cmd" are not modifiers */
1886 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1887 || *p == NUL || ends_excmd(*p))
1888 break;
1889 ea.cmd = p;
1890 cmdmod.hide = TRUE;
1891 continue;
1892
1893 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1894 {
1895 cmdmod.lockmarks = TRUE;
1896 continue;
1897 }
1898
1899 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 continue;
1903
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001904 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001905 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001906 if (cmdmod.save_ei == NULL)
1907 {
1908 /* Set 'eventignore' to "all". Restore the
1909 * existing option value later. */
1910 cmdmod.save_ei = vim_strsave(p_ei);
1911 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001912 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001913 }
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001914 continue;
1915 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001916 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001917 break;
1918 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001919 continue;
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 continue;
1925
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001926 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1927 {
1928#ifdef HAVE_SANDBOX
1929 if (!did_sandbox)
1930 ++sandbox;
1931 did_sandbox = TRUE;
1932#endif
1933 continue;
1934 }
1935 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001937 if (save_msg_silent == -1)
1938 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001940 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 /* ":silent!", but not "silent !cmd" */
1943 ea.cmd = skipwhite(ea.cmd + 1);
1944 ++emsg_silent;
1945 ++did_esilent;
1946 }
1947 continue;
1948
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001949 case 't': if (checkforcmd(&p, "tab", 3))
1950 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001951 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001952 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001953 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001954 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001955 else
1956 {
1957 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1958 {
1959 errormsg = (char_u *)_(e_invrange);
1960 goto doend;
1961 }
1962 cmdmod.tab = tabnr + 1;
1963 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001964 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001965 continue;
1966 }
1967 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 continue;
1971
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001972 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1973 break;
1974 if (save_msg_silent == -1)
1975 save_msg_silent = msg_silent;
1976 msg_silent = 0;
1977 continue;
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 continue;
1983 }
1984 if (!checkforcmd(&p, "verbose", 4))
1985 break;
1986 if (verbose_save < 0)
1987 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001988 if (vim_isdigit(*ea.cmd))
1989 p_verbose = atoi((char *)ea.cmd);
1990 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 p_verbose = 1;
1992 ea.cmd = p;
1993 continue;
1994 }
1995 break;
1996 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001997 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
1999#ifdef FEAT_EVAL
2000 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2001 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2002#else
2003 ea.skip = (if_level > 0);
2004#endif
2005
2006#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002007# ifdef FEAT_PROFILE
2008 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002009 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002010 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002011 if (getline_equal(fgetline, cookie, get_func_line))
2012 func_line_exec(getline_cookie(fgetline, cookie));
2013 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002014 script_line_exec();
2015 }
2016#endif
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 /* May go to debug mode. If this happens and the ">quit" debug command is
2019 * used, throw an interrupt exception and skip the next command. */
2020 dbg_check_breakpoint(&ea);
2021 if (!ea.skip && got_int)
2022 {
2023 ea.skip = TRUE;
2024 (void)do_intthrow(cstack);
2025 }
2026#endif
2027
2028/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002029 * 3. Skip over the range to find the command. Let "p" point to after it.
2030 *
2031 * We need the command to know what kind of range it uses.
2032 */
2033 cmd = ea.cmd;
2034 ea.cmd = skip_range(ea.cmd, NULL);
2035 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2036 ea.cmd = skipwhite(ea.cmd + 1);
2037 p = find_command(&ea, NULL);
2038
2039/*
2040 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 *
2042 * where 'addr' is:
2043 *
2044 * % (entire file)
2045 * $ [+-NUM]
2046 * 'x [+-NUM] (where x denotes a currently defined mark)
2047 * . [+-NUM]
2048 * [+-NUM]..
2049 * NUM
2050 *
2051 * The ea.cmd pointer is updated to point to the first character following the
2052 * range spec. If an initial address is found, but no second, the upper bound
2053 * is equal to the lower.
2054 */
2055
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002056 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002057 if (!IS_USER_CMDIDX(ea.cmdidx))
2058 {
2059 if (ea.cmdidx != CMD_SIZE)
2060 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2061 else
2062 ea.addr_type = ADDR_LINES;
2063
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002064 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002065 if (ea.cmdidx == CMD_wincmd && p != NULL)
2066 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002067 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002070 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 for (;;)
2072 {
2073 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002074 switch (ea.addr_type)
2075 {
2076 case ADDR_LINES:
2077 /* default is current line number */
2078 ea.line2 = curwin->w_cursor.lnum;
2079 break;
2080 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002081 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002082 break;
2083 case ADDR_ARGUMENTS:
2084 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002085 if (ea.line2 > ARGCOUNT)
2086 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002087 break;
2088 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002089 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002090 ea.line2 = curbuf->b_fnum;
2091 break;
2092 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002093 ea.line2 = CURRENT_TAB_NR;
2094 break;
2095 case ADDR_TABS_RELATIVE:
2096 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002097 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002098#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002099 case ADDR_QUICKFIX:
2100 ea.line2 = qf_get_cur_valid_idx(&ea);
2101 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002102#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002105 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002106 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (ea.cmd == NULL) /* error detected */
2108 goto doend;
2109 if (lnum == MAXLNUM)
2110 {
2111 if (*ea.cmd == '%') /* '%' - all lines */
2112 {
2113 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002114 switch (ea.addr_type)
2115 {
2116 case ADDR_LINES:
2117 ea.line1 = 1;
2118 ea.line2 = curbuf->b_ml.ml_line_count;
2119 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002120 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002121 {
2122 buf_T *buf = firstbuf;
2123
2124 while (buf->b_next != NULL
2125 && buf->b_ml.ml_mfp == NULL)
2126 buf = buf->b_next;
2127 ea.line1 = buf->b_fnum;
2128 buf = lastbuf;
2129 while (buf->b_prev != NULL
2130 && buf->b_ml.ml_mfp == NULL)
2131 buf = buf->b_prev;
2132 ea.line2 = buf->b_fnum;
2133 break;
2134 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002135 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002136 ea.line1 = firstbuf->b_fnum;
2137 ea.line2 = lastbuf->b_fnum;
2138 break;
2139 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002140 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002141 if (IS_USER_CMDIDX(ea.cmdidx))
2142 {
2143 ea.line1 = 1;
2144 ea.line2 = ea.addr_type == ADDR_WINDOWS
2145 ? LAST_WIN_NR : LAST_TAB_NR;
2146 }
2147 else
2148 {
2149 /* there is no Vim command which uses '%' and
2150 * ADDR_WINDOWS or ADDR_TABS */
2151 errormsg = (char_u *)_(e_invrange);
2152 goto doend;
2153 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002154 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002155 case ADDR_TABS_RELATIVE:
2156 errormsg = (char_u *)_(e_invrange);
2157 goto doend;
2158 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002159 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002160 if (ARGCOUNT == 0)
2161 ea.line1 = ea.line2 = 0;
2162 else
2163 {
2164 ea.line1 = 1;
2165 ea.line2 = ARGCOUNT;
2166 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002167 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002168#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002169 case ADDR_QUICKFIX:
2170 ea.line1 = 1;
2171 ea.line2 = qf_get_size(&ea);
2172 if (ea.line2 == 0)
2173 ea.line2 = 1;
2174 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002175#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 ++ea.addr_count;
2178 }
2179 /* '*' - visual area */
2180 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2181 {
2182 pos_T *fp;
2183
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002184 if (ea.addr_type != ADDR_LINES)
2185 {
2186 errormsg = (char_u *)_(e_invrange);
2187 goto doend;
2188 }
2189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ++ea.cmd;
2191 if (!ea.skip)
2192 {
2193 fp = getmark('<', FALSE);
2194 if (check_mark(fp) == FAIL)
2195 goto doend;
2196 ea.line1 = fp->lnum;
2197 fp = getmark('>', FALSE);
2198 if (check_mark(fp) == FAIL)
2199 goto doend;
2200 ea.line2 = fp->lnum;
2201 ++ea.addr_count;
2202 }
2203 }
2204 }
2205 else
2206 ea.line2 = lnum;
2207 ea.addr_count++;
2208
2209 if (*ea.cmd == ';')
2210 {
2211 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002214 /* don't leave the cursor on an illegal line or column */
2215 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218 else if (*ea.cmd != ',')
2219 break;
2220 ++ea.cmd;
2221 }
2222
2223 /* One address given: set start and end lines */
2224 if (ea.addr_count == 1)
2225 {
2226 ea.line1 = ea.line2;
2227 /* ... but only implicit: really no address given */
2228 if (lnum == MAXLNUM)
2229 ea.addr_count = 0;
2230 }
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002233 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
2235
2236 /*
2237 * Skip ':' and any white space
2238 */
2239 ea.cmd = skipwhite(ea.cmd);
2240 while (*ea.cmd == ':')
2241 ea.cmd = skipwhite(ea.cmd + 1);
2242
2243 /*
2244 * If we got a line, but no command, then go to the line.
2245 * If we find a '|' or '\n' we set ea.nextcmd.
2246 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002247 if (*ea.cmd == NUL || *ea.cmd == '"'
2248 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 /*
2251 * strange vi behaviour:
2252 * ":3" jumps to line 3
2253 * ":3|..." prints line 3
2254 * ":|" prints current line
2255 */
2256 if (ea.skip) /* skip this if inside :if */
2257 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002258 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
2260 ea.cmdidx = CMD_print;
2261 ea.argt = RANGE+COUNT+TRLBAR;
2262 if ((errormsg = invalid_range(&ea)) == NULL)
2263 {
2264 correct_range(&ea);
2265 ex_print(&ea);
2266 }
2267 }
2268 else if (ea.addr_count != 0)
2269 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002270 if (ea.line2 > curbuf->b_ml.ml_line_count)
2271 {
2272 /* With '-' in 'cpoptions' a line number past the file is an
2273 * error, otherwise put it at the end of the file. */
2274 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2275 ea.line2 = -1;
2276 else
2277 ea.line2 = curbuf->b_ml.ml_line_count;
2278 }
2279
2280 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else
2283 {
2284 if (ea.line2 == 0)
2285 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 else
2287 curwin->w_cursor.lnum = ea.line2;
2288 beginline(BL_SOL | BL_FIX);
2289 }
2290 }
2291 goto doend;
2292 }
2293
Bram Moolenaard5005162014-08-22 23:05:54 +02002294 /* If this looks like an undefined user command and there are CmdUndefined
2295 * autocommands defined, trigger the matching autocommands. */
2296 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2297 && ASCII_ISUPPER(*ea.cmd)
2298 && has_cmdundefined())
2299 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002300 int ret;
2301
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002302 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002303 while (ASCII_ISALNUM(*p))
2304 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002305 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002306 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2307 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002308 /* If the autocommands did something and didn't cause an error, try
2309 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002310 p = (ret
2311#ifdef FEAT_EVAL
2312 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002313#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002314 ) ? find_command(&ea, NULL) : ea.cmd;
2315 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#ifdef FEAT_USR_CMDS
2318 if (p == NULL)
2319 {
2320 if (!ea.skip)
2321 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2322 goto doend;
2323 }
2324 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002325 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2326 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 errormsg = uc_fun_cmd();
2329 goto doend;
2330 }
2331#endif
2332 if (ea.cmdidx == CMD_SIZE)
2333 {
2334 if (!ea.skip)
2335 {
2336 STRCPY(IObuff, _("E492: Not an editor command"));
2337 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002338 {
2339 /* If the modifier was parsed OK the error must be in the
2340 * following command */
2341 if (after_modifier != NULL)
2342 append_command(after_modifier);
2343 else
2344 append_command(*cmdlinep);
2345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002347 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349 goto doend;
2350 }
2351
Bram Moolenaar958636c2014-10-21 20:01:58 +02002352 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2353 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002354#ifdef HAVE_EX_SCRIPT_NI
2355 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2356#endif
2357 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359#ifndef FEAT_EVAL
2360 /*
2361 * When the expression evaluation is disabled, recognize the ":if" and
2362 * ":endif" commands and ignore everything in between it.
2363 */
2364 if (ea.cmdidx == CMD_if)
2365 ++if_level;
2366 if (if_level)
2367 {
2368 if (ea.cmdidx == CMD_endif)
2369 --if_level;
2370 goto doend;
2371 }
2372
2373#endif
2374
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002375 /* forced commands */
2376 if (*p == '!' && ea.cmdidx != CMD_substitute
2377 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 ++p;
2380 ea.forceit = TRUE;
2381 }
2382 else
2383 ea.forceit = FALSE;
2384
2385/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002386 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002388 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002389 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 if (!ea.skip)
2392 {
2393#ifdef HAVE_SANDBOX
2394 if (sandbox != 0 && !(ea.argt & SBOXOK))
2395 {
2396 /* Command not allowed in sandbox. */
2397 errormsg = (char_u *)_(e_sandbox);
2398 goto doend;
2399 }
2400#endif
2401 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2402 {
2403 /* Command not allowed in non-'modifiable' buffer */
2404 errormsg = (char_u *)_(e_modifiable);
2405 goto doend;
2406 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002407
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002408 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002409 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002411 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002412 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 goto doend;
2414 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002415 /* Disallow editing another buffer when "curbuf_lock" is set.
2416 * Do allow ":edit" (check for argument later).
2417 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002418 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002419 && ea.cmdidx != CMD_edit
2420 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002421 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002422 && curbuf_locked())
2423 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2426 {
2427 /* no range allowed */
2428 errormsg = (char_u *)_(e_norange);
2429 goto doend;
2430 }
2431 }
2432
2433 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2434 {
2435 errormsg = (char_u *)_(e_nobang);
2436 goto doend;
2437 }
2438
2439 /*
2440 * Don't complain about the range if it is not used
2441 * (could happen if line_count is accidentally set to 0).
2442 */
2443 if (!ea.skip && !ni)
2444 {
2445 /*
2446 * If the range is backwards, ask for confirmation and, if given, swap
2447 * ea.line1 & ea.line2 so it's forwards again.
2448 * When global command is busy, don't ask, will fail below.
2449 */
2450 if (!global_busy && ea.line1 > ea.line2)
2451 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002452 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002454 if (sourcing || exmode_active)
2455 {
2456 errormsg = (char_u *)_("E493: Backwards range given");
2457 goto doend;
2458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 if (ask_yesno((char_u *)
2460 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002461 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 lnum = ea.line1;
2464 ea.line1 = ea.line2;
2465 ea.line2 = lnum;
2466 }
2467 if ((errormsg = invalid_range(&ea)) != NULL)
2468 goto doend;
2469 }
2470
2471 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2472 ea.line2 = 1;
2473
2474 correct_range(&ea);
2475
2476#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002477 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2478 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 /* Put the first line at the start of a closed fold, put the last line
2481 * at the end of a closed fold. */
2482 (void)hasFolding(ea.line1, &ea.line1, NULL);
2483 (void)hasFolding(ea.line2, NULL, &ea.line2);
2484 }
2485#endif
2486
2487#ifdef FEAT_QUICKFIX
2488 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002489 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 * option here, so things like % get expanded.
2491 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002492 p = replace_makeprg(&ea, p, cmdlinep);
2493 if (p == NULL)
2494 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495#endif
2496
2497 /*
2498 * Skip to start of argument.
2499 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2500 */
2501 if (ea.cmdidx == CMD_bang)
2502 ea.arg = p;
2503 else
2504 ea.arg = skipwhite(p);
2505
2506 /*
2507 * Check for "++opt=val" argument.
2508 * Must be first, allow ":w ++enc=utf8 !cmd"
2509 */
2510 if (ea.argt & ARGOPT)
2511 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2512 if (getargopt(&ea) == FAIL && !ni)
2513 {
2514 errormsg = (char_u *)_(e_invarg);
2515 goto doend;
2516 }
2517
2518 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2519 {
2520 if (*ea.arg == '>') /* append */
2521 {
2522 if (*++ea.arg != '>') /* typed wrong */
2523 {
2524 errormsg = (char_u *)_("E494: Use w or w>>");
2525 goto doend;
2526 }
2527 ea.arg = skipwhite(ea.arg + 1);
2528 ea.append = TRUE;
2529 }
2530 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2531 {
2532 ++ea.arg;
2533 ea.usefilter = TRUE;
2534 }
2535 }
2536
2537 if (ea.cmdidx == CMD_read)
2538 {
2539 if (ea.forceit)
2540 {
2541 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2542 ea.forceit = FALSE;
2543 }
2544 else if (*ea.arg == '!') /* :r !filter */
2545 {
2546 ++ea.arg;
2547 ea.usefilter = TRUE;
2548 }
2549 }
2550
2551 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2552 {
2553 ea.amount = 1;
2554 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2555 {
2556 ++ea.arg;
2557 ++ea.amount;
2558 }
2559 ea.arg = skipwhite(ea.arg);
2560 }
2561
2562 /*
2563 * Check for "+command" argument, before checking for next command.
2564 * Don't do this for ":read !cmd" and ":write !cmd".
2565 */
2566 if ((ea.argt & EDITCMD) && !ea.usefilter)
2567 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2568
2569 /*
2570 * Check for '|' to separate commands and '"' to start comments.
2571 * Don't do this for ":read !cmd" and ":write !cmd".
2572 */
2573 if ((ea.argt & TRLBAR) && !ea.usefilter)
2574 separate_nextcmd(&ea);
2575
2576 /*
2577 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002578 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2579 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002581 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002582 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002583 || ea.cmdidx == CMD_global
2584 || ea.cmdidx == CMD_vglobal
2585 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 for (p = ea.arg; *p; ++p)
2588 {
2589 /* Remove one backslash before a newline, so that it's possible to
2590 * pass a newline to the shell and also a newline that is preceded
2591 * with a backslash. This makes it impossible to end a shell
2592 * command in a backslash, but that doesn't appear useful.
2593 * Halving the number of backslashes is incompatible with previous
2594 * versions. */
2595 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002596 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 else if (*p == '\n')
2598 {
2599 ea.nextcmd = p + 1;
2600 *p = NUL;
2601 break;
2602 }
2603 }
2604 }
2605
2606 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2607 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002608 buf_T *buf;
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002611 switch (ea.addr_type)
2612 {
2613 case ADDR_LINES:
2614 ea.line2 = curbuf->b_ml.ml_line_count;
2615 break;
2616 case ADDR_LOADED_BUFFERS:
2617 buf = firstbuf;
2618 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2619 buf = buf->b_next;
2620 ea.line1 = buf->b_fnum;
2621 buf = lastbuf;
2622 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2623 buf = buf->b_prev;
2624 ea.line2 = buf->b_fnum;
2625 break;
2626 case ADDR_BUFFERS:
2627 ea.line1 = firstbuf->b_fnum;
2628 ea.line2 = lastbuf->b_fnum;
2629 break;
2630 case ADDR_WINDOWS:
2631 ea.line2 = LAST_WIN_NR;
2632 break;
2633 case ADDR_TABS:
2634 ea.line2 = LAST_TAB_NR;
2635 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002636 case ADDR_TABS_RELATIVE:
2637 ea.line2 = 1;
2638 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002639 case ADDR_ARGUMENTS:
2640 if (ARGCOUNT == 0)
2641 ea.line1 = ea.line2 = 0;
2642 else
2643 ea.line2 = ARGCOUNT;
2644 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002645#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002646 case ADDR_QUICKFIX:
2647 ea.line2 = qf_get_size(&ea);
2648 if (ea.line2 == 0)
2649 ea.line2 = 1;
2650 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002651#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
2654
2655 /* accept numbered register only when no count allowed (:put) */
2656 if ( (ea.argt & REGSTR)
2657 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002658 /* Do not allow register = for user commands */
2659 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2661 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002662#ifndef FEAT_CLIPBOARD
2663 /* check these explicitly for a more specific error message */
2664 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002666 errormsg = (char_u *)_(e_invalidreg);
2667 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002670 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2671 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002672 {
2673 ea.regname = *ea.arg++;
2674#ifdef FEAT_EVAL
2675 /* for '=' register: accept the rest of the line as an expression */
2676 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2677 {
2678 set_expr_line(vim_strsave(ea.arg));
2679 ea.arg += STRLEN(ea.arg);
2680 }
2681#endif
2682 ea.arg = skipwhite(ea.arg);
2683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685
2686 /*
2687 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2688 * count, it's a buffer name.
2689 */
2690 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2691 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002692 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
2694 n = getdigits(&ea.arg);
2695 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002696 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 errormsg = (char_u *)_(e_zerocount);
2699 goto doend;
2700 }
2701 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2702 {
2703 ea.line2 = n;
2704 if (ea.addr_count == 0)
2705 ea.addr_count = 1;
2706 }
2707 else
2708 {
2709 ea.line1 = ea.line2;
2710 ea.line2 += n - 1;
2711 ++ea.addr_count;
2712 /*
2713 * Be vi compatible: no error message for out of range.
2714 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002715 if (ea.addr_type == ADDR_LINES
2716 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 ea.line2 = curbuf->b_ml.ml_line_count;
2718 }
2719 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002720
2721 /*
2722 * Check for flags: 'l', 'p' and '#'.
2723 */
2724 if (ea.argt & EXFLAGS)
2725 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002727 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002728 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
2730 errormsg = (char_u *)_(e_trailing);
2731 goto doend;
2732 }
2733
2734 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2735 {
2736 errormsg = (char_u *)_(e_argreq);
2737 goto doend;
2738 }
2739
2740#ifdef FEAT_EVAL
2741 /*
2742 * Skip the command when it's not going to be executed.
2743 * The commands like :if, :endif, etc. always need to be executed.
2744 * Also make an exception for commands that handle a trailing command
2745 * themselves.
2746 */
2747 if (ea.skip)
2748 {
2749 switch (ea.cmdidx)
2750 {
2751 /* commands that need evaluation */
2752 case CMD_while:
2753 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002754 case CMD_for:
2755 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 case CMD_if:
2757 case CMD_elseif:
2758 case CMD_else:
2759 case CMD_endif:
2760 case CMD_try:
2761 case CMD_catch:
2762 case CMD_finally:
2763 case CMD_endtry:
2764 case CMD_function:
2765 break;
2766
2767 /* Commands that handle '|' themselves. Check: A command should
2768 * either have the TRLBAR flag, appear in this list or appear in
2769 * the list at ":help :bar". */
2770 case CMD_aboveleft:
2771 case CMD_and:
2772 case CMD_belowright:
2773 case CMD_botright:
2774 case CMD_browse:
2775 case CMD_call:
2776 case CMD_confirm:
2777 case CMD_delfunction:
2778 case CMD_djump:
2779 case CMD_dlist:
2780 case CMD_dsearch:
2781 case CMD_dsplit:
2782 case CMD_echo:
2783 case CMD_echoerr:
2784 case CMD_echomsg:
2785 case CMD_echon:
2786 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002787 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 case CMD_help:
2789 case CMD_hide:
2790 case CMD_ijump:
2791 case CMD_ilist:
2792 case CMD_isearch:
2793 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002794 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 case CMD_keepjumps:
2796 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002797 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 case CMD_leftabove:
2799 case CMD_let:
2800 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002801 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002803 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002804 case CMD_noautocmd:
2805 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 case CMD_perl:
2807 case CMD_psearch:
2808 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002809 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002810 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 case CMD_return:
2812 case CMD_rightbelow:
2813 case CMD_ruby:
2814 case CMD_silent:
2815 case CMD_smagic:
2816 case CMD_snomagic:
2817 case CMD_substitute:
2818 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002819 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case CMD_tcl:
2821 case CMD_throw:
2822 case CMD_tilde:
2823 case CMD_topleft:
2824 case CMD_unlet:
2825 case CMD_verbose:
2826 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002827 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 break;
2829
2830 default: goto doend;
2831 }
2832 }
2833#endif
2834
2835 if (ea.argt & XFILE)
2836 {
2837 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2838 goto doend;
2839 }
2840
2841#ifdef FEAT_LISTCMDS
2842 /*
2843 * Accept buffer name. Cannot be used at the same time with a buffer
2844 * number. Don't do this for a user command.
2845 */
2846 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002847 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 /*
2850 * :bdelete, :bwipeout and :bunload take several arguments, separated
2851 * by spaces: find next space (skipping over escaped characters).
2852 * The others take one argument: ignore trailing spaces.
2853 */
2854 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2855 || ea.cmdidx == CMD_bunload)
2856 p = skiptowhite_esc(ea.arg);
2857 else
2858 {
2859 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002860 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 --p;
2862 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002863 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2864 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 if (ea.line2 < 0) /* failed */
2866 goto doend;
2867 ea.addr_count = 1;
2868 ea.arg = skipwhite(p);
2869 }
2870#endif
2871
Bram Moolenaar2be57332018-02-13 18:05:18 +01002872 /* The :try command saves the emsg_silent flag, reset it here when
2873 * ":silent! try" was used, it should only apply to :try itself. */
2874 if (ea.cmdidx == CMD_try && did_esilent > 0)
2875 {
2876 emsg_silent -= did_esilent;
2877 if (emsg_silent < 0)
2878 emsg_silent = 0;
2879 did_esilent = 0;
2880 }
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002883 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *
2885 * The "ea" structure holds the arguments that can be used.
2886 */
2887 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002888 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 ea.cookie = cookie;
2890#ifdef FEAT_EVAL
2891 ea.cstack = cstack;
2892#endif
2893
2894#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002895 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
2897 /*
2898 * Execute a user-defined command.
2899 */
2900 do_ucmd(&ea);
2901 }
2902 else
2903#endif
2904 {
2905 /*
2906 * Call the function to execute the command.
2907 */
2908 ea.errmsg = NULL;
2909 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2910 if (ea.errmsg != NULL)
2911 errormsg = (char_u *)_(ea.errmsg);
2912 }
2913
2914#ifdef FEAT_EVAL
2915 /*
2916 * If the command just executed called do_cmdline(), any throw or ":return"
2917 * or ":finish" encountered there must also check the cstack of the still
2918 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2919 * exception, or reanimate a returned function or finished script file and
2920 * return or finish it again.
2921 */
2922 if (need_rethrow)
2923 do_throw(cstack);
2924 else if (check_cstack)
2925 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002926 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002928 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 && current_func_returned())
2930 do_return(&ea, TRUE, FALSE, NULL);
2931 }
2932 need_rethrow = check_cstack = FALSE;
2933#endif
2934
2935doend:
2936 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002939 curwin->w_cursor.col = 0;
2940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2943 {
2944 if (sourcing)
2945 {
2946 if (errormsg != IObuff)
2947 {
2948 STRCPY(IObuff, errormsg);
2949 errormsg = IObuff;
2950 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002951 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 emsg(errormsg);
2954 }
2955#ifdef FEAT_EVAL
2956 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002957 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2958 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959#endif
2960
2961 if (verbose_save >= 0)
2962 p_verbose = verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002963
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002964 if (cmdmod.save_ei != NULL)
2965 {
2966 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002967 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2968 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002969 free_string_option(cmdmod.save_ei);
2970 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002971
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002972 if (cmdmod.filter_regmatch.regprog != NULL)
2973 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975 cmdmod = save_cmdmod;
2976
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002977 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
2979 /* messages could be enabled for a serious error, need to check if the
2980 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002981 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002982 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 emsg_silent -= did_esilent;
2984 if (emsg_silent < 0)
2985 emsg_silent = 0;
2986 /* Restore msg_scroll, it's set by file I/O commands, even when no
2987 * message is actually displayed. */
2988 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002989
2990 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2991 * somewhere in the line. Put it back in the first column. */
2992 if (redirecting())
2993 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002996#ifdef HAVE_SANDBOX
2997 if (did_sandbox)
2998 --sandbox;
2999#endif
3000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3002 ea.nextcmd = NULL;
3003
3004#ifdef FEAT_EVAL
3005 --ex_nesting_level;
3006#endif
3007
3008 return ea.nextcmd;
3009}
3010#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003011 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#endif
3013
3014/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003015 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 * If there is a match advance "pp" to the argument and return TRUE.
3017 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003019checkforcmd(
3020 char_u **pp, /* start of command */
3021 char *cmd, /* name of command */
3022 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 int i;
3025
3026 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003027 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 break;
3029 if (i >= len && !isalpha((*pp)[i]))
3030 {
3031 *pp = skipwhite(*pp + i);
3032 return TRUE;
3033 }
3034 return FALSE;
3035}
3036
3037/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003038 * Append "cmd" to the error message in IObuff.
3039 * Takes care of limiting the length and handling 0xa0, which would be
3040 * invisible otherwise.
3041 */
3042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003043append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003044{
3045 char_u *s = cmd;
3046 char_u *d;
3047
3048 STRCAT(IObuff, ": ");
3049 d = IObuff + STRLEN(IObuff);
3050 while (*s != NUL && d - IObuff < IOSIZE - 7)
3051 {
3052 if (
3053#ifdef FEAT_MBYTE
3054 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3055#endif
3056 *s == 0xa0)
3057 {
3058 s +=
3059#ifdef FEAT_MBYTE
3060 enc_utf8 ? 2 :
3061#endif
3062 1;
3063 STRCPY(d, "<a0>");
3064 d += 4;
3065 }
3066 else
3067 MB_COPY_CHAR(s, d);
3068 }
3069 *d = NUL;
3070}
3071
3072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003074 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003076 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 * Returns NULL for an ambiguous user command.
3078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003080find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 int len;
3083 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003084 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
3086 /*
3087 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003088 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * - the 'k' command can directly be followed by any character.
3090 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003091 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003093 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 */
3095 p = eap->cmd;
3096 if (*p == 'k')
3097 {
3098 eap->cmdidx = CMD_k;
3099 ++p;
3100 }
3101 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003102 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3103 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 || p[1] == 'g'
3105 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3106 || p[1] == 'I'
3107 || (p[1] == 'r' && p[2] != 'e')))
3108 {
3109 eap->cmdidx = CMD_substitute;
3110 ++p;
3111 }
3112 else
3113 {
3114 while (ASCII_ISALPHA(*p))
3115 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003116 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003117 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003118 while (ASCII_ISALNUM(*p))
3119 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003120
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 /* check for non-alpha command */
3122 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3123 ++p;
3124 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003125 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3126 {
3127 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3128 * :delete with the 'l' flag. Same for 'p'. */
3129 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003130 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003131 break;
3132 if (i == len - 1)
3133 {
3134 --len;
3135 if (p[-1] == 'l')
3136 eap->flags |= EXFLAG_LIST;
3137 else
3138 eap->flags |= EXFLAG_PRINT;
3139 }
3140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003142 if (ASCII_ISLOWER(eap->cmd[0]))
3143 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003144 int c1 = eap->cmd[0];
3145 int c2 = eap->cmd[1];
3146
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003147 if (command_count != (int)CMD_SIZE)
3148 {
3149 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3150 getout(1);
3151 }
3152
3153 /* Use a precomputed index for fast look-up in cmdnames[]
3154 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003155 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3156 if (ASCII_ISLOWER(c2))
3157 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003160 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3163 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3164 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3165 (size_t)len) == 0)
3166 {
3167#ifdef FEAT_EVAL
3168 if (full != NULL
3169 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3170 *full = TRUE;
3171#endif
3172 break;
3173 }
3174
3175#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003176 /* Look for a user defined command as a last resort. Let ":Print" be
3177 * overruled by a user defined command. */
3178 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3179 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 while (ASCII_ISALNUM(*p))
3183 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003184 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003187 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 eap->cmdidx = CMD_SIZE;
3189 }
3190
3191 return p;
3192}
3193
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194#ifdef FEAT_USR_CMDS
3195/*
3196 * Search for a user command that matches "eap->cmd".
3197 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3198 * Return a pointer to just after the command.
3199 * Return NULL if there is no matching command.
3200 */
3201 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003202find_ucmd(
3203 exarg_T *eap,
3204 char_u *p, /* end of the command (possibly including count) */
3205 int *full, /* set to TRUE for a full match */
3206 expand_T *xp, /* used for completion, NULL otherwise */
3207 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208{
3209 int len = (int)(p - eap->cmd);
3210 int j, k, matchlen = 0;
3211 ucmd_T *uc;
3212 int found = FALSE;
3213 int possible = FALSE;
3214 char_u *cp, *np; /* Point into typed cmd and test name */
3215 garray_T *gap;
3216 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3217 only full match global is accepted. */
3218
3219 /*
3220 * Look for buffer-local user commands first, then global ones.
3221 */
3222 gap = &curbuf->b_ucmds;
3223 for (;;)
3224 {
3225 for (j = 0; j < gap->ga_len; ++j)
3226 {
3227 uc = USER_CMD_GA(gap, j);
3228 cp = eap->cmd;
3229 np = uc->uc_name;
3230 k = 0;
3231 while (k < len && *np != NUL && *cp++ == *np++)
3232 k++;
3233 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3234 {
3235 /* If finding a second match, the command is ambiguous. But
3236 * not if a buffer-local command wasn't a full match and a
3237 * global command is a full match. */
3238 if (k == len && found && *np != NUL)
3239 {
3240 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003242 amb_local = TRUE;
3243 }
3244
3245 if (!found || (k == len && *np == NUL))
3246 {
3247 /* If we matched up to a digit, then there could
3248 * be another command including the digit that we
3249 * should use instead.
3250 */
3251 if (k == len)
3252 found = TRUE;
3253 else
3254 possible = TRUE;
3255
3256 if (gap == &ucmds)
3257 eap->cmdidx = CMD_USER;
3258 else
3259 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003260 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003262 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263
3264# ifdef FEAT_CMDL_COMPL
3265 if (compl != NULL)
3266 *compl = uc->uc_compl;
3267# ifdef FEAT_EVAL
3268 if (xp != NULL)
3269 {
3270 xp->xp_arg = uc->uc_compl_arg;
3271 xp->xp_scriptID = uc->uc_scriptID;
3272 }
3273# endif
3274# endif
3275 /* Do not search for further abbreviations
3276 * if this is an exact match. */
3277 matchlen = k;
3278 if (k == len && *np == NUL)
3279 {
3280 if (full != NULL)
3281 *full = TRUE;
3282 amb_local = FALSE;
3283 break;
3284 }
3285 }
3286 }
3287 }
3288
3289 /* Stop if we found a full match or searched all. */
3290 if (j < gap->ga_len || gap == &ucmds)
3291 break;
3292 gap = &ucmds;
3293 }
3294
3295 /* Only found ambiguous matches. */
3296 if (amb_local)
3297 {
3298 if (xp != NULL)
3299 xp->xp_context = EXPAND_UNSUCCESSFUL;
3300 return NULL;
3301 }
3302
3303 /* The match we found may be followed immediately by a number. Move "p"
3304 * back to point to it. */
3305 if (found || possible)
3306 return p + (matchlen - len);
3307 return p;
3308}
3309#endif
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003312static struct cmdmod
3313{
3314 char *name;
3315 int minlen;
3316 int has_count; /* :123verbose :3tab */
3317} cmdmods[] = {
3318 {"aboveleft", 3, FALSE},
3319 {"belowright", 3, FALSE},
3320 {"botright", 2, FALSE},
3321 {"browse", 3, FALSE},
3322 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003323 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003324 {"hide", 3, FALSE},
3325 {"keepalt", 5, FALSE},
3326 {"keepjumps", 5, FALSE},
3327 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003328 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003329 {"leftabove", 5, FALSE},
3330 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003331 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003332 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333 {"rightbelow", 6, FALSE},
3334 {"sandbox", 3, FALSE},
3335 {"silent", 3, FALSE},
3336 {"tab", 3, TRUE},
3337 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003338 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003339 {"verbose", 4, TRUE},
3340 {"vertical", 4, FALSE},
3341};
3342
3343/*
3344 * Return length of a command modifier (including optional count).
3345 * Return zero when it's not a modifier.
3346 */
3347 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003348modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003349{
3350 int i, j;
3351 char_u *p = cmd;
3352
3353 if (VIM_ISDIGIT(*cmd))
3354 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003355 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003356 {
3357 for (j = 0; p[j] != NUL; ++j)
3358 if (p[j] != cmdmods[i].name[j])
3359 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003360 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003361 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003362 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003363 }
3364 return 0;
3365}
3366
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367/*
3368 * Return > 0 if an Ex command "name" exists.
3369 * Return 2 if there is an exact match.
3370 * Return 3 if there is an ambiguous match.
3371 */
3372 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003373cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 exarg_T ea;
3376 int full = FALSE;
3377 int i;
3378 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003379 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003382 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 for (j = 0; name[j] != NUL; ++j)
3385 if (name[j] != cmdmods[i].name[j])
3386 break;
3387 if (name[j] == NUL && j >= cmdmods[i].minlen)
3388 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3389 }
3390
Bram Moolenaara9587612006-05-04 21:47:50 +00003391 /* Check built-in commands and user defined commands.
3392 * For ":2match" and ":3match" we need to skip the number. */
3393 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003395 p = find_command(&ea, &full);
3396 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003398 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3399 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003400 if (*skipwhite(p) != NUL)
3401 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3403}
3404#endif
3405
3406/*
3407 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3408 * we don't need/want deleted. Maybe this could be done better if we didn't
3409 * repeat all this stuff. The only problem is that they may not stay
3410 * perfectly compatible with each other, but then the command line syntax
3411 * probably won't change that much -- webb.
3412 */
3413 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003414set_one_cmd_context(
3415 expand_T *xp,
3416 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 char_u *p;
3419 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 int len = 0;
3421 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3423 int compl = EXPAND_NOTHING;
3424#endif
3425#ifdef FEAT_CMDL_COMPL
3426 int delim;
3427#endif
3428 int forceit = FALSE;
3429 int usefilter = FALSE; /* filter instead of file name */
3430
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003431 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 xp->xp_pattern = buff;
3433 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003434 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
3436/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003437 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
3439 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3440 ;
3441 xp->xp_pattern = cmd;
3442
3443 if (*cmd == NUL)
3444 return NULL;
3445 if (*cmd == '"') /* ignore comment lines */
3446 {
3447 xp->xp_context = EXPAND_NOTHING;
3448 return NULL;
3449 }
3450
3451/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003452 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 */
3454 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 xp->xp_pattern = cmd;
3456 if (*cmd == NUL)
3457 return NULL;
3458 if (*cmd == '"')
3459 {
3460 xp->xp_context = EXPAND_NOTHING;
3461 return NULL;
3462 }
3463
3464 if (*cmd == '|' || *cmd == '\n')
3465 return cmd + 1; /* There's another command */
3466
3467 /*
3468 * Isolate the command and search for it in the command table.
3469 * Exceptions:
3470 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003471 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3473 */
3474 if (*cmd == 'k' && cmd[1] != 'e')
3475 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003476 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 p = cmd + 1;
3478 }
3479 else
3480 {
3481 p = cmd;
3482 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3483 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003484 /* a user command may contain digits */
3485 if (ASCII_ISUPPER(cmd[0]))
3486 while (ASCII_ISALNUM(*p) || *p == '*')
3487 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003488 /* for python 3.x: ":py3*" commands completion */
3489 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003490 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003491 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003492 while (ASCII_ISALPHA(*p) || *p == '*')
3493 ++p;
3494 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003495 /* check for non-alpha command */
3496 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3497 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003500 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 xp->xp_context = EXPAND_UNSUCCESSFUL;
3503 return NULL;
3504 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003505 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003506 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3507 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3508 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 break;
3510
3511#ifdef FEAT_USR_CMDS
3512 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3514 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515#endif
3516 }
3517
3518 /*
3519 * If the cursor is touching the command, and it ends in an alpha-numeric
3520 * character, complete the command name.
3521 */
3522 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3523 return NULL;
3524
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003525 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3528 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003529 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 p = cmd + 1;
3531 }
3532#ifdef FEAT_USR_CMDS
3533 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3534 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535 ea.cmd = cmd;
3536 p = find_ucmd(&ea, p, NULL, xp,
3537# if defined(FEAT_CMDL_COMPL)
3538 &compl
3539# else
3540 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003543 if (p == NULL)
3544 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546#endif
3547 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 /* Not still touching the command and it was an illegal one */
3551 xp->xp_context = EXPAND_UNSUCCESSFUL;
3552 return NULL;
3553 }
3554
3555 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3556
3557 if (*p == '!') /* forced commands */
3558 {
3559 forceit = TRUE;
3560 ++p;
3561 }
3562
3563/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003564 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003566 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003567 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569 arg = skipwhite(p);
3570
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573 if (*arg == '>') /* append */
3574 {
3575 if (*++arg == '>')
3576 ++arg;
3577 arg = skipwhite(arg);
3578 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003579 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 ++arg;
3582 usefilter = TRUE;
3583 }
3584 }
3585
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 usefilter = forceit; /* :r! filter if forced */
3589 if (*arg == '!') /* :r !filter */
3590 {
3591 ++arg;
3592 usefilter = TRUE;
3593 }
3594 }
3595
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 while (*arg == *cmd) /* allow any number of '>' or '<' */
3599 ++arg;
3600 arg = skipwhite(arg);
3601 }
3602
3603 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003604 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 /* Check if we're in the +command */
3607 p = arg + 1;
3608 arg = skip_cmd_arg(arg, FALSE);
3609
3610 /* Still touching the command after '+'? */
3611 if (*arg == NUL)
3612 return p;
3613
3614 /* Skip space(s) after +command to get to the real argument */
3615 arg = skipwhite(arg);
3616 }
3617
3618 /*
3619 * Check for '|' to separate commands and '"' to start comments.
3620 * Don't do this for ":read !cmd" and ":write !cmd".
3621 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003622 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 p = arg;
3625 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003626 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 p += 2;
3628 while (*p)
3629 {
3630 if (*p == Ctrl_V)
3631 {
3632 if (p[1] != NUL)
3633 ++p;
3634 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 || *p == '|' || *p == '\n')
3637 {
3638 if (*(p - 1) != '\\')
3639 {
3640 if (*p == '|' || *p == '\n')
3641 return p + 1;
3642 return NULL; /* It's a comment */
3643 }
3644 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003645 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
3647 }
3648
3649 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 vim_strchr((char_u *)"|\"", *arg) == NULL)
3652 return NULL;
3653
3654 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003655 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003657 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003658 while (*p && p < buff + len)
3659 {
3660 if (*p == ' ' || *p == TAB)
3661 {
3662 /* argument starts after a space */
3663 xp->xp_pattern = ++p;
3664 }
3665 else
3666 {
3667 if (*p == '\\' && *(p + 1) != NUL)
3668 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003669 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003670 }
3671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003673 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003675 int c;
3676 int in_quote = FALSE;
3677 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 /*
3680 * Allow spaces within back-quotes to count as part of the argument
3681 * being expanded.
3682 */
3683 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003684 p = xp->xp_pattern;
3685 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003687#ifdef FEAT_MBYTE
3688 if (has_mbyte)
3689 c = mb_ptr2char(p);
3690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003692 c = *p;
3693 if (c == '\\' && p[1] != NUL)
3694 ++p;
3695 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 if (!in_quote)
3698 {
3699 xp->xp_pattern = p;
3700 bow = p + 1;
3701 }
3702 in_quote = !in_quote;
3703 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003704 /* An argument can contain just about everything, except
3705 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003706 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003707#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003708 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003709#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003710 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003711 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003712 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003713 while (*p != NUL)
3714 {
3715#ifdef FEAT_MBYTE
3716 if (has_mbyte)
3717 c = mb_ptr2char(p);
3718 else
3719#endif
3720 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003721 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722 break;
3723#ifdef FEAT_MBYTE
3724 if (has_mbyte)
3725 len = (*mb_ptr2len)(p);
3726 else
3727#endif
3728 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003729 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003730 }
3731 if (in_quote)
3732 bow = p;
3733 else
3734 xp->xp_pattern = p;
3735 p -= len;
3736 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003737 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739
3740 /*
3741 * If we are still inside the quotes, and we passed a space, just
3742 * expand from there.
3743 */
3744 if (bow != NULL && in_quote)
3745 xp->xp_pattern = bow;
3746 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003747
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003748 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003749 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003750 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003751#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003752 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003753#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003754 /* When still after the command name expand executables. */
3755 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003756 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 /* Check for environment variable */
3760 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003761#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 || *xp->xp_pattern == '%'
3763#endif
3764 )
3765 {
3766 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3767 if (!vim_isIDc(*p))
3768 break;
3769 if (*p == NUL)
3770 {
3771 xp->xp_context = EXPAND_ENV_VARS;
3772 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003773#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3774 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003775 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003776 compl = EXPAND_ENV_VARS;
3777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003780#if defined(FEAT_CMDL_COMPL)
3781 /* Check for user names */
3782 if (*xp->xp_pattern == '~')
3783 {
3784 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3785 ;
3786 /* Complete ~user only if it partially matches a user name.
3787 * A full match ~user<Tab> will be replaced by user's home
3788 * directory i.e. something like ~user<Tab> -> /home/user/ */
3789 if (*p == NUL && p > xp->xp_pattern + 1
3790 && match_user(xp->xp_pattern + 1) == 1)
3791 {
3792 xp->xp_context = EXPAND_USER;
3793 ++xp->xp_pattern;
3794 }
3795 }
3796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798
3799/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003800 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003802 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003804 case CMD_find:
3805 case CMD_sfind:
3806 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003807 if (xp->xp_context == EXPAND_FILES)
3808 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 case CMD_cd:
3811 case CMD_chdir:
3812 case CMD_lcd:
3813 case CMD_lchdir:
3814 if (xp->xp_context == EXPAND_FILES)
3815 xp->xp_context = EXPAND_DIRECTORIES;
3816 break;
3817 case CMD_help:
3818 xp->xp_context = EXPAND_HELP;
3819 xp->xp_pattern = arg;
3820 break;
3821
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003822 /* Command modifiers: return the argument.
3823 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003825 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 case CMD_belowright:
3827 case CMD_botright:
3828 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003829 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003830 case CMD_cdo:
3831 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003833 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 case CMD_folddoclosed:
3835 case CMD_folddoopen:
3836 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003837 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 case CMD_keepjumps:
3839 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003840 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003841 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003843 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003845 case CMD_noautocmd:
3846 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003848 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003850 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003851 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 case CMD_topleft:
3853 case CMD_verbose:
3854 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003855 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 return arg;
3857
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003858 case CMD_filter:
3859 if (*arg != NUL)
3860 arg = skip_vimgrep_pat(arg, NULL, NULL);
3861 if (arg == NULL || *arg == NUL)
3862 {
3863 xp->xp_context = EXPAND_NOTHING;
3864 return NULL;
3865 }
3866 return skipwhite(arg);
3867
Bram Moolenaar4f688582007-07-24 12:34:30 +00003868#ifdef FEAT_CMDL_COMPL
3869# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 case CMD_match:
3871 if (*arg == NUL || !ends_excmd(*arg))
3872 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003873 /* also complete "None" */
3874 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 arg = skipwhite(skiptowhite(arg));
3876 if (*arg != NUL)
3877 {
3878 xp->xp_context = EXPAND_NOTHING;
3879 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3880 }
3881 }
3882 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885/*
3886 * All completion for the +cmdline_compl feature goes here.
3887 */
3888
3889# ifdef FEAT_USR_CMDS
3890 case CMD_command:
3891 /* Check for attributes */
3892 while (*arg == '-')
3893 {
3894 arg++; /* Skip "-" */
3895 p = skiptowhite(arg);
3896 if (*p == NUL)
3897 {
3898 /* Cursor is still in the attribute */
3899 p = vim_strchr(arg, '=');
3900 if (p == NULL)
3901 {
3902 /* No "=", so complete attribute names */
3903 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3904 xp->xp_pattern = arg;
3905 return NULL;
3906 }
3907
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003908 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 * their arguments as well.
3910 */
3911 if (STRNICMP(arg, "complete", p - arg) == 0)
3912 {
3913 xp->xp_context = EXPAND_USER_COMPLETE;
3914 xp->xp_pattern = p + 1;
3915 return NULL;
3916 }
3917 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3918 {
3919 xp->xp_context = EXPAND_USER_NARGS;
3920 xp->xp_pattern = p + 1;
3921 return NULL;
3922 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003923 else if (STRNICMP(arg, "addr", p - arg) == 0)
3924 {
3925 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3926 xp->xp_pattern = p + 1;
3927 return NULL;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 return NULL;
3930 }
3931 arg = skipwhite(p);
3932 }
3933
3934 /* After the attributes comes the new command name */
3935 p = skiptowhite(arg);
3936 if (*p == NUL)
3937 {
3938 xp->xp_context = EXPAND_USER_COMMANDS;
3939 xp->xp_pattern = arg;
3940 break;
3941 }
3942
3943 /* And finally comes a normal command */
3944 return skipwhite(p);
3945
3946 case CMD_delcommand:
3947 xp->xp_context = EXPAND_USER_COMMANDS;
3948 xp->xp_pattern = arg;
3949 break;
3950# endif
3951
3952 case CMD_global:
3953 case CMD_vglobal:
3954 delim = *arg; /* get the delimiter */
3955 if (delim)
3956 ++arg; /* skip delimiter if there is one */
3957
3958 while (arg[0] != NUL && arg[0] != delim)
3959 {
3960 if (arg[0] == '\\' && arg[1] != NUL)
3961 ++arg;
3962 ++arg;
3963 }
3964 if (arg[0] != NUL)
3965 return arg + 1;
3966 break;
3967 case CMD_and:
3968 case CMD_substitute:
3969 delim = *arg;
3970 if (delim)
3971 {
3972 /* skip "from" part */
3973 ++arg;
3974 arg = skip_regexp(arg, delim, p_magic, NULL);
3975 }
3976 /* skip "to" part */
3977 while (arg[0] != NUL && arg[0] != delim)
3978 {
3979 if (arg[0] == '\\' && arg[1] != NUL)
3980 ++arg;
3981 ++arg;
3982 }
3983 if (arg[0] != NUL) /* skip delimiter */
3984 ++arg;
3985 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3986 ++arg;
3987 if (arg[0] != NUL)
3988 return arg;
3989 break;
3990 case CMD_isearch:
3991 case CMD_dsearch:
3992 case CMD_ilist:
3993 case CMD_dlist:
3994 case CMD_ijump:
3995 case CMD_psearch:
3996 case CMD_djump:
3997 case CMD_isplit:
3998 case CMD_dsplit:
3999 arg = skipwhite(skipdigits(arg)); /* skip count */
4000 if (*arg == '/') /* Match regexp, not just whole words */
4001 {
4002 for (++arg; *arg && *arg != '/'; arg++)
4003 if (*arg == '\\' && arg[1] != NUL)
4004 arg++;
4005 if (*arg)
4006 {
4007 arg = skipwhite(arg + 1);
4008
4009 /* Check for trailing illegal characters */
4010 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4011 xp->xp_context = EXPAND_NOTHING;
4012 else
4013 return arg;
4014 }
4015 }
4016 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 case CMD_autocmd:
4019 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004021 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 case CMD_set:
4024 set_context_in_set_cmd(xp, arg, 0);
4025 break;
4026 case CMD_setglobal:
4027 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4028 break;
4029 case CMD_setlocal:
4030 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4031 break;
4032 case CMD_tag:
4033 case CMD_stag:
4034 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004035 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 case CMD_tselect:
4037 case CMD_stselect:
4038 case CMD_ptselect:
4039 case CMD_tjump:
4040 case CMD_stjump:
4041 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004042 if (*p_wop != NUL)
4043 xp->xp_context = EXPAND_TAGS_LISTFILES;
4044 else
4045 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 xp->xp_pattern = arg;
4047 break;
4048 case CMD_augroup:
4049 xp->xp_context = EXPAND_AUGROUP;
4050 xp->xp_pattern = arg;
4051 break;
4052#ifdef FEAT_SYN_HL
4053 case CMD_syntax:
4054 set_context_in_syntax_cmd(xp, arg);
4055 break;
4056#endif
4057#ifdef FEAT_EVAL
4058 case CMD_let:
4059 case CMD_if:
4060 case CMD_elseif:
4061 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004062 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 case CMD_echo:
4064 case CMD_echon:
4065 case CMD_execute:
4066 case CMD_echomsg:
4067 case CMD_echoerr:
4068 case CMD_call:
4069 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004070 case CMD_cexpr:
4071 case CMD_caddexpr:
4072 case CMD_cgetexpr:
4073 case CMD_lexpr:
4074 case CMD_laddexpr:
4075 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004076 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 break;
4078
4079 case CMD_unlet:
4080 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4081 arg = xp->xp_pattern + 1;
4082 xp->xp_context = EXPAND_USER_VARS;
4083 xp->xp_pattern = arg;
4084 break;
4085
4086 case CMD_function:
4087 case CMD_delfunction:
4088 xp->xp_context = EXPAND_USER_FUNC;
4089 xp->xp_pattern = arg;
4090 break;
4091
4092 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004093 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 break;
4095#endif
4096 case CMD_highlight:
4097 set_context_in_highlight_cmd(xp, arg);
4098 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004099#ifdef FEAT_CSCOPE
4100 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004101 case CMD_lcscope:
4102 case CMD_scscope:
4103 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004104 break;
4105#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004106#ifdef FEAT_SIGNS
4107 case CMD_sign:
4108 set_context_in_sign_cmd(xp, arg);
4109 break;
4110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifdef FEAT_LISTCMDS
4112 case CMD_bdelete:
4113 case CMD_bwipeout:
4114 case CMD_bunload:
4115 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4116 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004117 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 case CMD_buffer:
4119 case CMD_sbuffer:
4120 case CMD_checktime:
4121 xp->xp_context = EXPAND_BUFFERS;
4122 xp->xp_pattern = arg;
4123 break;
4124#endif
4125#ifdef FEAT_USR_CMDS
4126 case CMD_USER:
4127 case CMD_USER_BUF:
4128 if (compl != EXPAND_NOTHING)
4129 {
4130 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004131 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
4133# ifdef FEAT_MENU
4134 if (compl == EXPAND_MENUS)
4135 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4136# endif
4137 if (compl == EXPAND_COMMANDS)
4138 return arg;
4139 if (compl == EXPAND_MAPPINGS)
4140 return set_context_in_map_cmd(xp, (char_u *)"map",
4141 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004142 /* Find start of last argument. */
4143 p = arg;
4144 while (*p)
4145 {
4146 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004147 /* argument starts after a space */
4148 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004149 else if (*p == '\\' && *(p + 1) != NUL)
4150 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004151 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 xp->xp_pattern = arg;
4154 }
4155 xp->xp_context = compl;
4156 }
4157 break;
4158#endif
4159 case CMD_map: case CMD_noremap:
4160 case CMD_nmap: case CMD_nnoremap:
4161 case CMD_vmap: case CMD_vnoremap:
4162 case CMD_omap: case CMD_onoremap:
4163 case CMD_imap: case CMD_inoremap:
4164 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004165 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004166 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004167 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004168 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004170 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 case CMD_unmap:
4172 case CMD_nunmap:
4173 case CMD_vunmap:
4174 case CMD_ounmap:
4175 case CMD_iunmap:
4176 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004177 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004178 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004179 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004180 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004182 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004183 case CMD_mapclear:
4184 case CMD_nmapclear:
4185 case CMD_vmapclear:
4186 case CMD_omapclear:
4187 case CMD_imapclear:
4188 case CMD_cmapclear:
4189 case CMD_lmapclear:
4190 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004191 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004192 case CMD_xmapclear:
4193 xp->xp_context = EXPAND_MAPCLEAR;
4194 xp->xp_pattern = arg;
4195 break;
4196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 case CMD_abbreviate: case CMD_noreabbrev:
4198 case CMD_cabbrev: case CMD_cnoreabbrev:
4199 case CMD_iabbrev: case CMD_inoreabbrev:
4200 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004201 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 case CMD_unabbreviate:
4203 case CMD_cunabbrev:
4204 case CMD_iunabbrev:
4205 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004206 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207#ifdef FEAT_MENU
4208 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4209 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4210 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4211 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4212 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4213 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4214 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4215 case CMD_tmenu: case CMD_tunmenu:
4216 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4217 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4218#endif
4219
4220 case CMD_colorscheme:
4221 xp->xp_context = EXPAND_COLORS;
4222 xp->xp_pattern = arg;
4223 break;
4224
4225 case CMD_compiler:
4226 xp->xp_context = EXPAND_COMPILER;
4227 xp->xp_pattern = arg;
4228 break;
4229
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004230 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004231 xp->xp_context = EXPAND_OWNSYNTAX;
4232 xp->xp_pattern = arg;
4233 break;
4234
4235 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004236 xp->xp_context = EXPAND_FILETYPE;
4237 xp->xp_pattern = arg;
4238 break;
4239
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004240 case CMD_packadd:
4241 xp->xp_context = EXPAND_PACKADD;
4242 xp->xp_pattern = arg;
4243 break;
4244
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4246 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4247 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004248 p = skiptowhite(arg);
4249 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
4251 xp->xp_context = EXPAND_LANGUAGE;
4252 xp->xp_pattern = arg;
4253 }
4254 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004255 {
4256 if ( STRNCMP(arg, "messages", p - arg) == 0
4257 || STRNCMP(arg, "ctype", p - arg) == 0
4258 || STRNCMP(arg, "time", p - arg) == 0)
4259 {
4260 xp->xp_context = EXPAND_LOCALES;
4261 xp->xp_pattern = skipwhite(p);
4262 }
4263 else
4264 xp->xp_context = EXPAND_NOTHING;
4265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 break;
4267#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004268#if defined(FEAT_PROFILE)
4269 case CMD_profile:
4270 set_context_in_profile_cmd(xp, arg);
4271 break;
4272#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004273 case CMD_behave:
4274 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004275 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004278 case CMD_messages:
4279 xp->xp_context = EXPAND_MESSAGES;
4280 xp->xp_pattern = arg;
4281 break;
4282
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004283#if defined(FEAT_CMDHIST)
4284 case CMD_history:
4285 xp->xp_context = EXPAND_HISTORY;
4286 xp->xp_pattern = arg;
4287 break;
4288#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004289#if defined(FEAT_PROFILE)
4290 case CMD_syntime:
4291 xp->xp_context = EXPAND_SYNTIME;
4292 xp->xp_pattern = arg;
4293 break;
4294#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296#endif /* FEAT_CMDL_COMPL */
4297
4298 default:
4299 break;
4300 }
4301 return NULL;
4302}
4303
4304/*
4305 * skip a range specifier of the form: addr [,addr] [;addr] ..
4306 *
4307 * Backslashed delimiters after / or ? will be skipped, and commands will
4308 * not be expanded between /'s and ?'s or after "'".
4309 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004310 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 * Returns the "cmd" pointer advanced to beyond the range.
4312 */
4313 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004314skip_range(
4315 char_u *cmd,
4316 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004318 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004320 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004322 if (*cmd == '\\')
4323 {
4324 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4325 ++cmd;
4326 else
4327 break;
4328 }
4329 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
4331 if (*++cmd == NUL && ctx != NULL)
4332 *ctx = EXPAND_NOTHING;
4333 }
4334 else if (*cmd == '/' || *cmd == '?')
4335 {
4336 delim = *cmd++;
4337 while (*cmd != NUL && *cmd != delim)
4338 if (*cmd++ == '\\' && *cmd != NUL)
4339 ++cmd;
4340 if (*cmd == NUL && ctx != NULL)
4341 *ctx = EXPAND_NOTHING;
4342 }
4343 if (*cmd != NUL)
4344 ++cmd;
4345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004346
4347 /* Skip ":" and white space. */
4348 while (*cmd == ':')
4349 cmd = skipwhite(cmd + 1);
4350
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 return cmd;
4352}
4353
4354/*
4355 * get a single EX address
4356 *
4357 * Set ptr to the next character after the part that was interpreted.
4358 * Set ptr to NULL when an error is encountered.
4359 *
4360 * Return MAXLNUM when no Ex address was found.
4361 */
4362 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004363get_address(
4364 exarg_T *eap UNUSED,
4365 char_u **ptr,
4366 int addr_type, /* flag: one of ADDR_LINES, ... */
4367 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004368 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004369 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
4371 int c;
4372 int i;
4373 long n;
4374 char_u *cmd;
4375 pos_T pos;
4376 pos_T *fp;
4377 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004378 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 cmd = skipwhite(*ptr);
4381 lnum = MAXLNUM;
4382 do
4383 {
4384 switch (*cmd)
4385 {
4386 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 ++cmd;
4388 switch (addr_type)
4389 {
4390 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 lnum = curwin->w_cursor.lnum;
4392 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004394 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 break;
4396 case ADDR_ARGUMENTS:
4397 lnum = curwin->w_arg_idx + 1;
4398 break;
4399 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004400 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 lnum = curbuf->b_fnum;
4402 break;
4403 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004404 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004406 case ADDR_TABS_RELATIVE:
4407 EMSG(_(e_invrange));
4408 cmd = NULL;
4409 goto error;
4410 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004411#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004412 case ADDR_QUICKFIX:
4413 lnum = qf_get_cur_valid_idx(eap);
4414 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004415#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 }
4417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 ++cmd;
4421 switch (addr_type)
4422 {
4423 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 lnum = curbuf->b_ml.ml_line_count;
4425 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004426 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004427 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 break;
4429 case ADDR_ARGUMENTS:
4430 lnum = ARGCOUNT;
4431 break;
4432 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004433 buf = lastbuf;
4434 while (buf->b_ml.ml_mfp == NULL)
4435 {
4436 if (buf->b_prev == NULL)
4437 break;
4438 buf = buf->b_prev;
4439 }
4440 lnum = buf->b_fnum;
4441 break;
4442 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004443 lnum = lastbuf->b_fnum;
4444 break;
4445 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004446 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004448 case ADDR_TABS_RELATIVE:
4449 EMSG(_(e_invrange));
4450 cmd = NULL;
4451 goto error;
4452 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004453#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004454 case ADDR_QUICKFIX:
4455 lnum = qf_get_size(eap);
4456 if (lnum == 0)
4457 lnum = 1;
4458 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004459#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 }
4461 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004464 if (*++cmd == NUL)
4465 {
4466 cmd = NULL;
4467 goto error;
4468 }
4469 if (addr_type != ADDR_LINES)
4470 {
4471 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004472 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004473 goto error;
4474 }
4475 if (skip)
4476 ++cmd;
4477 else
4478 {
4479 /* Only accept a mark in another file when it is
4480 * used by itself: ":'M". */
4481 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4482 ++cmd;
4483 if (fp == (pos_T *)-1)
4484 /* Jumped to another file. */
4485 lnum = curwin->w_cursor.lnum;
4486 else
4487 {
4488 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
4490 cmd = NULL;
4491 goto error;
4492 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 lnum = fp->lnum;
4494 }
4495 }
4496 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 case '/':
4499 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 c = *cmd++;
4501 if (addr_type != ADDR_LINES)
4502 {
4503 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004504 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 goto error;
4506 }
4507 if (skip) /* skip "/pat/" */
4508 {
4509 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4510 if (*cmd == c)
4511 ++cmd;
4512 }
4513 else
4514 {
4515 pos = curwin->w_cursor; /* save curwin->w_cursor */
4516 /*
4517 * When '/' or '?' follows another address, start
4518 * from there.
4519 */
4520 if (lnum != MAXLNUM)
4521 curwin->w_cursor.lnum = lnum;
4522 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004523 * Start a forward search at the end of the line (unless
4524 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 * Start a backward search at the start of the line.
4526 * This makes sure we never match in the current
4527 * line, and can match anywhere in the
4528 * next/previous line.
4529 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004530 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 curwin->w_cursor.col = MAXCOL;
4532 else
4533 curwin->w_cursor.col = 0;
4534 searchcmdlen = 0;
4535 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004536 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 {
4538 curwin->w_cursor = pos;
4539 cmd = NULL;
4540 goto error;
4541 }
4542 lnum = curwin->w_cursor.lnum;
4543 curwin->w_cursor = pos;
4544 /* adjust command string pointer */
4545 cmd += searchcmdlen;
4546 }
4547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 ++cmd;
4551 if (addr_type != ADDR_LINES)
4552 {
4553 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004554 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 goto error;
4556 }
4557 if (*cmd == '&')
4558 i = RE_SUBST;
4559 else if (*cmd == '?' || *cmd == '/')
4560 i = RE_SEARCH;
4561 else
4562 {
4563 EMSG(_(e_backslash));
4564 cmd = NULL;
4565 goto error;
4566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 if (!skip)
4569 {
4570 /*
4571 * When search follows another address, start from
4572 * there.
4573 */
4574 if (lnum != MAXLNUM)
4575 pos.lnum = lnum;
4576 else
4577 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 /*
4580 * Start the search just like for the above
4581 * do_search().
4582 */
4583 if (*cmd != '?')
4584 pos.col = MAXCOL;
4585 else
4586 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004587#ifdef FEAT_VIRTUALEDIT
4588 pos.coladd = 0;
4589#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 if (searchit(curwin, curbuf, &pos,
4591 *cmd == '?' ? BACKWARD : FORWARD,
4592 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004593 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 lnum = pos.lnum;
4595 else
4596 {
4597 cmd = NULL;
4598 goto error;
4599 }
4600 }
4601 ++cmd;
4602 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4606 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608
4609 for (;;)
4610 {
4611 cmd = skipwhite(cmd);
4612 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4613 break;
4614
4615 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 {
4617 switch (addr_type)
4618 {
4619 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004620 /* "+1" is same as ".+1" */
4621 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 break;
4623 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004624 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 break;
4626 case ADDR_ARGUMENTS:
4627 lnum = curwin->w_arg_idx + 1;
4628 break;
4629 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004630 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004631 lnum = curbuf->b_fnum;
4632 break;
4633 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004634 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004636 case ADDR_TABS_RELATIVE:
4637 lnum = 1;
4638 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004639#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004640 case ADDR_QUICKFIX:
4641 lnum = qf_get_cur_valid_idx(eap);
4642 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004643#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 }
4645 }
4646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (VIM_ISDIGIT(*cmd))
4648 i = '+'; /* "number" is same as "+number" */
4649 else
4650 i = *cmd++;
4651 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4652 n = 1;
4653 else
4654 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004655
4656 if (addr_type == ADDR_TABS_RELATIVE)
4657 {
4658 EMSG(_(e_invrange));
4659 cmd = NULL;
4660 goto error;
4661 }
4662 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004663 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004664 lnum = compute_buffer_local_count(
4665 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004667 {
4668#ifdef FEAT_FOLDING
4669 /* Relative line addressing, need to adjust for folded lines
4670 * now, but only do it after the first address. */
4671 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4672 && address_count >= 2)
4673 (void)hasFolding(lnum, NULL, &lnum);
4674#endif
4675 if (i == '-')
4676 lnum -= n;
4677 else
4678 lnum += n;
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681 } while (*cmd == '/' || *cmd == '?');
4682
4683error:
4684 *ptr = cmd;
4685 return lnum;
4686}
4687
4688/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004689 * Get flags from an Ex command argument.
4690 */
4691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004693{
4694 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4695 {
4696 if (*eap->arg == 'l')
4697 eap->flags |= EXFLAG_LIST;
4698 else if (*eap->arg == 'p')
4699 eap->flags |= EXFLAG_PRINT;
4700 else
4701 eap->flags |= EXFLAG_NR;
4702 eap->arg = skipwhite(eap->arg + 1);
4703 }
4704}
4705
4706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 * Function called for command which is Not Implemented. NI!
4708 */
4709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004710ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 if (!eap->skip)
4713 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4714}
4715
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004716#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717/*
4718 * Function called for script command which is Not Implemented. NI!
4719 * Skips over ":perl <<EOF" constructs.
4720 */
4721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004722ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 if (!eap->skip)
4725 ex_ni(eap);
4726 else
4727 vim_free(script_get(eap, eap->arg));
4728}
4729#endif
4730
4731/*
4732 * Check range in Ex command for validity.
4733 * Return NULL when valid, error message when invalid.
4734 */
4735 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004738 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if ( eap->line1 < 0
4740 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004741 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004743
4744 if (eap->argt & RANGE)
4745 {
4746 switch(eap->addr_type)
4747 {
4748 case ADDR_LINES:
4749 if (!(eap->argt & NOTADR)
4750 && eap->line2 > curbuf->b_ml.ml_line_count
4751#ifdef FEAT_DIFF
4752 + (eap->cmdidx == CMD_diffget)
4753#endif
4754 )
4755 return (char_u *)_(e_invrange);
4756 break;
4757 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004758 /* add 1 if ARGCOUNT is 0 */
4759 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004760 return (char_u *)_(e_invrange);
4761 break;
4762 case ADDR_BUFFERS:
4763 if (eap->line1 < firstbuf->b_fnum
4764 || eap->line2 > lastbuf->b_fnum)
4765 return (char_u *)_(e_invrange);
4766 break;
4767 case ADDR_LOADED_BUFFERS:
4768 buf = firstbuf;
4769 while (buf->b_ml.ml_mfp == NULL)
4770 {
4771 if (buf->b_next == NULL)
4772 return (char_u *)_(e_invrange);
4773 buf = buf->b_next;
4774 }
4775 if (eap->line1 < buf->b_fnum)
4776 return (char_u *)_(e_invrange);
4777 buf = lastbuf;
4778 while (buf->b_ml.ml_mfp == NULL)
4779 {
4780 if (buf->b_prev == NULL)
4781 return (char_u *)_(e_invrange);
4782 buf = buf->b_prev;
4783 }
4784 if (eap->line2 > buf->b_fnum)
4785 return (char_u *)_(e_invrange);
4786 break;
4787 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004788 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004789 return (char_u *)_(e_invrange);
4790 break;
4791 case ADDR_TABS:
4792 if (eap->line2 > LAST_TAB_NR)
4793 return (char_u *)_(e_invrange);
4794 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004795 case ADDR_TABS_RELATIVE:
4796 /* Do nothing */
4797 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004798#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004799 case ADDR_QUICKFIX:
4800 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4801 return (char_u *)_(e_invrange);
4802 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004803#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004804 }
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return NULL;
4807}
4808
4809/*
4810 * Correct the range for zero line number, if required.
4811 */
4812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004813correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
4815 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4816 {
4817 if (eap->line1 == 0)
4818 eap->line1 = 1;
4819 if (eap->line2 == 0)
4820 eap->line2 = 1;
4821 }
4822}
4823
Bram Moolenaar748bf032005-02-02 23:04:36 +00004824#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004825static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004826
4827/*
4828 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4829 * pattern. Otherwise return eap->arg.
4830 */
4831 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004832skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004833{
4834 char_u *p = eap->arg;
4835
Bram Moolenaara37420f2006-02-04 22:37:47 +00004836 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4837 || eap->cmdidx == CMD_vimgrepadd
4838 || eap->cmdidx == CMD_lvimgrepadd
4839 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004840 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004841 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004842 if (p == NULL)
4843 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004844 }
4845 return p;
4846}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004847
4848/*
4849 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4850 * in the command line, so that things like % get expanded.
4851 */
4852 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004854{
4855 char_u *new_cmdline;
4856 char_u *program;
4857 char_u *pos;
4858 char_u *ptr;
4859 int len;
4860 int i;
4861
4862 /*
4863 * Don't do it when ":vimgrep" is used for ":grep".
4864 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004865 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4866 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4867 || eap->cmdidx == CMD_grepadd
4868 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004869 && !grep_internal(eap->cmdidx))
4870 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004871 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4872 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004873 {
4874 if (*curbuf->b_p_gp == NUL)
4875 program = p_gp;
4876 else
4877 program = curbuf->b_p_gp;
4878 }
4879 else
4880 {
4881 if (*curbuf->b_p_mp == NUL)
4882 program = p_mp;
4883 else
4884 program = curbuf->b_p_mp;
4885 }
4886
4887 p = skipwhite(p);
4888
4889 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4890 {
4891 /* replace $* by given arguments */
4892 i = 1;
4893 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4894 ++i;
4895 len = (int)STRLEN(p);
4896 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4897 if (new_cmdline == NULL)
4898 return NULL; /* out of memory */
4899 ptr = new_cmdline;
4900 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4901 {
4902 i = (int)(pos - program);
4903 STRNCPY(ptr, program, i);
4904 STRCPY(ptr += i, p);
4905 ptr += len;
4906 program = pos + 2;
4907 }
4908 STRCPY(ptr, program);
4909 }
4910 else
4911 {
4912 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4913 if (new_cmdline == NULL)
4914 return NULL; /* out of memory */
4915 STRCPY(new_cmdline, program);
4916 STRCAT(new_cmdline, " ");
4917 STRCAT(new_cmdline, p);
4918 }
4919 msg_make(p);
4920
4921 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4922 vim_free(*cmdlinep);
4923 *cmdlinep = new_cmdline;
4924 p = new_cmdline;
4925 }
4926 return p;
4927}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004928#endif
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930/*
4931 * Expand file name in Ex command argument.
4932 * Return FAIL for failure, OK otherwise.
4933 */
4934 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935expand_filename(
4936 exarg_T *eap,
4937 char_u **cmdlinep,
4938 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 int has_wildcards; /* need to expand wildcards */
4941 char_u *repl;
4942 int srclen;
4943 char_u *p;
4944 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004945 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaar748bf032005-02-02 23:04:36 +00004947#ifdef FEAT_QUICKFIX
4948 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4949 p = skip_grep_pat(eap);
4950#else
4951 p = eap->arg;
4952#endif
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 /*
4955 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4956 * the file name contains a wildcard it should not cause expanding.
4957 * (it will be expanded anyway if there is a wildcard before replacing).
4958 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004959 has_wildcards = mch_has_wildcard(p);
4960 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004962#ifdef FEAT_EVAL
4963 /* Skip over `=expr`, wildcards in it are not expanded. */
4964 if (p[0] == '`' && p[1] == '=')
4965 {
4966 p += 2;
4967 (void)skip_expr(&p);
4968 if (*p == '`')
4969 ++p;
4970 continue;
4971 }
4972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 /*
4974 * Quick check if this cannot be the start of a special string.
4975 * Also removes backslash before '%', '#' and '<'.
4976 */
4977 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4978 {
4979 ++p;
4980 continue;
4981 }
4982
4983 /*
4984 * Try to find a match at this position.
4985 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004986 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4987 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (*errormsgp != NULL) /* error detected */
4989 return FAIL;
4990 if (repl == NULL) /* no match found */
4991 {
4992 p += srclen;
4993 continue;
4994 }
4995
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004996 /* Wildcards won't be expanded below, the replacement is taken
4997 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4998 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4999 {
5000 char_u *l = repl;
5001
5002 repl = expand_env_save(repl);
5003 vim_free(l);
5004 }
5005
Bram Moolenaar63b92542007-03-27 14:57:09 +00005006 /* Need to escape white space et al. with a backslash.
5007 * Don't do this for:
5008 * - replacement that already has been escaped: "##"
5009 * - shell commands (may have to use quotes instead).
5010 * - non-unix systems when there is a single argument (spaces don't
5011 * separate arguments then).
5012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005014 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 && eap->cmdidx != CMD_grep
5017 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005018 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005019 && eap->cmdidx != CMD_lgrep
5020 && eap->cmdidx != CMD_lgrepadd
5021 && eap->cmdidx != CMD_lmake
5022 && eap->cmdidx != CMD_make
5023 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024#ifndef UNIX
5025 && !(eap->argt & NOSPC)
5026#endif
5027 )
5028 {
5029 char_u *l;
5030#ifdef BACKSLASH_IN_FILENAME
5031 /* Don't escape a backslash here, because rem_backslash() doesn't
5032 * remove it later. */
5033 static char_u *nobslash = (char_u *)" \t\"|";
5034# define ESCAPE_CHARS nobslash
5035#else
5036# define ESCAPE_CHARS escape_chars
5037#endif
5038
5039 for (l = repl; *l; ++l)
5040 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5041 {
5042 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5043 if (l != NULL)
5044 {
5045 vim_free(repl);
5046 repl = l;
5047 }
5048 break;
5049 }
5050 }
5051
5052 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005053 if ((eap->usefilter || eap->cmdidx == CMD_bang
5054 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005055 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 char_u *l;
5058
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005059 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (l != NULL)
5061 {
5062 vim_free(repl);
5063 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 }
5066
5067 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5068 vim_free(repl);
5069 if (p == NULL)
5070 return FAIL;
5071 }
5072
5073 /*
5074 * One file argument: Expand wildcards.
5075 * Don't do this with ":r !command" or ":w !command".
5076 */
5077 if ((eap->argt & NOSPC) && !eap->usefilter)
5078 {
5079 /*
5080 * May do this twice:
5081 * 1. Replace environment variables.
5082 * 2. Replace any other wildcards, remove backslashes.
5083 */
5084 for (n = 1; n <= 2; ++n)
5085 {
5086 if (n == 2)
5087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 /*
5089 * Halve the number of backslashes (this is Vi compatible).
5090 * For Unix and OS/2, when wildcards are expanded, this is
5091 * done by ExpandOne() below.
5092 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005093#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (!has_wildcards)
5095#endif
5096 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098
5099 if (has_wildcards)
5100 {
5101 if (n == 1)
5102 {
5103 /*
5104 * First loop: May expand environment variables. This
5105 * can be done much faster with expand_env() than with
5106 * something else (e.g., calling a shell).
5107 * After expanding environment variables, check again
5108 * if there are still wildcards present.
5109 */
5110 if (vim_strchr(eap->arg, '$') != NULL
5111 || vim_strchr(eap->arg, '~') != NULL)
5112 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005113 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005114 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 has_wildcards = mch_has_wildcard(NameBuff);
5116 p = NameBuff;
5117 }
5118 else
5119 p = NULL;
5120 }
5121 else /* n == 2 */
5122 {
5123 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005124 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125
5126 ExpandInit(&xpc);
5127 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005128 if (p_wic)
5129 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005131 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (p == NULL)
5133 return FAIL;
5134 }
5135 if (p != NULL)
5136 {
5137 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5138 p, cmdlinep);
5139 if (n == 2) /* p came from ExpandOne() */
5140 vim_free(p);
5141 }
5142 }
5143 }
5144 }
5145 return OK;
5146}
5147
5148/*
5149 * Replace part of the command line, keeping eap->cmd, eap->arg and
5150 * eap->nextcmd correct.
5151 * "src" points to the part that is to be replaced, of length "srclen".
5152 * "repl" is the replacement string.
5153 * Returns a pointer to the character after the replaced string.
5154 * Returns NULL for failure.
5155 */
5156 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005157repl_cmdline(
5158 exarg_T *eap,
5159 char_u *src,
5160 int srclen,
5161 char_u *repl,
5162 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164 int len;
5165 int i;
5166 char_u *new_cmdline;
5167
5168 /*
5169 * The new command line is build in new_cmdline[].
5170 * First allocate it.
5171 * Careful: a "+cmd" argument may have been NUL terminated.
5172 */
5173 len = (int)STRLEN(repl);
5174 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005175 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5177 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5178 return NULL; /* out of memory! */
5179
5180 /*
5181 * Copy the stuff before the expanded part.
5182 * Copy the expanded stuff.
5183 * Copy what came after the expanded part.
5184 * Copy the next commands, if there are any.
5185 */
5186 i = (int)(src - *cmdlinep); /* length of part before match */
5187 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005188
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 mch_memmove(new_cmdline + i, repl, (size_t)len);
5190 i += len; /* remember the end of the string */
5191 STRCPY(new_cmdline + i, src + srclen);
5192 src = new_cmdline + i; /* remember where to continue */
5193
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005194 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
5196 i = (int)STRLEN(new_cmdline) + 1;
5197 STRCPY(new_cmdline + i, eap->nextcmd);
5198 eap->nextcmd = new_cmdline + i;
5199 }
5200 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5201 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5202 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5203 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5204 vim_free(*cmdlinep);
5205 *cmdlinep = new_cmdline;
5206
5207 return src;
5208}
5209
5210/*
5211 * Check for '|' to separate commands and '"' to start comments.
5212 */
5213 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005214separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
5216 char_u *p;
5217
Bram Moolenaar86b68352004-12-27 21:59:20 +00005218#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005219 p = skip_grep_pat(eap);
5220#else
5221 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005222#endif
5223
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005224 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
5226 if (*p == Ctrl_V)
5227 {
5228 if (eap->argt & (USECTRLV | XFILE))
5229 ++p; /* skip CTRL-V and next char */
5230 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005231 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005232 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if (*p == NUL) /* stop at NUL after CTRL-V */
5234 break;
5235 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005236
5237#ifdef FEAT_EVAL
5238 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005239 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005240 {
5241 p += 2;
5242 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005243 }
5244#endif
5245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 /* Check for '"': start of comment or '|': next command */
5247 /* :@" and :*" do not start a comment!
5248 * :redir @" doesn't either. */
5249 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5250 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5251 || p != eap->arg)
5252 && (eap->cmdidx != CMD_redir
5253 || p != eap->arg + 1 || p[-1] != '@'))
5254 || *p == '|' || *p == '\n')
5255 {
5256 /*
5257 * We remove the '\' before the '|', unless USECTRLV is used
5258 * AND 'b' is present in 'cpoptions'.
5259 */
5260 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5261 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5262 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005263 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 --p;
5265 }
5266 else
5267 {
5268 eap->nextcmd = check_nextcmd(p);
5269 *p = NUL;
5270 break;
5271 }
5272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5276 del_trailing_spaces(eap->arg);
5277}
5278
5279/*
5280 * get + command from ex argument
5281 */
5282 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005283getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
5285 char_u *arg = *argp;
5286 char_u *command = NULL;
5287
5288 if (*arg == '+') /* +[command] */
5289 {
5290 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005291 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 command = dollar_command;
5293 else
5294 {
5295 command = arg;
5296 arg = skip_cmd_arg(command, TRUE);
5297 if (*arg != NUL)
5298 *arg++ = NUL; /* terminate command with NUL */
5299 }
5300
5301 arg = skipwhite(arg); /* skip over spaces */
5302 *argp = arg;
5303 }
5304 return command;
5305}
5306
5307/*
5308 * Find end of "+command" argument. Skip over "\ " and "\\".
5309 */
5310 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005311skip_cmd_arg(
5312 char_u *p,
5313 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 while (*p && !vim_isspace(*p))
5316 {
5317 if (*p == '\\' && p[1] != NUL)
5318 {
5319 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005320 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 else
5322 ++p;
5323 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005324 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
5326 return p;
5327}
5328
5329/*
5330 * Get "++opt=arg" argument.
5331 * Return FAIL or OK.
5332 */
5333 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005334getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
5336 char_u *arg = eap->arg + 2;
5337 int *pp = NULL;
5338#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005339 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 char_u *p;
5341#endif
5342
5343 /* ":edit ++[no]bin[ary] file" */
5344 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5345 {
5346 if (*arg == 'n')
5347 {
5348 arg += 2;
5349 eap->force_bin = FORCE_NOBIN;
5350 }
5351 else
5352 eap->force_bin = FORCE_BIN;
5353 if (!checkforcmd(&arg, "binary", 3))
5354 return FAIL;
5355 eap->arg = skipwhite(arg);
5356 return OK;
5357 }
5358
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005359 /* ":read ++edit file" */
5360 if (STRNCMP(arg, "edit", 4) == 0)
5361 {
5362 eap->read_edit = TRUE;
5363 eap->arg = skipwhite(arg + 4);
5364 return OK;
5365 }
5366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 if (STRNCMP(arg, "ff", 2) == 0)
5368 {
5369 arg += 2;
5370 pp = &eap->force_ff;
5371 }
5372 else if (STRNCMP(arg, "fileformat", 10) == 0)
5373 {
5374 arg += 10;
5375 pp = &eap->force_ff;
5376 }
5377#ifdef FEAT_MBYTE
5378 else if (STRNCMP(arg, "enc", 3) == 0)
5379 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005380 if (STRNCMP(arg, "encoding", 8) == 0)
5381 arg += 8;
5382 else
5383 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 pp = &eap->force_enc;
5385 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005386 else if (STRNCMP(arg, "bad", 3) == 0)
5387 {
5388 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005389 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#endif
5392
5393 if (pp == NULL || *arg != '=')
5394 return FAIL;
5395
5396 ++arg;
5397 *pp = (int)(arg - eap->cmd);
5398 arg = skip_cmd_arg(arg, FALSE);
5399 eap->arg = skipwhite(arg);
5400 *arg = NUL;
5401
5402#ifdef FEAT_MBYTE
5403 if (pp == &eap->force_ff)
5404 {
5405#endif
5406 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5407 return FAIL;
5408#ifdef FEAT_MBYTE
5409 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005410 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
5412 /* Make 'fileencoding' lower case. */
5413 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5414 *p = TOLOWER_ASC(*p);
5415 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005416 else
5417 {
5418 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5419 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005420 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005421 if (STRICMP(p, "keep") == 0)
5422 eap->bad_char = BAD_KEEP;
5423 else if (STRICMP(p, "drop") == 0)
5424 eap->bad_char = BAD_DROP;
5425 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5426 eap->bad_char = *p;
5427 else
5428 return FAIL;
5429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#endif
5431
5432 return OK;
5433}
5434
5435/*
5436 * ":abbreviate" and friends.
5437 */
5438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005439ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 do_exmap(eap, TRUE); /* almost the same as mapping */
5442}
5443
5444/*
5445 * ":map" and friends.
5446 */
5447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 /*
5451 * If we are sourcing .exrc or .vimrc in current directory we
5452 * print the mappings for security reasons.
5453 */
5454 if (secure)
5455 {
5456 secure = 2;
5457 msg_outtrans(eap->cmd);
5458 msg_putchar('\n');
5459 }
5460 do_exmap(eap, FALSE);
5461}
5462
5463/*
5464 * ":unmap" and friends.
5465 */
5466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 do_exmap(eap, FALSE);
5470}
5471
5472/*
5473 * ":mapclear" and friends.
5474 */
5475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005476ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5479}
5480
5481/*
5482 * ":abclear" and friends.
5483 */
5484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005485ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5488}
5489
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524#ifdef FEAT_LISTCMDS
5525/*
5526 * :[N]bunload[!] [N] [bufname] unload buffer
5527 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5528 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 eap->errmsg = do_bufdel(
5534 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5535 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5536 : DOBUF_UNLOAD, eap->arg,
5537 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5538}
5539
5540/*
5541 * :[N]buffer [N] to buffer N
5542 * :[N]sbuffer [N] to buffer N
5543 */
5544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005545ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 if (*eap->arg)
5548 eap->errmsg = e_trailing;
5549 else
5550 {
5551 if (eap->addr_count == 0) /* default is current buffer */
5552 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5553 else
5554 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005555 if (eap->do_ecmd_cmd != NULL)
5556 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558}
5559
5560/*
5561 * :[N]bmodified [N] to next mod. buffer
5562 * :[N]sbmodified [N] to next mod. buffer
5563 */
5564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005568 if (eap->do_ecmd_cmd != NULL)
5569 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570}
5571
5572/*
5573 * :[N]bnext [N] to next buffer
5574 * :[N]sbnext [N] split and to next buffer
5575 */
5576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005580 if (eap->do_ecmd_cmd != NULL)
5581 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
5584/*
5585 * :[N]bNext [N] to previous buffer
5586 * :[N]bprevious [N] to previous buffer
5587 * :[N]sbNext [N] split and to previous buffer
5588 * :[N]sbprevious [N] split and to previous buffer
5589 */
5590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005591ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005594 if (eap->do_ecmd_cmd != NULL)
5595 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596}
5597
5598/*
5599 * :brewind to first buffer
5600 * :bfirst to first buffer
5601 * :sbrewind split and to first buffer
5602 * :sbfirst split and to first buffer
5603 */
5604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005605ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
5607 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005608 if (eap->do_ecmd_cmd != NULL)
5609 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610}
5611
5612/*
5613 * :blast to last buffer
5614 * :sblast split and to last buffer
5615 */
5616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005620 if (eap->do_ecmd_cmd != NULL)
5621 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
5623#endif
5624
5625 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 return (c == NUL || c == '|' || c == '"' || c == '\n');
5629}
5630
5631#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5632 || defined(PROTO)
5633/*
5634 * Return the next command, after the first '|' or '\n'.
5635 * Return NULL if not found.
5636 */
5637 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 while (*p != '|' && *p != '\n')
5641 {
5642 if (*p == NUL)
5643 return NULL;
5644 ++p;
5645 }
5646 return (p + 1);
5647}
5648#endif
5649
5650/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005651 * Check if *p is a separator between Ex commands, skipping over white space.
5652 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 */
5654 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005657 char_u *s = skipwhite(p);
5658
5659 if (*s == '|' || *s == '\n')
5660 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
5662 return NULL;
5663}
5664
5665/*
5666 * - if there are more files to edit
5667 * - and this is the last window
5668 * - and forceit not used
5669 * - and not repeated twice on a row
5670 * return FAIL and give error message if 'message' TRUE
5671 * return OK otherwise
5672 */
5673 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005674check_more(
5675 int message, /* when FALSE check only, no messages */
5676 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5679
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005680 if (!forceit && only_one_window()
5681 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
5683 if (message)
5684 {
5685#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5686 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5687 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005688 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005691 vim_strncpy(buff,
5692 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005693 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005695 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 _("%d more files to edit. Quit anyway?"), n);
5697 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5698 return OK;
5699 return FAIL;
5700 }
5701#endif
5702 if (n == 1)
5703 EMSG(_("E173: 1 more file to edit"));
5704 else
5705 EMSGN(_("E173: %ld more files to edit"), n);
5706 quitmore = 2; /* next try to quit is allowed */
5707 }
5708 return FAIL;
5709 }
5710 return OK;
5711}
5712
5713#ifdef FEAT_CMDL_COMPL
5714/*
5715 * Function given to ExpandGeneric() to obtain the list of command names.
5716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
5720 if (idx >= (int)CMD_SIZE)
5721# ifdef FEAT_USR_CMDS
5722 return get_user_command_name(idx);
5723# else
5724 return NULL;
5725# endif
5726 return cmdnames[idx].cmd_name;
5727}
5728#endif
5729
5730#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005731static 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);
5732static void uc_list(char_u *name, size_t name_len);
5733static 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);
5734static char_u *uc_split_args(char_u *arg, size_t *lenp);
5735static 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 +00005736
5737 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738uc_add_command(
5739 char_u *name,
5740 size_t name_len,
5741 char_u *rep,
5742 long argt,
5743 long def,
5744 int flags,
5745 int compl,
5746 char_u *compl_arg,
5747 int addr_type,
5748 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 ucmd_T *cmd = NULL;
5751 char_u *p;
5752 int i;
5753 int cmp = 1;
5754 char_u *rep_buf = NULL;
5755 garray_T *gap;
5756
Bram Moolenaar9c102382006-05-03 21:26:49 +00005757 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 if (rep_buf == NULL)
5759 {
5760 /* Can't replace termcodes - try using the string as is */
5761 rep_buf = vim_strsave(rep);
5762
5763 /* Give up if out of memory */
5764 if (rep_buf == NULL)
5765 return FAIL;
5766 }
5767
5768 /* get address of growarray: global or in curbuf */
5769 if (flags & UC_BUFFER)
5770 {
5771 gap = &curbuf->b_ucmds;
5772 if (gap->ga_itemsize == 0)
5773 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5774 }
5775 else
5776 gap = &ucmds;
5777
5778 /* Search for the command in the already defined commands. */
5779 for (i = 0; i < gap->ga_len; ++i)
5780 {
5781 size_t len;
5782
5783 cmd = USER_CMD_GA(gap, i);
5784 len = STRLEN(cmd->uc_name);
5785 cmp = STRNCMP(name, cmd->uc_name, name_len);
5786 if (cmp == 0)
5787 {
5788 if (name_len < len)
5789 cmp = -1;
5790 else if (name_len > len)
5791 cmp = 1;
5792 }
5793
5794 if (cmp == 0)
5795 {
5796 if (!force)
5797 {
5798 EMSG(_("E174: Command already exists: add ! to replace it"));
5799 goto fail;
5800 }
5801
Bram Moolenaard23a8232018-02-10 18:45:26 +01005802 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005803#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005804 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807 }
5808
5809 /* Stop as soon as we pass the name to add */
5810 if (cmp < 0)
5811 break;
5812 }
5813
5814 /* Extend the array unless we're replacing an existing command */
5815 if (cmp != 0)
5816 {
5817 if (ga_grow(gap, 1) != OK)
5818 goto fail;
5819 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5820 goto fail;
5821
5822 cmd = USER_CMD_GA(gap, i);
5823 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5824
5825 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827 cmd->uc_name = p;
5828 }
5829
5830 cmd->uc_rep = rep_buf;
5831 cmd->uc_argt = argt;
5832 cmd->uc_def = def;
5833 cmd->uc_compl = compl;
5834#ifdef FEAT_EVAL
5835 cmd->uc_scriptID = current_SID;
5836# ifdef FEAT_CMDL_COMPL
5837 cmd->uc_compl_arg = compl_arg;
5838# endif
5839#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005840 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 return OK;
5843
5844fail:
5845 vim_free(rep_buf);
5846#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5847 vim_free(compl_arg);
5848#endif
5849 return FAIL;
5850}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005853#if defined(FEAT_USR_CMDS)
5854static struct
5855{
5856 int expand;
5857 char *name;
5858} addr_type_complete[] =
5859{
5860 {ADDR_ARGUMENTS, "arguments"},
5861 {ADDR_LINES, "lines"},
5862 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5863 {ADDR_TABS, "tabs"},
5864 {ADDR_BUFFERS, "buffers"},
5865 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005866 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005867 {-1, NULL}
5868};
5869#endif
5870
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005871#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872/*
5873 * List of names for completion for ":command" with the EXPAND_ flag.
5874 * Must be alphabetical for completion.
5875 */
5876static struct
5877{
5878 int expand;
5879 char *name;
5880} command_complete[] =
5881{
5882 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005883 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005885 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005887 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005888#if defined(FEAT_CSCOPE)
5889 {EXPAND_CSCOPE, "cscope"},
5890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5892 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005893 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894#endif
5895 {EXPAND_DIRECTORIES, "dir"},
5896 {EXPAND_ENV_VARS, "environment"},
5897 {EXPAND_EVENTS, "event"},
5898 {EXPAND_EXPRESSION, "expression"},
5899 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005900 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005901 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {EXPAND_FUNCTIONS, "function"},
5903 {EXPAND_HELP, "help"},
5904 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005905#if defined(FEAT_CMDHIST)
5906 {EXPAND_HISTORY, "history"},
5907#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005908#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005909 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005910 {EXPAND_LOCALES, "locale"},
5911#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005912 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {EXPAND_MAPPINGS, "mapping"},
5914 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005915 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005916 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005917#if defined(FEAT_PROFILE)
5918 {EXPAND_SYNTIME, "syntime"},
5919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005921 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005922 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005923#if defined(FEAT_SIGNS)
5924 {EXPAND_SIGN, "sign"},
5925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {EXPAND_TAGS, "tag"},
5927 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005928 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {EXPAND_USER_VARS, "var"},
5930 {0, NULL}
5931};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005934#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005936uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 int i, j;
5939 int found = FALSE;
5940 ucmd_T *cmd;
5941 int len;
5942 long a;
5943 garray_T *gap;
5944
5945 gap = &curbuf->b_ucmds;
5946 for (;;)
5947 {
5948 for (i = 0; i < gap->ga_len; ++i)
5949 {
5950 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005951 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
Bram Moolenaard29459b2016-08-26 22:29:11 +02005953 /* Skip commands which don't match the requested prefix and
5954 * commands filtered out. */
5955 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5956 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 continue;
5958
5959 /* Put out the title first time */
5960 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005961 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 found = TRUE;
5963 msg_putchar('\n');
5964 if (got_int)
5965 break;
5966
5967 /* Special cases */
5968 msg_putchar(a & BANG ? '!' : ' ');
5969 msg_putchar(a & REGSTR ? '"' : ' ');
5970 msg_putchar(gap != &ucmds ? 'b' : ' ');
5971 msg_putchar(' ');
5972
Bram Moolenaar8820b482017-03-16 17:23:31 +01005973 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 len = (int)STRLEN(cmd->uc_name) + 4;
5975
5976 do {
5977 msg_putchar(' ');
5978 ++len;
5979 } while (len < 16);
5980
5981 len = 0;
5982
5983 /* Arguments */
5984 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5985 {
5986 case 0: IObuff[len++] = '0'; break;
5987 case (EXTRA): IObuff[len++] = '*'; break;
5988 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5989 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5990 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5991 }
5992
5993 do {
5994 IObuff[len++] = ' ';
5995 } while (len < 5);
5996
5997 /* Range */
5998 if (a & (RANGE|COUNT))
5999 {
6000 if (a & COUNT)
6001 {
6002 /* -count=N */
6003 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6004 len += (int)STRLEN(IObuff + len);
6005 }
6006 else if (a & DFLALL)
6007 IObuff[len++] = '%';
6008 else if (cmd->uc_def >= 0)
6009 {
6010 /* -range=N */
6011 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6012 len += (int)STRLEN(IObuff + len);
6013 }
6014 else
6015 IObuff[len++] = '.';
6016 }
6017
6018 do {
6019 IObuff[len++] = ' ';
6020 } while (len < 11);
6021
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006022 /* Address Type */
6023 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6024 if (addr_type_complete[j].expand != ADDR_LINES
6025 && addr_type_complete[j].expand == cmd->uc_addr_type)
6026 {
6027 STRCPY(IObuff + len, addr_type_complete[j].name);
6028 len += (int)STRLEN(IObuff + len);
6029 break;
6030 }
6031
6032 do {
6033 IObuff[len++] = ' ';
6034 } while (len < 21);
6035
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 /* Completion */
6037 for (j = 0; command_complete[j].expand != 0; ++j)
6038 if (command_complete[j].expand == cmd->uc_compl)
6039 {
6040 STRCPY(IObuff + len, command_complete[j].name);
6041 len += (int)STRLEN(IObuff + len);
6042 break;
6043 }
6044
6045 do {
6046 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006047 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049 IObuff[len] = '\0';
6050 msg_outtrans(IObuff);
6051
6052 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006053#ifdef FEAT_EVAL
6054 if (p_verbose > 0)
6055 last_set_msg(cmd->uc_scriptID);
6056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 out_flush();
6058 ui_breakcheck();
6059 if (got_int)
6060 break;
6061 }
6062 if (gap == &ucmds || i < gap->ga_len)
6063 break;
6064 gap = &ucmds;
6065 }
6066
6067 if (!found)
6068 MSG(_("No user-defined commands found"));
6069}
6070
6071 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006072uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
6074 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6075 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6076 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6077 0xb9, 0x7f, 0};
6078 int i;
6079
6080 for (i = 0; fcmd[i]; ++i)
6081 IObuff[i] = fcmd[i] - 0x40;
6082 IObuff[i] = 0;
6083 return IObuff;
6084}
6085
6086 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006087uc_scan_attr(
6088 char_u *attr,
6089 size_t len,
6090 long *argt,
6091 long *def,
6092 int *flags,
6093 int *compl,
6094 char_u **compl_arg,
6095 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 char_u *p;
6098
6099 if (len == 0)
6100 {
6101 EMSG(_("E175: No attribute specified"));
6102 return FAIL;
6103 }
6104
6105 /* First, try the simple attributes (no arguments) */
6106 if (STRNICMP(attr, "bang", len) == 0)
6107 *argt |= BANG;
6108 else if (STRNICMP(attr, "buffer", len) == 0)
6109 *flags |= UC_BUFFER;
6110 else if (STRNICMP(attr, "register", len) == 0)
6111 *argt |= REGSTR;
6112 else if (STRNICMP(attr, "bar", len) == 0)
6113 *argt |= TRLBAR;
6114 else
6115 {
6116 int i;
6117 char_u *val = NULL;
6118 size_t vallen = 0;
6119 size_t attrlen = len;
6120
6121 /* Look for the attribute name - which is the part before any '=' */
6122 for (i = 0; i < (int)len; ++i)
6123 {
6124 if (attr[i] == '=')
6125 {
6126 val = &attr[i + 1];
6127 vallen = len - i - 1;
6128 attrlen = i;
6129 break;
6130 }
6131 }
6132
6133 if (STRNICMP(attr, "nargs", attrlen) == 0)
6134 {
6135 if (vallen == 1)
6136 {
6137 if (*val == '0')
6138 /* Do nothing - this is the default */;
6139 else if (*val == '1')
6140 *argt |= (EXTRA | NOSPC | NEEDARG);
6141 else if (*val == '*')
6142 *argt |= EXTRA;
6143 else if (*val == '?')
6144 *argt |= (EXTRA | NOSPC);
6145 else if (*val == '+')
6146 *argt |= (EXTRA | NEEDARG);
6147 else
6148 goto wrong_nargs;
6149 }
6150 else
6151 {
6152wrong_nargs:
6153 EMSG(_("E176: Invalid number of arguments"));
6154 return FAIL;
6155 }
6156 }
6157 else if (STRNICMP(attr, "range", attrlen) == 0)
6158 {
6159 *argt |= RANGE;
6160 if (vallen == 1 && *val == '%')
6161 *argt |= DFLALL;
6162 else if (val != NULL)
6163 {
6164 p = val;
6165 if (*def >= 0)
6166 {
6167two_count:
6168 EMSG(_("E177: Count cannot be specified twice"));
6169 return FAIL;
6170 }
6171
6172 *def = getdigits(&p);
6173 *argt |= (ZEROR | NOTADR);
6174
6175 if (p != val + vallen || vallen == 0)
6176 {
6177invalid_count:
6178 EMSG(_("E178: Invalid default value for count"));
6179 return FAIL;
6180 }
6181 }
6182 }
6183 else if (STRNICMP(attr, "count", attrlen) == 0)
6184 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006185 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
6187 if (val != NULL)
6188 {
6189 p = val;
6190 if (*def >= 0)
6191 goto two_count;
6192
6193 *def = getdigits(&p);
6194
6195 if (p != val + vallen)
6196 goto invalid_count;
6197 }
6198
6199 if (*def < 0)
6200 *def = 0;
6201 }
6202 else if (STRNICMP(attr, "complete", attrlen) == 0)
6203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (val == NULL)
6205 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006206 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 return FAIL;
6208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006210 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6211 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006214 else if (STRNICMP(attr, "addr", attrlen) == 0)
6215 {
6216 *argt |= RANGE;
6217 if (val == NULL)
6218 {
6219 EMSG(_("E179: argument required for -addr"));
6220 return FAIL;
6221 }
6222 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6223 == FAIL)
6224 return FAIL;
6225 if (addr_type_arg != ADDR_LINES)
6226 *argt |= (ZEROR | NOTADR) ;
6227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 else
6229 {
6230 char_u ch = attr[len];
6231 attr[len] = '\0';
6232 EMSG2(_("E181: Invalid attribute: %s"), attr);
6233 attr[len] = ch;
6234 return FAIL;
6235 }
6236 }
6237
6238 return OK;
6239}
6240
Bram Moolenaara850a712009-01-28 14:42:59 +00006241/*
6242 * ":command ..."
6243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
6247 char_u *name;
6248 char_u *end;
6249 char_u *p;
6250 long argt = 0;
6251 long def = -1;
6252 int flags = 0;
6253 int compl = EXPAND_NOTHING;
6254 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006255 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006257 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258
6259 p = eap->arg;
6260
6261 /* Check for attributes */
6262 while (*p == '-')
6263 {
6264 ++p;
6265 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006266 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6267 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 == FAIL)
6269 return;
6270 p = skipwhite(end);
6271 }
6272
6273 /* Get the name (if any) and skip to the following argument */
6274 name = p;
6275 if (ASCII_ISALPHA(*p))
6276 while (ASCII_ISALNUM(*p))
6277 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006278 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
6280 EMSG(_("E182: Invalid command name"));
6281 return;
6282 }
6283 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006284 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
6286 /* If there is nothing after the name, and no attributes were specified,
6287 * we are listing commands
6288 */
6289 p = skipwhite(end);
6290 if (!has_attr && ends_excmd(*p))
6291 {
6292 uc_list(name, end - name);
6293 }
6294 else if (!ASCII_ISUPPER(*name))
6295 {
6296 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6297 return;
6298 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006299 else if ((name_len == 1 && *name == 'X')
6300 || (name_len <= 4
6301 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6302 {
6303 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6304 return;
6305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 else
6307 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006308 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309}
6310
6311/*
6312 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 * Clear all user commands, global and for current buffer.
6314 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006315 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006316ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 uc_clear(&ucmds);
6319 uc_clear(&curbuf->b_ucmds);
6320}
6321
6322/*
6323 * Clear all user commands for "gap".
6324 */
6325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006326uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 int i;
6329 ucmd_T *cmd;
6330
6331 for (i = 0; i < gap->ga_len; ++i)
6332 {
6333 cmd = USER_CMD_GA(gap, i);
6334 vim_free(cmd->uc_name);
6335 vim_free(cmd->uc_rep);
6336# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6337 vim_free(cmd->uc_compl_arg);
6338# endif
6339 }
6340 ga_clear(gap);
6341}
6342
6343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006344ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 int i = 0;
6347 ucmd_T *cmd = NULL;
6348 int cmp = -1;
6349 garray_T *gap;
6350
6351 gap = &curbuf->b_ucmds;
6352 for (;;)
6353 {
6354 for (i = 0; i < gap->ga_len; ++i)
6355 {
6356 cmd = USER_CMD_GA(gap, i);
6357 cmp = STRCMP(eap->arg, cmd->uc_name);
6358 if (cmp <= 0)
6359 break;
6360 }
6361 if (gap == &ucmds || cmp == 0)
6362 break;
6363 gap = &ucmds;
6364 }
6365
6366 if (cmp != 0)
6367 {
6368 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6369 return;
6370 }
6371
6372 vim_free(cmd->uc_name);
6373 vim_free(cmd->uc_rep);
6374# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6375 vim_free(cmd->uc_compl_arg);
6376# endif
6377
6378 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380 if (i < gap->ga_len)
6381 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6382}
6383
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006384/*
6385 * split and quote args for <f-args>
6386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006388uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
6390 char_u *buf;
6391 char_u *p;
6392 char_u *q;
6393 int len;
6394
6395 /* Precalculate length */
6396 p = arg;
6397 len = 2; /* Initial and final quotes */
6398
6399 while (*p)
6400 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006401 if (p[0] == '\\' && p[1] == '\\')
6402 {
6403 len += 2;
6404 p += 2;
6405 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006406 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 len += 1;
6409 p += 2;
6410 }
6411 else if (*p == '\\' || *p == '"')
6412 {
6413 len += 2;
6414 p += 1;
6415 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006416 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 {
6418 p = skipwhite(p);
6419 if (*p == NUL)
6420 break;
6421 len += 3; /* "," */
6422 }
6423 else
6424 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006425#ifdef FEAT_MBYTE
6426 int charlen = (*mb_ptr2len)(p);
6427 len += charlen;
6428 p += charlen;
6429#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 ++len;
6431 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 }
6434 }
6435
6436 buf = alloc(len + 1);
6437 if (buf == NULL)
6438 {
6439 *lenp = 0;
6440 return buf;
6441 }
6442
6443 p = arg;
6444 q = buf;
6445 *q++ = '"';
6446 while (*p)
6447 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006448 if (p[0] == '\\' && p[1] == '\\')
6449 {
6450 *q++ = '\\';
6451 *q++ = '\\';
6452 p += 2;
6453 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006454 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {
6456 *q++ = p[1];
6457 p += 2;
6458 }
6459 else if (*p == '\\' || *p == '"')
6460 {
6461 *q++ = '\\';
6462 *q++ = *p++;
6463 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006464 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 {
6466 p = skipwhite(p);
6467 if (*p == NUL)
6468 break;
6469 *q++ = '"';
6470 *q++ = ',';
6471 *q++ = '"';
6472 }
6473 else
6474 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006475 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 }
6477 }
6478 *q++ = '"';
6479 *q = 0;
6480
6481 *lenp = len;
6482 return buf;
6483}
6484
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006485 static size_t
6486add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6487{
6488 size_t result;
6489
6490 result = STRLEN(mod_str);
6491 if (*multi_mods)
6492 result += 1;
6493 if (buf != NULL)
6494 {
6495 if (*multi_mods)
6496 STRCAT(buf, " ");
6497 STRCAT(buf, mod_str);
6498 }
6499
6500 *multi_mods = 1;
6501
6502 return result;
6503}
6504
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505/*
6506 * Check for a <> code in a user command.
6507 * "code" points to the '<'. "len" the length of the <> (inclusive).
6508 * "buf" is where the result is to be added.
6509 * "split_buf" points to a buffer used for splitting, caller should free it.
6510 * "split_len" is the length of what "split_buf" contains.
6511 * Returns the length of the replacement, which has been added to "buf".
6512 * Returns -1 if there was no match, and only the "<" has been copied.
6513 */
6514 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006515uc_check_code(
6516 char_u *code,
6517 size_t len,
6518 char_u *buf,
6519 ucmd_T *cmd, /* the user command we're expanding */
6520 exarg_T *eap, /* ex arguments */
6521 char_u **split_buf,
6522 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 size_t result = 0;
6525 char_u *p = code + 1;
6526 size_t l = len - 2;
6527 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006528 enum {
6529 ct_ARGS,
6530 ct_BANG,
6531 ct_COUNT,
6532 ct_LINE1,
6533 ct_LINE2,
6534 ct_RANGE,
6535 ct_MODS,
6536 ct_REGISTER,
6537 ct_LT,
6538 ct_NONE
6539 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
6541 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6542 {
6543 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6544 p += 2;
6545 l -= 2;
6546 }
6547
Bram Moolenaar371d5402006-03-20 21:47:49 +00006548 ++l;
6549 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006551 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006553 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006555 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006557 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006559 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006561 else if (STRNICMP(p, "range>", l) == 0)
6562 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006563 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006565 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006567 else if (STRNICMP(p, "mods>", l) == 0)
6568 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 switch (type)
6571 {
6572 case ct_ARGS:
6573 /* Simple case first */
6574 if (*eap->arg == NUL)
6575 {
6576 if (quote == 1)
6577 {
6578 result = 2;
6579 if (buf != NULL)
6580 STRCPY(buf, "''");
6581 }
6582 else
6583 result = 0;
6584 break;
6585 }
6586
6587 /* When specified there is a single argument don't split it.
6588 * Works for ":Cmd %" when % is "a b c". */
6589 if ((eap->argt & NOSPC) && quote == 2)
6590 quote = 1;
6591
6592 switch (quote)
6593 {
6594 case 0: /* No quoting, no splitting */
6595 result = STRLEN(eap->arg);
6596 if (buf != NULL)
6597 STRCPY(buf, eap->arg);
6598 break;
6599 case 1: /* Quote, but don't split */
6600 result = STRLEN(eap->arg) + 2;
6601 for (p = eap->arg; *p; ++p)
6602 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006603#ifdef FEAT_MBYTE
6604 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6605 /* DBCS can contain \ in a trail byte, skip the
6606 * double-byte character. */
6607 ++p;
6608 else
6609#endif
6610 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 ++result;
6612 }
6613
6614 if (buf != NULL)
6615 {
6616 *buf++ = '"';
6617 for (p = eap->arg; *p; ++p)
6618 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006619#ifdef FEAT_MBYTE
6620 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6621 /* DBCS can contain \ in a trail byte, copy the
6622 * double-byte character to avoid escaping. */
6623 *buf++ = *p++;
6624 else
6625#endif
6626 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 *buf++ = '\\';
6628 *buf++ = *p;
6629 }
6630 *buf = '"';
6631 }
6632
6633 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006634 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 /* This is hard, so only do it once, and cache the result */
6636 if (*split_buf == NULL)
6637 *split_buf = uc_split_args(eap->arg, split_len);
6638
6639 result = *split_len;
6640 if (buf != NULL && result != 0)
6641 STRCPY(buf, *split_buf);
6642
6643 break;
6644 }
6645 break;
6646
6647 case ct_BANG:
6648 result = eap->forceit ? 1 : 0;
6649 if (quote)
6650 result += 2;
6651 if (buf != NULL)
6652 {
6653 if (quote)
6654 *buf++ = '"';
6655 if (eap->forceit)
6656 *buf++ = '!';
6657 if (quote)
6658 *buf = '"';
6659 }
6660 break;
6661
6662 case ct_LINE1:
6663 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006664 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 case ct_COUNT:
6666 {
6667 char num_buf[20];
6668 long num = (type == ct_LINE1) ? eap->line1 :
6669 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006670 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6672 size_t num_len;
6673
6674 sprintf(num_buf, "%ld", num);
6675 num_len = STRLEN(num_buf);
6676 result = num_len;
6677
6678 if (quote)
6679 result += 2;
6680
6681 if (buf != NULL)
6682 {
6683 if (quote)
6684 *buf++ = '"';
6685 STRCPY(buf, num_buf);
6686 buf += num_len;
6687 if (quote)
6688 *buf = '"';
6689 }
6690
6691 break;
6692 }
6693
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006694 case ct_MODS:
6695 {
6696 int multi_mods = 0;
6697 typedef struct {
6698 int *varp;
6699 char *name;
6700 } mod_entry_T;
6701 static mod_entry_T mod_entries[] = {
6702#ifdef FEAT_BROWSE_CMD
6703 {&cmdmod.browse, "browse"},
6704#endif
6705#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6706 {&cmdmod.confirm, "confirm"},
6707#endif
6708 {&cmdmod.hide, "hide"},
6709 {&cmdmod.keepalt, "keepalt"},
6710 {&cmdmod.keepjumps, "keepjumps"},
6711 {&cmdmod.keepmarks, "keepmarks"},
6712 {&cmdmod.keeppatterns, "keeppatterns"},
6713 {&cmdmod.lockmarks, "lockmarks"},
6714 {&cmdmod.noswapfile, "noswapfile"},
6715 {NULL, NULL}
6716 };
6717 int i;
6718
6719 result = quote ? 2 : 0;
6720 if (buf != NULL)
6721 {
6722 if (quote)
6723 *buf++ = '"';
6724 *buf = '\0';
6725 }
6726
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006727 /* :aboveleft and :leftabove */
6728 if (cmdmod.split & WSP_ABOVE)
6729 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6730 /* :belowright and :rightbelow */
6731 if (cmdmod.split & WSP_BELOW)
6732 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6733 /* :botright */
6734 if (cmdmod.split & WSP_BOT)
6735 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006736
6737 /* the modifiers that are simple flags */
6738 for (i = 0; mod_entries[i].varp != NULL; ++i)
6739 if (*mod_entries[i].varp)
6740 result += add_cmd_modifier(buf, mod_entries[i].name,
6741 &multi_mods);
6742
6743 /* TODO: How to support :noautocmd? */
6744#ifdef HAVE_SANDBOX
6745 /* TODO: How to support :sandbox?*/
6746#endif
6747 /* :silent */
6748 if (msg_silent > 0)
6749 result += add_cmd_modifier(buf,
6750 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006751 /* :tab */
6752 if (cmdmod.tab > 0)
6753 result += add_cmd_modifier(buf, "tab", &multi_mods);
6754 /* :topleft */
6755 if (cmdmod.split & WSP_TOP)
6756 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006757 /* TODO: How to support :unsilent?*/
6758 /* :verbose */
6759 if (p_verbose > 0)
6760 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006761 /* :vertical */
6762 if (cmdmod.split & WSP_VERT)
6763 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006764 if (quote && buf != NULL)
6765 {
6766 buf += result - 2;
6767 *buf = '"';
6768 }
6769 break;
6770 }
6771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 case ct_REGISTER:
6773 result = eap->regname ? 1 : 0;
6774 if (quote)
6775 result += 2;
6776 if (buf != NULL)
6777 {
6778 if (quote)
6779 *buf++ = '\'';
6780 if (eap->regname)
6781 *buf++ = eap->regname;
6782 if (quote)
6783 *buf = '\'';
6784 }
6785 break;
6786
6787 case ct_LT:
6788 result = 1;
6789 if (buf != NULL)
6790 *buf = '<';
6791 break;
6792
6793 default:
6794 /* Not recognized: just copy the '<' and return -1. */
6795 result = (size_t)-1;
6796 if (buf != NULL)
6797 *buf = '<';
6798 break;
6799 }
6800
6801 return result;
6802}
6803
6804 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006805do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 char_u *buf;
6808 char_u *p;
6809 char_u *q;
6810
6811 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006812 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006813 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 size_t len, totlen;
6815
6816 size_t split_len = 0;
6817 char_u *split_buf = NULL;
6818 ucmd_T *cmd;
6819#ifdef FEAT_EVAL
6820 scid_T save_current_SID = current_SID;
6821#endif
6822
6823 if (eap->cmdidx == CMD_USER)
6824 cmd = USER_CMD(eap->useridx);
6825 else
6826 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6827
6828 /*
6829 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006830 * First round: "buf" is NULL, compute length, allocate "buf".
6831 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 */
6833 buf = NULL;
6834 for (;;)
6835 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006836 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006837 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006839
6840 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006842 start = vim_strchr(p, '<');
6843 if (start != NULL)
6844 end = vim_strchr(start + 1, '>');
6845 if (buf != NULL)
6846 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006847 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6848 ;
6849 if (*ksp == K_SPECIAL
6850 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006851 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6852# ifdef FEAT_GUI
6853 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6854# endif
6855 ))
6856 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006857 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006858 * KS_SPECIAL KE_FILLER, like for mappings, but
6859 * do_cmdline() doesn't handle that, so convert it back.
6860 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6861 len = ksp - p;
6862 if (len > 0)
6863 {
6864 mch_memmove(q, p, len);
6865 q += len;
6866 }
6867 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6868 p = ksp + 3;
6869 continue;
6870 }
6871 }
6872
6873 /* break if there no <item> is found */
6874 if (start == NULL || end == NULL)
6875 break;
6876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 /* Include the '>' */
6878 ++end;
6879
6880 /* Take everything up to the '<' */
6881 len = start - p;
6882 if (buf == NULL)
6883 totlen += len;
6884 else
6885 {
6886 mch_memmove(q, p, len);
6887 q += len;
6888 }
6889
6890 len = uc_check_code(start, end - start, q, cmd, eap,
6891 &split_buf, &split_len);
6892 if (len == (size_t)-1)
6893 {
6894 /* no match, continue after '<' */
6895 p = start + 1;
6896 len = 1;
6897 }
6898 else
6899 p = end;
6900 if (buf == NULL)
6901 totlen += len;
6902 else
6903 q += len;
6904 }
6905 if (buf != NULL) /* second time here, finished */
6906 {
6907 STRCPY(q, p);
6908 break;
6909 }
6910
6911 totlen += STRLEN(p); /* Add on the trailing characters */
6912 buf = alloc((unsigned)(totlen + 1));
6913 if (buf == NULL)
6914 {
6915 vim_free(split_buf);
6916 return;
6917 }
6918 }
6919
6920#ifdef FEAT_EVAL
6921 current_SID = cmd->uc_scriptID;
6922#endif
6923 (void)do_cmdline(buf, eap->getline, eap->cookie,
6924 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6925#ifdef FEAT_EVAL
6926 current_SID = save_current_SID;
6927#endif
6928 vim_free(buf);
6929 vim_free(split_buf);
6930}
6931
6932# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6933 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006934get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6937}
6938
6939/*
6940 * Function given to ExpandGeneric() to obtain the list of user command names.
6941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006943get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 if (idx < curbuf->b_ucmds.ga_len)
6946 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6947 idx -= curbuf->b_ucmds.ga_len;
6948 if (idx < ucmds.ga_len)
6949 return USER_CMD(idx)->uc_name;
6950 return NULL;
6951}
6952
6953/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006954 * Function given to ExpandGeneric() to obtain the list of user address type names.
6955 */
6956 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006957get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006958{
6959 return (char_u *)addr_type_complete[idx].name;
6960}
6961
6962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 * Function given to ExpandGeneric() to obtain the list of user command
6964 * attributes.
6965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006967get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006970 {"addr", "bang", "bar", "buffer", "complete",
6971 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006973 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 return NULL;
6975 return (char_u *)user_cmd_flags[idx];
6976}
6977
6978/*
6979 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6985
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006986 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 return NULL;
6988 return (char_u *)user_cmd_nargs[idx];
6989}
6990
6991/*
6992 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006995get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996{
6997 return (char_u *)command_complete[idx].name;
6998}
6999# endif /* FEAT_CMDL_COMPL */
7000
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007001/*
7002 * Parse address type argument
7003 */
7004 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005parse_addr_type_arg(
7006 char_u *value,
7007 int vallen,
7008 long *argt,
7009 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007010{
7011 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007012
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007013 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7014 {
7015 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7016 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7017 if (a && b)
7018 {
7019 *addr_type_arg = addr_type_complete[i].expand;
7020 break;
7021 }
7022 }
7023
7024 if (addr_type_complete[i].expand == -1)
7025 {
7026 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007027
Bram Moolenaar1c465442017-03-12 20:10:05 +01007028 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007029 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007030 err[i] = NUL;
7031 EMSG2(_("E180: Invalid address type value: %s"), err);
7032 return FAIL;
7033 }
7034
7035 if (*addr_type_arg != ADDR_LINES)
7036 *argt |= NOTADR;
7037
7038 return OK;
7039}
7040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041#endif /* FEAT_USR_CMDS */
7042
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007043#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7044/*
7045 * Parse a completion argument "value[vallen]".
7046 * The detected completion goes in "*complp", argument type in "*argt".
7047 * When there is an argument, for function and user defined completion, it's
7048 * copied to allocated memory and stored in "*compl_arg".
7049 * Returns FAIL if something is wrong.
7050 */
7051 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007052parse_compl_arg(
7053 char_u *value,
7054 int vallen,
7055 int *complp,
7056 long *argt,
7057 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007058{
7059 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007060# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007061 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007062# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007063 int i;
7064 int valend = vallen;
7065
7066 /* Look for any argument part - which is the part after any ',' */
7067 for (i = 0; i < vallen; ++i)
7068 {
7069 if (value[i] == ',')
7070 {
7071 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007072# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007073 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007074# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007075 valend = i;
7076 break;
7077 }
7078 }
7079
7080 for (i = 0; command_complete[i].expand != 0; ++i)
7081 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007082 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007083 && STRNCMP(value, command_complete[i].name, valend) == 0)
7084 {
7085 *complp = command_complete[i].expand;
7086 if (command_complete[i].expand == EXPAND_BUFFERS)
7087 *argt |= BUFNAME;
7088 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7089 || command_complete[i].expand == EXPAND_FILES)
7090 *argt |= XFILE;
7091 break;
7092 }
7093 }
7094
7095 if (command_complete[i].expand == 0)
7096 {
7097 EMSG2(_("E180: Invalid complete value: %s"), value);
7098 return FAIL;
7099 }
7100
7101# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7102 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7103 && arg != NULL)
7104# else
7105 if (arg != NULL)
7106# endif
7107 {
7108 EMSG(_("E468: Completion argument only allowed for custom completion"));
7109 return FAIL;
7110 }
7111
7112# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7113 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7114 && arg == NULL)
7115 {
7116 EMSG(_("E467: Custom completion requires a function argument"));
7117 return FAIL;
7118 }
7119
7120 if (arg != NULL)
7121 *compl_arg = vim_strnsave(arg, (int)arglen);
7122# endif
7123 return OK;
7124}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007125
7126 int
7127cmdcomplete_str_to_type(char_u *complete_str)
7128{
7129 int i;
7130
7131 for (i = 0; command_complete[i].expand != 0; ++i)
7132 if (STRCMP(complete_str, command_complete[i].name) == 0)
7133 return command_complete[i].expand;
7134
7135 return EXPAND_NOTHING;
7136}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007137#endif
7138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007140ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
Bram Moolenaare6850792010-05-14 15:28:44 +02007142 if (*eap->arg == NUL)
7143 {
7144#ifdef FEAT_EVAL
7145 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7146 char_u *p = NULL;
7147
7148 if (expr != NULL)
7149 {
7150 ++emsg_off;
7151 p = eval_to_string(expr, NULL, FALSE);
7152 --emsg_off;
7153 vim_free(expr);
7154 }
7155 if (p != NULL)
7156 {
7157 MSG(p);
7158 vim_free(p);
7159 }
7160 else
7161 MSG("default");
7162#else
7163 MSG(_("unknown"));
7164#endif
7165 }
7166 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007167 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169
7170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 if (*eap->arg == NUL && eap->cmd[2] == '!')
7174 MSG(_("Greetings, Vim user!"));
7175 do_highlight(eap->arg, eap->forceit, FALSE);
7176}
7177
7178
7179/*
7180 * Call this function if we thought we were going to exit, but we won't
7181 * (because of an error). May need to restore the terminal mode.
7182 */
7183 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007184not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
7186 exiting = FALSE;
7187 settmode(TMODE_RAW);
7188}
7189
7190/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007191 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 */
7193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007194ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007196 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007197
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198#ifdef FEAT_CMDWIN
7199 if (cmdwin_type != 0)
7200 {
7201 cmdwin_result = Ctrl_C;
7202 return;
7203 }
7204#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007205 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007206 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007207 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007208 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007209 return;
7210 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007211 if (eap->addr_count > 0)
7212 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007213 int wnr = eap->line2;
7214
7215 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7216 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007217 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007218 }
7219 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007220 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007221
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007222 /* Refuse to quit when locked. */
7223 if (curbuf_locked())
7224 return;
7225 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7226 /* Bail out when autocommands closed the window.
7227 * Refuse to quit when the buffer in the last window is being closed (can
7228 * only happen in autocommands). */
7229 if (!win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007230 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007231 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233#ifdef FEAT_NETBEANS_INTG
7234 netbeansForcedQuit = eap->forceit;
7235#endif
7236
7237 /*
7238 * If there are more files or windows we won't exit.
7239 */
7240 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7241 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007242 if ((!buf_hide(wp->w_buffer)
7243 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007244 | (eap->forceit ? CCGD_FORCEIT : 0)
7245 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007247 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
7249 not_exiting();
7250 }
7251 else
7252 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007253 /* quit last window
7254 * Note: only_one_window() returns true, even so a help window is
7255 * still open. In that case only quit, if no address has been
7256 * specified. Example:
7257 * :h|wincmd w|1q - don't quit
7258 * :h|wincmd w|q - quit
7259 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007260 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007262 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007263#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007267 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
7269}
7270
7271/*
7272 * ":cquit".
7273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007275ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277 getout(1); /* this does not always pass on the exit code to the Manx
7278 compiler. why? */
7279}
7280
7281/*
7282 * ":qall": try to quit all windows
7283 */
7284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007285ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287# ifdef FEAT_CMDWIN
7288 if (cmdwin_type != 0)
7289 {
7290 if (eap->forceit)
7291 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7292 else
7293 cmdwin_result = K_XF2;
7294 return;
7295 }
7296# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007297
7298 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007299 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007300 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007301 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007302 return;
7303 }
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007304 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7305 /* Refuse to quit when locked or when the buffer in the last window is
7306 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007307 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007308 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007309
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007311 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 getout(0);
7313 not_exiting();
7314}
7315
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316/*
7317 * ":close": close current window, unless it is the last one
7318 */
7319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007320ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007322 win_T *win;
7323 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007324#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007326 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007328#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007329 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007330 {
7331 if (eap->addr_count == 0)
7332 ex_win_close(eap->forceit, curwin, NULL);
7333 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007334 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007335 {
7336 winnr++;
7337 if (winnr == eap->line2)
7338 break;
7339 }
7340 if (win == NULL)
7341 win = lastwin;
7342 ex_win_close(eap->forceit, win, NULL);
7343 }
7344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345}
7346
Bram Moolenaar4033c552017-09-16 20:54:51 +02007347#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007348/*
7349 * ":pclose": Close any preview window.
7350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007352ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007353{
7354 win_T *win;
7355
Bram Moolenaar29323592016-07-24 22:04:11 +02007356 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007357 if (win->w_p_pvw)
7358 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007359 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007360 break;
7361 }
7362}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007363#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007364
Bram Moolenaarf740b292006-02-16 22:11:02 +00007365/*
7366 * Close window "win" and take care of handling closing the last window for a
7367 * modified buffer.
7368 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007370ex_win_close(
7371 int forceit,
7372 win_T *win,
7373 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
7375 int need_hide;
7376 buf_T *buf = win->w_buffer;
7377
7378 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007379 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007381#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 if ((p_confirm || cmdmod.confirm) && p_write)
7383 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007384 bufref_T bufref;
7385
7386 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007388 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 return;
7390 need_hide = FALSE;
7391 }
7392 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007395 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 return;
7397 }
7398 }
7399
Bram Moolenaar4033c552017-09-16 20:54:51 +02007400#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007402#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007403
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007405 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007406 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007407 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007408 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409}
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007412 * Handle the argument for a tabpage related ex command.
7413 * Returns a tabpage number.
7414 * When an error is encountered then eap->errmsg is set.
7415 */
7416 static int
7417get_tabpage_arg(exarg_T *eap)
7418{
7419 int tab_number;
7420 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7421
7422 if (eap->arg && *eap->arg != NUL)
7423 {
7424 char_u *p = eap->arg;
7425 char_u *p_save;
7426 int relative = 0; /* argument +N/-N means: go to N places to the
7427 * right/left relative to the current position. */
7428
7429 if (*p == '-')
7430 {
7431 relative = -1;
7432 p++;
7433 }
7434 else if (*p == '+')
7435 {
7436 relative = 1;
7437 p++;
7438 }
7439
7440 p_save = p;
7441 tab_number = getdigits(&p);
7442
7443 if (relative == 0)
7444 {
7445 if (STRCMP(p, "$") == 0)
7446 tab_number = LAST_TAB_NR;
7447 else if (p == p_save || *p_save == '-' || *p != NUL
7448 || tab_number > LAST_TAB_NR)
7449 {
7450 /* No numbers as argument. */
7451 eap->errmsg = e_invarg;
7452 goto theend;
7453 }
7454 }
7455 else
7456 {
7457 if (*p_save == NUL)
7458 tab_number = 1;
7459 else if (p == p_save || *p_save == '-' || *p != NUL
7460 || tab_number == 0)
7461 {
7462 /* No numbers as argument. */
7463 eap->errmsg = e_invarg;
7464 goto theend;
7465 }
7466 tab_number = tab_number * relative + tabpage_index(curtab);
7467 if (!unaccept_arg0 && relative == -1)
7468 --tab_number;
7469 }
7470 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7471 eap->errmsg = e_invarg;
7472 }
7473 else if (eap->addr_count > 0)
7474 {
7475 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007476 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007477 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007478 tab_number = 0;
7479 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007480 else
7481 {
7482 tab_number = eap->line2;
7483 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7484 {
7485 --tab_number;
7486 if (tab_number < unaccept_arg0)
7487 eap->errmsg = e_invarg;
7488 }
7489 }
7490 }
7491 else
7492 {
7493 switch (eap->cmdidx)
7494 {
7495 case CMD_tabnext:
7496 tab_number = tabpage_index(curtab) + 1;
7497 if (tab_number > LAST_TAB_NR)
7498 tab_number = 1;
7499 break;
7500 case CMD_tabmove:
7501 tab_number = LAST_TAB_NR;
7502 break;
7503 default:
7504 tab_number = tabpage_index(curtab);
7505 }
7506 }
7507
7508theend:
7509 return tab_number;
7510}
7511
7512/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007513 * ":tabclose": close current tab page, unless it is the last one.
7514 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 */
7516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007517ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007519 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007520 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007521
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007522# ifdef FEAT_CMDWIN
7523 if (cmdwin_type != 0)
7524 cmdwin_result = K_IGNORE;
7525 else
7526# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007527 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007528 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007531 tab_number = get_tabpage_arg(eap);
7532 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007533 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007534 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007535 if (tp == NULL)
7536 {
7537 beep_flush();
7538 return;
7539 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007540 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007541 {
7542 tabpage_close_other(tp, eap->forceit);
7543 return;
7544 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007545 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007546 tabpage_close(eap->forceit);
7547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007549}
7550
7551/*
7552 * ":tabonly": close all tab pages except the current one
7553 */
7554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007555ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007556{
7557 tabpage_T *tp;
7558 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007559 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007560
7561# ifdef FEAT_CMDWIN
7562 if (cmdwin_type != 0)
7563 cmdwin_result = K_IGNORE;
7564 else
7565# endif
7566 if (first_tabpage->tp_next == NULL)
7567 MSG(_("Already only one tab page"));
7568 else
7569 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007570 tab_number = get_tabpage_arg(eap);
7571 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007572 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007573 goto_tabpage(tab_number);
7574 /* Repeat this up to a 1000 times, because autocommands may
7575 * mess up the lists. */
7576 for (done = 0; done < 1000; ++done)
7577 {
7578 FOR_ALL_TABPAGES(tp)
7579 if (tp->tp_topframe != topframe)
7580 {
7581 tabpage_close_other(tp, eap->forceit);
7582 /* if we failed to close it quit */
7583 if (valid_tabpage(tp))
7584 done = 1000;
7585 /* start over, "tp" is now invalid */
7586 break;
7587 }
7588 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007589 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007590 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007591 }
7592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594
7595/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007596 * Close the current tab page.
7597 */
7598 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007599tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007600{
7601 /* First close all the windows but the current one. If that worked then
7602 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007603 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007604 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007605 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007606 ex_win_close(forceit, curwin, NULL);
7607# ifdef FEAT_GUI
7608 need_mouse_correct = TRUE;
7609# endif
7610}
7611
7612/*
7613 * Close tab page "tp", which is not the current tab page.
7614 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007615 * Also takes care of the tab pages line disappearing when closing the
7616 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007617 */
7618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007619tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007620{
7621 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007622 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007623 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007624
7625 /* Limit to 1000 windows, autocommands may add a window while we close
7626 * one. OK, so I'm paranoid... */
7627 while (++done < 1000)
7628 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007629 wp = tp->tp_firstwin;
7630 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007631
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007632 /* Autocommands may delete the tab page under our fingers and we may
7633 * fail to close a window with a modified buffer. */
7634 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007635 break;
7636 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007637
Bram Moolenaar29323592016-07-24 22:04:11 +02007638 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007639
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007640 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007641 if (h != tabline_height())
7642 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007643}
7644
7645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 * ":only".
7647 */
7648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007649ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007651 win_T *wp;
7652 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653# ifdef FEAT_GUI
7654 need_mouse_correct = TRUE;
7655# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007656 if (eap->addr_count > 0)
7657 {
7658 wnr = eap->line2;
7659 for (wp = firstwin; --wnr > 0; )
7660 {
7661 if (wp->w_next == NULL)
7662 break;
7663 else
7664 wp = wp->w_next;
7665 }
7666 win_goto(wp);
7667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 close_others(TRUE, eap->forceit);
7669}
7670
7671/*
7672 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007673 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007676ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 if (eap->addr_count == 0)
7679 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007680 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
7683 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007684ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007686 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007687 if (!eap->skip)
7688 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007689#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007690 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007691#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007692 if (eap->addr_count == 0)
7693 win_close(curwin, FALSE); /* don't free buffer */
7694 else
7695 {
7696 int winnr = 0;
7697 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007698
Bram Moolenaar2256c992016-11-15 21:17:07 +01007699 FOR_ALL_WINDOWS(win)
7700 {
7701 winnr++;
7702 if (winnr == eap->line2)
7703 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007704 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007705 if (win == NULL)
7706 win = lastwin;
7707 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710}
7711
7712/*
7713 * ":stop" and ":suspend": Suspend Vim.
7714 */
7715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007716ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
7718 /*
7719 * Disallow suspending for "rvim".
7720 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007721 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
7723 if (!eap->forceit)
7724 autowrite_all();
7725 windgoto((int)Rows - 1, 0);
7726 out_char('\n');
7727 out_flush();
7728 stoptermcap();
7729 out_flush(); /* needed for SUN to restore xterm buffer */
7730#ifdef FEAT_TITLE
7731 mch_restore_title(3); /* restore window titles */
7732#endif
7733 ui_suspend(); /* call machine specific function */
7734#ifdef FEAT_TITLE
7735 maketitle();
7736 resettitle(); /* force updating the title */
7737#endif
7738 starttermcap();
7739 scroll_start(); /* scroll screen before redrawing */
7740 redraw_later_clear();
7741 shell_resized(); /* may have resized window */
7742 }
7743}
7744
7745/*
7746 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7747 */
7748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007749ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
7751#ifdef FEAT_CMDWIN
7752 if (cmdwin_type != 0)
7753 {
7754 cmdwin_result = Ctrl_C;
7755 return;
7756 }
7757#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007758 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007759 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007760 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007761 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007762 return;
7763 }
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007764 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7765 /* Refuse to quit when locked or when the buffer in the last window is
7766 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007767 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007768 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
7770 /*
7771 * if more files or windows we won't exit
7772 */
7773 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7774 exiting = TRUE;
7775 if ( ((eap->cmdidx == CMD_wq
7776 || curbufIsChanged())
7777 && do_write(eap) == FAIL)
7778 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007779 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {
7781 not_exiting();
7782 }
7783 else
7784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007787 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788# ifdef FEAT_GUI
7789 need_mouse_correct = TRUE;
7790# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007791 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007792 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 }
7794}
7795
7796/*
7797 * ":print", ":list", ":number".
7798 */
7799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007800ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007802 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7803 EMSG(_(e_emptybuf));
7804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007806 for ( ;!got_int; ui_breakcheck())
7807 {
7808 print_line(eap->line1,
7809 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7810 || (eap->flags & EXFLAG_NR)),
7811 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7812 if (++eap->line1 > eap->line2)
7813 break;
7814 out_flush(); /* show one line at a time */
7815 }
7816 setpcmark();
7817 /* put cursor at last line */
7818 curwin->w_cursor.lnum = eap->line2;
7819 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
7821
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823}
7824
7825#ifdef FEAT_BYTEOFF
7826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007827ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 goto_byte(eap->line2);
7830}
7831#endif
7832
7833/*
7834 * ":shell".
7835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007837ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 do_shell(NULL, 0);
7840}
7841
Bram Moolenaar4033c552017-09-16 20:54:51 +02007842#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007844 || defined(FEAT_GUI_MSWIN) \
7845 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 || defined(PROTO)
7847
7848/*
7849 * Handle a file drop. The code is here because a drop is *nearly* like an
7850 * :args command, but not quite (we have a list of exact filenames, so we
7851 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7852 * code is very similar to :args and hence needs access to a lot of the static
7853 * functions in this file.
7854 *
7855 * The list should be allocated using alloc(), as should each item in the
7856 * list. This function takes over responsibility for freeing the list.
7857 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007858 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007859 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 */
7861 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007862handle_drop(
7863 int filec, /* the number of files dropped */
7864 char_u **filev, /* the list of files dropped */
7865 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
7867 exarg_T ea;
7868 int save_msg_scroll = msg_scroll;
7869
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007870 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007871 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007873 if (curbuf_locked())
7874 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007875
Bram Moolenaarc236c162008-07-13 17:41:49 +00007876 /* When the screen is being updated we should not change buffers and
7877 * windows structures, it may cause freed memory to be used. */
7878 if (updating_screen)
7879 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880
7881 /* Check whether the current buffer is changed. If so, we will need
7882 * to split the current window or data could be lost.
7883 * We don't need to check if the 'hidden' option is set, as in this
7884 * case the buffer won't be lost.
7885 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007886 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
7888 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007889 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 --emsg_off;
7891 }
7892 if (split)
7893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (win_split(0, 0) == FAIL)
7895 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007896 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
7898 /* When splitting the window, create a new alist. Otherwise the
7899 * existing one is overwritten. */
7900 alist_unlink(curwin->w_alist);
7901 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903
7904 /*
7905 * Set up the new argument list.
7906 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007907 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
7909 /*
7910 * Move to the first file.
7911 */
7912 /* Fake up a minimal "next" command for do_argfile() */
7913 vim_memset(&ea, 0, sizeof(ea));
7914 ea.cmd = (char_u *)"next";
7915 do_argfile(&ea, 0);
7916
7917 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7918 * mode that is not needed here. */
7919 need_start_insertmode = FALSE;
7920
7921 /* Restore msg_scroll, otherwise a following command may cause scrolling
7922 * unexpectedly. The screen will be redrawn by the caller, thus
7923 * msg_scroll being set by displaying a message is irrelevant. */
7924 msg_scroll = save_msg_scroll;
7925}
7926#endif
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928/*
7929 * Clear an argument list: free all file names and reset it to zero entries.
7930 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007931 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007932alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934 while (--al->al_ga.ga_len >= 0)
7935 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7936 ga_clear(&al->al_ga);
7937}
7938
7939/*
7940 * Init an argument list.
7941 */
7942 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007943alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7946}
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948/*
7949 * Remove a reference from an argument list.
7950 * Ignored when the argument list is the global one.
7951 * If the argument list is no longer used by any window, free it.
7952 */
7953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007954alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 if (al != &global_alist && --al->al_refcount <= 0)
7957 {
7958 alist_clear(al);
7959 vim_free(al);
7960 }
7961}
7962
Bram Moolenaar4033c552017-09-16 20:54:51 +02007963#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964/*
7965 * Create a new argument list and use it for the current window.
7966 */
7967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7971 if (curwin->w_alist == NULL)
7972 {
7973 curwin->w_alist = &global_alist;
7974 ++global_alist.al_refcount;
7975 }
7976 else
7977 {
7978 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007979 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 alist_init(curwin->w_alist);
7981 }
7982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983#endif
7984
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007985#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986/*
7987 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007988 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7989 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 */
7991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007992alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
7994 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007995 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 char_u **new_arg_files;
7997 int new_arg_file_count;
7998 char_u *save_p_su = p_su;
7999 int i;
8000
8001 /* Don't use 'suffixes' here. This should work like the shell did the
8002 * expansion. Also, the vimrc file isn't read yet, thus the user
8003 * can't set the options. */
8004 p_su = empty_option;
8005 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8006 if (old_arg_files != NULL)
8007 {
8008 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008009 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8010 old_arg_count = GARGCOUNT;
8011 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008013 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 && new_arg_file_count > 0)
8015 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008016 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8017 TRUE, fnum_list, fnum_len);
8018 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 }
8021 p_su = save_p_su;
8022}
8023#endif
8024
8025/*
8026 * Set the argument list for the current window.
8027 * Takes over the allocated files[] and the allocated fnames in it.
8028 */
8029 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008030alist_set(
8031 alist_T *al,
8032 int count,
8033 char_u **files,
8034 int use_curbuf,
8035 int *fnum_list,
8036 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037{
8038 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008039 static int recursive = 0;
8040
8041 if (recursive)
8042 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008043 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008044 return;
8045 }
8046 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047
8048 alist_clear(al);
8049 if (ga_grow(&al->al_ga, count) == OK)
8050 {
8051 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008052 {
8053 if (got_int)
8054 {
8055 /* When adding many buffers this can take a long time. Allow
8056 * interrupting here. */
8057 while (i < count)
8058 vim_free(files[i++]);
8059 break;
8060 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008061
8062 /* May set buffer name of a buffer previously used for the
8063 * argument list, so that it's re-used by alist_add. */
8064 if (fnum_list != NULL && i < fnum_len)
8065 buf_set_name(fnum_list[i], files[i]);
8066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008068 ui_breakcheck();
8069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 vim_free(files);
8071 }
8072 else
8073 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008076
8077 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078}
8079
8080/*
8081 * Add file "fname" to argument list "al".
8082 * "fname" must have been allocated and "al" must have been checked for room.
8083 */
8084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008085alist_add(
8086 alist_T *al,
8087 char_u *fname,
8088 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 if (fname == NULL) /* don't add NULL file names */
8091 return;
8092#ifdef BACKSLASH_IN_FILENAME
8093 slash_adjust(fname);
8094#endif
8095 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8096 if (set_fnum > 0)
8097 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8098 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8099 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
8101
8102#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8103/*
8104 * Adjust slashes in file names. Called after 'shellslash' was set.
8105 */
8106 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008107alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008111 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
8113 for (i = 0; i < GARGCOUNT; ++i)
8114 if (GARGLIST[i].ae_fname != NULL)
8115 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008116 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 if (wp->w_alist != &global_alist)
8118 for (i = 0; i < WARGCOUNT(wp); ++i)
8119 if (WARGLIST(wp)[i].ae_fname != NULL)
8120 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121}
8122#endif
8123
8124/*
8125 * ":preserve".
8126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008128ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008130 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 ml_preserve(curbuf, TRUE);
8132}
8133
8134/*
8135 * ":recover".
8136 */
8137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008138ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8141 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008142 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8143 | CCGD_MULTWIN
8144 | (eap->forceit ? CCGD_FORCEIT : 0)
8145 | CCGD_EXCMD)
8146
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 && (*eap->arg == NUL
8148 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8149 ml_recover();
8150 recoverymode = FALSE;
8151}
8152
8153/*
8154 * Command modifier used in a wrong way.
8155 */
8156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008157ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159 eap->errmsg = e_invcmd;
8160}
8161
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162/*
8163 * :sview [+command] file split window with new file, read-only
8164 * :split [[+command] file] split window with current or new file
8165 * :vsplit [[+command] file] split window vertically with current or new file
8166 * :new [[+command] file] split window with no or new file
8167 * :vnew [[+command] file] split vertically window with no or new file
8168 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008169 *
8170 * :tabedit open new Tab page with empty window
8171 * :tabedit [+command] file open new Tab page and edit "file"
8172 * :tabnew [[+command] file] just like :tabedit
8173 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 */
8175 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008176ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008178 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008179#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008181#endif
8182#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185
Bram Moolenaar4033c552017-09-16 20:54:51 +02008186#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189
Bram Moolenaar4033c552017-09-16 20:54:51 +02008190#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008192 * quickfix windows. But it's OK when doing ":tab split". */
8193 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
8195 if (eap->cmdidx == CMD_split)
8196 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 if (eap->cmdidx == CMD_vsplit)
8198 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
Bram Moolenaar4033c552017-09-16 20:54:51 +02008202#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008203 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
8205 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8206 FNAME_MESS, TRUE, curbuf->b_ffname);
8207 if (fname == NULL)
8208 goto theend;
8209 eap->arg = fname;
8210 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008211# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008212 else
8213# endif
8214#endif
8215#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 && eap->cmdidx != CMD_new)
8219 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008220 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008221# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008222 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008223# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008224 au_has_group((char_u *)"FileExplorer"))
8225 {
8226 /* No browsing supported but we do have the file explorer:
8227 * Edit the directory. */
8228 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8229 eap->arg = (char_u *)".";
8230 }
8231 else
8232 {
8233 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008235 if (fname == NULL)
8236 goto theend;
8237 eap->arg = fname;
8238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 }
8240 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008243 /*
8244 * Either open new tab page or split the window.
8245 */
8246 if (eap->cmdidx == CMD_tabedit
8247 || eap->cmdidx == CMD_tabfind
8248 || eap->cmdidx == CMD_tabnew)
8249 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008250 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8251 : eap->addr_count == 0 ? 0
8252 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008253 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008254 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008255
8256 /* set the alternate buffer for the window we came from */
8257 if (curwin != old_curwin
8258 && win_valid(old_curwin)
8259 && old_curwin->w_buffer != curbuf
8260 && !cmdmod.keepalt)
8261 old_curwin->w_alt_fnum = curbuf->b_fnum;
8262 }
8263 }
8264 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 /* Reset 'scrollbind' when editing another file, but keep it when
8268 * doing ":split" without arguments. */
8269 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008270# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008274 {
8275 RESET_BINDING(curwin);
8276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 else
8278 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 do_exedit(eap, old_curwin);
8280 }
8281
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008282# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008286# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287theend:
8288 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008291
8292/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008293 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008294 */
8295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008296tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008297{
8298 exarg_T ea;
8299
8300 vim_memset(&ea, 0, sizeof(ea));
8301 ea.cmdidx = CMD_tabnew;
8302 ea.cmd = (char_u *)"tabn";
8303 ea.arg = (char_u *)"";
8304 ex_splitview(&ea);
8305}
8306
8307/*
8308 * :tabnext command
8309 */
8310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008311ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008312{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008313 int tab_number;
8314
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008315 switch (eap->cmdidx)
8316 {
8317 case CMD_tabfirst:
8318 case CMD_tabrewind:
8319 goto_tabpage(1);
8320 break;
8321 case CMD_tablast:
8322 goto_tabpage(9999);
8323 break;
8324 case CMD_tabprevious:
8325 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008326 if (eap->arg && *eap->arg != NUL)
8327 {
8328 char_u *p = eap->arg;
8329 char_u *p_save = p;
8330
8331 tab_number = getdigits(&p);
8332 if (p == p_save || *p_save == '-' || *p != NUL
8333 || tab_number == 0)
8334 {
8335 /* No numbers as argument. */
8336 eap->errmsg = e_invarg;
8337 return;
8338 }
8339 }
8340 else
8341 {
8342 if (eap->addr_count == 0)
8343 tab_number = 1;
8344 else
8345 {
8346 tab_number = eap->line2;
8347 if (tab_number < 1)
8348 {
8349 eap->errmsg = e_invrange;
8350 return;
8351 }
8352 }
8353 }
8354 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008355 break;
8356 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008357 tab_number = get_tabpage_arg(eap);
8358 if (eap->errmsg == NULL)
8359 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008360 break;
8361 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008362}
8363
8364/*
8365 * :tabmove command
8366 */
8367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008368ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008369{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008370 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008371
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008372 tab_number = get_tabpage_arg(eap);
8373 if (eap->errmsg == NULL)
8374 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008375}
8376
8377/*
8378 * :tabs command: List tabs and their contents.
8379 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008381ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008382{
8383 tabpage_T *tp;
8384 win_T *wp;
8385 int tabcount = 1;
8386
8387 msg_start();
8388 msg_scroll = TRUE;
8389 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8390 {
8391 msg_putchar('\n');
8392 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008393 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008394 out_flush(); /* output one line at a time */
8395 ui_breakcheck();
8396
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008397 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008398 wp = firstwin;
8399 else
8400 wp = tp->tp_firstwin;
8401 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8402 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008403 msg_putchar('\n');
8404 msg_putchar(wp == curwin ? '>' : ' ');
8405 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008406 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8407 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008409 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008410 else
8411 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8412 IObuff, IOSIZE, TRUE);
8413 msg_outtrans(IObuff);
8414 out_flush(); /* output one line at a time */
8415 ui_breakcheck();
8416 }
8417 }
8418}
8419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420/*
8421 * ":mode": Set screen mode.
8422 * If no argument given, just get the screen size and redraw.
8423 */
8424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008425ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427 if (*eap->arg == NUL)
8428 shell_resized();
8429 else
8430 mch_screenmode(eap->arg);
8431}
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433/*
8434 * ":resize".
8435 * set, increment or decrement current window height
8436 */
8437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008438ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439{
8440 int n;
8441 win_T *wp = curwin;
8442
8443 if (eap->addr_count > 0)
8444 {
8445 n = eap->line2;
8446 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8447 ;
8448 }
8449
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008450# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 if (cmdmod.split & WSP_VERT)
8455 {
8456 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008457 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8459 n = 9999;
8460 win_setwidth_win((int)n, wp);
8461 }
8462 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {
8464 if (*eap->arg == '-' || *eap->arg == '+')
8465 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008466 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 n = 9999;
8468 win_setheight_win((int)n, wp);
8469 }
8470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471
8472/*
8473 * ":find [+command] <file>" command.
8474 */
8475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008476ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478#ifdef FEAT_SEARCHPATH
8479 char_u *fname;
8480 int count;
8481
8482 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8483 TRUE, curbuf->b_ffname);
8484 if (eap->addr_count > 0)
8485 {
8486 /* Repeat finding the file "count" times. This matters when it
8487 * appears several times in the path. */
8488 count = eap->line2;
8489 while (fname != NULL && --count > 0)
8490 {
8491 vim_free(fname);
8492 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8493 FALSE, curbuf->b_ffname);
8494 }
8495 }
8496
8497 if (fname != NULL)
8498 {
8499 eap->arg = fname;
8500#endif
8501 do_exedit(eap, NULL);
8502#ifdef FEAT_SEARCHPATH
8503 vim_free(fname);
8504 }
8505#endif
8506}
8507
8508/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008509 * ":open" simulation: for now just work like ":visual".
8510 */
8511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008512ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008513{
8514 regmatch_T regmatch;
8515 char_u *p;
8516
8517 curwin->w_cursor.lnum = eap->line2;
8518 beginline(BL_SOL | BL_FIX);
8519 if (*eap->arg == '/')
8520 {
8521 /* ":open /pattern/": put cursor in column found with pattern */
8522 ++eap->arg;
8523 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8524 *p = NUL;
8525 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8526 if (regmatch.regprog != NULL)
8527 {
8528 regmatch.rm_ic = p_ic;
8529 p = ml_get_curline();
8530 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008531 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008532 else
8533 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008534 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008535 }
8536 /* Move to the NUL, ignore any other arguments. */
8537 eap->arg += STRLEN(eap->arg);
8538 }
8539 check_cursor();
8540
8541 eap->cmdidx = CMD_visual;
8542 do_exedit(eap, NULL);
8543}
8544
8545/*
8546 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 */
8548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008549ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 do_exedit(eap, NULL);
8552}
8553
8554/*
8555 * ":edit <file>" command and alikes.
8556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008558do_exedit(
8559 exarg_T *eap,
8560 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008564 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
8566 /*
8567 * ":vi" command ends Ex mode.
8568 */
8569 if (exmode_active && (eap->cmdidx == CMD_visual
8570 || eap->cmdidx == CMD_view))
8571 {
8572 exmode_active = FALSE;
8573 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008574 {
8575 /* Special case: ":global/pat/visual\NLvi-commands" */
8576 if (global_busy)
8577 {
8578 int rd = RedrawingDisabled;
8579 int nwr = no_wait_return;
8580 int ms = msg_scroll;
8581#ifdef FEAT_GUI
8582 int he = hold_gui_events;
8583#endif
8584
8585 if (eap->nextcmd != NULL)
8586 {
8587 stuffReadbuff(eap->nextcmd);
8588 eap->nextcmd = NULL;
8589 }
8590
8591 if (exmode_was != EXMODE_VIM)
8592 settmode(TMODE_RAW);
8593 RedrawingDisabled = 0;
8594 no_wait_return = 0;
8595 need_wait_return = FALSE;
8596 msg_scroll = 0;
8597#ifdef FEAT_GUI
8598 hold_gui_events = 0;
8599#endif
8600 must_redraw = CLEAR;
8601
8602 main_loop(FALSE, TRUE);
8603
8604 RedrawingDisabled = rd;
8605 no_wait_return = nwr;
8606 msg_scroll = ms;
8607#ifdef FEAT_GUI
8608 hold_gui_events = he;
8609#endif
8610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614
8615 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008616 || eap->cmdidx == CMD_tabnew
8617 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008618 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008620 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 setpcmark();
8622 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008623 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8624 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008626 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 || *eap->arg != NUL
8628#ifdef FEAT_BROWSE
8629 || cmdmod.browse
8630#endif
8631 )
8632 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008633 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8634 * can bring us here, others are stopped earlier. */
8635 if (*eap->arg != NUL && curbuf_locked())
8636 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 n = readonlymode;
8639 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8640 readonlymode = TRUE;
8641 else if (eap->cmdidx == CMD_enew)
8642 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8643 empty buffer */
8644 setpcmark();
8645 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8646 NULL, eap,
8647 /* ":edit" goes to first line if Vi compatible */
8648 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8649 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8650 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008651 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008653 /* after a split we can use an existing buffer */
8654 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655#ifdef FEAT_LISTCMDS
8656 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8657#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008658 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 {
8660 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 if (old_curwin != NULL)
8662 {
8663 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008664 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008666#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008667 cleanup_T cs;
8668
8669 /* Reset the error/interrupt/exception state here so that
8670 * aborting() returns FALSE when closing a window. */
8671 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008672#endif
8673#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008675#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008676 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008677
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008678#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008679 /* Restore the error/interrupt/exception state if not
8680 * discarded by a new aborting error, interrupt, or
8681 * uncaught exception. */
8682 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 }
8687 else if (readonlymode && curbuf->b_nwindows == 1)
8688 {
8689 /* When editing an already visited buffer, 'readonly' won't be set
8690 * but the previous value is kept. With ":view" and ":sview" we
8691 * want the file to be readonly, except when another window is
8692 * editing the same buffer. */
8693 curbuf->b_p_ro = TRUE;
8694 }
8695 readonlymode = n;
8696 }
8697 else
8698 {
8699 if (eap->do_ecmd_cmd != NULL)
8700 do_cmdline_cmd(eap->do_ecmd_cmd);
8701#ifdef FEAT_TITLE
8702 n = curwin->w_arg_idx_invalid;
8703#endif
8704 check_arg_idx(curwin);
8705#ifdef FEAT_TITLE
8706 if (n != curwin->w_arg_idx_invalid)
8707 maketitle();
8708#endif
8709 }
8710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 /*
8712 * if ":split file" worked, set alternate file name in old window to new
8713 * file
8714 */
8715 if (old_curwin != NULL
8716 && *eap->arg != NUL
8717 && curwin != old_curwin
8718 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008719 && old_curwin->w_buffer != curbuf
8720 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722
8723 ex_no_reprint = TRUE;
8724}
8725
8726#ifndef FEAT_GUI
8727/*
8728 * ":gui" and ":gvim" when there is no GUI.
8729 */
8730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008731ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733 eap->errmsg = e_nogvim;
8734}
8735#endif
8736
8737#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008739ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
8741 gui_make_tearoff(eap->arg);
8742}
8743#endif
8744
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008745#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8746 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008748ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008750# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8751 if (gui.in_use)
8752 gui_make_popup(eap->arg, eap->forceit);
8753# ifdef FEAT_TERM_POPUP_MENU
8754 else
8755# endif
8756# endif
8757# ifdef FEAT_TERM_POPUP_MENU
8758 pum_make_popup(eap->arg, eap->forceit);
8759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760}
8761#endif
8762
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008764ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8767 MSG(_("No swap file"));
8768 else
8769 msg(curbuf->b_ml.ml_mfp->mf_fname);
8770}
8771
8772/*
8773 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8774 * offset.
8775 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008778ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008781 win_T *save_curwin = curwin;
8782 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 long topline;
8784 long y;
8785 linenr_T old_linenr = curwin->w_cursor.lnum;
8786
8787 setpcmark();
8788
8789 /*
8790 * determine max topline
8791 */
8792 if (curwin->w_p_scb)
8793 {
8794 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008795 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 {
8797 if (wp->w_p_scb && wp->w_buffer)
8798 {
8799 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8800 if (topline > y)
8801 topline = y;
8802 }
8803 }
8804 if (topline < 1)
8805 topline = 1;
8806 }
8807 else
8808 {
8809 topline = 1;
8810 }
8811
8812
8813 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008814 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008816 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
8818 if (curwin->w_p_scb)
8819 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008820 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 y = topline - curwin->w_topline;
8822 if (y > 0)
8823 scrollup(y, TRUE);
8824 else
8825 scrolldown(-y, TRUE);
8826 curwin->w_scbind_pos = topline;
8827 redraw_later(VALID);
8828 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 }
8831 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008832 curwin = save_curwin;
8833 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (curwin->w_p_scb)
8835 {
8836 did_syncbind = TRUE;
8837 checkpcmark();
8838 if (old_linenr != curwin->w_cursor.lnum)
8839 {
8840 char_u ctrl_o[2];
8841
8842 ctrl_o[0] = Ctrl_O;
8843 ctrl_o[1] = 0;
8844 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8845 }
8846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847}
8848
8849
8850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008851ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008853 int i;
8854 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8855 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
8857 if (eap->usefilter) /* :r!cmd */
8858 do_bang(1, eap, FALSE, FALSE, TRUE);
8859 else
8860 {
8861 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8862 return;
8863
8864#ifdef FEAT_BROWSE
8865 if (cmdmod.browse)
8866 {
8867 char_u *browseFile;
8868
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008869 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 NULL, NULL, NULL, curbuf);
8871 if (browseFile != NULL)
8872 {
8873 i = readfile(browseFile, NULL,
8874 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8875 vim_free(browseFile);
8876 }
8877 else
8878 i = OK;
8879 }
8880 else
8881#endif
8882 if (*eap->arg == NUL)
8883 {
8884 if (check_fname() == FAIL) /* check for no file name */
8885 return;
8886 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8887 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8888 }
8889 else
8890 {
8891 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8892 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8893 i = readfile(eap->arg, NULL,
8894 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8895
8896 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008897 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008899#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 if (!aborting())
8901#endif
8902 EMSG2(_(e_notopen), eap->arg);
8903 }
8904 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008905 {
8906 if (empty && exmode_active)
8907 {
8908 /* Delete the empty line that remains. Historically ex does
8909 * this but vi doesn't. */
8910 if (eap->line2 == 0)
8911 lnum = curbuf->b_ml.ml_line_count;
8912 else
8913 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008914 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008915 {
8916 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008917 if (curwin->w_cursor.lnum > 1
8918 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008919 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008920 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008921 }
8922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 }
8926}
8927
Bram Moolenaarea408852005-06-25 22:49:46 +00008928static char_u *prev_dir = NULL;
8929
8930#if defined(EXITFREE) || defined(PROTO)
8931 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008932free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008933{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008934 VIM_CLEAR(prev_dir);
8935 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008936}
8937#endif
8938
Bram Moolenaarf4258302013-06-02 18:20:17 +02008939/*
8940 * Deal with the side effects of changing the current directory.
8941 * When "local" is TRUE then this was after an ":lcd" command.
8942 */
8943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008944post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008945{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008946 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008947 if (local)
8948 {
8949 /* If still in global directory, need to remember current
8950 * directory as global directory. */
8951 if (globaldir == NULL && prev_dir != NULL)
8952 globaldir = vim_strsave(prev_dir);
8953 /* Remember this local directory for the window. */
8954 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8955 curwin->w_localdir = vim_strsave(NameBuff);
8956 }
8957 else
8958 {
8959 /* We are now in the global directory, no need to remember its
8960 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008961 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008962 }
8963
8964 shorten_fnames(TRUE);
8965}
8966
Bram Moolenaarea408852005-06-25 22:49:46 +00008967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968/*
8969 * ":cd", ":lcd", ":chdir" and ":lchdir".
8970 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008971 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008972ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 char_u *new_dir;
8975 char_u *tofree;
8976
8977 new_dir = eap->arg;
8978#if !defined(UNIX) && !defined(VMS)
8979 /* for non-UNIX ":cd" means: print current directory */
8980 if (*new_dir == NUL)
8981 ex_pwd(NULL);
8982 else
8983#endif
8984 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008985 if (allbuf_locked())
8986 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008987 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8988 && !eap->forceit)
8989 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008990 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008991 return;
8992 }
8993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 /* ":cd -": Change to previous directory */
8995 if (STRCMP(new_dir, "-") == 0)
8996 {
8997 if (prev_dir == NULL)
8998 {
8999 EMSG(_("E186: No previous directory"));
9000 return;
9001 }
9002 new_dir = prev_dir;
9003 }
9004
9005 /* Save current directory for next ":cd -" */
9006 tofree = prev_dir;
9007 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9008 prev_dir = vim_strsave(NameBuff);
9009 else
9010 prev_dir = NULL;
9011
9012#if defined(UNIX) || defined(VMS)
9013 /* for UNIX ":cd" means: go to home directory */
9014 if (*new_dir == NUL)
9015 {
9016 /* use NameBuff for home directory name */
9017# ifdef VMS
9018 char_u *p;
9019
9020 p = mch_getenv((char_u *)"SYS$LOGIN");
9021 if (p == NULL || *p == NUL) /* empty is the same as not set */
9022 NameBuff[0] = NUL;
9023 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009024 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025# else
9026 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9027# endif
9028 new_dir = NameBuff;
9029 }
9030#endif
9031 if (new_dir == NULL || vim_chdir(new_dir))
9032 EMSG(_(e_failed));
9033 else
9034 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009035 int is_local_chdir = eap->cmdidx == CMD_lcd
9036 || eap->cmdidx == CMD_lchdir;
9037
9038 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039
9040 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009041 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009043 apply_autocmds(EVENT_DIRCHANGED,
9044 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9045 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 }
9047 vim_free(tofree);
9048 }
9049}
9050
9051/*
9052 * ":pwd".
9053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009055ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9058 {
9059#ifdef BACKSLASH_IN_FILENAME
9060 slash_adjust(NameBuff);
9061#endif
9062 msg(NameBuff);
9063 }
9064 else
9065 EMSG(_("E187: Unknown"));
9066}
9067
9068/*
9069 * ":=".
9070 */
9071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009072ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009075 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076}
9077
9078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009079ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009081 int n;
9082 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
9084 if (cursor_valid())
9085 {
9086 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9087 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009088 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009090
9091 len = eap->line2;
9092 switch (*eap->arg)
9093 {
9094 case 'm': break;
9095 case NUL: len *= 1000L; break;
9096 default: EMSG2(_(e_invarg2), eap->arg); return;
9097 }
9098 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099}
9100
9101/*
9102 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9103 */
9104 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009105do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009108 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
9110 cursor_on();
9111 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009112 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009114 wait_now = msec - done > 1000L ? 1000L : msec - done;
9115#ifdef FEAT_TIMERS
9116 {
9117 long due_time = check_due_timer();
9118
9119 if (due_time > 0 && due_time < wait_now)
9120 wait_now = due_time;
9121 }
9122#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009123#ifdef FEAT_JOB_CHANNEL
9124 if (has_any_channel() && wait_now > 100L)
9125 wait_now = 100L;
9126#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009127 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009128#ifdef FEAT_JOB_CHANNEL
9129 if (has_any_channel())
9130 ui_breakcheck_force(TRUE);
9131 else
9132#endif
9133 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009134#ifdef MESSAGE_QUEUE
9135 /* Process the netbeans and clientserver messages that may have been
9136 * received in the call to ui_breakcheck() when the GUI is in use. This
9137 * may occur when running a test case. */
9138 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 }
9141}
9142
9143 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009144do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 int mode;
9147 char_u *cmdp;
9148
9149 cmdp = eap->cmd;
9150 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9151
9152 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9153 eap->arg, mode, isabbrev))
9154 {
9155 case 1: EMSG(_(e_invarg));
9156 break;
9157 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9158 break;
9159 }
9160}
9161
9162/*
9163 * ":winsize" command (obsolete).
9164 */
9165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009166ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168 int w, h;
9169 char_u *arg = eap->arg;
9170 char_u *p;
9171
9172 w = getdigits(&arg);
9173 arg = skipwhite(arg);
9174 p = arg;
9175 h = getdigits(&arg);
9176 if (*p != NUL && *arg == NUL)
9177 set_shellsize(w, h, TRUE);
9178 else
9179 EMSG(_("E465: :winsize requires two number arguments"));
9180}
9181
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009183ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 int xchar = NUL;
9186 char_u *p;
9187
9188 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9189 {
9190 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9191 if (eap->arg[1] == NUL)
9192 {
9193 EMSG(_(e_invarg));
9194 return;
9195 }
9196 xchar = eap->arg[1];
9197 p = eap->arg + 2;
9198 }
9199 else
9200 p = eap->arg + 1;
9201
9202 eap->nextcmd = check_nextcmd(p);
9203 p = skipwhite(p);
9204 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9205 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009206 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 {
9208 /* Pass flags on for ":vertical wincmd ]". */
9209 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009210 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9212 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009213 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
9215}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216
Bram Moolenaar843ee412004-06-30 16:16:41 +00009217#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218/*
9219 * ":winpos".
9220 */
9221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009222ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 int x, y;
9225 char_u *arg = eap->arg;
9226 char_u *p;
9227
9228 if (*arg == NUL)
9229 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009230# if defined(FEAT_GUI) || defined(MSWIN)
9231# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009233# else
9234 if (mch_get_winpos(&x, &y) != FAIL)
9235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
9237 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9238 msg(IObuff);
9239 }
9240 else
9241# endif
9242 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9243 }
9244 else
9245 {
9246 x = getdigits(&arg);
9247 arg = skipwhite(arg);
9248 p = arg;
9249 y = getdigits(&arg);
9250 if (*p == NUL || *arg != NUL)
9251 {
9252 EMSG(_("E466: :winpos requires two number arguments"));
9253 return;
9254 }
9255# ifdef FEAT_GUI
9256 if (gui.in_use)
9257 gui_mch_set_winpos(x, y);
9258 else if (gui.starting)
9259 {
9260 /* Remember the coordinates for when the window is opened. */
9261 gui_win_x = x;
9262 gui_win_y = y;
9263 }
9264# ifdef HAVE_TGETENT
9265 else
9266# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009267# else
9268# ifdef MSWIN
9269 mch_set_winpos(x, y);
9270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271# endif
9272# ifdef HAVE_TGETENT
9273 if (*T_CWP)
9274 term_set_winpos(x, y);
9275# endif
9276 }
9277}
9278#endif
9279
9280/*
9281 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9282 */
9283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009284ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 oparg_T oa;
9287
9288 clear_oparg(&oa);
9289 oa.regname = eap->regname;
9290 oa.start.lnum = eap->line1;
9291 oa.end.lnum = eap->line2;
9292 oa.line_count = eap->line2 - eap->line1 + 1;
9293 oa.motion_type = MLINE;
9294#ifdef FEAT_VIRTUALEDIT
9295 virtual_op = FALSE;
9296#endif
9297 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9298 {
9299 setpcmark();
9300 curwin->w_cursor.lnum = eap->line1;
9301 beginline(BL_SOL | BL_FIX);
9302 }
9303
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009304 if (VIsual_active)
9305 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 switch (eap->cmdidx)
9308 {
9309 case CMD_delete:
9310 oa.op_type = OP_DELETE;
9311 op_delete(&oa);
9312 break;
9313
9314 case CMD_yank:
9315 oa.op_type = OP_YANK;
9316 (void)op_yank(&oa, FALSE, TRUE);
9317 break;
9318
9319 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009320 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009322 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9323#else
9324 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009326 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 oa.op_type = OP_RSHIFT;
9328 else
9329 oa.op_type = OP_LSHIFT;
9330 op_shift(&oa, FALSE, eap->amount);
9331 break;
9332 }
9333#ifdef FEAT_VIRTUALEDIT
9334 virtual_op = MAYBE;
9335#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009336 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337}
9338
9339/*
9340 * ":put".
9341 */
9342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009343ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
9345 /* ":0put" works like ":1put!". */
9346 if (eap->line2 == 0)
9347 {
9348 eap->line2 = 1;
9349 eap->forceit = TRUE;
9350 }
9351 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009352 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9353 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354}
9355
9356/*
9357 * Handle ":copy" and ":move".
9358 */
9359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009360ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361{
9362 long n;
9363
Bram Moolenaarded27822017-01-02 14:27:34 +01009364 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (eap->arg == NULL) /* error detected */
9366 {
9367 eap->nextcmd = NULL;
9368 return;
9369 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009370 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371
9372 /*
9373 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9374 */
9375 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9376 {
9377 EMSG(_(e_invaddr));
9378 return;
9379 }
9380
9381 if (eap->cmdidx == CMD_move)
9382 {
9383 if (do_move(eap->line1, eap->line2, n) == FAIL)
9384 return;
9385 }
9386 else
9387 ex_copy(eap->line1, eap->line2, n);
9388 u_clearline();
9389 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009390 ex_may_print(eap);
9391}
9392
9393/*
9394 * Print the current line if flags were given to the Ex command.
9395 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009397ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009398{
9399 if (eap->flags != 0)
9400 {
9401 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9402 (eap->flags & EXFLAG_LIST));
9403 ex_no_reprint = TRUE;
9404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405}
9406
9407/*
9408 * ":smagic" and ":snomagic".
9409 */
9410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009411ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
9413 int magic_save = p_magic;
9414
9415 p_magic = (eap->cmdidx == CMD_smagic);
9416 do_sub(eap);
9417 p_magic = magic_save;
9418}
9419
9420/*
9421 * ":join".
9422 */
9423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009424ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
9426 curwin->w_cursor.lnum = eap->line1;
9427 if (eap->line1 == eap->line2)
9428 {
9429 if (eap->addr_count >= 2) /* :2,2join does nothing */
9430 return;
9431 if (eap->line2 == curbuf->b_ml.ml_line_count)
9432 {
9433 beep_flush();
9434 return;
9435 }
9436 ++eap->line2;
9437 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009438 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009440 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441}
9442
9443/*
9444 * ":[addr]@r" or ":[addr]*r": execute register
9445 */
9446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009447ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
9449 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009450 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451
9452 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009453 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
9455#ifdef USE_ON_FLY_SCROLL
9456 dont_scroll = TRUE; /* disallow scrolling here */
9457#endif
9458
9459 /* get the register name. No name means to use the previous one */
9460 c = *eap->arg;
9461 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9462 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009463 /* Put the register in the typeahead buffer with the "silent" flag. */
9464 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9465 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 else
9470 {
9471 int save_efr = exec_from_reg;
9472
9473 exec_from_reg = TRUE;
9474
9475 /*
9476 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009477 * Continue until the stuff buffer is empty and all added characters
9478 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009480 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9482
9483 exec_from_reg = save_efr;
9484 }
9485}
9486
9487/*
9488 * ":!".
9489 */
9490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009491ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9494}
9495
9496/*
9497 * ":undo".
9498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009500ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009502 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009503 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009504 else
9505 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506}
9507
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009508#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009510ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009511{
9512 char_u hash[UNDO_HASH_SIZE];
9513
9514 u_compute_hash(hash);
9515 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9516}
9517
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009520{
9521 char_u hash[UNDO_HASH_SIZE];
9522
9523 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009524 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009525}
9526#endif
9527
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528/*
9529 * ":redo".
9530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009532ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533{
9534 u_redo(1);
9535}
9536
9537/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009538 * ":earlier" and ":later".
9539 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009541ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009542{
9543 long count = 0;
9544 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009545 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009546 char_u *p = eap->arg;
9547
9548 if (*p == NUL)
9549 count = 1;
9550 else if (isdigit(*p))
9551 {
9552 count = getdigits(&p);
9553 switch (*p)
9554 {
9555 case 's': ++p; sec = TRUE; break;
9556 case 'm': ++p; sec = TRUE; count *= 60; break;
9557 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009558 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9559 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009560 }
9561 }
9562
9563 if (*p != NUL)
9564 EMSG2(_(e_invarg2), eap->arg);
9565 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009566 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9567 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009568}
9569
9570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 * ":redir": start/stop redirection.
9572 */
9573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009574ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
9576 char *mode;
9577 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009578 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579
Bram Moolenaarba768492016-07-08 15:32:54 +02009580#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009581 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009582 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009583 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009584 return;
9585 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009586#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 if (STRICMP(eap->arg, "END") == 0)
9589 close_redir();
9590 else
9591 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009592 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009594 ++arg;
9595 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009597 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 mode = "a";
9599 }
9600 else
9601 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009602 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
9604 close_redir();
9605
9606 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009607 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 if (fname == NULL)
9609 return;
9610#ifdef FEAT_BROWSE
9611 if (cmdmod.browse)
9612 {
9613 char_u *browseFile;
9614
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009615 browseFile = do_browse(BROWSE_SAVE,
9616 (char_u *)_("Save Redirection"),
9617 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 if (browseFile == NULL)
9619 return; /* operation cancelled */
9620 vim_free(fname);
9621 fname = browseFile;
9622 eap->forceit = TRUE; /* since dialog already asked */
9623 }
9624#endif
9625
9626 redir_fd = open_exfile(fname, eap->forceit, mode);
9627 vim_free(fname);
9628 }
9629#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009630 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 {
9632 /* redirect to a register a-z (resp. A-Z for appending) */
9633 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009634 ++arg;
9635 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009637 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009638 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009640 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009642 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009643 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009644 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009645 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009647 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009648 if (*arg == '>')
9649 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009650 /* Make register empty when not using @A-@Z and the
9651 * command is valid. */
9652 if (*arg == NUL && !isupper(redir_reg))
9653 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009655 }
9656 if (*arg != NUL)
9657 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009658 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009659 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009660 }
9661 }
9662 else if (*arg == '=' && arg[1] == '>')
9663 {
9664 int append;
9665
9666 /* redirect to a variable */
9667 close_redir();
9668 arg += 2;
9669
9670 if (*arg == '>')
9671 {
9672 ++arg;
9673 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 }
9675 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009676 append = FALSE;
9677
9678 if (var_redir_start(skipwhite(arg), append) == OK)
9679 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 }
9681#endif
9682
9683 /* TODO: redirect to a buffer */
9684
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009686 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009688
9689 /* Make sure redirection is not off. Can happen for cmdline completion
9690 * that indirectly invokes a command to catch its output. */
9691 if (redir_fd != NULL
9692#ifdef FEAT_EVAL
9693 || redir_reg || redir_vname
9694#endif
9695 )
9696 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697}
9698
9699/*
9700 * ":redraw": force redraw
9701 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009702 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009703ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704{
9705 int r = RedrawingDisabled;
9706 int p = p_lz;
9707
9708 RedrawingDisabled = 0;
9709 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009710 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009712 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713#ifdef FEAT_TITLE
9714 if (need_maketitle)
9715 maketitle();
9716#endif
9717 RedrawingDisabled = r;
9718 p_lz = p;
9719
9720 /* Reset msg_didout, so that a message that's there is overwritten. */
9721 msg_didout = FALSE;
9722 msg_col = 0;
9723
Bram Moolenaar56667a52013-07-17 11:54:28 +02009724 /* No need to wait after an intentional redraw. */
9725 need_wait_return = FALSE;
9726
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 out_flush();
9728}
9729
9730/*
9731 * ":redrawstatus": force redraw of status line(s)
9732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009734ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 int r = RedrawingDisabled;
9737 int p = p_lz;
9738
9739 RedrawingDisabled = 0;
9740 p_lz = FALSE;
9741 if (eap->forceit)
9742 status_redraw_all();
9743 else
9744 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009745 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 RedrawingDisabled = r;
9747 p_lz = p;
9748 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749}
9750
9751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009752close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754 if (redir_fd != NULL)
9755 {
9756 fclose(redir_fd);
9757 redir_fd = NULL;
9758 }
9759#ifdef FEAT_EVAL
9760 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009761 if (redir_vname)
9762 {
9763 var_redir_stop();
9764 redir_vname = 0;
9765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#endif
9767}
9768
9769#if defined(FEAT_SESSION) && defined(USE_CRNL)
9770# define MKSESSION_NL
9771static int mksession_nl = FALSE; /* use NL only in put_eol() */
9772#endif
9773
9774/*
9775 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9776 */
9777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009778ex_mkrc(
9779 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
9781 FILE *fd;
9782 int failed = FALSE;
9783 char_u *fname;
9784#ifdef FEAT_BROWSE
9785 char_u *browseFile = NULL;
9786#endif
9787#ifdef FEAT_SESSION
9788 int view_session = FALSE;
9789 int using_vdir = FALSE; /* using 'viewdir'? */
9790 char_u *viewFile = NULL;
9791 unsigned *flagp;
9792#endif
9793
9794 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9795 {
9796#ifdef FEAT_SESSION
9797 view_session = TRUE;
9798#else
9799 ex_ni(eap);
9800 return;
9801#endif
9802 }
9803
9804#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009805 /* Use the short file name until ":lcd" is used. We also don't use the
9806 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009807 did_lcd = FALSE;
9808
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9810 if (eap->cmdidx == CMD_mkview
9811 && (*eap->arg == NUL
9812 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9813 {
9814 eap->forceit = TRUE;
9815 fname = get_view_file(*eap->arg);
9816 if (fname == NULL)
9817 return;
9818 viewFile = fname;
9819 using_vdir = TRUE;
9820 }
9821 else
9822#endif
9823 if (*eap->arg != NUL)
9824 fname = eap->arg;
9825 else if (eap->cmdidx == CMD_mkvimrc)
9826 fname = (char_u *)VIMRC_FILE;
9827#ifdef FEAT_SESSION
9828 else if (eap->cmdidx == CMD_mksession)
9829 fname = (char_u *)SESSION_FILE;
9830#endif
9831 else
9832 fname = (char_u *)EXRC_FILE;
9833
9834#ifdef FEAT_BROWSE
9835 if (cmdmod.browse)
9836 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009837 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838# ifdef FEAT_SESSION
9839 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9840 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9841# endif
9842 (char_u *)_("Save Setup"),
9843 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9844 if (browseFile == NULL)
9845 goto theend;
9846 fname = browseFile;
9847 eap->forceit = TRUE; /* since dialog already asked */
9848 }
9849#endif
9850
9851#if defined(FEAT_SESSION) && defined(vim_mkdir)
9852 /* When using 'viewdir' may have to create the directory. */
9853 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009854 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855#endif
9856
9857 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9858 if (fd != NULL)
9859 {
9860#ifdef FEAT_SESSION
9861 if (eap->cmdidx == CMD_mkview)
9862 flagp = &vop_flags;
9863 else
9864 flagp = &ssop_flags;
9865#endif
9866
9867#ifdef MKSESSION_NL
9868 /* "unix" in 'sessionoptions': use NL line separator */
9869 if (view_session && (*flagp & SSOP_UNIX))
9870 mksession_nl = TRUE;
9871#endif
9872
9873 /* Write the version command for :mkvimrc */
9874 if (eap->cmdidx == CMD_mkvimrc)
9875 (void)put_line(fd, "version 6.0");
9876
9877#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009878 if (eap->cmdidx == CMD_mksession)
9879 {
9880 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9881 failed = TRUE;
9882 }
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 if (eap->cmdidx != CMD_mkview)
9885#endif
9886 {
9887 /* Write setting 'compatible' first, because it has side effects.
9888 * For that same reason only do it when needed. */
9889 if (p_cp)
9890 (void)put_line(fd, "if !&cp | set cp | endif");
9891 else
9892 (void)put_line(fd, "if &cp | set nocp | endif");
9893 }
9894
9895#ifdef FEAT_SESSION
9896 if (!view_session
9897 || (eap->cmdidx == CMD_mksession
9898 && (*flagp & SSOP_OPTIONS)))
9899#endif
9900 failed |= (makemap(fd, NULL) == FAIL
9901 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9902
9903#ifdef FEAT_SESSION
9904 if (!failed && view_session)
9905 {
9906 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9907 failed = TRUE;
9908 if (eap->cmdidx == CMD_mksession)
9909 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009910 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911
Bram Moolenaard9462e32011-04-11 21:35:11 +02009912 dirnow = alloc(MAXPATHL);
9913 if (dirnow == NULL)
9914 failed = TRUE;
9915 else
9916 {
9917 /*
9918 * Change to session file's dir.
9919 */
9920 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009922 *dirnow = NUL;
9923 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9924 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009925 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009926 shorten_fnames(TRUE);
9927 }
9928 else if (*dirnow != NUL
9929 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9930 {
9931 if (mch_chdir((char *)globaldir) == 0)
9932 shorten_fnames(TRUE);
9933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
Bram Moolenaard9462e32011-04-11 21:35:11 +02009935 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936
Bram Moolenaard9462e32011-04-11 21:35:11 +02009937 /* restore original dir */
9938 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009940 {
9941 if (mch_chdir((char *)dirnow) != 0)
9942 EMSG(_(e_prev_dir));
9943 shorten_fnames(TRUE);
9944 }
9945 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 }
9947 }
9948 else
9949 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009950 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9951 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 }
9953 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9954 == FAIL)
9955 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009956 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9957 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009958 if (eap->cmdidx == CMD_mksession)
9959 {
9960 if (put_line(fd, "unlet SessionLoad") == FAIL)
9961 failed = TRUE;
9962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 }
9964#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009965 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9966 failed = TRUE;
9967
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 failed |= fclose(fd);
9969
9970 if (failed)
9971 EMSG(_(e_write));
9972#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9973 else if (eap->cmdidx == CMD_mksession)
9974 {
9975 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009976 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977
Bram Moolenaard9462e32011-04-11 21:35:11 +02009978 tbuf = alloc(MAXPATHL);
9979 if (tbuf != NULL)
9980 {
9981 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9982 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9983 vim_free(tbuf);
9984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 }
9986#endif
9987#ifdef MKSESSION_NL
9988 mksession_nl = FALSE;
9989#endif
9990 }
9991
9992#ifdef FEAT_BROWSE
9993theend:
9994 vim_free(browseFile);
9995#endif
9996#ifdef FEAT_SESSION
9997 vim_free(viewFile);
9998#endif
9999}
10000
Bram Moolenaardf177f62005-02-22 08:39:57 +000010001#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10002 || defined(PROTO)
10003 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010004vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010005{
10006 if (vim_mkdir(name, prot) != 0)
10007 {
10008 EMSG2(_("E739: Cannot create directory: %s"), name);
10009 return FAIL;
10010 }
10011 return OK;
10012}
10013#endif
10014
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015/*
10016 * Open a file for writing for an Ex command, with some checks.
10017 * Return file descriptor, or NULL on failure.
10018 */
10019 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010020open_exfile(
10021 char_u *fname,
10022 int forceit,
10023 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024{
10025 FILE *fd;
10026
10027#ifdef UNIX
10028 /* with Unix it is possible to open a directory */
10029 if (mch_isdir(fname))
10030 {
10031 EMSG2(_(e_isadir2), fname);
10032 return NULL;
10033 }
10034#endif
10035 if (!forceit && *mode != 'a' && vim_fexists(fname))
10036 {
10037 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10038 return NULL;
10039 }
10040
10041 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10042 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10043
10044 return fd;
10045}
10046
10047/*
10048 * ":mark" and ":k".
10049 */
10050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010051ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
10053 pos_T pos;
10054
10055 if (*eap->arg == NUL) /* No argument? */
10056 EMSG(_(e_argreq));
10057 else if (eap->arg[1] != NUL) /* more than one character? */
10058 EMSG(_(e_trailing));
10059 else
10060 {
10061 pos = curwin->w_cursor; /* save curwin->w_cursor */
10062 curwin->w_cursor.lnum = eap->line2;
10063 beginline(BL_WHITE | BL_FIX);
10064 if (setmark(*eap->arg) == FAIL) /* set mark */
10065 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10066 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10067 }
10068}
10069
10070/*
10071 * Update w_topline, w_leftcol and the cursor position.
10072 */
10073 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010074update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 check_cursor(); /* put cursor on valid line */
10077 update_topline();
10078 if (!curwin->w_p_wrap)
10079 validate_cursor();
10080 update_curswant();
10081}
10082
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010084 * Save the current State and go to Normal mode.
10085 * Return TRUE if the typeahead could be saved.
10086 */
10087 int
10088save_current_state(save_state_T *sst)
10089{
10090 sst->save_msg_scroll = msg_scroll;
10091 sst->save_restart_edit = restart_edit;
10092 sst->save_msg_didout = msg_didout;
10093 sst->save_State = State;
10094 sst->save_insertmode = p_im;
10095 sst->save_finish_op = finish_op;
10096 sst->save_opcount = opcount;
10097
10098 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10099 restart_edit = 0; /* don't go to Insert mode */
10100 p_im = FALSE; /* don't use 'insertmode' */
10101
10102 /*
10103 * Save the current typeahead. This is required to allow using ":normal"
10104 * from an event handler and makes sure we don't hang when the argument
10105 * ends with half a command.
10106 */
10107 save_typeahead(&sst->tabuf);
10108 return sst->tabuf.typebuf_valid;
10109}
10110
10111 void
10112restore_current_state(save_state_T *sst)
10113{
10114 /* Restore the previous typeahead. */
10115 restore_typeahead(&sst->tabuf);
10116
10117 msg_scroll = sst->save_msg_scroll;
10118 restart_edit = sst->save_restart_edit;
10119 p_im = sst->save_insertmode;
10120 finish_op = sst->save_finish_op;
10121 opcount = sst->save_opcount;
10122 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10123
10124 /* Restore the state (needed when called from a function executed for
10125 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10126 State = sst->save_State;
10127#ifdef CURSOR_SHAPE
10128 ui_cursor_shape(); /* may show different cursor shape */
10129#endif
10130}
10131
10132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 * ":normal[!] {commands}": Execute normal mode commands.
10134 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010136ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010138 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139#ifdef FEAT_MBYTE
10140 char_u *arg = NULL;
10141 int l;
10142 char_u *p;
10143#endif
10144
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010145 if (ex_normal_lock > 0)
10146 {
10147 EMSG(_(e_secure));
10148 return;
10149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 if (ex_normal_busy >= p_mmd)
10151 {
10152 EMSG(_("E192: Recursive use of :normal too deep"));
10153 return;
10154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
10156#ifdef FEAT_MBYTE
10157 /*
10158 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10159 * this for the K_SPECIAL leading byte, otherwise special keys will not
10160 * work.
10161 */
10162 if (has_mbyte)
10163 {
10164 int len = 0;
10165
10166 /* Count the number of characters to be escaped. */
10167 for (p = eap->arg; *p != NUL; ++p)
10168 {
10169# ifdef FEAT_GUI
10170 if (*p == CSI) /* leadbyte CSI */
10171 len += 2;
10172# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010173 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10175# ifdef FEAT_GUI
10176 || *p == CSI
10177# endif
10178 )
10179 len += 2;
10180 }
10181 if (len > 0)
10182 {
10183 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10184 if (arg != NULL)
10185 {
10186 len = 0;
10187 for (p = eap->arg; *p != NUL; ++p)
10188 {
10189 arg[len++] = *p;
10190# ifdef FEAT_GUI
10191 if (*p == CSI)
10192 {
10193 arg[len++] = KS_EXTRA;
10194 arg[len++] = (int)KE_CSI;
10195 }
10196# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010197 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
10199 arg[len++] = *++p;
10200 if (*p == K_SPECIAL)
10201 {
10202 arg[len++] = KS_SPECIAL;
10203 arg[len++] = KE_FILLER;
10204 }
10205# ifdef FEAT_GUI
10206 else if (*p == CSI)
10207 {
10208 arg[len++] = KS_EXTRA;
10209 arg[len++] = (int)KE_CSI;
10210 }
10211# endif
10212 }
10213 arg[len] = NUL;
10214 }
10215 }
10216 }
10217 }
10218#endif
10219
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010220 ++ex_normal_busy;
10221 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 {
10223 /*
10224 * Repeat the :normal command for each line in the range. When no
10225 * range given, execute it just once, without positioning the cursor
10226 * first.
10227 */
10228 do
10229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (eap->addr_count != 0)
10231 {
10232 curwin->w_cursor.lnum = eap->line1++;
10233 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010234 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 }
10236
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010237 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238#ifdef FEAT_MBYTE
10239 arg != NULL ? arg :
10240#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010241 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 }
10243 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10244 }
10245
10246 /* Might not return to the main loop when in an event handler. */
10247 update_topline_cursor();
10248
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010249 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010251#ifdef FEAT_MOUSE
10252 setmouse();
10253#endif
10254#ifdef CURSOR_SHAPE
10255 ui_cursor_shape(); /* may show different cursor shape */
10256#endif
10257
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258#ifdef FEAT_MBYTE
10259 vim_free(arg);
10260#endif
10261}
10262
10263/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010264 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 */
10266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010267ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010269 if (eap->forceit)
10270 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010271 /* cursor line can be zero on startup */
10272 if (!curwin->w_cursor.lnum)
10273 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010274 coladvance((colnr_T)MAXCOL);
10275 curwin->w_curswant = MAXCOL;
10276 curwin->w_set_curswant = FALSE;
10277 }
10278
Bram Moolenaara40c5002005-01-09 21:16:21 +000010279 /* Ignore the command when already in Insert mode. Inserting an
10280 * expression register that invokes a function can do this. */
10281 if (State & INSERT)
10282 return;
10283
Bram Moolenaar64969662005-12-14 21:59:55 +000010284 if (eap->cmdidx == CMD_startinsert)
10285 restart_edit = 'a';
10286 else if (eap->cmdidx == CMD_startreplace)
10287 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010289 restart_edit = 'V';
10290
10291 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010293 if (eap->cmdidx == CMD_startinsert)
10294 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 curwin->w_curswant = 0; /* avoid MAXCOL */
10296 }
10297}
10298
10299/*
10300 * ":stopinsert"
10301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010303ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304{
10305 restart_edit = 0;
10306 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010307 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010310/*
10311 * Execute normal mode command "cmd".
10312 * "remap" can be REMAP_NONE or REMAP_YES.
10313 */
10314 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010315exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010316{
Bram Moolenaar25281632016-01-21 23:32:32 +010010317 /* Stuff the argument into the typeahead buffer. */
10318 ins_typebuf(cmd, remap, 0, TRUE, silent);
10319 exec_normal(FALSE);
10320}
Bram Moolenaar25281632016-01-21 23:32:32 +010010321
Bram Moolenaar25281632016-01-21 23:32:32 +010010322/*
10323 * Execute normal_cmd() until there is no typeahead left.
10324 */
10325 void
10326exec_normal(int was_typed)
10327{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010328 oparg_T oa;
10329
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010330 clear_oparg(&oa);
10331 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010332 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10333 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010334 {
10335 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010336 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010337 }
10338}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010339
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_FIND_ID
10341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010342ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343{
10344 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10345 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10346 (linenr_T)1, (linenr_T)MAXLNUM);
10347}
10348
Bram Moolenaar4033c552017-09-16 20:54:51 +020010349#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350/*
10351 * ":psearch"
10352 */
10353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010354ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355{
10356 g_do_tagpreview = p_pvh;
10357 ex_findpat(eap);
10358 g_do_tagpreview = 0;
10359}
10360#endif
10361
10362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010363ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
10365 int whole = TRUE;
10366 long n;
10367 char_u *p;
10368 int action;
10369
10370 switch (cmdnames[eap->cmdidx].cmd_name[2])
10371 {
10372 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10373 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10374 action = ACTION_GOTO;
10375 else
10376 action = ACTION_SHOW;
10377 break;
10378 case 'i': /* ":ilist" and ":dlist" */
10379 action = ACTION_SHOW_ALL;
10380 break;
10381 case 'u': /* ":ijump" and ":djump" */
10382 action = ACTION_GOTO;
10383 break;
10384 default: /* ":isplit" and ":dsplit" */
10385 action = ACTION_SPLIT;
10386 break;
10387 }
10388
10389 n = 1;
10390 if (vim_isdigit(*eap->arg)) /* get count */
10391 {
10392 n = getdigits(&eap->arg);
10393 eap->arg = skipwhite(eap->arg);
10394 }
10395 if (*eap->arg == '/') /* Match regexp, not just whole words */
10396 {
10397 whole = FALSE;
10398 ++eap->arg;
10399 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10400 if (*p)
10401 {
10402 *p++ = NUL;
10403 p = skipwhite(p);
10404
10405 /* Check for trailing illegal characters */
10406 if (!ends_excmd(*p))
10407 eap->errmsg = e_trailing;
10408 else
10409 eap->nextcmd = check_nextcmd(p);
10410 }
10411 }
10412 if (!eap->skip)
10413 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10414 whole, !eap->forceit,
10415 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10416 n, action, eap->line1, eap->line2);
10417}
10418#endif
10419
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420
Bram Moolenaar4033c552017-09-16 20:54:51 +020010421#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422/*
10423 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10424 */
10425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010426ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010428 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10430}
10431
10432/*
10433 * ":pedit"
10434 */
10435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010436ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
10438 win_T *curwin_save = curwin;
10439
10440 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010441 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010442 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 do_exedit(eap, NULL);
10444 keep_help_flag = FALSE;
10445 if (curwin != curwin_save && win_valid(curwin_save))
10446 {
10447 /* Return cursor to where we were */
10448 validate_cursor();
10449 redraw_later(VALID);
10450 win_enter(curwin_save, TRUE);
10451 }
10452 g_do_tagpreview = 0;
10453}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455
10456/*
10457 * ":stag", ":stselect" and ":stjump".
10458 */
10459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010460ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
10462 postponed_split = -1;
10463 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010464 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10466 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010467 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469
10470/*
10471 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10472 */
10473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010474ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10477}
10478
10479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010480ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
10482 int cmd;
10483
10484 switch (name[1])
10485 {
10486 case 'j': cmd = DT_JUMP; /* ":tjump" */
10487 break;
10488 case 's': cmd = DT_SELECT; /* ":tselect" */
10489 break;
10490 case 'p': cmd = DT_PREV; /* ":tprevious" */
10491 break;
10492 case 'N': cmd = DT_PREV; /* ":tNext" */
10493 break;
10494 case 'n': cmd = DT_NEXT; /* ":tnext" */
10495 break;
10496 case 'o': cmd = DT_POP; /* ":pop" */
10497 break;
10498 case 'f': /* ":tfirst" */
10499 case 'r': cmd = DT_FIRST; /* ":trewind" */
10500 break;
10501 case 'l': cmd = DT_LAST; /* ":tlast" */
10502 break;
10503 default: /* ":tag" */
10504#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010505 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010507 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 return;
10509 }
10510#endif
10511 cmd = DT_TAG;
10512 break;
10513 }
10514
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010515 if (name[0] == 'l')
10516 {
10517#ifndef FEAT_QUICKFIX
10518 ex_ni(eap);
10519 return;
10520#else
10521 cmd = DT_LTAG;
10522#endif
10523 }
10524
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10526 eap->forceit, TRUE);
10527}
10528
10529/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010530 * Check "str" for starting with a special cmdline variable.
10531 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10532 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10533 */
10534 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010535find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010536{
10537 int len;
10538 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010539 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010540 "%",
10541#define SPEC_PERC 0
10542 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010543#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010544 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010545#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010546 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010547#define SPEC_CCWORD (SPEC_CWORD + 1)
10548 "<cexpr>", /* expr under cursor */
10549#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010550 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010551#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010552 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010553#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010554 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010555#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010556 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010557#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010558 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010559#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010560 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010561#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010562#ifdef FEAT_CLIENTSERVER
10563 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010564# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010565#endif
10566 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010567
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010568 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010569 {
10570 len = (int)STRLEN(spec_str[i]);
10571 if (STRNCMP(src, spec_str[i], len) == 0)
10572 {
10573 *usedlen = len;
10574 return i;
10575 }
10576 }
10577 return -1;
10578}
10579
10580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 * Evaluate cmdline variables.
10582 *
10583 * change '%' to curbuf->b_ffname
10584 * '#' to curwin->w_altfile
10585 * '<cword>' to word under the cursor
10586 * '<cWORD>' to WORD under the cursor
10587 * '<cfile>' to path name under the cursor
10588 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010589 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 * '<afile>' to file name for autocommand
10591 * '<abuf>' to buffer number for autocommand
10592 * '<amatch>' to matching name for autocommand
10593 *
10594 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10595 * "" for error without a message) and NULL is returned.
10596 * Returns an allocated string if a valid match was found.
10597 * Returns NULL if no match was found. "usedlen" then still contains the
10598 * number of characters to skip.
10599 */
10600 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010601eval_vars(
10602 char_u *src, /* pointer into commandline */
10603 char_u *srcstart, /* beginning of valid memory for src */
10604 int *usedlen, /* characters after src that are used */
10605 linenr_T *lnump, /* line number for :e command, or NULL */
10606 char_u **errormsg, /* pointer to error message */
10607 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010608 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609{
10610 int i;
10611 char_u *s;
10612 char_u *result;
10613 char_u *resultbuf = NULL;
10614 int resultlen;
10615 buf_T *buf;
10616 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10617 int spec_idx;
10618#ifdef FEAT_MODIFY_FNAME
10619 int skip_mod = FALSE;
10620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622
10623 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010624 if (escaped != NULL)
10625 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626
10627 /*
10628 * Check if there is something to do.
10629 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010630 spec_idx = find_cmdline_var(src, usedlen);
10631 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 {
10633 *usedlen = 1;
10634 return NULL;
10635 }
10636
10637 /*
10638 * Skip when preceded with a backslash "\%" and "\#".
10639 * Note: In "\\%" the % is also not recognized!
10640 */
10641 if (src > srcstart && src[-1] == '\\')
10642 {
10643 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010644 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 return NULL;
10646 }
10647
10648 /*
10649 * word or WORD under cursor
10650 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010651 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10652 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010654 resultlen = find_ident_under_cursor(&result,
10655 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10656 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10657 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 if (resultlen == 0)
10659 {
10660 *errormsg = (char_u *)"";
10661 return NULL;
10662 }
10663 }
10664
10665 /*
10666 * '#': Alternate file name
10667 * '%': Current file name
10668 * File name under the cursor
10669 * File name for autocommand
10670 * and following modifiers
10671 */
10672 else
10673 {
10674 switch (spec_idx)
10675 {
10676 case SPEC_PERC: /* '%': current file */
10677 if (curbuf->b_fname == NULL)
10678 {
10679 result = (char_u *)"";
10680 valid = 0; /* Must have ":p:h" to be valid */
10681 }
10682 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 break;
10685
10686 case SPEC_HASH: /* '#' or "#99": alternate file */
10687 if (src[1] == '#') /* "##": the argument list */
10688 {
10689 result = arg_all();
10690 resultbuf = result;
10691 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010692 if (escaped != NULL)
10693 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694#ifdef FEAT_MODIFY_FNAME
10695 skip_mod = TRUE;
10696#endif
10697 break;
10698 }
10699 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010700 if (*s == '<') /* "#<99" uses v:oldfiles */
10701 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010703 if (s == src + 2 && src[1] == '-')
10704 /* just a minus sign, don't skip over it */
10705 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 *usedlen = (int)(s - src); /* length of what we expand */
10707
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010708 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010710 if (*usedlen < 2)
10711 {
10712 /* Should we give an error message for #<text? */
10713 *usedlen = 1;
10714 return NULL;
10715 }
10716#ifdef FEAT_EVAL
10717 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10718 (long)i);
10719 if (result == NULL)
10720 {
10721 *errormsg = (char_u *)"";
10722 return NULL;
10723 }
10724#else
10725 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 }
10729 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010730 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010731 if (i == 0 && src[1] == '<' && *usedlen > 1)
10732 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010733 buf = buflist_findnr(i);
10734 if (buf == NULL)
10735 {
10736 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10737 return NULL;
10738 }
10739 if (lnump != NULL)
10740 *lnump = ECMD_LAST;
10741 if (buf->b_fname == NULL)
10742 {
10743 result = (char_u *)"";
10744 valid = 0; /* Must have ":p:h" to be valid */
10745 }
10746 else
10747 result = buf->b_fname;
10748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 break;
10750
10751#ifdef FEAT_SEARCHPATH
10752 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010753 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 if (result == NULL)
10755 {
10756 *errormsg = (char_u *)"";
10757 return NULL;
10758 }
10759 resultbuf = result; /* remember allocated string */
10760 break;
10761#endif
10762
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 case SPEC_AFILE: /* file name for autocommand */
10764 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010765 if (result != NULL && !autocmd_fname_full)
10766 {
10767 /* Still need to turn the fname into a full path. It is
10768 * postponed to avoid a delay when <afile> is not used. */
10769 autocmd_fname_full = TRUE;
10770 result = FullName_save(autocmd_fname, FALSE);
10771 vim_free(autocmd_fname);
10772 autocmd_fname = result;
10773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 if (result == NULL)
10775 {
10776 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10777 return NULL;
10778 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010779 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 break;
10781
10782 case SPEC_ABUF: /* buffer number for autocommand */
10783 if (autocmd_bufnr <= 0)
10784 {
10785 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10786 return NULL;
10787 }
10788 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10789 result = strbuf;
10790 break;
10791
10792 case SPEC_AMATCH: /* match name for autocommand */
10793 result = autocmd_match;
10794 if (result == NULL)
10795 {
10796 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10797 return NULL;
10798 }
10799 break;
10800
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 case SPEC_SFILE: /* file name for ":so" command */
10802 result = sourcing_name;
10803 if (result == NULL)
10804 {
10805 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10806 return NULL;
10807 }
10808 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010809 case SPEC_SLNUM: /* line in file for ":so" command */
10810 if (sourcing_name == NULL || sourcing_lnum == 0)
10811 {
10812 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10813 return NULL;
10814 }
10815 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10816 result = strbuf;
10817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818#if defined(FEAT_CLIENTSERVER)
10819 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010820 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10821 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 result = strbuf;
10823 break;
10824#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010825 default:
10826 result = (char_u *)""; /* avoid gcc warning */
10827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 }
10829
10830 resultlen = (int)STRLEN(result); /* length of new string */
10831 if (src[*usedlen] == '<') /* remove the file name extension */
10832 {
10833 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 resultlen = (int)(s - result);
10836 }
10837#ifdef FEAT_MODIFY_FNAME
10838 else if (!skip_mod)
10839 {
10840 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10841 &resultlen);
10842 if (result == NULL)
10843 {
10844 *errormsg = (char_u *)"";
10845 return NULL;
10846 }
10847 }
10848#endif
10849 }
10850
10851 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10852 {
10853 if (valid != VALID_HEAD + VALID_PATH)
10854 /* xgettext:no-c-format */
10855 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10856 else
10857 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10858 result = NULL;
10859 }
10860 else
10861 result = vim_strnsave(result, resultlen);
10862 vim_free(resultbuf);
10863 return result;
10864}
10865
10866/*
10867 * Concatenate all files in the argument list, separated by spaces, and return
10868 * it in one allocated string.
10869 * Spaces and backslashes in the file names are escaped with a backslash.
10870 * Returns NULL when out of memory.
10871 */
10872 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010873arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874{
10875 int len;
10876 int idx;
10877 char_u *retval = NULL;
10878 char_u *p;
10879
10880 /*
10881 * Do this loop two times:
10882 * first time: compute the total length
10883 * second time: concatenate the names
10884 */
10885 for (;;)
10886 {
10887 len = 0;
10888 for (idx = 0; idx < ARGCOUNT; ++idx)
10889 {
10890 p = alist_name(&ARGLIST[idx]);
10891 if (p != NULL)
10892 {
10893 if (len > 0)
10894 {
10895 /* insert a space in between names */
10896 if (retval != NULL)
10897 retval[len] = ' ';
10898 ++len;
10899 }
10900 for ( ; *p != NUL; ++p)
10901 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010902 if (*p == ' '
10903#ifndef BACKSLASH_IN_FILENAME
10904 || *p == '\\'
10905#endif
10906 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 {
10908 /* insert a backslash */
10909 if (retval != NULL)
10910 retval[len] = '\\';
10911 ++len;
10912 }
10913 if (retval != NULL)
10914 retval[len] = *p;
10915 ++len;
10916 }
10917 }
10918 }
10919
10920 /* second time: break here */
10921 if (retval != NULL)
10922 {
10923 retval[len] = NUL;
10924 break;
10925 }
10926
10927 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010928 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 if (retval == NULL)
10930 break;
10931 }
10932
10933 return retval;
10934}
10935
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936/*
10937 * Expand the <sfile> string in "arg".
10938 *
10939 * Returns an allocated string, or NULL for any error.
10940 */
10941 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010942expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
10944 char_u *errormsg;
10945 int len;
10946 char_u *result;
10947 char_u *newres;
10948 char_u *repl;
10949 int srclen;
10950 char_u *p;
10951
10952 result = vim_strsave(arg);
10953 if (result == NULL)
10954 return NULL;
10955
10956 for (p = result; *p; )
10957 {
10958 if (STRNCMP(p, "<sfile>", 7) != 0)
10959 ++p;
10960 else
10961 {
10962 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010963 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 if (errormsg != NULL)
10965 {
10966 if (*errormsg)
10967 emsg(errormsg);
10968 vim_free(result);
10969 return NULL;
10970 }
10971 if (repl == NULL) /* no match (cannot happen) */
10972 {
10973 p += srclen;
10974 continue;
10975 }
10976 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10977 newres = alloc(len);
10978 if (newres == NULL)
10979 {
10980 vim_free(repl);
10981 vim_free(result);
10982 return NULL;
10983 }
10984 mch_memmove(newres, result, (size_t)(p - result));
10985 STRCPY(newres + (p - result), repl);
10986 len = (int)STRLEN(newres);
10987 STRCAT(newres, p + srclen);
10988 vim_free(repl);
10989 vim_free(result);
10990 result = newres;
10991 p = newres + len; /* continue after the match */
10992 }
10993 }
10994
10995 return result;
10996}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997
10998#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010999static int ses_winsizes(FILE *fd, int restore_size,
11000 win_T *tab_firstwin);
11001static int ses_win_rec(FILE *fd, frame_T *fr);
11002static frame_T *ses_skipframe(frame_T *fr);
11003static int ses_do_frame(frame_T *fr);
11004static int ses_do_win(win_T *wp);
11005static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11006static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011007static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008
11009/*
11010 * Write openfile commands for the current buffers to an .exrc file.
11011 * Return FAIL on error, OK otherwise.
11012 */
11013 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011014makeopens(
11015 FILE *fd,
11016 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
11018 buf_T *buf;
11019 int only_save_windows = TRUE;
11020 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 int restore_size = TRUE;
11022 win_T *wp;
11023 char_u *sname;
11024 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011025 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011026 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011027 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011028 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011029 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011030 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031
11032 if (ssop_flags & SSOP_BUFFERS)
11033 only_save_windows = FALSE; /* Save ALL buffers */
11034
11035 /*
11036 * Begin by setting the this_session variable, and then other
11037 * sessionable variables.
11038 */
11039#ifdef FEAT_EVAL
11040 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11041 return FAIL;
11042 if (ssop_flags & SSOP_GLOBALS)
11043 if (store_session_globals(fd) == FAIL)
11044 return FAIL;
11045#endif
11046
11047 /*
11048 * Close all windows but one.
11049 */
11050 if (put_line(fd, "silent only") == FAIL)
11051 return FAIL;
11052
11053 /*
11054 * Now a :cd command to the session directory or the current directory
11055 */
11056 if (ssop_flags & SSOP_SESDIR)
11057 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011058 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11059 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 return FAIL;
11061 }
11062 else if (ssop_flags & SSOP_CURDIR)
11063 {
11064 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11065 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011066 || fputs("cd ", fd) < 0
11067 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11068 || put_eol(fd) == FAIL)
11069 {
11070 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 vim_free(sname);
11074 }
11075
11076 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011077 * If there is an empty, unnamed buffer we will wipe it out later.
11078 * Remember the buffer number.
11079 */
11080 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11081 return FAIL;
11082 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11083 return FAIL;
11084 if (put_line(fd, "endif") == FAIL)
11085 return FAIL;
11086
11087 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 * Now save the current files, current buffer first.
11089 */
11090 if (put_line(fd, "set shortmess=aoO") == FAIL)
11091 return FAIL;
11092
11093 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011094 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 {
11096 if (!(only_save_windows && buf->b_nwindows == 0)
11097 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11098 && buf->b_fname != NULL
11099 && buf->b_p_bl)
11100 {
11101 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11102 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011103 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 return FAIL;
11105 }
11106 }
11107
11108 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011109 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11111 return FAIL;
11112
11113 if (ssop_flags & SSOP_RESIZE)
11114 {
11115 /* Note: after the restore we still check it worked!*/
11116 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11117 || put_eol(fd) == FAIL)
11118 return FAIL;
11119 }
11120
11121#ifdef FEAT_GUI
11122 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11123 {
11124 int x, y;
11125
11126 if (gui_mch_get_winpos(&x, &y) == OK)
11127 {
11128 /* Note: after the restore we still check it worked!*/
11129 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11130 return FAIL;
11131 }
11132 }
11133#endif
11134
11135 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011136 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11137 * will be displayed when creating the next tab. That resizes the windows
11138 * in the first tab, which may cause problems. Set 'showtabline' to 2
11139 * temporarily to avoid that.
11140 */
11141 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11142 {
11143 if (put_line(fd, "set stal=2") == FAIL)
11144 return FAIL;
11145 restore_stal = TRUE;
11146 }
11147
11148 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011149 * May repeat putting Windows for each tab, when "tabpages" is in
11150 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011151 * Don't use goto_tabpage(), it may change directory and trigger
11152 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011154 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011155 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011156 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011158 int need_tabnew = FALSE;
11159 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011160
Bram Moolenaar18144c82006-04-12 21:52:12 +000011161 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011163 tabpage_T *tp = find_tabpage(tabnr);
11164
11165 if (tp == NULL)
11166 break; /* done all tab pages */
11167 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011168 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011169 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011170 tab_topframe = topframe;
11171 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011172 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011173 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011174 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011175 tab_topframe = tp->tp_topframe;
11176 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011177 if (tabnr > 1)
11178 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180
Bram Moolenaar18144c82006-04-12 21:52:12 +000011181 /*
11182 * Before creating the window layout, try loading one file. If this
11183 * is aborted we don't end up with a number of useless windows.
11184 * This may have side effects! (e.g., compressed or network file).
11185 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011186 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011187 {
11188 if (ses_do_win(wp)
11189 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011190 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011191#ifdef FEAT_QUICKFIX
11192 && !bt_nofile(wp->w_buffer)
11193#endif
11194 )
11195 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011196 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011197 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11198 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011199 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011200 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011201 if (!wp->w_arg_idx_invalid)
11202 edited_win = wp;
11203 break;
11204 }
11205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011207 /* If no file got edited create an empty tab page. */
11208 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11209 return FAIL;
11210
Bram Moolenaar18144c82006-04-12 21:52:12 +000011211 /*
11212 * Save current window layout.
11213 */
11214 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011216 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011218 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11219 return FAIL;
11220 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11221 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222
Bram Moolenaar18144c82006-04-12 21:52:12 +000011223 /*
11224 * Check if window sizes can be restored (no windows omitted).
11225 * Remember the window number of the current window after restoring.
11226 */
11227 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011228 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011229 {
11230 if (ses_do_win(wp))
11231 ++nr;
11232 else
11233 restore_size = FALSE;
11234 if (curwin == wp)
11235 cnr = nr;
11236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237
Bram Moolenaar18144c82006-04-12 21:52:12 +000011238 /* Go to the first window. */
11239 if (put_line(fd, "wincmd t") == FAIL)
11240 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011241
Bram Moolenaar18144c82006-04-12 21:52:12 +000011242 /*
11243 * If more than one window, see if sizes can be restored.
11244 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11245 * resized when moving between windows.
11246 * Do this before restoring the view, so that the topline and the
11247 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011248 * winminheight and winminwidth need to be set to avoid an error if the
11249 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011250 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011251 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011252 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011253 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255
Bram Moolenaar18144c82006-04-12 21:52:12 +000011256 /*
11257 * Restore the view of the window (options, file, cursor, etc.).
11258 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011259 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011260 {
11261 if (!ses_do_win(wp))
11262 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011263 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11264 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011265 return FAIL;
11266 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11267 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011268 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011269 }
11270
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011271 /* The argument index in the first tab page is zero, need to set it in
11272 * each window. For further tab pages it's the window where we do
11273 * "tabedit". */
11274 cur_arg_idx = next_arg_idx;
11275
Bram Moolenaar18144c82006-04-12 21:52:12 +000011276 /*
11277 * Restore cursor to the current window if it's not the first one.
11278 */
11279 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11280 || put_eol(fd) == FAIL))
11281 return FAIL;
11282
11283 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011284 * Restore window sizes again after jumping around in windows, because
11285 * the current window has a minimum size while others may not.
11286 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011287 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011288 return FAIL;
11289
Bram Moolenaar18144c82006-04-12 21:52:12 +000011290 /* Don't continue in another tab page when doing only the current one
11291 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011292 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011293 break;
11294 }
11295
11296 if (ssop_flags & SSOP_TABPAGES)
11297 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011298 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11299 || put_eol(fd) == FAIL)
11300 return FAIL;
11301 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011302 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11303 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011304
Bram Moolenaar9c102382006-05-03 21:26:49 +000011305 /*
11306 * Wipe out an empty unnamed buffer we started in.
11307 */
11308 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11309 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011310 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011311 return FAIL;
11312 if (put_line(fd, "endif") == FAIL)
11313 return FAIL;
11314 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11315 return FAIL;
11316
11317 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11318 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11319 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11320 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011321 /* Re-apply 'winminheight' and 'winminwidth'. */
11322 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11323 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325
11326 /*
11327 * Lastly, execute the x.vim file if it exists.
11328 */
11329 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11330 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011331 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 || put_line(fd, "endif") == FAIL)
11333 return FAIL;
11334
11335 return OK;
11336}
11337
11338 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011339ses_winsizes(
11340 FILE *fd,
11341 int restore_size,
11342 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343{
11344 int n = 0;
11345 win_T *wp;
11346
11347 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11348 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011349 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 {
11351 if (!ses_do_win(wp))
11352 continue;
11353 ++n;
11354
11355 /* restore height when not full height */
11356 if (wp->w_height + wp->w_status_height < topframe->fr_height
11357 && (fprintf(fd,
11358 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11359 n, (long)wp->w_height, Rows / 2, Rows) < 0
11360 || put_eol(fd) == FAIL))
11361 return FAIL;
11362
11363 /* restore width when not full width */
11364 if (wp->w_width < Columns && (fprintf(fd,
11365 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11366 n, (long)wp->w_width, Columns / 2, Columns) < 0
11367 || put_eol(fd) == FAIL))
11368 return FAIL;
11369 }
11370 }
11371 else
11372 {
11373 /* Just equalise window sizes */
11374 if (put_line(fd, "wincmd =") == FAIL)
11375 return FAIL;
11376 }
11377 return OK;
11378}
11379
11380/*
11381 * Write commands to "fd" to recursively create windows for frame "fr",
11382 * horizontally and vertically split.
11383 * After the commands the last window in the frame is the current window.
11384 * Returns FAIL when writing the commands to "fd" fails.
11385 */
11386 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011387ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388{
11389 frame_T *frc;
11390 int count = 0;
11391
11392 if (fr->fr_layout != FR_LEAF)
11393 {
11394 /* Find first frame that's not skipped and then create a window for
11395 * each following one (first frame is already there). */
11396 frc = ses_skipframe(fr->fr_child);
11397 if (frc != NULL)
11398 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11399 {
11400 /* Make window as big as possible so that we have lots of room
11401 * to split. */
11402 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11403 || put_line(fd, fr->fr_layout == FR_COL
11404 ? "split" : "vsplit") == FAIL)
11405 return FAIL;
11406 ++count;
11407 }
11408
11409 /* Go back to the first window. */
11410 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11411 ? "%dwincmd k" : "%dwincmd h", count) < 0
11412 || put_eol(fd) == FAIL))
11413 return FAIL;
11414
11415 /* Recursively create frames/windows in each window of this column or
11416 * row. */
11417 frc = ses_skipframe(fr->fr_child);
11418 while (frc != NULL)
11419 {
11420 ses_win_rec(fd, frc);
11421 frc = ses_skipframe(frc->fr_next);
11422 /* Go to next window. */
11423 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11424 return FAIL;
11425 }
11426 }
11427 return OK;
11428}
11429
11430/*
11431 * Skip frames that don't contain windows we want to save in the Session.
11432 * Returns NULL when there none.
11433 */
11434 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011435ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436{
11437 frame_T *frc;
11438
11439 for (frc = fr; frc != NULL; frc = frc->fr_next)
11440 if (ses_do_frame(frc))
11441 break;
11442 return frc;
11443}
11444
11445/*
11446 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11447 * the Session.
11448 */
11449 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011450ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451{
11452 frame_T *frc;
11453
11454 if (fr->fr_layout == FR_LEAF)
11455 return ses_do_win(fr->fr_win);
11456 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11457 if (ses_do_frame(frc))
11458 return TRUE;
11459 return FALSE;
11460}
11461
11462/*
11463 * Return non-zero if window "wp" is to be stored in the Session.
11464 */
11465 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011466ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467{
11468 if (wp->w_buffer->b_fname == NULL
11469#ifdef FEAT_QUICKFIX
11470 /* When 'buftype' is "nofile" can't restore the window contents. */
11471 || bt_nofile(wp->w_buffer)
11472#endif
11473 )
11474 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011475 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 return (ssop_flags & SSOP_HELP);
11477 return TRUE;
11478}
11479
11480/*
11481 * Write commands to "fd" to restore the view of a window.
11482 * Caller must make sure 'scrolloff' is zero.
11483 */
11484 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011485put_view(
11486 FILE *fd,
11487 win_T *wp,
11488 int add_edit, /* add ":edit" command to view */
11489 unsigned *flagp, /* vop_flags or ssop_flags */
11490 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011491 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492{
11493 win_T *save_curwin;
11494 int f;
11495 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011496 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497
11498 /* Always restore cursor position for ":mksession". For ":mkview" only
11499 * when 'viewoptions' contains "cursor". */
11500 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11501
11502 /*
11503 * Local argument list.
11504 */
11505 if (wp->w_alist == &global_alist)
11506 {
11507 if (put_line(fd, "argglobal") == FAIL)
11508 return FAIL;
11509 }
11510 else
11511 {
11512 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11513 flagp == &vop_flags
11514 || !(*flagp & SSOP_CURDIR)
11515 || wp->w_localdir != NULL, flagp) == FAIL)
11516 return FAIL;
11517 }
11518
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011519 /* Only when part of a session: restore the argument index. Some
11520 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011521 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011522 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011524 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 || put_eol(fd) == FAIL)
11526 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011527 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 }
11529
11530 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011531 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 {
11533 /*
11534 * Load the file.
11535 */
11536 if (wp->w_buffer->b_ffname != NULL
11537#ifdef FEAT_QUICKFIX
11538 && !bt_nofile(wp->w_buffer)
11539#endif
11540 )
11541 {
11542 /*
11543 * Editing a file in this buffer: use ":edit file".
11544 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011545 *
11546 * Note, if a buffer for that file already exists, use :badd to
11547 * edit that buffer, to not lose folding information (:edit resets
11548 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011550 if (fputs("if bufexists('", fd) < 0
11551 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11552 || fputs("') | buffer ", fd) < 0
11553 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11554 || fputs(" | else | edit ", fd) < 0
11555 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11556 || fputs(" | endif", fd) < 0
11557 ||
11558 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 return FAIL;
11560 }
11561 else
11562 {
11563 /* No file in this buffer, just make it empty. */
11564 if (put_line(fd, "enew") == FAIL)
11565 return FAIL;
11566#ifdef FEAT_QUICKFIX
11567 if (wp->w_buffer->b_ffname != NULL)
11568 {
11569 /* The buffer does have a name, but it's not a file name. */
11570 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011571 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 return FAIL;
11573 }
11574#endif
11575 do_cursor = FALSE;
11576 }
11577 }
11578
11579 /*
11580 * Local mappings and abbreviations.
11581 */
11582 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11583 && makemap(fd, wp->w_buffer) == FAIL)
11584 return FAIL;
11585
11586 /*
11587 * Local options. Need to go to the window temporarily.
11588 * Store only local values when using ":mkview" and when ":mksession" is
11589 * used and 'sessionoptions' doesn't include "options".
11590 * Some folding options are always stored when "folds" is included,
11591 * otherwise the folds would not be restored correctly.
11592 */
11593 save_curwin = curwin;
11594 curwin = wp;
11595 curbuf = curwin->w_buffer;
11596 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11597 f = makeset(fd, OPT_LOCAL,
11598 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11599#ifdef FEAT_FOLDING
11600 else if (*flagp & SSOP_FOLDS)
11601 f = makefoldset(fd);
11602#endif
11603 else
11604 f = OK;
11605 curwin = save_curwin;
11606 curbuf = curwin->w_buffer;
11607 if (f == FAIL)
11608 return FAIL;
11609
11610#ifdef FEAT_FOLDING
11611 /*
11612 * Save Folds when 'buftype' is empty and for help files.
11613 */
11614 if ((*flagp & SSOP_FOLDS)
11615 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011616 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 {
11618 if (put_folds(fd, wp) == FAIL)
11619 return FAIL;
11620 }
11621#endif
11622
11623 /*
11624 * Set the cursor after creating folds, since that moves the cursor.
11625 */
11626 if (do_cursor)
11627 {
11628
11629 /* Restore the cursor line in the file and relatively in the
11630 * window. Don't use "G", it changes the jumplist. */
11631 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11632 (long)wp->w_cursor.lnum,
11633 (long)(wp->w_cursor.lnum - wp->w_topline),
11634 (long)wp->w_height / 2, (long)wp->w_height) < 0
11635 || put_eol(fd) == FAIL
11636 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11637 || put_line(fd, "exe s:l") == FAIL
11638 || put_line(fd, "normal! zt") == FAIL
11639 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11640 || put_eol(fd) == FAIL)
11641 return FAIL;
11642 /* Restore the cursor column and left offset when not wrapping. */
11643 if (wp->w_cursor.col == 0)
11644 {
11645 if (put_line(fd, "normal! 0") == FAIL)
11646 return FAIL;
11647 }
11648 else
11649 {
11650 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11651 {
11652 if (fprintf(fd,
11653 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011654 (long)wp->w_virtcol + 1,
11655 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 (long)wp->w_width / 2, (long)wp->w_width) < 0
11657 || put_eol(fd) == FAIL
11658 || put_line(fd, "if s:c > 0") == FAIL
11659 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011660 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11661 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 || put_eol(fd) == FAIL
11663 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011664 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 || put_eol(fd) == FAIL
11666 || put_line(fd, "endif") == FAIL)
11667 return FAIL;
11668 }
11669 else
11670 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011671 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 || put_eol(fd) == FAIL)
11673 return FAIL;
11674 }
11675 }
11676 }
11677
11678 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011679 * Local directory, if the current flag is not view options or the "curdir"
11680 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011682 if (wp->w_localdir != NULL
11683 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 {
11685 if (fputs("lcd ", fd) < 0
11686 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11687 || put_eol(fd) == FAIL)
11688 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011689 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 }
11691
11692 return OK;
11693}
11694
11695/*
11696 * Write an argument list to the session file.
11697 * Returns FAIL if writing fails.
11698 */
11699 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011700ses_arglist(
11701 FILE *fd,
11702 char *cmd,
11703 garray_T *gap,
11704 int fullname, /* TRUE: use full path name */
11705 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706{
11707 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011708 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 char_u *s;
11710
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011711 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11712 return FAIL;
11713 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 return FAIL;
11715 for (i = 0; i < gap->ga_len; ++i)
11716 {
11717 /* NULL file names are skipped (only happens when out of memory). */
11718 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11719 if (s != NULL)
11720 {
11721 if (fullname)
11722 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011723 buf = alloc(MAXPATHL);
11724 if (buf != NULL)
11725 {
11726 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11727 s = buf;
11728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011730 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011731 || ses_put_fname(fd, s, flagp) == FAIL
11732 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011733 {
11734 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011736 }
11737 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 }
11739 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011740 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741}
11742
11743/*
11744 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011745 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 * Returns FAIL if writing fails.
11747 */
11748 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011749ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750{
11751 char_u *name;
11752
11753 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011754 * the session file will be sourced.
11755 * Don't do this for ":mkview", we don't know the current directory.
11756 * Don't do this after ":lcd", we don't keep track of what the current
11757 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 if (buf->b_sfname != NULL
11759 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011760 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011761#ifdef FEAT_AUTOCHDIR
11762 && !p_acd
11763#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011764 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 name = buf->b_sfname;
11766 else
11767 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011768 if (ses_put_fname(fd, name, flagp) == FAIL
11769 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 return FAIL;
11771 return OK;
11772}
11773
11774/*
11775 * Write a file name to the session file.
11776 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11777 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011778 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 */
11780 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011781ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782{
11783 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011784 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786
11787 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011788 if (sname == NULL)
11789 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011791 if (*flagp & SSOP_SLASH)
11792 {
11793 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011794 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011795 if (*p == '\\')
11796 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011798
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011799 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011800 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011802 if (p == NULL)
11803 return FAIL;
11804
11805 /* write the result */
11806 if (fputs((char *)p, fd) < 0)
11807 retval = FAIL;
11808
11809 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 return retval;
11811}
11812
11813/*
11814 * ":loadview [nr]"
11815 */
11816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011817ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818{
11819 char_u *fname;
11820
11821 fname = get_view_file(*eap->arg);
11822 if (fname != NULL)
11823 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011824 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 vim_free(fname);
11826 }
11827}
11828
11829/*
11830 * Get the name of the view file for the current buffer.
11831 */
11832 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011833get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834{
11835 int len = 0;
11836 char_u *p, *s;
11837 char_u *retval;
11838 char_u *sname;
11839
11840 if (curbuf->b_ffname == NULL)
11841 {
11842 EMSG(_(e_noname));
11843 return NULL;
11844 }
11845 sname = home_replace_save(NULL, curbuf->b_ffname);
11846 if (sname == NULL)
11847 return NULL;
11848
11849 /*
11850 * We want a file name without separators, because we're not going to make
11851 * a directory.
11852 * "normal" path separator -> "=+"
11853 * "=" -> "=="
11854 * ":" path separator -> "=-"
11855 */
11856 for (p = sname; *p; ++p)
11857 if (*p == '=' || vim_ispathsep(*p))
11858 ++len;
11859 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11860 if (retval != NULL)
11861 {
11862 STRCPY(retval, p_vdir);
11863 add_pathsep(retval);
11864 s = retval + STRLEN(retval);
11865 for (p = sname; *p; ++p)
11866 {
11867 if (*p == '=')
11868 {
11869 *s++ = '=';
11870 *s++ = '=';
11871 }
11872 else if (vim_ispathsep(*p))
11873 {
11874 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011875#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 if (*p == ':')
11877 *s++ = '-';
11878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011880 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 }
11882 else
11883 *s++ = *p;
11884 }
11885 *s++ = '=';
11886 *s++ = c;
11887 STRCPY(s, ".vim");
11888 }
11889
11890 vim_free(sname);
11891 return retval;
11892}
11893
11894#endif /* FEAT_SESSION */
11895
11896/*
11897 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11898 * Return FAIL for a write error.
11899 */
11900 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011901put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902{
11903 if (
11904#ifdef USE_CRNL
11905 (
11906# ifdef MKSESSION_NL
11907 !mksession_nl &&
11908# endif
11909 (putc('\r', fd) < 0)) ||
11910#endif
11911 (putc('\n', fd) < 0))
11912 return FAIL;
11913 return OK;
11914}
11915
11916/*
11917 * Write a line to "fd".
11918 * Return FAIL for a write error.
11919 */
11920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011921put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922{
11923 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11924 return FAIL;
11925 return OK;
11926}
11927
11928#ifdef FEAT_VIMINFO
11929/*
11930 * ":rviminfo" and ":wviminfo".
11931 */
11932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011933ex_viminfo(
11934 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935{
11936 char_u *save_viminfo;
11937
11938 save_viminfo = p_viminfo;
11939 if (*p_viminfo == NUL)
11940 p_viminfo = (char_u *)"'100";
11941 if (eap->cmdidx == CMD_rviminfo)
11942 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011943 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11944 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 EMSG(_("E195: Cannot open viminfo file for reading"));
11946 }
11947 else
11948 write_viminfo(eap->arg, eap->forceit);
11949 p_viminfo = save_viminfo;
11950}
11951#endif
11952
11953#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011954/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011955 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011956 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011959dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 if (fname == NULL)
11962 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011963 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964}
11965#endif
11966
11967/*
11968 * ":behave {mswin,xterm}"
11969 */
11970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011971ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972{
11973 if (STRCMP(eap->arg, "mswin") == 0)
11974 {
11975 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11976 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11977 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11978 set_option_value((char_u *)"keymodel", 0L,
11979 (char_u *)"startsel,stopsel", 0);
11980 }
11981 else if (STRCMP(eap->arg, "xterm") == 0)
11982 {
11983 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11984 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11985 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11986 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11987 }
11988 else
11989 EMSG2(_(e_invarg2), eap->arg);
11990}
11991
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011992#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11993/*
11994 * Function given to ExpandGeneric() to obtain the possible arguments of the
11995 * ":behave {mswin,xterm}" command.
11996 */
11997 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011998get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011999{
12000 if (idx == 0)
12001 return (char_u *)"mswin";
12002 if (idx == 1)
12003 return (char_u *)"xterm";
12004 return NULL;
12005}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012006
12007/*
12008 * Function given to ExpandGeneric() to obtain the possible arguments of the
12009 * ":messages {clear}" command.
12010 */
12011 char_u *
12012get_messages_arg(expand_T *xp UNUSED, int idx)
12013{
12014 if (idx == 0)
12015 return (char_u *)"clear";
12016 return NULL;
12017}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012018#endif
12019
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012020 char_u *
12021get_mapclear_arg(expand_T *xp UNUSED, int idx)
12022{
12023 if (idx == 0)
12024 return (char_u *)"<buffer>";
12025 return NULL;
12026}
12027
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028static int filetype_detect = FALSE;
12029static int filetype_plugin = FALSE;
12030static int filetype_indent = FALSE;
12031
12032/*
12033 * ":filetype [plugin] [indent] {on,off,detect}"
12034 * on: Load the filetype.vim file to install autocommands for file types.
12035 * off: Load the ftoff.vim file to remove all autocommands for file types.
12036 * plugin on: load filetype.vim and ftplugin.vim
12037 * plugin off: load ftplugof.vim
12038 * indent on: load filetype.vim and indent.vim
12039 * indent off: load indoff.vim
12040 */
12041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012042ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043{
12044 char_u *arg = eap->arg;
12045 int plugin = FALSE;
12046 int indent = FALSE;
12047
12048 if (*eap->arg == NUL)
12049 {
12050 /* Print current status. */
12051 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12052 filetype_detect ? "ON" : "OFF",
12053 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12054 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12055 return;
12056 }
12057
12058 /* Accept "plugin" and "indent" in any order. */
12059 for (;;)
12060 {
12061 if (STRNCMP(arg, "plugin", 6) == 0)
12062 {
12063 plugin = TRUE;
12064 arg = skipwhite(arg + 6);
12065 continue;
12066 }
12067 if (STRNCMP(arg, "indent", 6) == 0)
12068 {
12069 indent = TRUE;
12070 arg = skipwhite(arg + 6);
12071 continue;
12072 }
12073 break;
12074 }
12075 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12076 {
12077 if (*arg == 'o' || !filetype_detect)
12078 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012079 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 filetype_detect = TRUE;
12081 if (plugin)
12082 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012083 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 filetype_plugin = TRUE;
12085 }
12086 if (indent)
12087 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012088 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 filetype_indent = TRUE;
12090 }
12091 }
12092 if (*arg == 'd')
12093 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012094 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012095 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 }
12097 }
12098 else if (STRCMP(arg, "off") == 0)
12099 {
12100 if (plugin || indent)
12101 {
12102 if (plugin)
12103 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012104 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 filetype_plugin = FALSE;
12106 }
12107 if (indent)
12108 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012109 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 filetype_indent = FALSE;
12111 }
12112 }
12113 else
12114 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012115 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 filetype_detect = FALSE;
12117 }
12118 }
12119 else
12120 EMSG2(_(e_invarg2), arg);
12121}
12122
12123/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012124 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 */
12126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012127ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128{
12129 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012130 {
12131 char_u *arg = eap->arg;
12132
12133 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12134 arg += 9;
12135
12136 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12137 if (arg != eap->arg)
12138 did_filetype = FALSE;
12139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012143ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144{
12145#ifdef FEAT_DIGRAPHS
12146 if (*eap->arg != NUL)
12147 putdigraph(eap->arg);
12148 else
12149 listdigraphs();
12150#else
12151 EMSG(_("E196: No digraphs in this version"));
12152#endif
12153}
12154
12155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012156ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157{
12158 int flags = 0;
12159
12160 if (eap->cmdidx == CMD_setlocal)
12161 flags = OPT_LOCAL;
12162 else if (eap->cmdidx == CMD_setglobal)
12163 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012164#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 if (cmdmod.browse && flags == 0)
12166 ex_options(eap);
12167 else
12168#endif
12169 (void)do_set(eap->arg, flags);
12170}
12171
12172#ifdef FEAT_SEARCH_EXTRA
12173/*
12174 * ":nohlsearch"
12175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012177ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012179 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012180 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181}
12182
12183/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012184 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 * Sets nextcmd to the start of the next command, if any. Also called when
12186 * skipping commands to find the next command.
12187 */
12188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012189ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
12191 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012192 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 char_u *end;
12194 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012195 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012196
12197 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012198 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012199 else
12200 {
12201 EMSG(e_invcmd);
12202 return;
12203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204
12205 /* First clear any old pattern. */
12206 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012207 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208
12209 if (ends_excmd(*eap->arg))
12210 end = eap->arg;
12211 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012212 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 end = eap->arg + 4;
12214 else
12215 {
12216 p = skiptowhite(eap->arg);
12217 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012218 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 p = skipwhite(p);
12220 if (*p == NUL)
12221 {
12222 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012223 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 EMSG2(_(e_invarg2), eap->arg);
12225 return;
12226 }
12227 end = skip_regexp(p + 1, *p, TRUE, NULL);
12228 if (!eap->skip)
12229 {
12230 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12231 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012232 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 eap->errmsg = e_trailing;
12234 return;
12235 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012236 if (*end != *p)
12237 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012238 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012239 EMSG2(_(e_invarg2), p);
12240 return;
12241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242
12243 c = *end;
12244 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012245 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012246 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012247 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 }
12249 }
12250 eap->nextcmd = find_nextcmd(end);
12251}
12252#endif
12253
12254#ifdef FEAT_CRYPT
12255/*
12256 * ":X": Get crypt key
12257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012259ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012261 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012262 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263}
12264#endif
12265
12266#ifdef FEAT_FOLDING
12267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012268ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269{
12270 if (foldManualAllowed(TRUE))
12271 foldCreate(eap->line1, eap->line2);
12272}
12273
12274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012275ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276{
12277 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12278 eap->forceit, FALSE);
12279}
12280
12281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012282ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283{
12284 linenr_T lnum;
12285
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012286#ifdef FEAT_CLIPBOARD
12287 start_global_changes();
12288#endif
12289
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 /* First set the marks for all lines closed/open. */
12291 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12292 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12293 ml_setmarked(lnum);
12294
12295 /* Execute the command on the marked lines. */
12296 global_exe(eap->arg);
12297 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012298#ifdef FEAT_CLIPBOARD
12299 end_global_changes();
12300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301}
12302#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012303
12304# if defined(FEAT_TIMERS) || defined(PROTO)
12305 int
12306get_pressedreturn(void)
12307{
12308 return ex_pressedreturn;
12309}
12310
12311 void
12312set_pressedreturn(int val)
12313{
12314 ex_pressedreturn = val;
12315}
12316#endif