blob: c2d69670b8eeac73228b0801eecc31bc7b3505d9 [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 Moolenaarcd43eff2018-03-29 15:55:38 +02004296 case CMD_argdelete:
4297 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4298 arg = xp->xp_pattern + 1;
4299 xp->xp_context = EXPAND_ARGLIST;
4300 xp->xp_pattern = arg;
4301 break;
4302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#endif /* FEAT_CMDL_COMPL */
4304
4305 default:
4306 break;
4307 }
4308 return NULL;
4309}
4310
4311/*
4312 * skip a range specifier of the form: addr [,addr] [;addr] ..
4313 *
4314 * Backslashed delimiters after / or ? will be skipped, and commands will
4315 * not be expanded between /'s and ?'s or after "'".
4316 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004317 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 * Returns the "cmd" pointer advanced to beyond the range.
4319 */
4320 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004321skip_range(
4322 char_u *cmd,
4323 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004325 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004327 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004329 if (*cmd == '\\')
4330 {
4331 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4332 ++cmd;
4333 else
4334 break;
4335 }
4336 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
4338 if (*++cmd == NUL && ctx != NULL)
4339 *ctx = EXPAND_NOTHING;
4340 }
4341 else if (*cmd == '/' || *cmd == '?')
4342 {
4343 delim = *cmd++;
4344 while (*cmd != NUL && *cmd != delim)
4345 if (*cmd++ == '\\' && *cmd != NUL)
4346 ++cmd;
4347 if (*cmd == NUL && ctx != NULL)
4348 *ctx = EXPAND_NOTHING;
4349 }
4350 if (*cmd != NUL)
4351 ++cmd;
4352 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004353
4354 /* Skip ":" and white space. */
4355 while (*cmd == ':')
4356 cmd = skipwhite(cmd + 1);
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 return cmd;
4359}
4360
4361/*
4362 * get a single EX address
4363 *
4364 * Set ptr to the next character after the part that was interpreted.
4365 * Set ptr to NULL when an error is encountered.
4366 *
4367 * Return MAXLNUM when no Ex address was found.
4368 */
4369 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004370get_address(
4371 exarg_T *eap UNUSED,
4372 char_u **ptr,
4373 int addr_type, /* flag: one of ADDR_LINES, ... */
4374 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004375 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004376 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
4378 int c;
4379 int i;
4380 long n;
4381 char_u *cmd;
4382 pos_T pos;
4383 pos_T *fp;
4384 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004385 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 cmd = skipwhite(*ptr);
4388 lnum = MAXLNUM;
4389 do
4390 {
4391 switch (*cmd)
4392 {
4393 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 ++cmd;
4395 switch (addr_type)
4396 {
4397 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 lnum = curwin->w_cursor.lnum;
4399 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004401 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 break;
4403 case ADDR_ARGUMENTS:
4404 lnum = curwin->w_arg_idx + 1;
4405 break;
4406 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004407 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 lnum = curbuf->b_fnum;
4409 break;
4410 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004411 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004413 case ADDR_TABS_RELATIVE:
4414 EMSG(_(e_invrange));
4415 cmd = NULL;
4416 goto error;
4417 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004418#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004419 case ADDR_QUICKFIX:
4420 lnum = qf_get_cur_valid_idx(eap);
4421 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004422#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 }
4424 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 ++cmd;
4428 switch (addr_type)
4429 {
4430 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 lnum = curbuf->b_ml.ml_line_count;
4432 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004434 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 break;
4436 case ADDR_ARGUMENTS:
4437 lnum = ARGCOUNT;
4438 break;
4439 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004440 buf = lastbuf;
4441 while (buf->b_ml.ml_mfp == NULL)
4442 {
4443 if (buf->b_prev == NULL)
4444 break;
4445 buf = buf->b_prev;
4446 }
4447 lnum = buf->b_fnum;
4448 break;
4449 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 lnum = lastbuf->b_fnum;
4451 break;
4452 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004453 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004455 case ADDR_TABS_RELATIVE:
4456 EMSG(_(e_invrange));
4457 cmd = NULL;
4458 goto error;
4459 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004460#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004461 case ADDR_QUICKFIX:
4462 lnum = qf_get_size(eap);
4463 if (lnum == 0)
4464 lnum = 1;
4465 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004466#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 }
4468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 if (*++cmd == NUL)
4472 {
4473 cmd = NULL;
4474 goto error;
4475 }
4476 if (addr_type != ADDR_LINES)
4477 {
4478 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004479 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 goto error;
4481 }
4482 if (skip)
4483 ++cmd;
4484 else
4485 {
4486 /* Only accept a mark in another file when it is
4487 * used by itself: ":'M". */
4488 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4489 ++cmd;
4490 if (fp == (pos_T *)-1)
4491 /* Jumped to another file. */
4492 lnum = curwin->w_cursor.lnum;
4493 else
4494 {
4495 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
4497 cmd = NULL;
4498 goto error;
4499 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 lnum = fp->lnum;
4501 }
4502 }
4503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505 case '/':
4506 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004507 c = *cmd++;
4508 if (addr_type != ADDR_LINES)
4509 {
4510 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004511 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 goto error;
4513 }
4514 if (skip) /* skip "/pat/" */
4515 {
4516 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4517 if (*cmd == c)
4518 ++cmd;
4519 }
4520 else
4521 {
4522 pos = curwin->w_cursor; /* save curwin->w_cursor */
4523 /*
4524 * When '/' or '?' follows another address, start
4525 * from there.
4526 */
4527 if (lnum != MAXLNUM)
4528 curwin->w_cursor.lnum = lnum;
4529 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004530 * Start a forward search at the end of the line (unless
4531 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 * Start a backward search at the start of the line.
4533 * This makes sure we never match in the current
4534 * line, and can match anywhere in the
4535 * next/previous line.
4536 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004537 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 curwin->w_cursor.col = MAXCOL;
4539 else
4540 curwin->w_cursor.col = 0;
4541 searchcmdlen = 0;
4542 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004543 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 {
4545 curwin->w_cursor = pos;
4546 cmd = NULL;
4547 goto error;
4548 }
4549 lnum = curwin->w_cursor.lnum;
4550 curwin->w_cursor = pos;
4551 /* adjust command string pointer */
4552 cmd += searchcmdlen;
4553 }
4554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 ++cmd;
4558 if (addr_type != ADDR_LINES)
4559 {
4560 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004561 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004562 goto error;
4563 }
4564 if (*cmd == '&')
4565 i = RE_SUBST;
4566 else if (*cmd == '?' || *cmd == '/')
4567 i = RE_SEARCH;
4568 else
4569 {
4570 EMSG(_(e_backslash));
4571 cmd = NULL;
4572 goto error;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004575 if (!skip)
4576 {
4577 /*
4578 * When search follows another address, start from
4579 * there.
4580 */
4581 if (lnum != MAXLNUM)
4582 pos.lnum = lnum;
4583 else
4584 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004586 /*
4587 * Start the search just like for the above
4588 * do_search().
4589 */
4590 if (*cmd != '?')
4591 pos.col = MAXCOL;
4592 else
4593 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004594#ifdef FEAT_VIRTUALEDIT
4595 pos.coladd = 0;
4596#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 if (searchit(curwin, curbuf, &pos,
4598 *cmd == '?' ? BACKWARD : FORWARD,
4599 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004600 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004601 lnum = pos.lnum;
4602 else
4603 {
4604 cmd = NULL;
4605 goto error;
4606 }
4607 }
4608 ++cmd;
4609 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004612 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4613 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615
4616 for (;;)
4617 {
4618 cmd = skipwhite(cmd);
4619 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4620 break;
4621
4622 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 {
4624 switch (addr_type)
4625 {
4626 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004627 /* "+1" is same as ".+1" */
4628 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 break;
4630 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004631 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004632 break;
4633 case ADDR_ARGUMENTS:
4634 lnum = curwin->w_arg_idx + 1;
4635 break;
4636 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004637 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 lnum = curbuf->b_fnum;
4639 break;
4640 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004641 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004642 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004643 case ADDR_TABS_RELATIVE:
4644 lnum = 1;
4645 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004646#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004647 case ADDR_QUICKFIX:
4648 lnum = qf_get_cur_valid_idx(eap);
4649 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004650#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004651 }
4652 }
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (VIM_ISDIGIT(*cmd))
4655 i = '+'; /* "number" is same as "+number" */
4656 else
4657 i = *cmd++;
4658 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4659 n = 1;
4660 else
4661 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004662
4663 if (addr_type == ADDR_TABS_RELATIVE)
4664 {
4665 EMSG(_(e_invrange));
4666 cmd = NULL;
4667 goto error;
4668 }
4669 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004670 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004671 lnum = compute_buffer_local_count(
4672 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004674 {
4675#ifdef FEAT_FOLDING
4676 /* Relative line addressing, need to adjust for folded lines
4677 * now, but only do it after the first address. */
4678 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4679 && address_count >= 2)
4680 (void)hasFolding(lnum, NULL, &lnum);
4681#endif
4682 if (i == '-')
4683 lnum -= n;
4684 else
4685 lnum += n;
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688 } while (*cmd == '/' || *cmd == '?');
4689
4690error:
4691 *ptr = cmd;
4692 return lnum;
4693}
4694
4695/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004696 * Get flags from an Ex command argument.
4697 */
4698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004699get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004700{
4701 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4702 {
4703 if (*eap->arg == 'l')
4704 eap->flags |= EXFLAG_LIST;
4705 else if (*eap->arg == 'p')
4706 eap->flags |= EXFLAG_PRINT;
4707 else
4708 eap->flags |= EXFLAG_NR;
4709 eap->arg = skipwhite(eap->arg + 1);
4710 }
4711}
4712
4713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 * Function called for command which is Not Implemented. NI!
4715 */
4716 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004717ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
4719 if (!eap->skip)
4720 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4721}
4722
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004723#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724/*
4725 * Function called for script command which is Not Implemented. NI!
4726 * Skips over ":perl <<EOF" constructs.
4727 */
4728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004729ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 if (!eap->skip)
4732 ex_ni(eap);
4733 else
4734 vim_free(script_get(eap, eap->arg));
4735}
4736#endif
4737
4738/*
4739 * Check range in Ex command for validity.
4740 * Return NULL when valid, error message when invalid.
4741 */
4742 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004743invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004745 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if ( eap->line1 < 0
4747 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004748 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004750
4751 if (eap->argt & RANGE)
4752 {
4753 switch(eap->addr_type)
4754 {
4755 case ADDR_LINES:
4756 if (!(eap->argt & NOTADR)
4757 && eap->line2 > curbuf->b_ml.ml_line_count
4758#ifdef FEAT_DIFF
4759 + (eap->cmdidx == CMD_diffget)
4760#endif
4761 )
4762 return (char_u *)_(e_invrange);
4763 break;
4764 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004765 /* add 1 if ARGCOUNT is 0 */
4766 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004767 return (char_u *)_(e_invrange);
4768 break;
4769 case ADDR_BUFFERS:
4770 if (eap->line1 < firstbuf->b_fnum
4771 || eap->line2 > lastbuf->b_fnum)
4772 return (char_u *)_(e_invrange);
4773 break;
4774 case ADDR_LOADED_BUFFERS:
4775 buf = firstbuf;
4776 while (buf->b_ml.ml_mfp == NULL)
4777 {
4778 if (buf->b_next == NULL)
4779 return (char_u *)_(e_invrange);
4780 buf = buf->b_next;
4781 }
4782 if (eap->line1 < buf->b_fnum)
4783 return (char_u *)_(e_invrange);
4784 buf = lastbuf;
4785 while (buf->b_ml.ml_mfp == NULL)
4786 {
4787 if (buf->b_prev == NULL)
4788 return (char_u *)_(e_invrange);
4789 buf = buf->b_prev;
4790 }
4791 if (eap->line2 > buf->b_fnum)
4792 return (char_u *)_(e_invrange);
4793 break;
4794 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004795 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004796 return (char_u *)_(e_invrange);
4797 break;
4798 case ADDR_TABS:
4799 if (eap->line2 > LAST_TAB_NR)
4800 return (char_u *)_(e_invrange);
4801 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004802 case ADDR_TABS_RELATIVE:
4803 /* Do nothing */
4804 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004805#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004806 case ADDR_QUICKFIX:
4807 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4808 return (char_u *)_(e_invrange);
4809 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004810#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004811 }
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return NULL;
4814}
4815
4816/*
4817 * Correct the range for zero line number, if required.
4818 */
4819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004820correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
4822 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4823 {
4824 if (eap->line1 == 0)
4825 eap->line1 = 1;
4826 if (eap->line2 == 0)
4827 eap->line2 = 1;
4828 }
4829}
4830
Bram Moolenaar748bf032005-02-02 23:04:36 +00004831#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004832static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004833
4834/*
4835 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4836 * pattern. Otherwise return eap->arg.
4837 */
4838 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004839skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004840{
4841 char_u *p = eap->arg;
4842
Bram Moolenaara37420f2006-02-04 22:37:47 +00004843 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4844 || eap->cmdidx == CMD_vimgrepadd
4845 || eap->cmdidx == CMD_lvimgrepadd
4846 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004847 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004848 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004849 if (p == NULL)
4850 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004851 }
4852 return p;
4853}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004854
4855/*
4856 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4857 * in the command line, so that things like % get expanded.
4858 */
4859 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004860replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004861{
4862 char_u *new_cmdline;
4863 char_u *program;
4864 char_u *pos;
4865 char_u *ptr;
4866 int len;
4867 int i;
4868
4869 /*
4870 * Don't do it when ":vimgrep" is used for ":grep".
4871 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004872 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4873 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4874 || eap->cmdidx == CMD_grepadd
4875 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004876 && !grep_internal(eap->cmdidx))
4877 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004878 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4879 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004880 {
4881 if (*curbuf->b_p_gp == NUL)
4882 program = p_gp;
4883 else
4884 program = curbuf->b_p_gp;
4885 }
4886 else
4887 {
4888 if (*curbuf->b_p_mp == NUL)
4889 program = p_mp;
4890 else
4891 program = curbuf->b_p_mp;
4892 }
4893
4894 p = skipwhite(p);
4895
4896 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4897 {
4898 /* replace $* by given arguments */
4899 i = 1;
4900 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4901 ++i;
4902 len = (int)STRLEN(p);
4903 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4904 if (new_cmdline == NULL)
4905 return NULL; /* out of memory */
4906 ptr = new_cmdline;
4907 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4908 {
4909 i = (int)(pos - program);
4910 STRNCPY(ptr, program, i);
4911 STRCPY(ptr += i, p);
4912 ptr += len;
4913 program = pos + 2;
4914 }
4915 STRCPY(ptr, program);
4916 }
4917 else
4918 {
4919 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4920 if (new_cmdline == NULL)
4921 return NULL; /* out of memory */
4922 STRCPY(new_cmdline, program);
4923 STRCAT(new_cmdline, " ");
4924 STRCAT(new_cmdline, p);
4925 }
4926 msg_make(p);
4927
4928 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4929 vim_free(*cmdlinep);
4930 *cmdlinep = new_cmdline;
4931 p = new_cmdline;
4932 }
4933 return p;
4934}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004935#endif
4936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937/*
4938 * Expand file name in Ex command argument.
4939 * Return FAIL for failure, OK otherwise.
4940 */
4941 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004942expand_filename(
4943 exarg_T *eap,
4944 char_u **cmdlinep,
4945 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 int has_wildcards; /* need to expand wildcards */
4948 char_u *repl;
4949 int srclen;
4950 char_u *p;
4951 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004952 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
Bram Moolenaar748bf032005-02-02 23:04:36 +00004954#ifdef FEAT_QUICKFIX
4955 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4956 p = skip_grep_pat(eap);
4957#else
4958 p = eap->arg;
4959#endif
4960
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 /*
4962 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4963 * the file name contains a wildcard it should not cause expanding.
4964 * (it will be expanded anyway if there is a wildcard before replacing).
4965 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004966 has_wildcards = mch_has_wildcard(p);
4967 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004969#ifdef FEAT_EVAL
4970 /* Skip over `=expr`, wildcards in it are not expanded. */
4971 if (p[0] == '`' && p[1] == '=')
4972 {
4973 p += 2;
4974 (void)skip_expr(&p);
4975 if (*p == '`')
4976 ++p;
4977 continue;
4978 }
4979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 /*
4981 * Quick check if this cannot be the start of a special string.
4982 * Also removes backslash before '%', '#' and '<'.
4983 */
4984 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4985 {
4986 ++p;
4987 continue;
4988 }
4989
4990 /*
4991 * Try to find a match at this position.
4992 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004993 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4994 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (*errormsgp != NULL) /* error detected */
4996 return FAIL;
4997 if (repl == NULL) /* no match found */
4998 {
4999 p += srclen;
5000 continue;
5001 }
5002
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005003 /* Wildcards won't be expanded below, the replacement is taken
5004 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5005 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5006 {
5007 char_u *l = repl;
5008
5009 repl = expand_env_save(repl);
5010 vim_free(l);
5011 }
5012
Bram Moolenaar63b92542007-03-27 14:57:09 +00005013 /* Need to escape white space et al. with a backslash.
5014 * Don't do this for:
5015 * - replacement that already has been escaped: "##"
5016 * - shell commands (may have to use quotes instead).
5017 * - non-unix systems when there is a single argument (spaces don't
5018 * separate arguments then).
5019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005021 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 && eap->cmdidx != CMD_grep
5024 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005025 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005026 && eap->cmdidx != CMD_lgrep
5027 && eap->cmdidx != CMD_lgrepadd
5028 && eap->cmdidx != CMD_lmake
5029 && eap->cmdidx != CMD_make
5030 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031#ifndef UNIX
5032 && !(eap->argt & NOSPC)
5033#endif
5034 )
5035 {
5036 char_u *l;
5037#ifdef BACKSLASH_IN_FILENAME
5038 /* Don't escape a backslash here, because rem_backslash() doesn't
5039 * remove it later. */
5040 static char_u *nobslash = (char_u *)" \t\"|";
5041# define ESCAPE_CHARS nobslash
5042#else
5043# define ESCAPE_CHARS escape_chars
5044#endif
5045
5046 for (l = repl; *l; ++l)
5047 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5048 {
5049 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5050 if (l != NULL)
5051 {
5052 vim_free(repl);
5053 repl = l;
5054 }
5055 break;
5056 }
5057 }
5058
5059 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005060 if ((eap->usefilter || eap->cmdidx == CMD_bang
5061 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005062 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 char_u *l;
5065
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005066 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (l != NULL)
5068 {
5069 vim_free(repl);
5070 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
5073
5074 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5075 vim_free(repl);
5076 if (p == NULL)
5077 return FAIL;
5078 }
5079
5080 /*
5081 * One file argument: Expand wildcards.
5082 * Don't do this with ":r !command" or ":w !command".
5083 */
5084 if ((eap->argt & NOSPC) && !eap->usefilter)
5085 {
5086 /*
5087 * May do this twice:
5088 * 1. Replace environment variables.
5089 * 2. Replace any other wildcards, remove backslashes.
5090 */
5091 for (n = 1; n <= 2; ++n)
5092 {
5093 if (n == 2)
5094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 /*
5096 * Halve the number of backslashes (this is Vi compatible).
5097 * For Unix and OS/2, when wildcards are expanded, this is
5098 * done by ExpandOne() below.
5099 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005100#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (!has_wildcards)
5102#endif
5103 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105
5106 if (has_wildcards)
5107 {
5108 if (n == 1)
5109 {
5110 /*
5111 * First loop: May expand environment variables. This
5112 * can be done much faster with expand_env() than with
5113 * something else (e.g., calling a shell).
5114 * After expanding environment variables, check again
5115 * if there are still wildcards present.
5116 */
5117 if (vim_strchr(eap->arg, '$') != NULL
5118 || vim_strchr(eap->arg, '~') != NULL)
5119 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005120 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005121 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 has_wildcards = mch_has_wildcard(NameBuff);
5123 p = NameBuff;
5124 }
5125 else
5126 p = NULL;
5127 }
5128 else /* n == 2 */
5129 {
5130 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005131 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 ExpandInit(&xpc);
5134 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005135 if (p_wic)
5136 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005138 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (p == NULL)
5140 return FAIL;
5141 }
5142 if (p != NULL)
5143 {
5144 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5145 p, cmdlinep);
5146 if (n == 2) /* p came from ExpandOne() */
5147 vim_free(p);
5148 }
5149 }
5150 }
5151 }
5152 return OK;
5153}
5154
5155/*
5156 * Replace part of the command line, keeping eap->cmd, eap->arg and
5157 * eap->nextcmd correct.
5158 * "src" points to the part that is to be replaced, of length "srclen".
5159 * "repl" is the replacement string.
5160 * Returns a pointer to the character after the replaced string.
5161 * Returns NULL for failure.
5162 */
5163 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005164repl_cmdline(
5165 exarg_T *eap,
5166 char_u *src,
5167 int srclen,
5168 char_u *repl,
5169 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
5171 int len;
5172 int i;
5173 char_u *new_cmdline;
5174
5175 /*
5176 * The new command line is build in new_cmdline[].
5177 * First allocate it.
5178 * Careful: a "+cmd" argument may have been NUL terminated.
5179 */
5180 len = (int)STRLEN(repl);
5181 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005182 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5184 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5185 return NULL; /* out of memory! */
5186
5187 /*
5188 * Copy the stuff before the expanded part.
5189 * Copy the expanded stuff.
5190 * Copy what came after the expanded part.
5191 * Copy the next commands, if there are any.
5192 */
5193 i = (int)(src - *cmdlinep); /* length of part before match */
5194 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 mch_memmove(new_cmdline + i, repl, (size_t)len);
5197 i += len; /* remember the end of the string */
5198 STRCPY(new_cmdline + i, src + srclen);
5199 src = new_cmdline + i; /* remember where to continue */
5200
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005201 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
5203 i = (int)STRLEN(new_cmdline) + 1;
5204 STRCPY(new_cmdline + i, eap->nextcmd);
5205 eap->nextcmd = new_cmdline + i;
5206 }
5207 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5208 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5209 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5210 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5211 vim_free(*cmdlinep);
5212 *cmdlinep = new_cmdline;
5213
5214 return src;
5215}
5216
5217/*
5218 * Check for '|' to separate commands and '"' to start comments.
5219 */
5220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
5223 char_u *p;
5224
Bram Moolenaar86b68352004-12-27 21:59:20 +00005225#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005226 p = skip_grep_pat(eap);
5227#else
5228 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005229#endif
5230
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005231 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 if (*p == Ctrl_V)
5234 {
5235 if (eap->argt & (USECTRLV | XFILE))
5236 ++p; /* skip CTRL-V and next char */
5237 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005238 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005239 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (*p == NUL) /* stop at NUL after CTRL-V */
5241 break;
5242 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005243
5244#ifdef FEAT_EVAL
5245 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005246 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005247 {
5248 p += 2;
5249 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005250 }
5251#endif
5252
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 /* Check for '"': start of comment or '|': next command */
5254 /* :@" and :*" do not start a comment!
5255 * :redir @" doesn't either. */
5256 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5257 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5258 || p != eap->arg)
5259 && (eap->cmdidx != CMD_redir
5260 || p != eap->arg + 1 || p[-1] != '@'))
5261 || *p == '|' || *p == '\n')
5262 {
5263 /*
5264 * We remove the '\' before the '|', unless USECTRLV is used
5265 * AND 'b' is present in 'cpoptions'.
5266 */
5267 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5268 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5269 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005270 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 --p;
5272 }
5273 else
5274 {
5275 eap->nextcmd = check_nextcmd(p);
5276 *p = NUL;
5277 break;
5278 }
5279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5283 del_trailing_spaces(eap->arg);
5284}
5285
5286/*
5287 * get + command from ex argument
5288 */
5289 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005290getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 char_u *arg = *argp;
5293 char_u *command = NULL;
5294
5295 if (*arg == '+') /* +[command] */
5296 {
5297 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005298 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 command = dollar_command;
5300 else
5301 {
5302 command = arg;
5303 arg = skip_cmd_arg(command, TRUE);
5304 if (*arg != NUL)
5305 *arg++ = NUL; /* terminate command with NUL */
5306 }
5307
5308 arg = skipwhite(arg); /* skip over spaces */
5309 *argp = arg;
5310 }
5311 return command;
5312}
5313
5314/*
5315 * Find end of "+command" argument. Skip over "\ " and "\\".
5316 */
5317 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318skip_cmd_arg(
5319 char_u *p,
5320 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322 while (*p && !vim_isspace(*p))
5323 {
5324 if (*p == '\\' && p[1] != NUL)
5325 {
5326 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005327 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 else
5329 ++p;
5330 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005331 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333 return p;
5334}
5335
5336/*
5337 * Get "++opt=arg" argument.
5338 * Return FAIL or OK.
5339 */
5340 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005341getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
5343 char_u *arg = eap->arg + 2;
5344 int *pp = NULL;
5345#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005346 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 char_u *p;
5348#endif
5349
5350 /* ":edit ++[no]bin[ary] file" */
5351 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5352 {
5353 if (*arg == 'n')
5354 {
5355 arg += 2;
5356 eap->force_bin = FORCE_NOBIN;
5357 }
5358 else
5359 eap->force_bin = FORCE_BIN;
5360 if (!checkforcmd(&arg, "binary", 3))
5361 return FAIL;
5362 eap->arg = skipwhite(arg);
5363 return OK;
5364 }
5365
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005366 /* ":read ++edit file" */
5367 if (STRNCMP(arg, "edit", 4) == 0)
5368 {
5369 eap->read_edit = TRUE;
5370 eap->arg = skipwhite(arg + 4);
5371 return OK;
5372 }
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (STRNCMP(arg, "ff", 2) == 0)
5375 {
5376 arg += 2;
5377 pp = &eap->force_ff;
5378 }
5379 else if (STRNCMP(arg, "fileformat", 10) == 0)
5380 {
5381 arg += 10;
5382 pp = &eap->force_ff;
5383 }
5384#ifdef FEAT_MBYTE
5385 else if (STRNCMP(arg, "enc", 3) == 0)
5386 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005387 if (STRNCMP(arg, "encoding", 8) == 0)
5388 arg += 8;
5389 else
5390 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 pp = &eap->force_enc;
5392 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005393 else if (STRNCMP(arg, "bad", 3) == 0)
5394 {
5395 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005396 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398#endif
5399
5400 if (pp == NULL || *arg != '=')
5401 return FAIL;
5402
5403 ++arg;
5404 *pp = (int)(arg - eap->cmd);
5405 arg = skip_cmd_arg(arg, FALSE);
5406 eap->arg = skipwhite(arg);
5407 *arg = NUL;
5408
5409#ifdef FEAT_MBYTE
5410 if (pp == &eap->force_ff)
5411 {
5412#endif
5413 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5414 return FAIL;
5415#ifdef FEAT_MBYTE
5416 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005417 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
5419 /* Make 'fileencoding' lower case. */
5420 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5421 *p = TOLOWER_ASC(*p);
5422 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005423 else
5424 {
5425 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5426 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005427 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005428 if (STRICMP(p, "keep") == 0)
5429 eap->bad_char = BAD_KEEP;
5430 else if (STRICMP(p, "drop") == 0)
5431 eap->bad_char = BAD_DROP;
5432 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5433 eap->bad_char = *p;
5434 else
5435 return FAIL;
5436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#endif
5438
5439 return OK;
5440}
5441
5442/*
5443 * ":abbreviate" and friends.
5444 */
5445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005446ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 do_exmap(eap, TRUE); /* almost the same as mapping */
5449}
5450
5451/*
5452 * ":map" and friends.
5453 */
5454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005455ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
5457 /*
5458 * If we are sourcing .exrc or .vimrc in current directory we
5459 * print the mappings for security reasons.
5460 */
5461 if (secure)
5462 {
5463 secure = 2;
5464 msg_outtrans(eap->cmd);
5465 msg_putchar('\n');
5466 }
5467 do_exmap(eap, FALSE);
5468}
5469
5470/*
5471 * ":unmap" and friends.
5472 */
5473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 do_exmap(eap, FALSE);
5477}
5478
5479/*
5480 * ":mapclear" and friends.
5481 */
5482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5486}
5487
5488/*
5489 * ":abclear" and friends.
5490 */
5491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005492ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5495}
5496
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 /*
5501 * Disallow auto commands from .exrc and .vimrc in current
5502 * directory for security reasons.
5503 */
5504 if (secure)
5505 {
5506 secure = 2;
5507 eap->errmsg = e_curdir;
5508 }
5509 else if (eap->cmdidx == CMD_autocmd)
5510 do_autocmd(eap->arg, eap->forceit);
5511 else
5512 do_augroup(eap->arg, eap->forceit);
5513}
5514
5515/*
5516 * ":doautocmd": Apply the automatic commands to the current buffer.
5517 */
5518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005521 char_u *arg = eap->arg;
5522 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005523 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005524
Bram Moolenaar1610d052016-06-09 22:53:01 +02005525 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5526 /* Only when there is no <nomodeline>. */
5527 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005528 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531#ifdef FEAT_LISTCMDS
5532/*
5533 * :[N]bunload[!] [N] [bufname] unload buffer
5534 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5535 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5536 */
5537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005538ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 eap->errmsg = do_bufdel(
5541 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5542 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5543 : DOBUF_UNLOAD, eap->arg,
5544 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5545}
5546
5547/*
5548 * :[N]buffer [N] to buffer N
5549 * :[N]sbuffer [N] to buffer N
5550 */
5551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005552ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 if (*eap->arg)
5555 eap->errmsg = e_trailing;
5556 else
5557 {
5558 if (eap->addr_count == 0) /* default is current buffer */
5559 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5560 else
5561 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005562 if (eap->do_ecmd_cmd != NULL)
5563 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565}
5566
5567/*
5568 * :[N]bmodified [N] to next mod. buffer
5569 * :[N]sbmodified [N] to next mod. buffer
5570 */
5571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
5574 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005575 if (eap->do_ecmd_cmd != NULL)
5576 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577}
5578
5579/*
5580 * :[N]bnext [N] to next buffer
5581 * :[N]sbnext [N] split and to next buffer
5582 */
5583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005584ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
5586 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005587 if (eap->do_ecmd_cmd != NULL)
5588 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589}
5590
5591/*
5592 * :[N]bNext [N] to previous buffer
5593 * :[N]bprevious [N] to previous buffer
5594 * :[N]sbNext [N] split and to previous buffer
5595 * :[N]sbprevious [N] split and to previous buffer
5596 */
5597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005598ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005601 if (eap->do_ecmd_cmd != NULL)
5602 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605/*
5606 * :brewind to first buffer
5607 * :bfirst to first buffer
5608 * :sbrewind split and to first buffer
5609 * :sbfirst split and to first buffer
5610 */
5611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005615 if (eap->do_ecmd_cmd != NULL)
5616 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617}
5618
5619/*
5620 * :blast to last buffer
5621 * :sblast split and to last buffer
5622 */
5623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005627 if (eap->do_ecmd_cmd != NULL)
5628 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629}
5630#endif
5631
5632 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 return (c == NUL || c == '|' || c == '"' || c == '\n');
5636}
5637
5638#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5639 || defined(PROTO)
5640/*
5641 * Return the next command, after the first '|' or '\n'.
5642 * Return NULL if not found.
5643 */
5644 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 while (*p != '|' && *p != '\n')
5648 {
5649 if (*p == NUL)
5650 return NULL;
5651 ++p;
5652 }
5653 return (p + 1);
5654}
5655#endif
5656
5657/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005658 * Check if *p is a separator between Ex commands, skipping over white space.
5659 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 */
5661 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005664 char_u *s = skipwhite(p);
5665
5666 if (*s == '|' || *s == '\n')
5667 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 else
5669 return NULL;
5670}
5671
5672/*
5673 * - if there are more files to edit
5674 * - and this is the last window
5675 * - and forceit not used
5676 * - and not repeated twice on a row
5677 * return FAIL and give error message if 'message' TRUE
5678 * return OK otherwise
5679 */
5680 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681check_more(
5682 int message, /* when FALSE check only, no messages */
5683 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5686
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005687 if (!forceit && only_one_window()
5688 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 if (message)
5691 {
5692#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5693 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5694 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005695 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005698 vim_strncpy(buff,
5699 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005700 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005702 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 _("%d more files to edit. Quit anyway?"), n);
5704 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5705 return OK;
5706 return FAIL;
5707 }
5708#endif
5709 if (n == 1)
5710 EMSG(_("E173: 1 more file to edit"));
5711 else
5712 EMSGN(_("E173: %ld more files to edit"), n);
5713 quitmore = 2; /* next try to quit is allowed */
5714 }
5715 return FAIL;
5716 }
5717 return OK;
5718}
5719
5720#ifdef FEAT_CMDL_COMPL
5721/*
5722 * Function given to ExpandGeneric() to obtain the list of command names.
5723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005725get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 if (idx >= (int)CMD_SIZE)
5728# ifdef FEAT_USR_CMDS
5729 return get_user_command_name(idx);
5730# else
5731 return NULL;
5732# endif
5733 return cmdnames[idx].cmd_name;
5734}
5735#endif
5736
5737#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005738static 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);
5739static void uc_list(char_u *name, size_t name_len);
5740static 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);
5741static char_u *uc_split_args(char_u *arg, size_t *lenp);
5742static 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 +00005743
5744 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005745uc_add_command(
5746 char_u *name,
5747 size_t name_len,
5748 char_u *rep,
5749 long argt,
5750 long def,
5751 int flags,
5752 int compl,
5753 char_u *compl_arg,
5754 int addr_type,
5755 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 ucmd_T *cmd = NULL;
5758 char_u *p;
5759 int i;
5760 int cmp = 1;
5761 char_u *rep_buf = NULL;
5762 garray_T *gap;
5763
Bram Moolenaar9c102382006-05-03 21:26:49 +00005764 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (rep_buf == NULL)
5766 {
5767 /* Can't replace termcodes - try using the string as is */
5768 rep_buf = vim_strsave(rep);
5769
5770 /* Give up if out of memory */
5771 if (rep_buf == NULL)
5772 return FAIL;
5773 }
5774
5775 /* get address of growarray: global or in curbuf */
5776 if (flags & UC_BUFFER)
5777 {
5778 gap = &curbuf->b_ucmds;
5779 if (gap->ga_itemsize == 0)
5780 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5781 }
5782 else
5783 gap = &ucmds;
5784
5785 /* Search for the command in the already defined commands. */
5786 for (i = 0; i < gap->ga_len; ++i)
5787 {
5788 size_t len;
5789
5790 cmd = USER_CMD_GA(gap, i);
5791 len = STRLEN(cmd->uc_name);
5792 cmp = STRNCMP(name, cmd->uc_name, name_len);
5793 if (cmp == 0)
5794 {
5795 if (name_len < len)
5796 cmp = -1;
5797 else if (name_len > len)
5798 cmp = 1;
5799 }
5800
5801 if (cmp == 0)
5802 {
5803 if (!force)
5804 {
5805 EMSG(_("E174: Command already exists: add ! to replace it"));
5806 goto fail;
5807 }
5808
Bram Moolenaard23a8232018-02-10 18:45:26 +01005809 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005810#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005811 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814 }
5815
5816 /* Stop as soon as we pass the name to add */
5817 if (cmp < 0)
5818 break;
5819 }
5820
5821 /* Extend the array unless we're replacing an existing command */
5822 if (cmp != 0)
5823 {
5824 if (ga_grow(gap, 1) != OK)
5825 goto fail;
5826 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5827 goto fail;
5828
5829 cmd = USER_CMD_GA(gap, i);
5830 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5831
5832 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834 cmd->uc_name = p;
5835 }
5836
5837 cmd->uc_rep = rep_buf;
5838 cmd->uc_argt = argt;
5839 cmd->uc_def = def;
5840 cmd->uc_compl = compl;
5841#ifdef FEAT_EVAL
5842 cmd->uc_scriptID = current_SID;
5843# ifdef FEAT_CMDL_COMPL
5844 cmd->uc_compl_arg = compl_arg;
5845# endif
5846#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005847 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 return OK;
5850
5851fail:
5852 vim_free(rep_buf);
5853#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5854 vim_free(compl_arg);
5855#endif
5856 return FAIL;
5857}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005860#if defined(FEAT_USR_CMDS)
5861static struct
5862{
5863 int expand;
5864 char *name;
5865} addr_type_complete[] =
5866{
5867 {ADDR_ARGUMENTS, "arguments"},
5868 {ADDR_LINES, "lines"},
5869 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5870 {ADDR_TABS, "tabs"},
5871 {ADDR_BUFFERS, "buffers"},
5872 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005873 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005874 {-1, NULL}
5875};
5876#endif
5877
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005878#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879/*
5880 * List of names for completion for ":command" with the EXPAND_ flag.
5881 * Must be alphabetical for completion.
5882 */
5883static struct
5884{
5885 int expand;
5886 char *name;
5887} command_complete[] =
5888{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005889 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005891 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005893 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005895 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005896#if defined(FEAT_CSCOPE)
5897 {EXPAND_CSCOPE, "cscope"},
5898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5900 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005901 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#endif
5903 {EXPAND_DIRECTORIES, "dir"},
5904 {EXPAND_ENV_VARS, "environment"},
5905 {EXPAND_EVENTS, "event"},
5906 {EXPAND_EXPRESSION, "expression"},
5907 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005908 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005909 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {EXPAND_FUNCTIONS, "function"},
5911 {EXPAND_HELP, "help"},
5912 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005913#if defined(FEAT_CMDHIST)
5914 {EXPAND_HISTORY, "history"},
5915#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005916#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005917 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005918 {EXPAND_LOCALES, "locale"},
5919#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005920 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {EXPAND_MAPPINGS, "mapping"},
5922 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005923 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005924 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005925#if defined(FEAT_PROFILE)
5926 {EXPAND_SYNTIME, "syntime"},
5927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005929 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005930 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005931#if defined(FEAT_SIGNS)
5932 {EXPAND_SIGN, "sign"},
5933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {EXPAND_TAGS, "tag"},
5935 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005936 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {EXPAND_USER_VARS, "var"},
5938 {0, NULL}
5939};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005942#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005944uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
5946 int i, j;
5947 int found = FALSE;
5948 ucmd_T *cmd;
5949 int len;
5950 long a;
5951 garray_T *gap;
5952
5953 gap = &curbuf->b_ucmds;
5954 for (;;)
5955 {
5956 for (i = 0; i < gap->ga_len; ++i)
5957 {
5958 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005959 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaard29459b2016-08-26 22:29:11 +02005961 /* Skip commands which don't match the requested prefix and
5962 * commands filtered out. */
5963 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5964 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 continue;
5966
5967 /* Put out the title first time */
5968 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005969 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 found = TRUE;
5971 msg_putchar('\n');
5972 if (got_int)
5973 break;
5974
5975 /* Special cases */
5976 msg_putchar(a & BANG ? '!' : ' ');
5977 msg_putchar(a & REGSTR ? '"' : ' ');
5978 msg_putchar(gap != &ucmds ? 'b' : ' ');
5979 msg_putchar(' ');
5980
Bram Moolenaar8820b482017-03-16 17:23:31 +01005981 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 len = (int)STRLEN(cmd->uc_name) + 4;
5983
5984 do {
5985 msg_putchar(' ');
5986 ++len;
5987 } while (len < 16);
5988
5989 len = 0;
5990
5991 /* Arguments */
5992 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5993 {
5994 case 0: IObuff[len++] = '0'; break;
5995 case (EXTRA): IObuff[len++] = '*'; break;
5996 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5997 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5998 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5999 }
6000
6001 do {
6002 IObuff[len++] = ' ';
6003 } while (len < 5);
6004
6005 /* Range */
6006 if (a & (RANGE|COUNT))
6007 {
6008 if (a & COUNT)
6009 {
6010 /* -count=N */
6011 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6012 len += (int)STRLEN(IObuff + len);
6013 }
6014 else if (a & DFLALL)
6015 IObuff[len++] = '%';
6016 else if (cmd->uc_def >= 0)
6017 {
6018 /* -range=N */
6019 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6020 len += (int)STRLEN(IObuff + len);
6021 }
6022 else
6023 IObuff[len++] = '.';
6024 }
6025
6026 do {
6027 IObuff[len++] = ' ';
6028 } while (len < 11);
6029
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006030 /* Address Type */
6031 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6032 if (addr_type_complete[j].expand != ADDR_LINES
6033 && addr_type_complete[j].expand == cmd->uc_addr_type)
6034 {
6035 STRCPY(IObuff + len, addr_type_complete[j].name);
6036 len += (int)STRLEN(IObuff + len);
6037 break;
6038 }
6039
6040 do {
6041 IObuff[len++] = ' ';
6042 } while (len < 21);
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 /* Completion */
6045 for (j = 0; command_complete[j].expand != 0; ++j)
6046 if (command_complete[j].expand == cmd->uc_compl)
6047 {
6048 STRCPY(IObuff + len, command_complete[j].name);
6049 len += (int)STRLEN(IObuff + len);
6050 break;
6051 }
6052
6053 do {
6054 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006055 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 IObuff[len] = '\0';
6058 msg_outtrans(IObuff);
6059
6060 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006061#ifdef FEAT_EVAL
6062 if (p_verbose > 0)
6063 last_set_msg(cmd->uc_scriptID);
6064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 out_flush();
6066 ui_breakcheck();
6067 if (got_int)
6068 break;
6069 }
6070 if (gap == &ucmds || i < gap->ga_len)
6071 break;
6072 gap = &ucmds;
6073 }
6074
6075 if (!found)
6076 MSG(_("No user-defined commands found"));
6077}
6078
6079 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006080uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6083 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6084 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6085 0xb9, 0x7f, 0};
6086 int i;
6087
6088 for (i = 0; fcmd[i]; ++i)
6089 IObuff[i] = fcmd[i] - 0x40;
6090 IObuff[i] = 0;
6091 return IObuff;
6092}
6093
6094 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095uc_scan_attr(
6096 char_u *attr,
6097 size_t len,
6098 long *argt,
6099 long *def,
6100 int *flags,
6101 int *compl,
6102 char_u **compl_arg,
6103 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 char_u *p;
6106
6107 if (len == 0)
6108 {
6109 EMSG(_("E175: No attribute specified"));
6110 return FAIL;
6111 }
6112
6113 /* First, try the simple attributes (no arguments) */
6114 if (STRNICMP(attr, "bang", len) == 0)
6115 *argt |= BANG;
6116 else if (STRNICMP(attr, "buffer", len) == 0)
6117 *flags |= UC_BUFFER;
6118 else if (STRNICMP(attr, "register", len) == 0)
6119 *argt |= REGSTR;
6120 else if (STRNICMP(attr, "bar", len) == 0)
6121 *argt |= TRLBAR;
6122 else
6123 {
6124 int i;
6125 char_u *val = NULL;
6126 size_t vallen = 0;
6127 size_t attrlen = len;
6128
6129 /* Look for the attribute name - which is the part before any '=' */
6130 for (i = 0; i < (int)len; ++i)
6131 {
6132 if (attr[i] == '=')
6133 {
6134 val = &attr[i + 1];
6135 vallen = len - i - 1;
6136 attrlen = i;
6137 break;
6138 }
6139 }
6140
6141 if (STRNICMP(attr, "nargs", attrlen) == 0)
6142 {
6143 if (vallen == 1)
6144 {
6145 if (*val == '0')
6146 /* Do nothing - this is the default */;
6147 else if (*val == '1')
6148 *argt |= (EXTRA | NOSPC | NEEDARG);
6149 else if (*val == '*')
6150 *argt |= EXTRA;
6151 else if (*val == '?')
6152 *argt |= (EXTRA | NOSPC);
6153 else if (*val == '+')
6154 *argt |= (EXTRA | NEEDARG);
6155 else
6156 goto wrong_nargs;
6157 }
6158 else
6159 {
6160wrong_nargs:
6161 EMSG(_("E176: Invalid number of arguments"));
6162 return FAIL;
6163 }
6164 }
6165 else if (STRNICMP(attr, "range", attrlen) == 0)
6166 {
6167 *argt |= RANGE;
6168 if (vallen == 1 && *val == '%')
6169 *argt |= DFLALL;
6170 else if (val != NULL)
6171 {
6172 p = val;
6173 if (*def >= 0)
6174 {
6175two_count:
6176 EMSG(_("E177: Count cannot be specified twice"));
6177 return FAIL;
6178 }
6179
6180 *def = getdigits(&p);
6181 *argt |= (ZEROR | NOTADR);
6182
6183 if (p != val + vallen || vallen == 0)
6184 {
6185invalid_count:
6186 EMSG(_("E178: Invalid default value for count"));
6187 return FAIL;
6188 }
6189 }
6190 }
6191 else if (STRNICMP(attr, "count", attrlen) == 0)
6192 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006193 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
6195 if (val != NULL)
6196 {
6197 p = val;
6198 if (*def >= 0)
6199 goto two_count;
6200
6201 *def = getdigits(&p);
6202
6203 if (p != val + vallen)
6204 goto invalid_count;
6205 }
6206
6207 if (*def < 0)
6208 *def = 0;
6209 }
6210 else if (STRNICMP(attr, "complete", attrlen) == 0)
6211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 if (val == NULL)
6213 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006214 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 return FAIL;
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006218 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6219 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006222 else if (STRNICMP(attr, "addr", attrlen) == 0)
6223 {
6224 *argt |= RANGE;
6225 if (val == NULL)
6226 {
6227 EMSG(_("E179: argument required for -addr"));
6228 return FAIL;
6229 }
6230 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6231 == FAIL)
6232 return FAIL;
6233 if (addr_type_arg != ADDR_LINES)
6234 *argt |= (ZEROR | NOTADR) ;
6235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 else
6237 {
6238 char_u ch = attr[len];
6239 attr[len] = '\0';
6240 EMSG2(_("E181: Invalid attribute: %s"), attr);
6241 attr[len] = ch;
6242 return FAIL;
6243 }
6244 }
6245
6246 return OK;
6247}
6248
Bram Moolenaara850a712009-01-28 14:42:59 +00006249/*
6250 * ":command ..."
6251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006253ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 char_u *name;
6256 char_u *end;
6257 char_u *p;
6258 long argt = 0;
6259 long def = -1;
6260 int flags = 0;
6261 int compl = EXPAND_NOTHING;
6262 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006263 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006265 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 p = eap->arg;
6268
6269 /* Check for attributes */
6270 while (*p == '-')
6271 {
6272 ++p;
6273 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006274 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6275 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 == FAIL)
6277 return;
6278 p = skipwhite(end);
6279 }
6280
6281 /* Get the name (if any) and skip to the following argument */
6282 name = p;
6283 if (ASCII_ISALPHA(*p))
6284 while (ASCII_ISALNUM(*p))
6285 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006286 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
6288 EMSG(_("E182: Invalid command name"));
6289 return;
6290 }
6291 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006292 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
6294 /* If there is nothing after the name, and no attributes were specified,
6295 * we are listing commands
6296 */
6297 p = skipwhite(end);
6298 if (!has_attr && ends_excmd(*p))
6299 {
6300 uc_list(name, end - name);
6301 }
6302 else if (!ASCII_ISUPPER(*name))
6303 {
6304 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6305 return;
6306 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006307 else if ((name_len == 1 && *name == 'X')
6308 || (name_len <= 4
6309 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6310 {
6311 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6312 return;
6313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 else
6315 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006316 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317}
6318
6319/*
6320 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * Clear all user commands, global and for current buffer.
6322 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 uc_clear(&ucmds);
6327 uc_clear(&curbuf->b_ucmds);
6328}
6329
6330/*
6331 * Clear all user commands for "gap".
6332 */
6333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006334uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
6336 int i;
6337 ucmd_T *cmd;
6338
6339 for (i = 0; i < gap->ga_len; ++i)
6340 {
6341 cmd = USER_CMD_GA(gap, i);
6342 vim_free(cmd->uc_name);
6343 vim_free(cmd->uc_rep);
6344# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6345 vim_free(cmd->uc_compl_arg);
6346# endif
6347 }
6348 ga_clear(gap);
6349}
6350
6351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006352ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 int i = 0;
6355 ucmd_T *cmd = NULL;
6356 int cmp = -1;
6357 garray_T *gap;
6358
6359 gap = &curbuf->b_ucmds;
6360 for (;;)
6361 {
6362 for (i = 0; i < gap->ga_len; ++i)
6363 {
6364 cmd = USER_CMD_GA(gap, i);
6365 cmp = STRCMP(eap->arg, cmd->uc_name);
6366 if (cmp <= 0)
6367 break;
6368 }
6369 if (gap == &ucmds || cmp == 0)
6370 break;
6371 gap = &ucmds;
6372 }
6373
6374 if (cmp != 0)
6375 {
6376 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6377 return;
6378 }
6379
6380 vim_free(cmd->uc_name);
6381 vim_free(cmd->uc_rep);
6382# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6383 vim_free(cmd->uc_compl_arg);
6384# endif
6385
6386 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 if (i < gap->ga_len)
6389 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6390}
6391
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006392/*
6393 * split and quote args for <f-args>
6394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006396uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 char_u *buf;
6399 char_u *p;
6400 char_u *q;
6401 int len;
6402
6403 /* Precalculate length */
6404 p = arg;
6405 len = 2; /* Initial and final quotes */
6406
6407 while (*p)
6408 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006409 if (p[0] == '\\' && p[1] == '\\')
6410 {
6411 len += 2;
6412 p += 2;
6413 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006414 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
6416 len += 1;
6417 p += 2;
6418 }
6419 else if (*p == '\\' || *p == '"')
6420 {
6421 len += 2;
6422 p += 1;
6423 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006424 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 {
6426 p = skipwhite(p);
6427 if (*p == NUL)
6428 break;
6429 len += 3; /* "," */
6430 }
6431 else
6432 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006433#ifdef FEAT_MBYTE
6434 int charlen = (*mb_ptr2len)(p);
6435 len += charlen;
6436 p += charlen;
6437#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 ++len;
6439 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 }
6442 }
6443
6444 buf = alloc(len + 1);
6445 if (buf == NULL)
6446 {
6447 *lenp = 0;
6448 return buf;
6449 }
6450
6451 p = arg;
6452 q = buf;
6453 *q++ = '"';
6454 while (*p)
6455 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006456 if (p[0] == '\\' && p[1] == '\\')
6457 {
6458 *q++ = '\\';
6459 *q++ = '\\';
6460 p += 2;
6461 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006462 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
6464 *q++ = p[1];
6465 p += 2;
6466 }
6467 else if (*p == '\\' || *p == '"')
6468 {
6469 *q++ = '\\';
6470 *q++ = *p++;
6471 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006472 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 {
6474 p = skipwhite(p);
6475 if (*p == NUL)
6476 break;
6477 *q++ = '"';
6478 *q++ = ',';
6479 *q++ = '"';
6480 }
6481 else
6482 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006483 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485 }
6486 *q++ = '"';
6487 *q = 0;
6488
6489 *lenp = len;
6490 return buf;
6491}
6492
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006493 static size_t
6494add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6495{
6496 size_t result;
6497
6498 result = STRLEN(mod_str);
6499 if (*multi_mods)
6500 result += 1;
6501 if (buf != NULL)
6502 {
6503 if (*multi_mods)
6504 STRCAT(buf, " ");
6505 STRCAT(buf, mod_str);
6506 }
6507
6508 *multi_mods = 1;
6509
6510 return result;
6511}
6512
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513/*
6514 * Check for a <> code in a user command.
6515 * "code" points to the '<'. "len" the length of the <> (inclusive).
6516 * "buf" is where the result is to be added.
6517 * "split_buf" points to a buffer used for splitting, caller should free it.
6518 * "split_len" is the length of what "split_buf" contains.
6519 * Returns the length of the replacement, which has been added to "buf".
6520 * Returns -1 if there was no match, and only the "<" has been copied.
6521 */
6522 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006523uc_check_code(
6524 char_u *code,
6525 size_t len,
6526 char_u *buf,
6527 ucmd_T *cmd, /* the user command we're expanding */
6528 exarg_T *eap, /* ex arguments */
6529 char_u **split_buf,
6530 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 size_t result = 0;
6533 char_u *p = code + 1;
6534 size_t l = len - 2;
6535 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006536 enum {
6537 ct_ARGS,
6538 ct_BANG,
6539 ct_COUNT,
6540 ct_LINE1,
6541 ct_LINE2,
6542 ct_RANGE,
6543 ct_MODS,
6544 ct_REGISTER,
6545 ct_LT,
6546 ct_NONE
6547 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6550 {
6551 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6552 p += 2;
6553 l -= 2;
6554 }
6555
Bram Moolenaar371d5402006-03-20 21:47:49 +00006556 ++l;
6557 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006559 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006561 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006563 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006565 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006567 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006569 else if (STRNICMP(p, "range>", l) == 0)
6570 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006571 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006573 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006575 else if (STRNICMP(p, "mods>", l) == 0)
6576 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 switch (type)
6579 {
6580 case ct_ARGS:
6581 /* Simple case first */
6582 if (*eap->arg == NUL)
6583 {
6584 if (quote == 1)
6585 {
6586 result = 2;
6587 if (buf != NULL)
6588 STRCPY(buf, "''");
6589 }
6590 else
6591 result = 0;
6592 break;
6593 }
6594
6595 /* When specified there is a single argument don't split it.
6596 * Works for ":Cmd %" when % is "a b c". */
6597 if ((eap->argt & NOSPC) && quote == 2)
6598 quote = 1;
6599
6600 switch (quote)
6601 {
6602 case 0: /* No quoting, no splitting */
6603 result = STRLEN(eap->arg);
6604 if (buf != NULL)
6605 STRCPY(buf, eap->arg);
6606 break;
6607 case 1: /* Quote, but don't split */
6608 result = STRLEN(eap->arg) + 2;
6609 for (p = eap->arg; *p; ++p)
6610 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006611#ifdef FEAT_MBYTE
6612 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6613 /* DBCS can contain \ in a trail byte, skip the
6614 * double-byte character. */
6615 ++p;
6616 else
6617#endif
6618 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 ++result;
6620 }
6621
6622 if (buf != NULL)
6623 {
6624 *buf++ = '"';
6625 for (p = eap->arg; *p; ++p)
6626 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006627#ifdef FEAT_MBYTE
6628 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6629 /* DBCS can contain \ in a trail byte, copy the
6630 * double-byte character to avoid escaping. */
6631 *buf++ = *p++;
6632 else
6633#endif
6634 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 *buf++ = '\\';
6636 *buf++ = *p;
6637 }
6638 *buf = '"';
6639 }
6640
6641 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006642 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 /* This is hard, so only do it once, and cache the result */
6644 if (*split_buf == NULL)
6645 *split_buf = uc_split_args(eap->arg, split_len);
6646
6647 result = *split_len;
6648 if (buf != NULL && result != 0)
6649 STRCPY(buf, *split_buf);
6650
6651 break;
6652 }
6653 break;
6654
6655 case ct_BANG:
6656 result = eap->forceit ? 1 : 0;
6657 if (quote)
6658 result += 2;
6659 if (buf != NULL)
6660 {
6661 if (quote)
6662 *buf++ = '"';
6663 if (eap->forceit)
6664 *buf++ = '!';
6665 if (quote)
6666 *buf = '"';
6667 }
6668 break;
6669
6670 case ct_LINE1:
6671 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006672 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 case ct_COUNT:
6674 {
6675 char num_buf[20];
6676 long num = (type == ct_LINE1) ? eap->line1 :
6677 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006678 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6680 size_t num_len;
6681
6682 sprintf(num_buf, "%ld", num);
6683 num_len = STRLEN(num_buf);
6684 result = num_len;
6685
6686 if (quote)
6687 result += 2;
6688
6689 if (buf != NULL)
6690 {
6691 if (quote)
6692 *buf++ = '"';
6693 STRCPY(buf, num_buf);
6694 buf += num_len;
6695 if (quote)
6696 *buf = '"';
6697 }
6698
6699 break;
6700 }
6701
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006702 case ct_MODS:
6703 {
6704 int multi_mods = 0;
6705 typedef struct {
6706 int *varp;
6707 char *name;
6708 } mod_entry_T;
6709 static mod_entry_T mod_entries[] = {
6710#ifdef FEAT_BROWSE_CMD
6711 {&cmdmod.browse, "browse"},
6712#endif
6713#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6714 {&cmdmod.confirm, "confirm"},
6715#endif
6716 {&cmdmod.hide, "hide"},
6717 {&cmdmod.keepalt, "keepalt"},
6718 {&cmdmod.keepjumps, "keepjumps"},
6719 {&cmdmod.keepmarks, "keepmarks"},
6720 {&cmdmod.keeppatterns, "keeppatterns"},
6721 {&cmdmod.lockmarks, "lockmarks"},
6722 {&cmdmod.noswapfile, "noswapfile"},
6723 {NULL, NULL}
6724 };
6725 int i;
6726
6727 result = quote ? 2 : 0;
6728 if (buf != NULL)
6729 {
6730 if (quote)
6731 *buf++ = '"';
6732 *buf = '\0';
6733 }
6734
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006735 /* :aboveleft and :leftabove */
6736 if (cmdmod.split & WSP_ABOVE)
6737 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6738 /* :belowright and :rightbelow */
6739 if (cmdmod.split & WSP_BELOW)
6740 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6741 /* :botright */
6742 if (cmdmod.split & WSP_BOT)
6743 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006744
6745 /* the modifiers that are simple flags */
6746 for (i = 0; mod_entries[i].varp != NULL; ++i)
6747 if (*mod_entries[i].varp)
6748 result += add_cmd_modifier(buf, mod_entries[i].name,
6749 &multi_mods);
6750
6751 /* TODO: How to support :noautocmd? */
6752#ifdef HAVE_SANDBOX
6753 /* TODO: How to support :sandbox?*/
6754#endif
6755 /* :silent */
6756 if (msg_silent > 0)
6757 result += add_cmd_modifier(buf,
6758 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006759 /* :tab */
6760 if (cmdmod.tab > 0)
6761 result += add_cmd_modifier(buf, "tab", &multi_mods);
6762 /* :topleft */
6763 if (cmdmod.split & WSP_TOP)
6764 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006765 /* TODO: How to support :unsilent?*/
6766 /* :verbose */
6767 if (p_verbose > 0)
6768 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006769 /* :vertical */
6770 if (cmdmod.split & WSP_VERT)
6771 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006772 if (quote && buf != NULL)
6773 {
6774 buf += result - 2;
6775 *buf = '"';
6776 }
6777 break;
6778 }
6779
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 case ct_REGISTER:
6781 result = eap->regname ? 1 : 0;
6782 if (quote)
6783 result += 2;
6784 if (buf != NULL)
6785 {
6786 if (quote)
6787 *buf++ = '\'';
6788 if (eap->regname)
6789 *buf++ = eap->regname;
6790 if (quote)
6791 *buf = '\'';
6792 }
6793 break;
6794
6795 case ct_LT:
6796 result = 1;
6797 if (buf != NULL)
6798 *buf = '<';
6799 break;
6800
6801 default:
6802 /* Not recognized: just copy the '<' and return -1. */
6803 result = (size_t)-1;
6804 if (buf != NULL)
6805 *buf = '<';
6806 break;
6807 }
6808
6809 return result;
6810}
6811
6812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006813do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 char_u *buf;
6816 char_u *p;
6817 char_u *q;
6818
6819 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006820 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006821 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 size_t len, totlen;
6823
6824 size_t split_len = 0;
6825 char_u *split_buf = NULL;
6826 ucmd_T *cmd;
6827#ifdef FEAT_EVAL
6828 scid_T save_current_SID = current_SID;
6829#endif
6830
6831 if (eap->cmdidx == CMD_USER)
6832 cmd = USER_CMD(eap->useridx);
6833 else
6834 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6835
6836 /*
6837 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006838 * First round: "buf" is NULL, compute length, allocate "buf".
6839 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
6841 buf = NULL;
6842 for (;;)
6843 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006844 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006845 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006847
6848 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006850 start = vim_strchr(p, '<');
6851 if (start != NULL)
6852 end = vim_strchr(start + 1, '>');
6853 if (buf != NULL)
6854 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006855 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6856 ;
6857 if (*ksp == K_SPECIAL
6858 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006859 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6860# ifdef FEAT_GUI
6861 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6862# endif
6863 ))
6864 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006865 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006866 * KS_SPECIAL KE_FILLER, like for mappings, but
6867 * do_cmdline() doesn't handle that, so convert it back.
6868 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6869 len = ksp - p;
6870 if (len > 0)
6871 {
6872 mch_memmove(q, p, len);
6873 q += len;
6874 }
6875 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6876 p = ksp + 3;
6877 continue;
6878 }
6879 }
6880
6881 /* break if there no <item> is found */
6882 if (start == NULL || end == NULL)
6883 break;
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 /* Include the '>' */
6886 ++end;
6887
6888 /* Take everything up to the '<' */
6889 len = start - p;
6890 if (buf == NULL)
6891 totlen += len;
6892 else
6893 {
6894 mch_memmove(q, p, len);
6895 q += len;
6896 }
6897
6898 len = uc_check_code(start, end - start, q, cmd, eap,
6899 &split_buf, &split_len);
6900 if (len == (size_t)-1)
6901 {
6902 /* no match, continue after '<' */
6903 p = start + 1;
6904 len = 1;
6905 }
6906 else
6907 p = end;
6908 if (buf == NULL)
6909 totlen += len;
6910 else
6911 q += len;
6912 }
6913 if (buf != NULL) /* second time here, finished */
6914 {
6915 STRCPY(q, p);
6916 break;
6917 }
6918
6919 totlen += STRLEN(p); /* Add on the trailing characters */
6920 buf = alloc((unsigned)(totlen + 1));
6921 if (buf == NULL)
6922 {
6923 vim_free(split_buf);
6924 return;
6925 }
6926 }
6927
6928#ifdef FEAT_EVAL
6929 current_SID = cmd->uc_scriptID;
6930#endif
6931 (void)do_cmdline(buf, eap->getline, eap->cookie,
6932 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6933#ifdef FEAT_EVAL
6934 current_SID = save_current_SID;
6935#endif
6936 vim_free(buf);
6937 vim_free(split_buf);
6938}
6939
6940# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6941 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006942get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6945}
6946
6947/*
6948 * Function given to ExpandGeneric() to obtain the list of user command names.
6949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006951get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
6953 if (idx < curbuf->b_ucmds.ga_len)
6954 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6955 idx -= curbuf->b_ucmds.ga_len;
6956 if (idx < ucmds.ga_len)
6957 return USER_CMD(idx)->uc_name;
6958 return NULL;
6959}
6960
6961/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006962 * Function given to ExpandGeneric() to obtain the list of user address type names.
6963 */
6964 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006965get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006966{
6967 return (char_u *)addr_type_complete[idx].name;
6968}
6969
6970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * Function given to ExpandGeneric() to obtain the list of user command
6972 * attributes.
6973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006978 {"addr", "bang", "bar", "buffer", "complete",
6979 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006981 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 return NULL;
6983 return (char_u *)user_cmd_flags[idx];
6984}
6985
6986/*
6987 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006990get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6993
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006994 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 return NULL;
6996 return (char_u *)user_cmd_nargs[idx];
6997}
6998
6999/*
7000 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007003get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 return (char_u *)command_complete[idx].name;
7006}
7007# endif /* FEAT_CMDL_COMPL */
7008
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007009/*
7010 * Parse address type argument
7011 */
7012 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013parse_addr_type_arg(
7014 char_u *value,
7015 int vallen,
7016 long *argt,
7017 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007018{
7019 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007020
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007021 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7022 {
7023 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7024 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7025 if (a && b)
7026 {
7027 *addr_type_arg = addr_type_complete[i].expand;
7028 break;
7029 }
7030 }
7031
7032 if (addr_type_complete[i].expand == -1)
7033 {
7034 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007035
Bram Moolenaar1c465442017-03-12 20:10:05 +01007036 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007037 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007038 err[i] = NUL;
7039 EMSG2(_("E180: Invalid address type value: %s"), err);
7040 return FAIL;
7041 }
7042
7043 if (*addr_type_arg != ADDR_LINES)
7044 *argt |= NOTADR;
7045
7046 return OK;
7047}
7048
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049#endif /* FEAT_USR_CMDS */
7050
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007051#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7052/*
7053 * Parse a completion argument "value[vallen]".
7054 * The detected completion goes in "*complp", argument type in "*argt".
7055 * When there is an argument, for function and user defined completion, it's
7056 * copied to allocated memory and stored in "*compl_arg".
7057 * Returns FAIL if something is wrong.
7058 */
7059 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007060parse_compl_arg(
7061 char_u *value,
7062 int vallen,
7063 int *complp,
7064 long *argt,
7065 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007066{
7067 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007068# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007069 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007070# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007071 int i;
7072 int valend = vallen;
7073
7074 /* Look for any argument part - which is the part after any ',' */
7075 for (i = 0; i < vallen; ++i)
7076 {
7077 if (value[i] == ',')
7078 {
7079 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007080# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007081 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007082# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007083 valend = i;
7084 break;
7085 }
7086 }
7087
7088 for (i = 0; command_complete[i].expand != 0; ++i)
7089 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007090 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007091 && STRNCMP(value, command_complete[i].name, valend) == 0)
7092 {
7093 *complp = command_complete[i].expand;
7094 if (command_complete[i].expand == EXPAND_BUFFERS)
7095 *argt |= BUFNAME;
7096 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7097 || command_complete[i].expand == EXPAND_FILES)
7098 *argt |= XFILE;
7099 break;
7100 }
7101 }
7102
7103 if (command_complete[i].expand == 0)
7104 {
7105 EMSG2(_("E180: Invalid complete value: %s"), value);
7106 return FAIL;
7107 }
7108
7109# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7110 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7111 && arg != NULL)
7112# else
7113 if (arg != NULL)
7114# endif
7115 {
7116 EMSG(_("E468: Completion argument only allowed for custom completion"));
7117 return FAIL;
7118 }
7119
7120# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7121 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7122 && arg == NULL)
7123 {
7124 EMSG(_("E467: Custom completion requires a function argument"));
7125 return FAIL;
7126 }
7127
7128 if (arg != NULL)
7129 *compl_arg = vim_strnsave(arg, (int)arglen);
7130# endif
7131 return OK;
7132}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007133
7134 int
7135cmdcomplete_str_to_type(char_u *complete_str)
7136{
7137 int i;
7138
7139 for (i = 0; command_complete[i].expand != 0; ++i)
7140 if (STRCMP(complete_str, command_complete[i].name) == 0)
7141 return command_complete[i].expand;
7142
7143 return EXPAND_NOTHING;
7144}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007145#endif
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
Bram Moolenaare6850792010-05-14 15:28:44 +02007150 if (*eap->arg == NUL)
7151 {
7152#ifdef FEAT_EVAL
7153 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7154 char_u *p = NULL;
7155
7156 if (expr != NULL)
7157 {
7158 ++emsg_off;
7159 p = eval_to_string(expr, NULL, FALSE);
7160 --emsg_off;
7161 vim_free(expr);
7162 }
7163 if (p != NULL)
7164 {
7165 MSG(p);
7166 vim_free(p);
7167 }
7168 else
7169 MSG("default");
7170#else
7171 MSG(_("unknown"));
7172#endif
7173 }
7174 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007175 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176}
7177
7178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007179ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 if (*eap->arg == NUL && eap->cmd[2] == '!')
7182 MSG(_("Greetings, Vim user!"));
7183 do_highlight(eap->arg, eap->forceit, FALSE);
7184}
7185
7186
7187/*
7188 * Call this function if we thought we were going to exit, but we won't
7189 * (because of an error). May need to restore the terminal mode.
7190 */
7191 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007192not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 exiting = FALSE;
7195 settmode(TMODE_RAW);
7196}
7197
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007198 static int
7199before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7200{
7201 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7202
7203 /* Bail out when autocommands closed the window.
7204 * Refuse to quit when the buffer in the last window is being closed (can
7205 * only happen in autocommands). */
7206 if (!win_valid(wp)
7207 || curbuf_locked()
7208 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7209 return TRUE;
7210
7211 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7212 {
7213 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7214 /* Refuse to quit when locked or when the buffer in the last window is
7215 * being closed (can only happen in autocommands). */
7216 if (curbuf_locked()
7217 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7218 return TRUE;
7219 }
7220
7221 return FALSE;
7222}
7223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007225 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007226 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 */
7228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007229ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007231 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233#ifdef FEAT_CMDWIN
7234 if (cmdwin_type != 0)
7235 {
7236 cmdwin_result = Ctrl_C;
7237 return;
7238 }
7239#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007240 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007241 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007242 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007243 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007244 return;
7245 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007246 if (eap->addr_count > 0)
7247 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007248 int wnr = eap->line2;
7249
7250 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7251 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007252 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007253 }
7254 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007255 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007256
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007257 /* Refuse to quit when locked. */
7258 if (curbuf_locked())
7259 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007260
7261 /* Trigger QuitPre and maybe ExitPre */
7262 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007263 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265#ifdef FEAT_NETBEANS_INTG
7266 netbeansForcedQuit = eap->forceit;
7267#endif
7268
7269 /*
7270 * If there are more files or windows we won't exit.
7271 */
7272 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7273 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007274 if ((!buf_hide(wp->w_buffer)
7275 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007276 | (eap->forceit ? CCGD_FORCEIT : 0)
7277 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007279 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
7281 not_exiting();
7282 }
7283 else
7284 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007285 /* quit last window
7286 * Note: only_one_window() returns true, even so a help window is
7287 * still open. In that case only quit, if no address has been
7288 * specified. Example:
7289 * :h|wincmd w|1q - don't quit
7290 * :h|wincmd w|q - quit
7291 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007292 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007294 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007295#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007299 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
7301}
7302
7303/*
7304 * ":cquit".
7305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007307ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
7309 getout(1); /* this does not always pass on the exit code to the Manx
7310 compiler. why? */
7311}
7312
7313/*
7314 * ":qall": try to quit all windows
7315 */
7316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007317ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
7319# ifdef FEAT_CMDWIN
7320 if (cmdwin_type != 0)
7321 {
7322 if (eap->forceit)
7323 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7324 else
7325 cmdwin_result = K_XF2;
7326 return;
7327 }
7328# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007329
7330 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007331 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007332 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007333 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007334 return;
7335 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007336
7337 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007338 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007339
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007341 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 getout(0);
7343 not_exiting();
7344}
7345
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346/*
7347 * ":close": close current window, unless it is the last one
7348 */
7349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007350ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007352 win_T *win;
7353 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007354#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007356 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007358#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007359 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007360 {
7361 if (eap->addr_count == 0)
7362 ex_win_close(eap->forceit, curwin, NULL);
7363 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007364 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007365 {
7366 winnr++;
7367 if (winnr == eap->line2)
7368 break;
7369 }
7370 if (win == NULL)
7371 win = lastwin;
7372 ex_win_close(eap->forceit, win, NULL);
7373 }
7374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375}
7376
Bram Moolenaar4033c552017-09-16 20:54:51 +02007377#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378/*
7379 * ":pclose": Close any preview window.
7380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007382ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383{
7384 win_T *win;
7385
Bram Moolenaar29323592016-07-24 22:04:11 +02007386 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387 if (win->w_p_pvw)
7388 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007389 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390 break;
7391 }
7392}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007393#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007394
Bram Moolenaarf740b292006-02-16 22:11:02 +00007395/*
7396 * Close window "win" and take care of handling closing the last window for a
7397 * modified buffer.
7398 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007400ex_win_close(
7401 int forceit,
7402 win_T *win,
7403 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 int need_hide;
7406 buf_T *buf = win->w_buffer;
7407
7408 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007409 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007411#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if ((p_confirm || cmdmod.confirm) && p_write)
7413 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007414 bufref_T bufref;
7415
7416 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007418 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 return;
7420 need_hide = FALSE;
7421 }
7422 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007425 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 return;
7427 }
7428 }
7429
Bram Moolenaar4033c552017-09-16 20:54:51 +02007430#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007432#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007435 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007436 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007437 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007438 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007442 * Handle the argument for a tabpage related ex command.
7443 * Returns a tabpage number.
7444 * When an error is encountered then eap->errmsg is set.
7445 */
7446 static int
7447get_tabpage_arg(exarg_T *eap)
7448{
7449 int tab_number;
7450 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7451
7452 if (eap->arg && *eap->arg != NUL)
7453 {
7454 char_u *p = eap->arg;
7455 char_u *p_save;
7456 int relative = 0; /* argument +N/-N means: go to N places to the
7457 * right/left relative to the current position. */
7458
7459 if (*p == '-')
7460 {
7461 relative = -1;
7462 p++;
7463 }
7464 else if (*p == '+')
7465 {
7466 relative = 1;
7467 p++;
7468 }
7469
7470 p_save = p;
7471 tab_number = getdigits(&p);
7472
7473 if (relative == 0)
7474 {
7475 if (STRCMP(p, "$") == 0)
7476 tab_number = LAST_TAB_NR;
7477 else if (p == p_save || *p_save == '-' || *p != NUL
7478 || tab_number > LAST_TAB_NR)
7479 {
7480 /* No numbers as argument. */
7481 eap->errmsg = e_invarg;
7482 goto theend;
7483 }
7484 }
7485 else
7486 {
7487 if (*p_save == NUL)
7488 tab_number = 1;
7489 else if (p == p_save || *p_save == '-' || *p != NUL
7490 || tab_number == 0)
7491 {
7492 /* No numbers as argument. */
7493 eap->errmsg = e_invarg;
7494 goto theend;
7495 }
7496 tab_number = tab_number * relative + tabpage_index(curtab);
7497 if (!unaccept_arg0 && relative == -1)
7498 --tab_number;
7499 }
7500 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7501 eap->errmsg = e_invarg;
7502 }
7503 else if (eap->addr_count > 0)
7504 {
7505 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007506 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007507 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007508 tab_number = 0;
7509 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007510 else
7511 {
7512 tab_number = eap->line2;
7513 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7514 {
7515 --tab_number;
7516 if (tab_number < unaccept_arg0)
7517 eap->errmsg = e_invarg;
7518 }
7519 }
7520 }
7521 else
7522 {
7523 switch (eap->cmdidx)
7524 {
7525 case CMD_tabnext:
7526 tab_number = tabpage_index(curtab) + 1;
7527 if (tab_number > LAST_TAB_NR)
7528 tab_number = 1;
7529 break;
7530 case CMD_tabmove:
7531 tab_number = LAST_TAB_NR;
7532 break;
7533 default:
7534 tab_number = tabpage_index(curtab);
7535 }
7536 }
7537
7538theend:
7539 return tab_number;
7540}
7541
7542/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007543 * ":tabclose": close current tab page, unless it is the last one.
7544 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 */
7546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007547ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007549 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007550 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007552# ifdef FEAT_CMDWIN
7553 if (cmdwin_type != 0)
7554 cmdwin_result = K_IGNORE;
7555 else
7556# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007557 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007558 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007561 tab_number = get_tabpage_arg(eap);
7562 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007563 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007564 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007565 if (tp == NULL)
7566 {
7567 beep_flush();
7568 return;
7569 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007570 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007571 {
7572 tabpage_close_other(tp, eap->forceit);
7573 return;
7574 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007575 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007576 tabpage_close(eap->forceit);
7577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007579}
7580
7581/*
7582 * ":tabonly": close all tab pages except the current one
7583 */
7584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007585ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007586{
7587 tabpage_T *tp;
7588 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007589 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007590
7591# ifdef FEAT_CMDWIN
7592 if (cmdwin_type != 0)
7593 cmdwin_result = K_IGNORE;
7594 else
7595# endif
7596 if (first_tabpage->tp_next == NULL)
7597 MSG(_("Already only one tab page"));
7598 else
7599 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007600 tab_number = get_tabpage_arg(eap);
7601 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007602 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007603 goto_tabpage(tab_number);
7604 /* Repeat this up to a 1000 times, because autocommands may
7605 * mess up the lists. */
7606 for (done = 0; done < 1000; ++done)
7607 {
7608 FOR_ALL_TABPAGES(tp)
7609 if (tp->tp_topframe != topframe)
7610 {
7611 tabpage_close_other(tp, eap->forceit);
7612 /* if we failed to close it quit */
7613 if (valid_tabpage(tp))
7614 done = 1000;
7615 /* start over, "tp" is now invalid */
7616 break;
7617 }
7618 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007619 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007620 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007621 }
7622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
7625/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007626 * Close the current tab page.
7627 */
7628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007629tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007630{
7631 /* First close all the windows but the current one. If that worked then
7632 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007633 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007634 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007635 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007636 ex_win_close(forceit, curwin, NULL);
7637# ifdef FEAT_GUI
7638 need_mouse_correct = TRUE;
7639# endif
7640}
7641
7642/*
7643 * Close tab page "tp", which is not the current tab page.
7644 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007645 * Also takes care of the tab pages line disappearing when closing the
7646 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007647 */
7648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007649tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007650{
7651 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007652 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007653 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007654
7655 /* Limit to 1000 windows, autocommands may add a window while we close
7656 * one. OK, so I'm paranoid... */
7657 while (++done < 1000)
7658 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007659 wp = tp->tp_firstwin;
7660 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007661
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662 /* Autocommands may delete the tab page under our fingers and we may
7663 * fail to close a window with a modified buffer. */
7664 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007665 break;
7666 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007667
Bram Moolenaar29323592016-07-24 22:04:11 +02007668 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007669
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007670 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007671 if (h != tabline_height())
7672 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007673}
7674
7675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 * ":only".
7677 */
7678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007679ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007681 win_T *wp;
7682 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683# ifdef FEAT_GUI
7684 need_mouse_correct = TRUE;
7685# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007686 if (eap->addr_count > 0)
7687 {
7688 wnr = eap->line2;
7689 for (wp = firstwin; --wnr > 0; )
7690 {
7691 if (wp->w_next == NULL)
7692 break;
7693 else
7694 wp = wp->w_next;
7695 }
7696 win_goto(wp);
7697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 close_others(TRUE, eap->forceit);
7699}
7700
7701/*
7702 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007703 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007705 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007706ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
7708 if (eap->addr_count == 0)
7709 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007710 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
7713 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007714ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007716 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007717 if (!eap->skip)
7718 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007719#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007720 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007721#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007722 if (eap->addr_count == 0)
7723 win_close(curwin, FALSE); /* don't free buffer */
7724 else
7725 {
7726 int winnr = 0;
7727 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007728
Bram Moolenaar2256c992016-11-15 21:17:07 +01007729 FOR_ALL_WINDOWS(win)
7730 {
7731 winnr++;
7732 if (winnr == eap->line2)
7733 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007734 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007735 if (win == NULL)
7736 win = lastwin;
7737 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740}
7741
7742/*
7743 * ":stop" and ":suspend": Suspend Vim.
7744 */
7745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007746ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748 /*
7749 * Disallow suspending for "rvim".
7750 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007751 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {
7753 if (!eap->forceit)
7754 autowrite_all();
7755 windgoto((int)Rows - 1, 0);
7756 out_char('\n');
7757 out_flush();
7758 stoptermcap();
7759 out_flush(); /* needed for SUN to restore xterm buffer */
7760#ifdef FEAT_TITLE
7761 mch_restore_title(3); /* restore window titles */
7762#endif
7763 ui_suspend(); /* call machine specific function */
7764#ifdef FEAT_TITLE
7765 maketitle();
7766 resettitle(); /* force updating the title */
7767#endif
7768 starttermcap();
7769 scroll_start(); /* scroll screen before redrawing */
7770 redraw_later_clear();
7771 shell_resized(); /* may have resized window */
7772 }
7773}
7774
7775/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007776 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 */
7778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007779ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781#ifdef FEAT_CMDWIN
7782 if (cmdwin_type != 0)
7783 {
7784 cmdwin_result = Ctrl_C;
7785 return;
7786 }
7787#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007788 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007789 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007790 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007791 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007792 return;
7793 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007794
7795 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007796 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
7798 /*
7799 * if more files or windows we won't exit
7800 */
7801 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7802 exiting = TRUE;
7803 if ( ((eap->cmdidx == CMD_wq
7804 || curbufIsChanged())
7805 && do_write(eap) == FAIL)
7806 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007807 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {
7809 not_exiting();
7810 }
7811 else
7812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007815 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816# ifdef FEAT_GUI
7817 need_mouse_correct = TRUE;
7818# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007819 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007820 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 }
7822}
7823
7824/*
7825 * ":print", ":list", ":number".
7826 */
7827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007828ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007830 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7831 EMSG(_(e_emptybuf));
7832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007834 for ( ;!got_int; ui_breakcheck())
7835 {
7836 print_line(eap->line1,
7837 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7838 || (eap->flags & EXFLAG_NR)),
7839 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7840 if (++eap->line1 > eap->line2)
7841 break;
7842 out_flush(); /* show one line at a time */
7843 }
7844 setpcmark();
7845 /* put cursor at last line */
7846 curwin->w_cursor.lnum = eap->line2;
7847 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851}
7852
7853#ifdef FEAT_BYTEOFF
7854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007855ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 goto_byte(eap->line2);
7858}
7859#endif
7860
7861/*
7862 * ":shell".
7863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007865ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
7867 do_shell(NULL, 0);
7868}
7869
Bram Moolenaar4033c552017-09-16 20:54:51 +02007870#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007872 || defined(FEAT_GUI_MSWIN) \
7873 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 || defined(PROTO)
7875
7876/*
7877 * Handle a file drop. The code is here because a drop is *nearly* like an
7878 * :args command, but not quite (we have a list of exact filenames, so we
7879 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7880 * code is very similar to :args and hence needs access to a lot of the static
7881 * functions in this file.
7882 *
7883 * The list should be allocated using alloc(), as should each item in the
7884 * list. This function takes over responsibility for freeing the list.
7885 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007886 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007887 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 */
7889 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007890handle_drop(
7891 int filec, /* the number of files dropped */
7892 char_u **filev, /* the list of files dropped */
7893 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 exarg_T ea;
7896 int save_msg_scroll = msg_scroll;
7897
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007898 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007899 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007901 if (curbuf_locked())
7902 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007903
Bram Moolenaarc236c162008-07-13 17:41:49 +00007904 /* When the screen is being updated we should not change buffers and
7905 * windows structures, it may cause freed memory to be used. */
7906 if (updating_screen)
7907 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
7909 /* Check whether the current buffer is changed. If so, we will need
7910 * to split the current window or data could be lost.
7911 * We don't need to check if the 'hidden' option is set, as in this
7912 * case the buffer won't be lost.
7913 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007914 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {
7916 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007917 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 --emsg_off;
7919 }
7920 if (split)
7921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 if (win_split(0, 0) == FAIL)
7923 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007924 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
7926 /* When splitting the window, create a new alist. Otherwise the
7927 * existing one is overwritten. */
7928 alist_unlink(curwin->w_alist);
7929 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931
7932 /*
7933 * Set up the new argument list.
7934 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007935 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937 /*
7938 * Move to the first file.
7939 */
7940 /* Fake up a minimal "next" command for do_argfile() */
7941 vim_memset(&ea, 0, sizeof(ea));
7942 ea.cmd = (char_u *)"next";
7943 do_argfile(&ea, 0);
7944
7945 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7946 * mode that is not needed here. */
7947 need_start_insertmode = FALSE;
7948
7949 /* Restore msg_scroll, otherwise a following command may cause scrolling
7950 * unexpectedly. The screen will be redrawn by the caller, thus
7951 * msg_scroll being set by displaying a message is irrelevant. */
7952 msg_scroll = save_msg_scroll;
7953}
7954#endif
7955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956/*
7957 * Clear an argument list: free all file names and reset it to zero entries.
7958 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007959 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007960alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 while (--al->al_ga.ga_len >= 0)
7963 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7964 ga_clear(&al->al_ga);
7965}
7966
7967/*
7968 * Init an argument list.
7969 */
7970 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007971alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7974}
7975
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976/*
7977 * Remove a reference from an argument list.
7978 * Ignored when the argument list is the global one.
7979 * If the argument list is no longer used by any window, free it.
7980 */
7981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007982alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983{
7984 if (al != &global_alist && --al->al_refcount <= 0)
7985 {
7986 alist_clear(al);
7987 vim_free(al);
7988 }
7989}
7990
Bram Moolenaar4033c552017-09-16 20:54:51 +02007991#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992/*
7993 * Create a new argument list and use it for the current window.
7994 */
7995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7999 if (curwin->w_alist == NULL)
8000 {
8001 curwin->w_alist = &global_alist;
8002 ++global_alist.al_refcount;
8003 }
8004 else
8005 {
8006 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008007 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 alist_init(curwin->w_alist);
8009 }
8010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#endif
8012
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008013#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014/*
8015 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008016 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8017 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 */
8019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008020alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021{
8022 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008023 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 char_u **new_arg_files;
8025 int new_arg_file_count;
8026 char_u *save_p_su = p_su;
8027 int i;
8028
8029 /* Don't use 'suffixes' here. This should work like the shell did the
8030 * expansion. Also, the vimrc file isn't read yet, thus the user
8031 * can't set the options. */
8032 p_su = empty_option;
8033 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8034 if (old_arg_files != NULL)
8035 {
8036 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008037 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8038 old_arg_count = GARGCOUNT;
8039 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008041 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 && new_arg_file_count > 0)
8043 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008044 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8045 TRUE, fnum_list, fnum_len);
8046 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 }
8049 p_su = save_p_su;
8050}
8051#endif
8052
8053/*
8054 * Set the argument list for the current window.
8055 * Takes over the allocated files[] and the allocated fnames in it.
8056 */
8057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008058alist_set(
8059 alist_T *al,
8060 int count,
8061 char_u **files,
8062 int use_curbuf,
8063 int *fnum_list,
8064 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008067 static int recursive = 0;
8068
8069 if (recursive)
8070 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008071 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008072 return;
8073 }
8074 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075
8076 alist_clear(al);
8077 if (ga_grow(&al->al_ga, count) == OK)
8078 {
8079 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008080 {
8081 if (got_int)
8082 {
8083 /* When adding many buffers this can take a long time. Allow
8084 * interrupting here. */
8085 while (i < count)
8086 vim_free(files[i++]);
8087 break;
8088 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008089
8090 /* May set buffer name of a buffer previously used for the
8091 * argument list, so that it's re-used by alist_add. */
8092 if (fnum_list != NULL && i < fnum_len)
8093 buf_set_name(fnum_list[i], files[i]);
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008096 ui_breakcheck();
8097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 vim_free(files);
8099 }
8100 else
8101 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008104
8105 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106}
8107
8108/*
8109 * Add file "fname" to argument list "al".
8110 * "fname" must have been allocated and "al" must have been checked for room.
8111 */
8112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008113alist_add(
8114 alist_T *al,
8115 char_u *fname,
8116 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117{
8118 if (fname == NULL) /* don't add NULL file names */
8119 return;
8120#ifdef BACKSLASH_IN_FILENAME
8121 slash_adjust(fname);
8122#endif
8123 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8124 if (set_fnum > 0)
8125 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8126 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8127 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128}
8129
8130#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8131/*
8132 * Adjust slashes in file names. Called after 'shellslash' was set.
8133 */
8134 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008135alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008139 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141 for (i = 0; i < GARGCOUNT; ++i)
8142 if (GARGLIST[i].ae_fname != NULL)
8143 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008144 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (wp->w_alist != &global_alist)
8146 for (i = 0; i < WARGCOUNT(wp); ++i)
8147 if (WARGLIST(wp)[i].ae_fname != NULL)
8148 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149}
8150#endif
8151
8152/*
8153 * ":preserve".
8154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008158 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 ml_preserve(curbuf, TRUE);
8160}
8161
8162/*
8163 * ":recover".
8164 */
8165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008166ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
8168 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8169 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008170 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8171 | CCGD_MULTWIN
8172 | (eap->forceit ? CCGD_FORCEIT : 0)
8173 | CCGD_EXCMD)
8174
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 && (*eap->arg == NUL
8176 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8177 ml_recover();
8178 recoverymode = FALSE;
8179}
8180
8181/*
8182 * Command modifier used in a wrong way.
8183 */
8184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008185ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
8187 eap->errmsg = e_invcmd;
8188}
8189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190/*
8191 * :sview [+command] file split window with new file, read-only
8192 * :split [[+command] file] split window with current or new file
8193 * :vsplit [[+command] file] split window vertically with current or new file
8194 * :new [[+command] file] split window with no or new file
8195 * :vnew [[+command] file] split vertically window with no or new file
8196 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008197 *
8198 * :tabedit open new Tab page with empty window
8199 * :tabedit [+command] file open new Tab page and edit "file"
8200 * :tabnew [[+command] file] just like :tabedit
8201 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 */
8203 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008204ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008206 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008207#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008209#endif
8210#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213
Bram Moolenaar4033c552017-09-16 20:54:51 +02008214#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217
Bram Moolenaar4033c552017-09-16 20:54:51 +02008218#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008220 * quickfix windows. But it's OK when doing ":tab split". */
8221 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {
8223 if (eap->cmdidx == CMD_split)
8224 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 if (eap->cmdidx == CMD_vsplit)
8226 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
Bram Moolenaar4033c552017-09-16 20:54:51 +02008230#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008231 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
8233 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8234 FNAME_MESS, TRUE, curbuf->b_ffname);
8235 if (fname == NULL)
8236 goto theend;
8237 eap->arg = fname;
8238 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008239# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008240 else
8241# endif
8242#endif
8243#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 && eap->cmdidx != CMD_new)
8247 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008248 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008249# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008250 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008251# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008252 au_has_group((char_u *)"FileExplorer"))
8253 {
8254 /* No browsing supported but we do have the file explorer:
8255 * Edit the directory. */
8256 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8257 eap->arg = (char_u *)".";
8258 }
8259 else
8260 {
8261 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008263 if (fname == NULL)
8264 goto theend;
8265 eap->arg = fname;
8266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 }
8268 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008271 /*
8272 * Either open new tab page or split the window.
8273 */
8274 if (eap->cmdidx == CMD_tabedit
8275 || eap->cmdidx == CMD_tabfind
8276 || eap->cmdidx == CMD_tabnew)
8277 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008278 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8279 : eap->addr_count == 0 ? 0
8280 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008281 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008282 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008283
8284 /* set the alternate buffer for the window we came from */
8285 if (curwin != old_curwin
8286 && win_valid(old_curwin)
8287 && old_curwin->w_buffer != curbuf
8288 && !cmdmod.keepalt)
8289 old_curwin->w_alt_fnum = curbuf->b_fnum;
8290 }
8291 }
8292 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 /* Reset 'scrollbind' when editing another file, but keep it when
8296 * doing ":split" without arguments. */
8297 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008298# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008302 {
8303 RESET_BINDING(curwin);
8304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 else
8306 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 do_exedit(eap, old_curwin);
8308 }
8309
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315theend:
8316 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008319
8320/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008321 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322 */
8323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008324tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008325{
8326 exarg_T ea;
8327
8328 vim_memset(&ea, 0, sizeof(ea));
8329 ea.cmdidx = CMD_tabnew;
8330 ea.cmd = (char_u *)"tabn";
8331 ea.arg = (char_u *)"";
8332 ex_splitview(&ea);
8333}
8334
8335/*
8336 * :tabnext command
8337 */
8338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008339ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008340{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008341 int tab_number;
8342
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008343 switch (eap->cmdidx)
8344 {
8345 case CMD_tabfirst:
8346 case CMD_tabrewind:
8347 goto_tabpage(1);
8348 break;
8349 case CMD_tablast:
8350 goto_tabpage(9999);
8351 break;
8352 case CMD_tabprevious:
8353 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008354 if (eap->arg && *eap->arg != NUL)
8355 {
8356 char_u *p = eap->arg;
8357 char_u *p_save = p;
8358
8359 tab_number = getdigits(&p);
8360 if (p == p_save || *p_save == '-' || *p != NUL
8361 || tab_number == 0)
8362 {
8363 /* No numbers as argument. */
8364 eap->errmsg = e_invarg;
8365 return;
8366 }
8367 }
8368 else
8369 {
8370 if (eap->addr_count == 0)
8371 tab_number = 1;
8372 else
8373 {
8374 tab_number = eap->line2;
8375 if (tab_number < 1)
8376 {
8377 eap->errmsg = e_invrange;
8378 return;
8379 }
8380 }
8381 }
8382 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008383 break;
8384 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008385 tab_number = get_tabpage_arg(eap);
8386 if (eap->errmsg == NULL)
8387 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008388 break;
8389 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008390}
8391
8392/*
8393 * :tabmove command
8394 */
8395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008396ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008397{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008398 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008399
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008400 tab_number = get_tabpage_arg(eap);
8401 if (eap->errmsg == NULL)
8402 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008403}
8404
8405/*
8406 * :tabs command: List tabs and their contents.
8407 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008409ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008410{
8411 tabpage_T *tp;
8412 win_T *wp;
8413 int tabcount = 1;
8414
8415 msg_start();
8416 msg_scroll = TRUE;
8417 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8418 {
8419 msg_putchar('\n');
8420 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008421 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008422 out_flush(); /* output one line at a time */
8423 ui_breakcheck();
8424
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008425 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426 wp = firstwin;
8427 else
8428 wp = tp->tp_firstwin;
8429 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8430 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008431 msg_putchar('\n');
8432 msg_putchar(wp == curwin ? '>' : ' ');
8433 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008434 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8435 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008437 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008438 else
8439 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8440 IObuff, IOSIZE, TRUE);
8441 msg_outtrans(IObuff);
8442 out_flush(); /* output one line at a time */
8443 ui_breakcheck();
8444 }
8445 }
8446}
8447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448/*
8449 * ":mode": Set screen mode.
8450 * If no argument given, just get the screen size and redraw.
8451 */
8452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 if (*eap->arg == NUL)
8456 shell_resized();
8457 else
8458 mch_screenmode(eap->arg);
8459}
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461/*
8462 * ":resize".
8463 * set, increment or decrement current window height
8464 */
8465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008466ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468 int n;
8469 win_T *wp = curwin;
8470
8471 if (eap->addr_count > 0)
8472 {
8473 n = eap->line2;
8474 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8475 ;
8476 }
8477
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008478# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (cmdmod.split & WSP_VERT)
8483 {
8484 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008485 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8487 n = 9999;
8488 win_setwidth_win((int)n, wp);
8489 }
8490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {
8492 if (*eap->arg == '-' || *eap->arg == '+')
8493 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008494 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 n = 9999;
8496 win_setheight_win((int)n, wp);
8497 }
8498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500/*
8501 * ":find [+command] <file>" command.
8502 */
8503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008504ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506#ifdef FEAT_SEARCHPATH
8507 char_u *fname;
8508 int count;
8509
8510 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8511 TRUE, curbuf->b_ffname);
8512 if (eap->addr_count > 0)
8513 {
8514 /* Repeat finding the file "count" times. This matters when it
8515 * appears several times in the path. */
8516 count = eap->line2;
8517 while (fname != NULL && --count > 0)
8518 {
8519 vim_free(fname);
8520 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8521 FALSE, curbuf->b_ffname);
8522 }
8523 }
8524
8525 if (fname != NULL)
8526 {
8527 eap->arg = fname;
8528#endif
8529 do_exedit(eap, NULL);
8530#ifdef FEAT_SEARCHPATH
8531 vim_free(fname);
8532 }
8533#endif
8534}
8535
8536/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008537 * ":open" simulation: for now just work like ":visual".
8538 */
8539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008540ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008541{
8542 regmatch_T regmatch;
8543 char_u *p;
8544
8545 curwin->w_cursor.lnum = eap->line2;
8546 beginline(BL_SOL | BL_FIX);
8547 if (*eap->arg == '/')
8548 {
8549 /* ":open /pattern/": put cursor in column found with pattern */
8550 ++eap->arg;
8551 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8552 *p = NUL;
8553 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8554 if (regmatch.regprog != NULL)
8555 {
8556 regmatch.rm_ic = p_ic;
8557 p = ml_get_curline();
8558 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008560 else
8561 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008562 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008563 }
8564 /* Move to the NUL, ignore any other arguments. */
8565 eap->arg += STRLEN(eap->arg);
8566 }
8567 check_cursor();
8568
8569 eap->cmdidx = CMD_visual;
8570 do_exedit(eap, NULL);
8571}
8572
8573/*
8574 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 */
8576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 do_exedit(eap, NULL);
8580}
8581
8582/*
8583 * ":edit <file>" command and alikes.
8584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008586do_exedit(
8587 exarg_T *eap,
8588 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008592 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594 /*
8595 * ":vi" command ends Ex mode.
8596 */
8597 if (exmode_active && (eap->cmdidx == CMD_visual
8598 || eap->cmdidx == CMD_view))
8599 {
8600 exmode_active = FALSE;
8601 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 {
8603 /* Special case: ":global/pat/visual\NLvi-commands" */
8604 if (global_busy)
8605 {
8606 int rd = RedrawingDisabled;
8607 int nwr = no_wait_return;
8608 int ms = msg_scroll;
8609#ifdef FEAT_GUI
8610 int he = hold_gui_events;
8611#endif
8612
8613 if (eap->nextcmd != NULL)
8614 {
8615 stuffReadbuff(eap->nextcmd);
8616 eap->nextcmd = NULL;
8617 }
8618
8619 if (exmode_was != EXMODE_VIM)
8620 settmode(TMODE_RAW);
8621 RedrawingDisabled = 0;
8622 no_wait_return = 0;
8623 need_wait_return = FALSE;
8624 msg_scroll = 0;
8625#ifdef FEAT_GUI
8626 hold_gui_events = 0;
8627#endif
8628 must_redraw = CLEAR;
8629
8630 main_loop(FALSE, TRUE);
8631
8632 RedrawingDisabled = rd;
8633 no_wait_return = nwr;
8634 msg_scroll = ms;
8635#ifdef FEAT_GUI
8636 hold_gui_events = he;
8637#endif
8638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642
8643 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008644 || eap->cmdidx == CMD_tabnew
8645 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008646 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008648 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 setpcmark();
8650 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008651 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8652 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008654 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 || *eap->arg != NUL
8656#ifdef FEAT_BROWSE
8657 || cmdmod.browse
8658#endif
8659 )
8660 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008661 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8662 * can bring us here, others are stopped earlier. */
8663 if (*eap->arg != NUL && curbuf_locked())
8664 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 n = readonlymode;
8667 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8668 readonlymode = TRUE;
8669 else if (eap->cmdidx == CMD_enew)
8670 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8671 empty buffer */
8672 setpcmark();
8673 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8674 NULL, eap,
8675 /* ":edit" goes to first line if Vi compatible */
8676 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8677 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8678 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008679 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008681 /* after a split we can use an existing buffer */
8682 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683#ifdef FEAT_LISTCMDS
8684 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8685#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008686 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 {
8688 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if (old_curwin != NULL)
8690 {
8691 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008692 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008694#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008695 cleanup_T cs;
8696
8697 /* Reset the error/interrupt/exception state here so that
8698 * aborting() returns FALSE when closing a window. */
8699 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008700#endif
8701#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008703#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008704 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008705
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008706#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008707 /* Restore the error/interrupt/exception state if not
8708 * discarded by a new aborting error, interrupt, or
8709 * uncaught exception. */
8710 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
8713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
8715 else if (readonlymode && curbuf->b_nwindows == 1)
8716 {
8717 /* When editing an already visited buffer, 'readonly' won't be set
8718 * but the previous value is kept. With ":view" and ":sview" we
8719 * want the file to be readonly, except when another window is
8720 * editing the same buffer. */
8721 curbuf->b_p_ro = TRUE;
8722 }
8723 readonlymode = n;
8724 }
8725 else
8726 {
8727 if (eap->do_ecmd_cmd != NULL)
8728 do_cmdline_cmd(eap->do_ecmd_cmd);
8729#ifdef FEAT_TITLE
8730 n = curwin->w_arg_idx_invalid;
8731#endif
8732 check_arg_idx(curwin);
8733#ifdef FEAT_TITLE
8734 if (n != curwin->w_arg_idx_invalid)
8735 maketitle();
8736#endif
8737 }
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 /*
8740 * if ":split file" worked, set alternate file name in old window to new
8741 * file
8742 */
8743 if (old_curwin != NULL
8744 && *eap->arg != NUL
8745 && curwin != old_curwin
8746 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008747 && old_curwin->w_buffer != curbuf
8748 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
8751 ex_no_reprint = TRUE;
8752}
8753
8754#ifndef FEAT_GUI
8755/*
8756 * ":gui" and ":gvim" when there is no GUI.
8757 */
8758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008759ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 eap->errmsg = e_nogvim;
8762}
8763#endif
8764
8765#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008767ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 gui_make_tearoff(eap->arg);
8770}
8771#endif
8772
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008773#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8774 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008776ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008778# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8779 if (gui.in_use)
8780 gui_make_popup(eap->arg, eap->forceit);
8781# ifdef FEAT_TERM_POPUP_MENU
8782 else
8783# endif
8784# endif
8785# ifdef FEAT_TERM_POPUP_MENU
8786 pum_make_popup(eap->arg, eap->forceit);
8787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789#endif
8790
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008792ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
8794 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8795 MSG(_("No swap file"));
8796 else
8797 msg(curbuf->b_ml.ml_mfp->mf_fname);
8798}
8799
8800/*
8801 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8802 * offset.
8803 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008806ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008809 win_T *save_curwin = curwin;
8810 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 long topline;
8812 long y;
8813 linenr_T old_linenr = curwin->w_cursor.lnum;
8814
8815 setpcmark();
8816
8817 /*
8818 * determine max topline
8819 */
8820 if (curwin->w_p_scb)
8821 {
8822 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008823 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
8825 if (wp->w_p_scb && wp->w_buffer)
8826 {
8827 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8828 if (topline > y)
8829 topline = y;
8830 }
8831 }
8832 if (topline < 1)
8833 topline = 1;
8834 }
8835 else
8836 {
8837 topline = 1;
8838 }
8839
8840
8841 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008842 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008844 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
8846 if (curwin->w_p_scb)
8847 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008848 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 y = topline - curwin->w_topline;
8850 if (y > 0)
8851 scrollup(y, TRUE);
8852 else
8853 scrolldown(-y, TRUE);
8854 curwin->w_scbind_pos = topline;
8855 redraw_later(VALID);
8856 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
8859 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008860 curwin = save_curwin;
8861 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (curwin->w_p_scb)
8863 {
8864 did_syncbind = TRUE;
8865 checkpcmark();
8866 if (old_linenr != curwin->w_cursor.lnum)
8867 {
8868 char_u ctrl_o[2];
8869
8870 ctrl_o[0] = Ctrl_O;
8871 ctrl_o[1] = 0;
8872 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8873 }
8874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875}
8876
8877
8878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008879ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008881 int i;
8882 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8883 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884
8885 if (eap->usefilter) /* :r!cmd */
8886 do_bang(1, eap, FALSE, FALSE, TRUE);
8887 else
8888 {
8889 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8890 return;
8891
8892#ifdef FEAT_BROWSE
8893 if (cmdmod.browse)
8894 {
8895 char_u *browseFile;
8896
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008897 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 NULL, NULL, NULL, curbuf);
8899 if (browseFile != NULL)
8900 {
8901 i = readfile(browseFile, NULL,
8902 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8903 vim_free(browseFile);
8904 }
8905 else
8906 i = OK;
8907 }
8908 else
8909#endif
8910 if (*eap->arg == NUL)
8911 {
8912 if (check_fname() == FAIL) /* check for no file name */
8913 return;
8914 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8915 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8916 }
8917 else
8918 {
8919 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8920 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8921 i = readfile(eap->arg, NULL,
8922 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8923
8924 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008925 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008927#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (!aborting())
8929#endif
8930 EMSG2(_(e_notopen), eap->arg);
8931 }
8932 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008933 {
8934 if (empty && exmode_active)
8935 {
8936 /* Delete the empty line that remains. Historically ex does
8937 * this but vi doesn't. */
8938 if (eap->line2 == 0)
8939 lnum = curbuf->b_ml.ml_line_count;
8940 else
8941 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008942 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008943 {
8944 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008945 if (curwin->w_cursor.lnum > 1
8946 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008947 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008948 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008949 }
8950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 }
8954}
8955
Bram Moolenaarea408852005-06-25 22:49:46 +00008956static char_u *prev_dir = NULL;
8957
8958#if defined(EXITFREE) || defined(PROTO)
8959 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008960free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008961{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008962 VIM_CLEAR(prev_dir);
8963 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008964}
8965#endif
8966
Bram Moolenaarf4258302013-06-02 18:20:17 +02008967/*
8968 * Deal with the side effects of changing the current directory.
8969 * When "local" is TRUE then this was after an ":lcd" command.
8970 */
8971 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008972post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008973{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008974 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008975 if (local)
8976 {
8977 /* If still in global directory, need to remember current
8978 * directory as global directory. */
8979 if (globaldir == NULL && prev_dir != NULL)
8980 globaldir = vim_strsave(prev_dir);
8981 /* Remember this local directory for the window. */
8982 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8983 curwin->w_localdir = vim_strsave(NameBuff);
8984 }
8985 else
8986 {
8987 /* We are now in the global directory, no need to remember its
8988 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008989 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008990 }
8991
8992 shorten_fnames(TRUE);
8993}
8994
Bram Moolenaarea408852005-06-25 22:49:46 +00008995
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996/*
8997 * ":cd", ":lcd", ":chdir" and ":lchdir".
8998 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008999 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009000ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 char_u *new_dir;
9003 char_u *tofree;
9004
9005 new_dir = eap->arg;
9006#if !defined(UNIX) && !defined(VMS)
9007 /* for non-UNIX ":cd" means: print current directory */
9008 if (*new_dir == NUL)
9009 ex_pwd(NULL);
9010 else
9011#endif
9012 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009013 if (allbuf_locked())
9014 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009015 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9016 && !eap->forceit)
9017 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009018 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009019 return;
9020 }
9021
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 /* ":cd -": Change to previous directory */
9023 if (STRCMP(new_dir, "-") == 0)
9024 {
9025 if (prev_dir == NULL)
9026 {
9027 EMSG(_("E186: No previous directory"));
9028 return;
9029 }
9030 new_dir = prev_dir;
9031 }
9032
9033 /* Save current directory for next ":cd -" */
9034 tofree = prev_dir;
9035 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9036 prev_dir = vim_strsave(NameBuff);
9037 else
9038 prev_dir = NULL;
9039
9040#if defined(UNIX) || defined(VMS)
9041 /* for UNIX ":cd" means: go to home directory */
9042 if (*new_dir == NUL)
9043 {
9044 /* use NameBuff for home directory name */
9045# ifdef VMS
9046 char_u *p;
9047
9048 p = mch_getenv((char_u *)"SYS$LOGIN");
9049 if (p == NULL || *p == NUL) /* empty is the same as not set */
9050 NameBuff[0] = NUL;
9051 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009052 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053# else
9054 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9055# endif
9056 new_dir = NameBuff;
9057 }
9058#endif
9059 if (new_dir == NULL || vim_chdir(new_dir))
9060 EMSG(_(e_failed));
9061 else
9062 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009063 int is_local_chdir = eap->cmdidx == CMD_lcd
9064 || eap->cmdidx == CMD_lchdir;
9065
9066 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009069 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009071 apply_autocmds(EVENT_DIRCHANGED,
9072 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9073 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 }
9075 vim_free(tofree);
9076 }
9077}
9078
9079/*
9080 * ":pwd".
9081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009083ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
9085 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9086 {
9087#ifdef BACKSLASH_IN_FILENAME
9088 slash_adjust(NameBuff);
9089#endif
9090 msg(NameBuff);
9091 }
9092 else
9093 EMSG(_("E187: Unknown"));
9094}
9095
9096/*
9097 * ":=".
9098 */
9099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009100ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009103 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
9105
9106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009109 int n;
9110 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111
9112 if (cursor_valid())
9113 {
9114 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9115 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009116 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009118
9119 len = eap->line2;
9120 switch (*eap->arg)
9121 {
9122 case 'm': break;
9123 case NUL: len *= 1000L; break;
9124 default: EMSG2(_(e_invarg2), eap->arg); return;
9125 }
9126 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127}
9128
9129/*
9130 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9131 */
9132 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009133do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009136 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
9138 cursor_on();
9139 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009140 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009142 wait_now = msec - done > 1000L ? 1000L : msec - done;
9143#ifdef FEAT_TIMERS
9144 {
9145 long due_time = check_due_timer();
9146
9147 if (due_time > 0 && due_time < wait_now)
9148 wait_now = due_time;
9149 }
9150#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009151#ifdef FEAT_JOB_CHANNEL
9152 if (has_any_channel() && wait_now > 100L)
9153 wait_now = 100L;
9154#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009155 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009156#ifdef FEAT_JOB_CHANNEL
9157 if (has_any_channel())
9158 ui_breakcheck_force(TRUE);
9159 else
9160#endif
9161 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009162#ifdef MESSAGE_QUEUE
9163 /* Process the netbeans and clientserver messages that may have been
9164 * received in the call to ui_breakcheck() when the GUI is in use. This
9165 * may occur when running a test case. */
9166 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169}
9170
9171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009172do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
9174 int mode;
9175 char_u *cmdp;
9176
9177 cmdp = eap->cmd;
9178 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9179
9180 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9181 eap->arg, mode, isabbrev))
9182 {
9183 case 1: EMSG(_(e_invarg));
9184 break;
9185 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9186 break;
9187 }
9188}
9189
9190/*
9191 * ":winsize" command (obsolete).
9192 */
9193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009194ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 int w, h;
9197 char_u *arg = eap->arg;
9198 char_u *p;
9199
9200 w = getdigits(&arg);
9201 arg = skipwhite(arg);
9202 p = arg;
9203 h = getdigits(&arg);
9204 if (*p != NUL && *arg == NUL)
9205 set_shellsize(w, h, TRUE);
9206 else
9207 EMSG(_("E465: :winsize requires two number arguments"));
9208}
9209
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009211ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213 int xchar = NUL;
9214 char_u *p;
9215
9216 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9217 {
9218 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9219 if (eap->arg[1] == NUL)
9220 {
9221 EMSG(_(e_invarg));
9222 return;
9223 }
9224 xchar = eap->arg[1];
9225 p = eap->arg + 2;
9226 }
9227 else
9228 p = eap->arg + 1;
9229
9230 eap->nextcmd = check_nextcmd(p);
9231 p = skipwhite(p);
9232 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9233 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009234 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 {
9236 /* Pass flags on for ":vertical wincmd ]". */
9237 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009238 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9240 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009241 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 }
9243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
Bram Moolenaar843ee412004-06-30 16:16:41 +00009245#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246/*
9247 * ":winpos".
9248 */
9249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009250ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251{
9252 int x, y;
9253 char_u *arg = eap->arg;
9254 char_u *p;
9255
9256 if (*arg == NUL)
9257 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009258# if defined(FEAT_GUI) || defined(MSWIN)
9259# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009261# else
9262 if (mch_get_winpos(&x, &y) != FAIL)
9263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 {
9265 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9266 msg(IObuff);
9267 }
9268 else
9269# endif
9270 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9271 }
9272 else
9273 {
9274 x = getdigits(&arg);
9275 arg = skipwhite(arg);
9276 p = arg;
9277 y = getdigits(&arg);
9278 if (*p == NUL || *arg != NUL)
9279 {
9280 EMSG(_("E466: :winpos requires two number arguments"));
9281 return;
9282 }
9283# ifdef FEAT_GUI
9284 if (gui.in_use)
9285 gui_mch_set_winpos(x, y);
9286 else if (gui.starting)
9287 {
9288 /* Remember the coordinates for when the window is opened. */
9289 gui_win_x = x;
9290 gui_win_y = y;
9291 }
9292# ifdef HAVE_TGETENT
9293 else
9294# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009295# else
9296# ifdef MSWIN
9297 mch_set_winpos(x, y);
9298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299# endif
9300# ifdef HAVE_TGETENT
9301 if (*T_CWP)
9302 term_set_winpos(x, y);
9303# endif
9304 }
9305}
9306#endif
9307
9308/*
9309 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9310 */
9311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009312ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 oparg_T oa;
9315
9316 clear_oparg(&oa);
9317 oa.regname = eap->regname;
9318 oa.start.lnum = eap->line1;
9319 oa.end.lnum = eap->line2;
9320 oa.line_count = eap->line2 - eap->line1 + 1;
9321 oa.motion_type = MLINE;
9322#ifdef FEAT_VIRTUALEDIT
9323 virtual_op = FALSE;
9324#endif
9325 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9326 {
9327 setpcmark();
9328 curwin->w_cursor.lnum = eap->line1;
9329 beginline(BL_SOL | BL_FIX);
9330 }
9331
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009332 if (VIsual_active)
9333 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009334
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 switch (eap->cmdidx)
9336 {
9337 case CMD_delete:
9338 oa.op_type = OP_DELETE;
9339 op_delete(&oa);
9340 break;
9341
9342 case CMD_yank:
9343 oa.op_type = OP_YANK;
9344 (void)op_yank(&oa, FALSE, TRUE);
9345 break;
9346
9347 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009348 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009350 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9351#else
9352 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009354 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 oa.op_type = OP_RSHIFT;
9356 else
9357 oa.op_type = OP_LSHIFT;
9358 op_shift(&oa, FALSE, eap->amount);
9359 break;
9360 }
9361#ifdef FEAT_VIRTUALEDIT
9362 virtual_op = MAYBE;
9363#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009364 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365}
9366
9367/*
9368 * ":put".
9369 */
9370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009371ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372{
9373 /* ":0put" works like ":1put!". */
9374 if (eap->line2 == 0)
9375 {
9376 eap->line2 = 1;
9377 eap->forceit = TRUE;
9378 }
9379 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009380 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9381 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382}
9383
9384/*
9385 * Handle ":copy" and ":move".
9386 */
9387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009388ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 long n;
9391
Bram Moolenaarded27822017-01-02 14:27:34 +01009392 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (eap->arg == NULL) /* error detected */
9394 {
9395 eap->nextcmd = NULL;
9396 return;
9397 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009398 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
9400 /*
9401 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9402 */
9403 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9404 {
9405 EMSG(_(e_invaddr));
9406 return;
9407 }
9408
9409 if (eap->cmdidx == CMD_move)
9410 {
9411 if (do_move(eap->line1, eap->line2, n) == FAIL)
9412 return;
9413 }
9414 else
9415 ex_copy(eap->line1, eap->line2, n);
9416 u_clearline();
9417 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009418 ex_may_print(eap);
9419}
9420
9421/*
9422 * Print the current line if flags were given to the Ex command.
9423 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009424 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009425ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009426{
9427 if (eap->flags != 0)
9428 {
9429 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9430 (eap->flags & EXFLAG_LIST));
9431 ex_no_reprint = TRUE;
9432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433}
9434
9435/*
9436 * ":smagic" and ":snomagic".
9437 */
9438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009439ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
9441 int magic_save = p_magic;
9442
9443 p_magic = (eap->cmdidx == CMD_smagic);
9444 do_sub(eap);
9445 p_magic = magic_save;
9446}
9447
9448/*
9449 * ":join".
9450 */
9451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009452ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
9454 curwin->w_cursor.lnum = eap->line1;
9455 if (eap->line1 == eap->line2)
9456 {
9457 if (eap->addr_count >= 2) /* :2,2join does nothing */
9458 return;
9459 if (eap->line2 == curbuf->b_ml.ml_line_count)
9460 {
9461 beep_flush();
9462 return;
9463 }
9464 ++eap->line2;
9465 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009466 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009468 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471/*
9472 * ":[addr]@r" or ":[addr]*r": execute register
9473 */
9474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009475ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009478 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009481 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
9483#ifdef USE_ON_FLY_SCROLL
9484 dont_scroll = TRUE; /* disallow scrolling here */
9485#endif
9486
9487 /* get the register name. No name means to use the previous one */
9488 c = *eap->arg;
9489 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9490 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009491 /* Put the register in the typeahead buffer with the "silent" flag. */
9492 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9493 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 else
9498 {
9499 int save_efr = exec_from_reg;
9500
9501 exec_from_reg = TRUE;
9502
9503 /*
9504 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009505 * Continue until the stuff buffer is empty and all added characters
9506 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009508 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9510
9511 exec_from_reg = save_efr;
9512 }
9513}
9514
9515/*
9516 * ":!".
9517 */
9518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9522}
9523
9524/*
9525 * ":undo".
9526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009528ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009530 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009531 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009532 else
9533 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534}
9535
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009536#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009538ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009539{
9540 char_u hash[UNDO_HASH_SIZE];
9541
9542 u_compute_hash(hash);
9543 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9544}
9545
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009547ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009548{
9549 char_u hash[UNDO_HASH_SIZE];
9550
9551 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009552 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009553}
9554#endif
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556/*
9557 * ":redo".
9558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009560ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 u_redo(1);
9563}
9564
9565/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009566 * ":earlier" and ":later".
9567 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009569ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009570{
9571 long count = 0;
9572 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009573 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009574 char_u *p = eap->arg;
9575
9576 if (*p == NUL)
9577 count = 1;
9578 else if (isdigit(*p))
9579 {
9580 count = getdigits(&p);
9581 switch (*p)
9582 {
9583 case 's': ++p; sec = TRUE; break;
9584 case 'm': ++p; sec = TRUE; count *= 60; break;
9585 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009586 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9587 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009588 }
9589 }
9590
9591 if (*p != NUL)
9592 EMSG2(_(e_invarg2), eap->arg);
9593 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009594 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9595 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009596}
9597
9598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 * ":redir": start/stop redirection.
9600 */
9601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009602ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603{
9604 char *mode;
9605 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009606 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607
Bram Moolenaarba768492016-07-08 15:32:54 +02009608#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009609 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009610 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009611 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009612 return;
9613 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009614#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009615
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 if (STRICMP(eap->arg, "END") == 0)
9617 close_redir();
9618 else
9619 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009620 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009622 ++arg;
9623 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009625 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 mode = "a";
9627 }
9628 else
9629 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009630 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631
9632 close_redir();
9633
9634 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009635 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (fname == NULL)
9637 return;
9638#ifdef FEAT_BROWSE
9639 if (cmdmod.browse)
9640 {
9641 char_u *browseFile;
9642
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009643 browseFile = do_browse(BROWSE_SAVE,
9644 (char_u *)_("Save Redirection"),
9645 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (browseFile == NULL)
9647 return; /* operation cancelled */
9648 vim_free(fname);
9649 fname = browseFile;
9650 eap->forceit = TRUE; /* since dialog already asked */
9651 }
9652#endif
9653
9654 redir_fd = open_exfile(fname, eap->forceit, mode);
9655 vim_free(fname);
9656 }
9657#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009658 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 {
9660 /* redirect to a register a-z (resp. A-Z for appending) */
9661 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009662 ++arg;
9663 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009665 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009666 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009668 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009670 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009671 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009672 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009675 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009676 if (*arg == '>')
9677 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009678 /* Make register empty when not using @A-@Z and the
9679 * command is valid. */
9680 if (*arg == NUL && !isupper(redir_reg))
9681 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009683 }
9684 if (*arg != NUL)
9685 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009686 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009687 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009688 }
9689 }
9690 else if (*arg == '=' && arg[1] == '>')
9691 {
9692 int append;
9693
9694 /* redirect to a variable */
9695 close_redir();
9696 arg += 2;
9697
9698 if (*arg == '>')
9699 {
9700 ++arg;
9701 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 }
9703 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009704 append = FALSE;
9705
9706 if (var_redir_start(skipwhite(arg), append) == OK)
9707 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
9709#endif
9710
9711 /* TODO: redirect to a buffer */
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009714 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009716
9717 /* Make sure redirection is not off. Can happen for cmdline completion
9718 * that indirectly invokes a command to catch its output. */
9719 if (redir_fd != NULL
9720#ifdef FEAT_EVAL
9721 || redir_reg || redir_vname
9722#endif
9723 )
9724 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725}
9726
9727/*
9728 * ":redraw": force redraw
9729 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009730 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009731ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 int r = RedrawingDisabled;
9734 int p = p_lz;
9735
9736 RedrawingDisabled = 0;
9737 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009738 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009740 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741#ifdef FEAT_TITLE
9742 if (need_maketitle)
9743 maketitle();
9744#endif
9745 RedrawingDisabled = r;
9746 p_lz = p;
9747
9748 /* Reset msg_didout, so that a message that's there is overwritten. */
9749 msg_didout = FALSE;
9750 msg_col = 0;
9751
Bram Moolenaar56667a52013-07-17 11:54:28 +02009752 /* No need to wait after an intentional redraw. */
9753 need_wait_return = FALSE;
9754
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 out_flush();
9756}
9757
9758/*
9759 * ":redrawstatus": force redraw of status line(s)
9760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009762ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 int r = RedrawingDisabled;
9765 int p = p_lz;
9766
9767 RedrawingDisabled = 0;
9768 p_lz = FALSE;
9769 if (eap->forceit)
9770 status_redraw_all();
9771 else
9772 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009773 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 RedrawingDisabled = r;
9775 p_lz = p;
9776 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
9779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009780close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782 if (redir_fd != NULL)
9783 {
9784 fclose(redir_fd);
9785 redir_fd = NULL;
9786 }
9787#ifdef FEAT_EVAL
9788 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009789 if (redir_vname)
9790 {
9791 var_redir_stop();
9792 redir_vname = 0;
9793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794#endif
9795}
9796
9797#if defined(FEAT_SESSION) && defined(USE_CRNL)
9798# define MKSESSION_NL
9799static int mksession_nl = FALSE; /* use NL only in put_eol() */
9800#endif
9801
9802/*
9803 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9804 */
9805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009806ex_mkrc(
9807 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809 FILE *fd;
9810 int failed = FALSE;
9811 char_u *fname;
9812#ifdef FEAT_BROWSE
9813 char_u *browseFile = NULL;
9814#endif
9815#ifdef FEAT_SESSION
9816 int view_session = FALSE;
9817 int using_vdir = FALSE; /* using 'viewdir'? */
9818 char_u *viewFile = NULL;
9819 unsigned *flagp;
9820#endif
9821
9822 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9823 {
9824#ifdef FEAT_SESSION
9825 view_session = TRUE;
9826#else
9827 ex_ni(eap);
9828 return;
9829#endif
9830 }
9831
9832#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009833 /* Use the short file name until ":lcd" is used. We also don't use the
9834 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009835 did_lcd = FALSE;
9836
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9838 if (eap->cmdidx == CMD_mkview
9839 && (*eap->arg == NUL
9840 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9841 {
9842 eap->forceit = TRUE;
9843 fname = get_view_file(*eap->arg);
9844 if (fname == NULL)
9845 return;
9846 viewFile = fname;
9847 using_vdir = TRUE;
9848 }
9849 else
9850#endif
9851 if (*eap->arg != NUL)
9852 fname = eap->arg;
9853 else if (eap->cmdidx == CMD_mkvimrc)
9854 fname = (char_u *)VIMRC_FILE;
9855#ifdef FEAT_SESSION
9856 else if (eap->cmdidx == CMD_mksession)
9857 fname = (char_u *)SESSION_FILE;
9858#endif
9859 else
9860 fname = (char_u *)EXRC_FILE;
9861
9862#ifdef FEAT_BROWSE
9863 if (cmdmod.browse)
9864 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009865 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866# ifdef FEAT_SESSION
9867 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9868 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9869# endif
9870 (char_u *)_("Save Setup"),
9871 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9872 if (browseFile == NULL)
9873 goto theend;
9874 fname = browseFile;
9875 eap->forceit = TRUE; /* since dialog already asked */
9876 }
9877#endif
9878
9879#if defined(FEAT_SESSION) && defined(vim_mkdir)
9880 /* When using 'viewdir' may have to create the directory. */
9881 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009882 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883#endif
9884
9885 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9886 if (fd != NULL)
9887 {
9888#ifdef FEAT_SESSION
9889 if (eap->cmdidx == CMD_mkview)
9890 flagp = &vop_flags;
9891 else
9892 flagp = &ssop_flags;
9893#endif
9894
9895#ifdef MKSESSION_NL
9896 /* "unix" in 'sessionoptions': use NL line separator */
9897 if (view_session && (*flagp & SSOP_UNIX))
9898 mksession_nl = TRUE;
9899#endif
9900
9901 /* Write the version command for :mkvimrc */
9902 if (eap->cmdidx == CMD_mkvimrc)
9903 (void)put_line(fd, "version 6.0");
9904
9905#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009906 if (eap->cmdidx == CMD_mksession)
9907 {
9908 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9909 failed = TRUE;
9910 }
9911
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 if (eap->cmdidx != CMD_mkview)
9913#endif
9914 {
9915 /* Write setting 'compatible' first, because it has side effects.
9916 * For that same reason only do it when needed. */
9917 if (p_cp)
9918 (void)put_line(fd, "if !&cp | set cp | endif");
9919 else
9920 (void)put_line(fd, "if &cp | set nocp | endif");
9921 }
9922
9923#ifdef FEAT_SESSION
9924 if (!view_session
9925 || (eap->cmdidx == CMD_mksession
9926 && (*flagp & SSOP_OPTIONS)))
9927#endif
9928 failed |= (makemap(fd, NULL) == FAIL
9929 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9930
9931#ifdef FEAT_SESSION
9932 if (!failed && view_session)
9933 {
9934 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9935 failed = TRUE;
9936 if (eap->cmdidx == CMD_mksession)
9937 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009938 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
Bram Moolenaard9462e32011-04-11 21:35:11 +02009940 dirnow = alloc(MAXPATHL);
9941 if (dirnow == NULL)
9942 failed = TRUE;
9943 else
9944 {
9945 /*
9946 * Change to session file's dir.
9947 */
9948 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009950 *dirnow = NUL;
9951 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9952 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009953 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009954 shorten_fnames(TRUE);
9955 }
9956 else if (*dirnow != NUL
9957 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9958 {
9959 if (mch_chdir((char *)globaldir) == 0)
9960 shorten_fnames(TRUE);
9961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962
Bram Moolenaard9462e32011-04-11 21:35:11 +02009963 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
Bram Moolenaard9462e32011-04-11 21:35:11 +02009965 /* restore original dir */
9966 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009968 {
9969 if (mch_chdir((char *)dirnow) != 0)
9970 EMSG(_(e_prev_dir));
9971 shorten_fnames(TRUE);
9972 }
9973 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 }
9975 }
9976 else
9977 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009978 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9979 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 }
9981 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9982 == FAIL)
9983 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009984 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9985 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009986 if (eap->cmdidx == CMD_mksession)
9987 {
9988 if (put_line(fd, "unlet SessionLoad") == FAIL)
9989 failed = TRUE;
9990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
9992#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009993 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9994 failed = TRUE;
9995
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 failed |= fclose(fd);
9997
9998 if (failed)
9999 EMSG(_(e_write));
10000#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10001 else if (eap->cmdidx == CMD_mksession)
10002 {
10003 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010004 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005
Bram Moolenaard9462e32011-04-11 21:35:11 +020010006 tbuf = alloc(MAXPATHL);
10007 if (tbuf != NULL)
10008 {
10009 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10010 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10011 vim_free(tbuf);
10012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 }
10014#endif
10015#ifdef MKSESSION_NL
10016 mksession_nl = FALSE;
10017#endif
10018 }
10019
10020#ifdef FEAT_BROWSE
10021theend:
10022 vim_free(browseFile);
10023#endif
10024#ifdef FEAT_SESSION
10025 vim_free(viewFile);
10026#endif
10027}
10028
Bram Moolenaardf177f62005-02-22 08:39:57 +000010029#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10030 || defined(PROTO)
10031 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010032vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010033{
10034 if (vim_mkdir(name, prot) != 0)
10035 {
10036 EMSG2(_("E739: Cannot create directory: %s"), name);
10037 return FAIL;
10038 }
10039 return OK;
10040}
10041#endif
10042
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043/*
10044 * Open a file for writing for an Ex command, with some checks.
10045 * Return file descriptor, or NULL on failure.
10046 */
10047 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010048open_exfile(
10049 char_u *fname,
10050 int forceit,
10051 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
10053 FILE *fd;
10054
10055#ifdef UNIX
10056 /* with Unix it is possible to open a directory */
10057 if (mch_isdir(fname))
10058 {
10059 EMSG2(_(e_isadir2), fname);
10060 return NULL;
10061 }
10062#endif
10063 if (!forceit && *mode != 'a' && vim_fexists(fname))
10064 {
10065 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10066 return NULL;
10067 }
10068
10069 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10070 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10071
10072 return fd;
10073}
10074
10075/*
10076 * ":mark" and ":k".
10077 */
10078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010079ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080{
10081 pos_T pos;
10082
10083 if (*eap->arg == NUL) /* No argument? */
10084 EMSG(_(e_argreq));
10085 else if (eap->arg[1] != NUL) /* more than one character? */
10086 EMSG(_(e_trailing));
10087 else
10088 {
10089 pos = curwin->w_cursor; /* save curwin->w_cursor */
10090 curwin->w_cursor.lnum = eap->line2;
10091 beginline(BL_WHITE | BL_FIX);
10092 if (setmark(*eap->arg) == FAIL) /* set mark */
10093 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10094 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10095 }
10096}
10097
10098/*
10099 * Update w_topline, w_leftcol and the cursor position.
10100 */
10101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010102update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104 check_cursor(); /* put cursor on valid line */
10105 update_topline();
10106 if (!curwin->w_p_wrap)
10107 validate_cursor();
10108 update_curswant();
10109}
10110
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010112 * Save the current State and go to Normal mode.
10113 * Return TRUE if the typeahead could be saved.
10114 */
10115 int
10116save_current_state(save_state_T *sst)
10117{
10118 sst->save_msg_scroll = msg_scroll;
10119 sst->save_restart_edit = restart_edit;
10120 sst->save_msg_didout = msg_didout;
10121 sst->save_State = State;
10122 sst->save_insertmode = p_im;
10123 sst->save_finish_op = finish_op;
10124 sst->save_opcount = opcount;
10125
10126 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10127 restart_edit = 0; /* don't go to Insert mode */
10128 p_im = FALSE; /* don't use 'insertmode' */
10129
10130 /*
10131 * Save the current typeahead. This is required to allow using ":normal"
10132 * from an event handler and makes sure we don't hang when the argument
10133 * ends with half a command.
10134 */
10135 save_typeahead(&sst->tabuf);
10136 return sst->tabuf.typebuf_valid;
10137}
10138
10139 void
10140restore_current_state(save_state_T *sst)
10141{
10142 /* Restore the previous typeahead. */
10143 restore_typeahead(&sst->tabuf);
10144
10145 msg_scroll = sst->save_msg_scroll;
10146 restart_edit = sst->save_restart_edit;
10147 p_im = sst->save_insertmode;
10148 finish_op = sst->save_finish_op;
10149 opcount = sst->save_opcount;
10150 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10151
10152 /* Restore the state (needed when called from a function executed for
10153 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10154 State = sst->save_State;
10155#ifdef CURSOR_SHAPE
10156 ui_cursor_shape(); /* may show different cursor shape */
10157#endif
10158}
10159
10160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 * ":normal[!] {commands}": Execute normal mode commands.
10162 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010163 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010164ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010166 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167#ifdef FEAT_MBYTE
10168 char_u *arg = NULL;
10169 int l;
10170 char_u *p;
10171#endif
10172
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010173 if (ex_normal_lock > 0)
10174 {
10175 EMSG(_(e_secure));
10176 return;
10177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 if (ex_normal_busy >= p_mmd)
10179 {
10180 EMSG(_("E192: Recursive use of :normal too deep"));
10181 return;
10182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183
10184#ifdef FEAT_MBYTE
10185 /*
10186 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10187 * this for the K_SPECIAL leading byte, otherwise special keys will not
10188 * work.
10189 */
10190 if (has_mbyte)
10191 {
10192 int len = 0;
10193
10194 /* Count the number of characters to be escaped. */
10195 for (p = eap->arg; *p != NUL; ++p)
10196 {
10197# ifdef FEAT_GUI
10198 if (*p == CSI) /* leadbyte CSI */
10199 len += 2;
10200# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010201 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10203# ifdef FEAT_GUI
10204 || *p == CSI
10205# endif
10206 )
10207 len += 2;
10208 }
10209 if (len > 0)
10210 {
10211 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10212 if (arg != NULL)
10213 {
10214 len = 0;
10215 for (p = eap->arg; *p != NUL; ++p)
10216 {
10217 arg[len++] = *p;
10218# ifdef FEAT_GUI
10219 if (*p == CSI)
10220 {
10221 arg[len++] = KS_EXTRA;
10222 arg[len++] = (int)KE_CSI;
10223 }
10224# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010225 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 {
10227 arg[len++] = *++p;
10228 if (*p == K_SPECIAL)
10229 {
10230 arg[len++] = KS_SPECIAL;
10231 arg[len++] = KE_FILLER;
10232 }
10233# ifdef FEAT_GUI
10234 else if (*p == CSI)
10235 {
10236 arg[len++] = KS_EXTRA;
10237 arg[len++] = (int)KE_CSI;
10238 }
10239# endif
10240 }
10241 arg[len] = NUL;
10242 }
10243 }
10244 }
10245 }
10246#endif
10247
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010248 ++ex_normal_busy;
10249 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 {
10251 /*
10252 * Repeat the :normal command for each line in the range. When no
10253 * range given, execute it just once, without positioning the cursor
10254 * first.
10255 */
10256 do
10257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 if (eap->addr_count != 0)
10259 {
10260 curwin->w_cursor.lnum = eap->line1++;
10261 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010262 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 }
10264
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010265 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266#ifdef FEAT_MBYTE
10267 arg != NULL ? arg :
10268#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010269 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 }
10271 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10272 }
10273
10274 /* Might not return to the main loop when in an event handler. */
10275 update_topline_cursor();
10276
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010277 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010279#ifdef FEAT_MOUSE
10280 setmouse();
10281#endif
10282#ifdef CURSOR_SHAPE
10283 ui_cursor_shape(); /* may show different cursor shape */
10284#endif
10285
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286#ifdef FEAT_MBYTE
10287 vim_free(arg);
10288#endif
10289}
10290
10291/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010292 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 */
10294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010295ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010297 if (eap->forceit)
10298 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010299 /* cursor line can be zero on startup */
10300 if (!curwin->w_cursor.lnum)
10301 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010302 coladvance((colnr_T)MAXCOL);
10303 curwin->w_curswant = MAXCOL;
10304 curwin->w_set_curswant = FALSE;
10305 }
10306
Bram Moolenaara40c5002005-01-09 21:16:21 +000010307 /* Ignore the command when already in Insert mode. Inserting an
10308 * expression register that invokes a function can do this. */
10309 if (State & INSERT)
10310 return;
10311
Bram Moolenaar64969662005-12-14 21:59:55 +000010312 if (eap->cmdidx == CMD_startinsert)
10313 restart_edit = 'a';
10314 else if (eap->cmdidx == CMD_startreplace)
10315 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010317 restart_edit = 'V';
10318
10319 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010321 if (eap->cmdidx == CMD_startinsert)
10322 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 curwin->w_curswant = 0; /* avoid MAXCOL */
10324 }
10325}
10326
10327/*
10328 * ":stopinsert"
10329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010331ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
10333 restart_edit = 0;
10334 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010335 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010338/*
10339 * Execute normal mode command "cmd".
10340 * "remap" can be REMAP_NONE or REMAP_YES.
10341 */
10342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010343exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010344{
Bram Moolenaar25281632016-01-21 23:32:32 +010010345 /* Stuff the argument into the typeahead buffer. */
10346 ins_typebuf(cmd, remap, 0, TRUE, silent);
10347 exec_normal(FALSE);
10348}
Bram Moolenaar25281632016-01-21 23:32:32 +010010349
Bram Moolenaar25281632016-01-21 23:32:32 +010010350/*
10351 * Execute normal_cmd() until there is no typeahead left.
10352 */
10353 void
10354exec_normal(int was_typed)
10355{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010356 oparg_T oa;
10357
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010358 clear_oparg(&oa);
10359 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010360 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10361 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010362 {
10363 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010364 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010365 }
10366}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010367
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368#ifdef FEAT_FIND_ID
10369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010370ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371{
10372 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10373 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10374 (linenr_T)1, (linenr_T)MAXLNUM);
10375}
10376
Bram Moolenaar4033c552017-09-16 20:54:51 +020010377#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378/*
10379 * ":psearch"
10380 */
10381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010382ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383{
10384 g_do_tagpreview = p_pvh;
10385 ex_findpat(eap);
10386 g_do_tagpreview = 0;
10387}
10388#endif
10389
10390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010391ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 int whole = TRUE;
10394 long n;
10395 char_u *p;
10396 int action;
10397
10398 switch (cmdnames[eap->cmdidx].cmd_name[2])
10399 {
10400 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10401 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10402 action = ACTION_GOTO;
10403 else
10404 action = ACTION_SHOW;
10405 break;
10406 case 'i': /* ":ilist" and ":dlist" */
10407 action = ACTION_SHOW_ALL;
10408 break;
10409 case 'u': /* ":ijump" and ":djump" */
10410 action = ACTION_GOTO;
10411 break;
10412 default: /* ":isplit" and ":dsplit" */
10413 action = ACTION_SPLIT;
10414 break;
10415 }
10416
10417 n = 1;
10418 if (vim_isdigit(*eap->arg)) /* get count */
10419 {
10420 n = getdigits(&eap->arg);
10421 eap->arg = skipwhite(eap->arg);
10422 }
10423 if (*eap->arg == '/') /* Match regexp, not just whole words */
10424 {
10425 whole = FALSE;
10426 ++eap->arg;
10427 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10428 if (*p)
10429 {
10430 *p++ = NUL;
10431 p = skipwhite(p);
10432
10433 /* Check for trailing illegal characters */
10434 if (!ends_excmd(*p))
10435 eap->errmsg = e_trailing;
10436 else
10437 eap->nextcmd = check_nextcmd(p);
10438 }
10439 }
10440 if (!eap->skip)
10441 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10442 whole, !eap->forceit,
10443 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10444 n, action, eap->line1, eap->line2);
10445}
10446#endif
10447
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448
Bram Moolenaar4033c552017-09-16 20:54:51 +020010449#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450/*
10451 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10452 */
10453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010454ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010456 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10458}
10459
10460/*
10461 * ":pedit"
10462 */
10463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010464ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465{
10466 win_T *curwin_save = curwin;
10467
10468 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010469 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010470 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 do_exedit(eap, NULL);
10472 keep_help_flag = FALSE;
10473 if (curwin != curwin_save && win_valid(curwin_save))
10474 {
10475 /* Return cursor to where we were */
10476 validate_cursor();
10477 redraw_later(VALID);
10478 win_enter(curwin_save, TRUE);
10479 }
10480 g_do_tagpreview = 0;
10481}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
10484/*
10485 * ":stag", ":stselect" and ":stjump".
10486 */
10487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010488ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489{
10490 postponed_split = -1;
10491 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010492 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10494 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010495 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497
10498/*
10499 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10500 */
10501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010502ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10505}
10506
10507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010508ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 int cmd;
10511
10512 switch (name[1])
10513 {
10514 case 'j': cmd = DT_JUMP; /* ":tjump" */
10515 break;
10516 case 's': cmd = DT_SELECT; /* ":tselect" */
10517 break;
10518 case 'p': cmd = DT_PREV; /* ":tprevious" */
10519 break;
10520 case 'N': cmd = DT_PREV; /* ":tNext" */
10521 break;
10522 case 'n': cmd = DT_NEXT; /* ":tnext" */
10523 break;
10524 case 'o': cmd = DT_POP; /* ":pop" */
10525 break;
10526 case 'f': /* ":tfirst" */
10527 case 'r': cmd = DT_FIRST; /* ":trewind" */
10528 break;
10529 case 'l': cmd = DT_LAST; /* ":tlast" */
10530 break;
10531 default: /* ":tag" */
10532#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010533 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010535 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 return;
10537 }
10538#endif
10539 cmd = DT_TAG;
10540 break;
10541 }
10542
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010543 if (name[0] == 'l')
10544 {
10545#ifndef FEAT_QUICKFIX
10546 ex_ni(eap);
10547 return;
10548#else
10549 cmd = DT_LTAG;
10550#endif
10551 }
10552
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10554 eap->forceit, TRUE);
10555}
10556
10557/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010558 * Check "str" for starting with a special cmdline variable.
10559 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10560 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10561 */
10562 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010563find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010564{
10565 int len;
10566 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010567 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010568 "%",
10569#define SPEC_PERC 0
10570 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010571#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010572 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010573#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010574 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010575#define SPEC_CCWORD (SPEC_CWORD + 1)
10576 "<cexpr>", /* expr under cursor */
10577#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010578 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010579#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010580 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010581#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010582 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010583#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010584 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010585#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010586 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010587#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010588 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010589#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010590#ifdef FEAT_CLIENTSERVER
10591 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010592# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010593#endif
10594 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010595
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010596 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010597 {
10598 len = (int)STRLEN(spec_str[i]);
10599 if (STRNCMP(src, spec_str[i], len) == 0)
10600 {
10601 *usedlen = len;
10602 return i;
10603 }
10604 }
10605 return -1;
10606}
10607
10608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 * Evaluate cmdline variables.
10610 *
10611 * change '%' to curbuf->b_ffname
10612 * '#' to curwin->w_altfile
10613 * '<cword>' to word under the cursor
10614 * '<cWORD>' to WORD under the cursor
10615 * '<cfile>' to path name under the cursor
10616 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010617 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 * '<afile>' to file name for autocommand
10619 * '<abuf>' to buffer number for autocommand
10620 * '<amatch>' to matching name for autocommand
10621 *
10622 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10623 * "" for error without a message) and NULL is returned.
10624 * Returns an allocated string if a valid match was found.
10625 * Returns NULL if no match was found. "usedlen" then still contains the
10626 * number of characters to skip.
10627 */
10628 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010629eval_vars(
10630 char_u *src, /* pointer into commandline */
10631 char_u *srcstart, /* beginning of valid memory for src */
10632 int *usedlen, /* characters after src that are used */
10633 linenr_T *lnump, /* line number for :e command, or NULL */
10634 char_u **errormsg, /* pointer to error message */
10635 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010636 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 int i;
10639 char_u *s;
10640 char_u *result;
10641 char_u *resultbuf = NULL;
10642 int resultlen;
10643 buf_T *buf;
10644 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10645 int spec_idx;
10646#ifdef FEAT_MODIFY_FNAME
10647 int skip_mod = FALSE;
10648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650
10651 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010652 if (escaped != NULL)
10653 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654
10655 /*
10656 * Check if there is something to do.
10657 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010658 spec_idx = find_cmdline_var(src, usedlen);
10659 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 {
10661 *usedlen = 1;
10662 return NULL;
10663 }
10664
10665 /*
10666 * Skip when preceded with a backslash "\%" and "\#".
10667 * Note: In "\\%" the % is also not recognized!
10668 */
10669 if (src > srcstart && src[-1] == '\\')
10670 {
10671 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010672 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 return NULL;
10674 }
10675
10676 /*
10677 * word or WORD under cursor
10678 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010679 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10680 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010682 resultlen = find_ident_under_cursor(&result,
10683 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10684 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10685 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 if (resultlen == 0)
10687 {
10688 *errormsg = (char_u *)"";
10689 return NULL;
10690 }
10691 }
10692
10693 /*
10694 * '#': Alternate file name
10695 * '%': Current file name
10696 * File name under the cursor
10697 * File name for autocommand
10698 * and following modifiers
10699 */
10700 else
10701 {
10702 switch (spec_idx)
10703 {
10704 case SPEC_PERC: /* '%': current file */
10705 if (curbuf->b_fname == NULL)
10706 {
10707 result = (char_u *)"";
10708 valid = 0; /* Must have ":p:h" to be valid */
10709 }
10710 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 break;
10713
10714 case SPEC_HASH: /* '#' or "#99": alternate file */
10715 if (src[1] == '#') /* "##": the argument list */
10716 {
10717 result = arg_all();
10718 resultbuf = result;
10719 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010720 if (escaped != NULL)
10721 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722#ifdef FEAT_MODIFY_FNAME
10723 skip_mod = TRUE;
10724#endif
10725 break;
10726 }
10727 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010728 if (*s == '<') /* "#<99" uses v:oldfiles */
10729 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010731 if (s == src + 2 && src[1] == '-')
10732 /* just a minus sign, don't skip over it */
10733 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 *usedlen = (int)(s - src); /* length of what we expand */
10735
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010736 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010738 if (*usedlen < 2)
10739 {
10740 /* Should we give an error message for #<text? */
10741 *usedlen = 1;
10742 return NULL;
10743 }
10744#ifdef FEAT_EVAL
10745 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10746 (long)i);
10747 if (result == NULL)
10748 {
10749 *errormsg = (char_u *)"";
10750 return NULL;
10751 }
10752#else
10753 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 }
10757 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010758 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010759 if (i == 0 && src[1] == '<' && *usedlen > 1)
10760 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010761 buf = buflist_findnr(i);
10762 if (buf == NULL)
10763 {
10764 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10765 return NULL;
10766 }
10767 if (lnump != NULL)
10768 *lnump = ECMD_LAST;
10769 if (buf->b_fname == NULL)
10770 {
10771 result = (char_u *)"";
10772 valid = 0; /* Must have ":p:h" to be valid */
10773 }
10774 else
10775 result = buf->b_fname;
10776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 break;
10778
10779#ifdef FEAT_SEARCHPATH
10780 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010781 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 if (result == NULL)
10783 {
10784 *errormsg = (char_u *)"";
10785 return NULL;
10786 }
10787 resultbuf = result; /* remember allocated string */
10788 break;
10789#endif
10790
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 case SPEC_AFILE: /* file name for autocommand */
10792 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010793 if (result != NULL && !autocmd_fname_full)
10794 {
10795 /* Still need to turn the fname into a full path. It is
10796 * postponed to avoid a delay when <afile> is not used. */
10797 autocmd_fname_full = TRUE;
10798 result = FullName_save(autocmd_fname, FALSE);
10799 vim_free(autocmd_fname);
10800 autocmd_fname = result;
10801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 if (result == NULL)
10803 {
10804 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10805 return NULL;
10806 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010807 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 break;
10809
10810 case SPEC_ABUF: /* buffer number for autocommand */
10811 if (autocmd_bufnr <= 0)
10812 {
10813 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10814 return NULL;
10815 }
10816 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10817 result = strbuf;
10818 break;
10819
10820 case SPEC_AMATCH: /* match name for autocommand */
10821 result = autocmd_match;
10822 if (result == NULL)
10823 {
10824 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10825 return NULL;
10826 }
10827 break;
10828
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 case SPEC_SFILE: /* file name for ":so" command */
10830 result = sourcing_name;
10831 if (result == NULL)
10832 {
10833 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10834 return NULL;
10835 }
10836 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010837 case SPEC_SLNUM: /* line in file for ":so" command */
10838 if (sourcing_name == NULL || sourcing_lnum == 0)
10839 {
10840 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10841 return NULL;
10842 }
10843 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10844 result = strbuf;
10845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846#if defined(FEAT_CLIENTSERVER)
10847 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010848 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10849 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 result = strbuf;
10851 break;
10852#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010853 default:
10854 result = (char_u *)""; /* avoid gcc warning */
10855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 }
10857
10858 resultlen = (int)STRLEN(result); /* length of new string */
10859 if (src[*usedlen] == '<') /* remove the file name extension */
10860 {
10861 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 resultlen = (int)(s - result);
10864 }
10865#ifdef FEAT_MODIFY_FNAME
10866 else if (!skip_mod)
10867 {
10868 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10869 &resultlen);
10870 if (result == NULL)
10871 {
10872 *errormsg = (char_u *)"";
10873 return NULL;
10874 }
10875 }
10876#endif
10877 }
10878
10879 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10880 {
10881 if (valid != VALID_HEAD + VALID_PATH)
10882 /* xgettext:no-c-format */
10883 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10884 else
10885 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10886 result = NULL;
10887 }
10888 else
10889 result = vim_strnsave(result, resultlen);
10890 vim_free(resultbuf);
10891 return result;
10892}
10893
10894/*
10895 * Concatenate all files in the argument list, separated by spaces, and return
10896 * it in one allocated string.
10897 * Spaces and backslashes in the file names are escaped with a backslash.
10898 * Returns NULL when out of memory.
10899 */
10900 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010901arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902{
10903 int len;
10904 int idx;
10905 char_u *retval = NULL;
10906 char_u *p;
10907
10908 /*
10909 * Do this loop two times:
10910 * first time: compute the total length
10911 * second time: concatenate the names
10912 */
10913 for (;;)
10914 {
10915 len = 0;
10916 for (idx = 0; idx < ARGCOUNT; ++idx)
10917 {
10918 p = alist_name(&ARGLIST[idx]);
10919 if (p != NULL)
10920 {
10921 if (len > 0)
10922 {
10923 /* insert a space in between names */
10924 if (retval != NULL)
10925 retval[len] = ' ';
10926 ++len;
10927 }
10928 for ( ; *p != NUL; ++p)
10929 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010930 if (*p == ' '
10931#ifndef BACKSLASH_IN_FILENAME
10932 || *p == '\\'
10933#endif
10934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 {
10936 /* insert a backslash */
10937 if (retval != NULL)
10938 retval[len] = '\\';
10939 ++len;
10940 }
10941 if (retval != NULL)
10942 retval[len] = *p;
10943 ++len;
10944 }
10945 }
10946 }
10947
10948 /* second time: break here */
10949 if (retval != NULL)
10950 {
10951 retval[len] = NUL;
10952 break;
10953 }
10954
10955 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010956 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 if (retval == NULL)
10958 break;
10959 }
10960
10961 return retval;
10962}
10963
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964/*
10965 * Expand the <sfile> string in "arg".
10966 *
10967 * Returns an allocated string, or NULL for any error.
10968 */
10969 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010970expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
10972 char_u *errormsg;
10973 int len;
10974 char_u *result;
10975 char_u *newres;
10976 char_u *repl;
10977 int srclen;
10978 char_u *p;
10979
10980 result = vim_strsave(arg);
10981 if (result == NULL)
10982 return NULL;
10983
10984 for (p = result; *p; )
10985 {
10986 if (STRNCMP(p, "<sfile>", 7) != 0)
10987 ++p;
10988 else
10989 {
10990 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010991 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 if (errormsg != NULL)
10993 {
10994 if (*errormsg)
10995 emsg(errormsg);
10996 vim_free(result);
10997 return NULL;
10998 }
10999 if (repl == NULL) /* no match (cannot happen) */
11000 {
11001 p += srclen;
11002 continue;
11003 }
11004 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11005 newres = alloc(len);
11006 if (newres == NULL)
11007 {
11008 vim_free(repl);
11009 vim_free(result);
11010 return NULL;
11011 }
11012 mch_memmove(newres, result, (size_t)(p - result));
11013 STRCPY(newres + (p - result), repl);
11014 len = (int)STRLEN(newres);
11015 STRCAT(newres, p + srclen);
11016 vim_free(repl);
11017 vim_free(result);
11018 result = newres;
11019 p = newres + len; /* continue after the match */
11020 }
11021 }
11022
11023 return result;
11024}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025
11026#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011027static int ses_winsizes(FILE *fd, int restore_size,
11028 win_T *tab_firstwin);
11029static int ses_win_rec(FILE *fd, frame_T *fr);
11030static frame_T *ses_skipframe(frame_T *fr);
11031static int ses_do_frame(frame_T *fr);
11032static int ses_do_win(win_T *wp);
11033static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11034static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011035static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036
11037/*
11038 * Write openfile commands for the current buffers to an .exrc file.
11039 * Return FAIL on error, OK otherwise.
11040 */
11041 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011042makeopens(
11043 FILE *fd,
11044 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045{
11046 buf_T *buf;
11047 int only_save_windows = TRUE;
11048 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 int restore_size = TRUE;
11050 win_T *wp;
11051 char_u *sname;
11052 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011053 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011054 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011055 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011056 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011057 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011058 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059
11060 if (ssop_flags & SSOP_BUFFERS)
11061 only_save_windows = FALSE; /* Save ALL buffers */
11062
11063 /*
11064 * Begin by setting the this_session variable, and then other
11065 * sessionable variables.
11066 */
11067#ifdef FEAT_EVAL
11068 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11069 return FAIL;
11070 if (ssop_flags & SSOP_GLOBALS)
11071 if (store_session_globals(fd) == FAIL)
11072 return FAIL;
11073#endif
11074
11075 /*
11076 * Close all windows but one.
11077 */
11078 if (put_line(fd, "silent only") == FAIL)
11079 return FAIL;
11080
11081 /*
11082 * Now a :cd command to the session directory or the current directory
11083 */
11084 if (ssop_flags & SSOP_SESDIR)
11085 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011086 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11087 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 return FAIL;
11089 }
11090 else if (ssop_flags & SSOP_CURDIR)
11091 {
11092 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11093 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011094 || fputs("cd ", fd) < 0
11095 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11096 || put_eol(fd) == FAIL)
11097 {
11098 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 vim_free(sname);
11102 }
11103
11104 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011105 * If there is an empty, unnamed buffer we will wipe it out later.
11106 * Remember the buffer number.
11107 */
11108 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11109 return FAIL;
11110 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11111 return FAIL;
11112 if (put_line(fd, "endif") == FAIL)
11113 return FAIL;
11114
11115 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 * Now save the current files, current buffer first.
11117 */
11118 if (put_line(fd, "set shortmess=aoO") == FAIL)
11119 return FAIL;
11120
11121 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011122 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 {
11124 if (!(only_save_windows && buf->b_nwindows == 0)
11125 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011126#ifdef FEAT_TERMINAL
11127 /* skip terminal buffers: finished ones are not useful, others
11128 * will be resurrected and result in a new buffer */
11129 && !bt_terminal(buf)
11130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 && buf->b_fname != NULL
11132 && buf->b_p_bl)
11133 {
11134 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11135 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011136 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 return FAIL;
11138 }
11139 }
11140
11141 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011142 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11144 return FAIL;
11145
11146 if (ssop_flags & SSOP_RESIZE)
11147 {
11148 /* Note: after the restore we still check it worked!*/
11149 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11150 || put_eol(fd) == FAIL)
11151 return FAIL;
11152 }
11153
11154#ifdef FEAT_GUI
11155 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11156 {
11157 int x, y;
11158
11159 if (gui_mch_get_winpos(&x, &y) == OK)
11160 {
11161 /* Note: after the restore we still check it worked!*/
11162 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11163 return FAIL;
11164 }
11165 }
11166#endif
11167
11168 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011169 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11170 * will be displayed when creating the next tab. That resizes the windows
11171 * in the first tab, which may cause problems. Set 'showtabline' to 2
11172 * temporarily to avoid that.
11173 */
11174 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11175 {
11176 if (put_line(fd, "set stal=2") == FAIL)
11177 return FAIL;
11178 restore_stal = TRUE;
11179 }
11180
11181 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011182 * May repeat putting Windows for each tab, when "tabpages" is in
11183 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011184 * Don't use goto_tabpage(), it may change directory and trigger
11185 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011187 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011188 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011189 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011191 int need_tabnew = FALSE;
11192 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011193
Bram Moolenaar18144c82006-04-12 21:52:12 +000011194 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011196 tabpage_T *tp = find_tabpage(tabnr);
11197
11198 if (tp == NULL)
11199 break; /* done all tab pages */
11200 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011201 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011202 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011203 tab_topframe = topframe;
11204 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011205 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011206 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011207 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011208 tab_topframe = tp->tp_topframe;
11209 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011210 if (tabnr > 1)
11211 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213
Bram Moolenaar18144c82006-04-12 21:52:12 +000011214 /*
11215 * Before creating the window layout, try loading one file. If this
11216 * is aborted we don't end up with a number of useless windows.
11217 * This may have side effects! (e.g., compressed or network file).
11218 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011219 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011220 {
11221 if (ses_do_win(wp)
11222 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011223 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011224#ifdef FEAT_QUICKFIX
11225 && !bt_nofile(wp->w_buffer)
11226#endif
11227 )
11228 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011229 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011230 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11231 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011232 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011233 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011234 if (!wp->w_arg_idx_invalid)
11235 edited_win = wp;
11236 break;
11237 }
11238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011240 /* If no file got edited create an empty tab page. */
11241 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11242 return FAIL;
11243
Bram Moolenaar18144c82006-04-12 21:52:12 +000011244 /*
11245 * Save current window layout.
11246 */
11247 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011249 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011251 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11252 return FAIL;
11253 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255
Bram Moolenaar18144c82006-04-12 21:52:12 +000011256 /*
11257 * Check if window sizes can be restored (no windows omitted).
11258 * Remember the window number of the current window after restoring.
11259 */
11260 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011261 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011262 {
11263 if (ses_do_win(wp))
11264 ++nr;
11265 else
11266 restore_size = FALSE;
11267 if (curwin == wp)
11268 cnr = nr;
11269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270
Bram Moolenaar18144c82006-04-12 21:52:12 +000011271 /* Go to the first window. */
11272 if (put_line(fd, "wincmd t") == FAIL)
11273 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011274
Bram Moolenaar18144c82006-04-12 21:52:12 +000011275 /*
11276 * If more than one window, see if sizes can be restored.
11277 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11278 * resized when moving between windows.
11279 * Do this before restoring the view, so that the topline and the
11280 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011281 * winminheight and winminwidth need to be set to avoid an error if the
11282 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011283 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011284 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011285 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011286 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011287 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288
Bram Moolenaar18144c82006-04-12 21:52:12 +000011289 /*
11290 * Restore the view of the window (options, file, cursor, etc.).
11291 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011292 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011293 {
11294 if (!ses_do_win(wp))
11295 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011296 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11297 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011298 return FAIL;
11299 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11300 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011301 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011302 }
11303
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011304 /* The argument index in the first tab page is zero, need to set it in
11305 * each window. For further tab pages it's the window where we do
11306 * "tabedit". */
11307 cur_arg_idx = next_arg_idx;
11308
Bram Moolenaar18144c82006-04-12 21:52:12 +000011309 /*
11310 * Restore cursor to the current window if it's not the first one.
11311 */
11312 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11313 || put_eol(fd) == FAIL))
11314 return FAIL;
11315
11316 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011317 * Restore window sizes again after jumping around in windows, because
11318 * the current window has a minimum size while others may not.
11319 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011320 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011321 return FAIL;
11322
Bram Moolenaar18144c82006-04-12 21:52:12 +000011323 /* Don't continue in another tab page when doing only the current one
11324 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011325 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011326 break;
11327 }
11328
11329 if (ssop_flags & SSOP_TABPAGES)
11330 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011331 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11332 || put_eol(fd) == FAIL)
11333 return FAIL;
11334 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011335 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11336 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011337
Bram Moolenaar9c102382006-05-03 21:26:49 +000011338 /*
11339 * Wipe out an empty unnamed buffer we started in.
11340 */
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011341 if (put_line(fd, "if exists('s:wipebuf') && s:wipebuf != bufnr('%')")
11342 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011343 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011344 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011345 return FAIL;
11346 if (put_line(fd, "endif") == FAIL)
11347 return FAIL;
11348 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11349 return FAIL;
11350
11351 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11352 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11353 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11354 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011355 /* Re-apply 'winminheight' and 'winminwidth'. */
11356 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11357 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
11360 /*
11361 * Lastly, execute the x.vim file if it exists.
11362 */
11363 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11364 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011365 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 || put_line(fd, "endif") == FAIL)
11367 return FAIL;
11368
11369 return OK;
11370}
11371
11372 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011373ses_winsizes(
11374 FILE *fd,
11375 int restore_size,
11376 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
11378 int n = 0;
11379 win_T *wp;
11380
11381 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11382 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011383 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 {
11385 if (!ses_do_win(wp))
11386 continue;
11387 ++n;
11388
11389 /* restore height when not full height */
11390 if (wp->w_height + wp->w_status_height < topframe->fr_height
11391 && (fprintf(fd,
11392 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11393 n, (long)wp->w_height, Rows / 2, Rows) < 0
11394 || put_eol(fd) == FAIL))
11395 return FAIL;
11396
11397 /* restore width when not full width */
11398 if (wp->w_width < Columns && (fprintf(fd,
11399 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11400 n, (long)wp->w_width, Columns / 2, Columns) < 0
11401 || put_eol(fd) == FAIL))
11402 return FAIL;
11403 }
11404 }
11405 else
11406 {
11407 /* Just equalise window sizes */
11408 if (put_line(fd, "wincmd =") == FAIL)
11409 return FAIL;
11410 }
11411 return OK;
11412}
11413
11414/*
11415 * Write commands to "fd" to recursively create windows for frame "fr",
11416 * horizontally and vertically split.
11417 * After the commands the last window in the frame is the current window.
11418 * Returns FAIL when writing the commands to "fd" fails.
11419 */
11420 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011421ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
11423 frame_T *frc;
11424 int count = 0;
11425
11426 if (fr->fr_layout != FR_LEAF)
11427 {
11428 /* Find first frame that's not skipped and then create a window for
11429 * each following one (first frame is already there). */
11430 frc = ses_skipframe(fr->fr_child);
11431 if (frc != NULL)
11432 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11433 {
11434 /* Make window as big as possible so that we have lots of room
11435 * to split. */
11436 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11437 || put_line(fd, fr->fr_layout == FR_COL
11438 ? "split" : "vsplit") == FAIL)
11439 return FAIL;
11440 ++count;
11441 }
11442
11443 /* Go back to the first window. */
11444 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11445 ? "%dwincmd k" : "%dwincmd h", count) < 0
11446 || put_eol(fd) == FAIL))
11447 return FAIL;
11448
11449 /* Recursively create frames/windows in each window of this column or
11450 * row. */
11451 frc = ses_skipframe(fr->fr_child);
11452 while (frc != NULL)
11453 {
11454 ses_win_rec(fd, frc);
11455 frc = ses_skipframe(frc->fr_next);
11456 /* Go to next window. */
11457 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11458 return FAIL;
11459 }
11460 }
11461 return OK;
11462}
11463
11464/*
11465 * Skip frames that don't contain windows we want to save in the Session.
11466 * Returns NULL when there none.
11467 */
11468 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011469ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470{
11471 frame_T *frc;
11472
11473 for (frc = fr; frc != NULL; frc = frc->fr_next)
11474 if (ses_do_frame(frc))
11475 break;
11476 return frc;
11477}
11478
11479/*
11480 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11481 * the Session.
11482 */
11483 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011484ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485{
11486 frame_T *frc;
11487
11488 if (fr->fr_layout == FR_LEAF)
11489 return ses_do_win(fr->fr_win);
11490 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11491 if (ses_do_frame(frc))
11492 return TRUE;
11493 return FALSE;
11494}
11495
11496/*
11497 * Return non-zero if window "wp" is to be stored in the Session.
11498 */
11499 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011500ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011502#ifdef FEAT_TERMINAL
11503 if (bt_terminal(wp->w_buffer))
11504 return !term_is_finished(wp->w_buffer)
11505 && (ssop_flags & SSOP_TERMINAL)
11506 && term_should_restore(wp->w_buffer);
11507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 if (wp->w_buffer->b_fname == NULL
11509#ifdef FEAT_QUICKFIX
11510 /* When 'buftype' is "nofile" can't restore the window contents. */
11511 || bt_nofile(wp->w_buffer)
11512#endif
11513 )
11514 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011515 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 return (ssop_flags & SSOP_HELP);
11517 return TRUE;
11518}
11519
11520/*
11521 * Write commands to "fd" to restore the view of a window.
11522 * Caller must make sure 'scrolloff' is zero.
11523 */
11524 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011525put_view(
11526 FILE *fd,
11527 win_T *wp,
11528 int add_edit, /* add ":edit" command to view */
11529 unsigned *flagp, /* vop_flags or ssop_flags */
11530 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011531 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532{
11533 win_T *save_curwin;
11534 int f;
11535 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011536 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537
11538 /* Always restore cursor position for ":mksession". For ":mkview" only
11539 * when 'viewoptions' contains "cursor". */
11540 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11541
11542 /*
11543 * Local argument list.
11544 */
11545 if (wp->w_alist == &global_alist)
11546 {
11547 if (put_line(fd, "argglobal") == FAIL)
11548 return FAIL;
11549 }
11550 else
11551 {
11552 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11553 flagp == &vop_flags
11554 || !(*flagp & SSOP_CURDIR)
11555 || wp->w_localdir != NULL, flagp) == FAIL)
11556 return FAIL;
11557 }
11558
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011559 /* Only when part of a session: restore the argument index. Some
11560 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011561 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011562 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011564 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 || put_eol(fd) == FAIL)
11566 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011567 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 }
11569
11570 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011571 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011573# ifdef FEAT_TERMINAL
11574 if (bt_terminal(wp->w_buffer))
11575 {
11576 if (term_write_session(fd, wp) == FAIL)
11577 return FAIL;
11578 }
11579 else
11580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 /*
11582 * Load the file.
11583 */
11584 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011585# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 )
11589 {
11590 /*
11591 * Editing a file in this buffer: use ":edit file".
11592 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011593 *
11594 * Note, if a buffer for that file already exists, use :badd to
11595 * edit that buffer, to not lose folding information (:edit resets
11596 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011598 if (fputs("if bufexists('", fd) < 0
11599 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11600 || fputs("') | buffer ", fd) < 0
11601 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11602 || fputs(" | else | edit ", fd) < 0
11603 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11604 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011605 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 return FAIL;
11607 }
11608 else
11609 {
11610 /* No file in this buffer, just make it empty. */
11611 if (put_line(fd, "enew") == FAIL)
11612 return FAIL;
11613#ifdef FEAT_QUICKFIX
11614 if (wp->w_buffer->b_ffname != NULL)
11615 {
11616 /* The buffer does have a name, but it's not a file name. */
11617 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011618 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 return FAIL;
11620 }
11621#endif
11622 do_cursor = FALSE;
11623 }
11624 }
11625
11626 /*
11627 * Local mappings and abbreviations.
11628 */
11629 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11630 && makemap(fd, wp->w_buffer) == FAIL)
11631 return FAIL;
11632
11633 /*
11634 * Local options. Need to go to the window temporarily.
11635 * Store only local values when using ":mkview" and when ":mksession" is
11636 * used and 'sessionoptions' doesn't include "options".
11637 * Some folding options are always stored when "folds" is included,
11638 * otherwise the folds would not be restored correctly.
11639 */
11640 save_curwin = curwin;
11641 curwin = wp;
11642 curbuf = curwin->w_buffer;
11643 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11644 f = makeset(fd, OPT_LOCAL,
11645 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11646#ifdef FEAT_FOLDING
11647 else if (*flagp & SSOP_FOLDS)
11648 f = makefoldset(fd);
11649#endif
11650 else
11651 f = OK;
11652 curwin = save_curwin;
11653 curbuf = curwin->w_buffer;
11654 if (f == FAIL)
11655 return FAIL;
11656
11657#ifdef FEAT_FOLDING
11658 /*
11659 * Save Folds when 'buftype' is empty and for help files.
11660 */
11661 if ((*flagp & SSOP_FOLDS)
11662 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011663 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 {
11665 if (put_folds(fd, wp) == FAIL)
11666 return FAIL;
11667 }
11668#endif
11669
11670 /*
11671 * Set the cursor after creating folds, since that moves the cursor.
11672 */
11673 if (do_cursor)
11674 {
11675
11676 /* Restore the cursor line in the file and relatively in the
11677 * window. Don't use "G", it changes the jumplist. */
11678 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11679 (long)wp->w_cursor.lnum,
11680 (long)(wp->w_cursor.lnum - wp->w_topline),
11681 (long)wp->w_height / 2, (long)wp->w_height) < 0
11682 || put_eol(fd) == FAIL
11683 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11684 || put_line(fd, "exe s:l") == FAIL
11685 || put_line(fd, "normal! zt") == FAIL
11686 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11687 || put_eol(fd) == FAIL)
11688 return FAIL;
11689 /* Restore the cursor column and left offset when not wrapping. */
11690 if (wp->w_cursor.col == 0)
11691 {
11692 if (put_line(fd, "normal! 0") == FAIL)
11693 return FAIL;
11694 }
11695 else
11696 {
11697 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11698 {
11699 if (fprintf(fd,
11700 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011701 (long)wp->w_virtcol + 1,
11702 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 (long)wp->w_width / 2, (long)wp->w_width) < 0
11704 || put_eol(fd) == FAIL
11705 || put_line(fd, "if s:c > 0") == FAIL
11706 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011707 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11708 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 || put_eol(fd) == FAIL
11710 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011711 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 || put_eol(fd) == FAIL
11713 || put_line(fd, "endif") == FAIL)
11714 return FAIL;
11715 }
11716 else
11717 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011718 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 || put_eol(fd) == FAIL)
11720 return FAIL;
11721 }
11722 }
11723 }
11724
11725 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011726 * Local directory, if the current flag is not view options or the "curdir"
11727 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011729 if (wp->w_localdir != NULL
11730 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 {
11732 if (fputs("lcd ", fd) < 0
11733 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11734 || put_eol(fd) == FAIL)
11735 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011736 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 }
11738
11739 return OK;
11740}
11741
11742/*
11743 * Write an argument list to the session file.
11744 * Returns FAIL if writing fails.
11745 */
11746 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011747ses_arglist(
11748 FILE *fd,
11749 char *cmd,
11750 garray_T *gap,
11751 int fullname, /* TRUE: use full path name */
11752 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753{
11754 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011755 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 char_u *s;
11757
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011758 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11759 return FAIL;
11760 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 return FAIL;
11762 for (i = 0; i < gap->ga_len; ++i)
11763 {
11764 /* NULL file names are skipped (only happens when out of memory). */
11765 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11766 if (s != NULL)
11767 {
11768 if (fullname)
11769 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011770 buf = alloc(MAXPATHL);
11771 if (buf != NULL)
11772 {
11773 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11774 s = buf;
11775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011777 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011778 || ses_put_fname(fd, s, flagp) == FAIL
11779 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011780 {
11781 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011783 }
11784 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 }
11786 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011787 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788}
11789
11790/*
11791 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011792 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 * Returns FAIL if writing fails.
11794 */
11795 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011796ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797{
11798 char_u *name;
11799
11800 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011801 * the session file will be sourced.
11802 * Don't do this for ":mkview", we don't know the current directory.
11803 * Don't do this after ":lcd", we don't keep track of what the current
11804 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 if (buf->b_sfname != NULL
11806 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011807 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011808#ifdef FEAT_AUTOCHDIR
11809 && !p_acd
11810#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011811 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 name = buf->b_sfname;
11813 else
11814 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011815 if (ses_put_fname(fd, name, flagp) == FAIL
11816 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 return FAIL;
11818 return OK;
11819}
11820
11821/*
11822 * Write a file name to the session file.
11823 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11824 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011825 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 */
11827 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011828ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829{
11830 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011831 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833
11834 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011835 if (sname == NULL)
11836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011838 if (*flagp & SSOP_SLASH)
11839 {
11840 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011841 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011842 if (*p == '\\')
11843 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011845
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011846 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011847 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011849 if (p == NULL)
11850 return FAIL;
11851
11852 /* write the result */
11853 if (fputs((char *)p, fd) < 0)
11854 retval = FAIL;
11855
11856 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 return retval;
11858}
11859
11860/*
11861 * ":loadview [nr]"
11862 */
11863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011864ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865{
11866 char_u *fname;
11867
11868 fname = get_view_file(*eap->arg);
11869 if (fname != NULL)
11870 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011871 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 vim_free(fname);
11873 }
11874}
11875
11876/*
11877 * Get the name of the view file for the current buffer.
11878 */
11879 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011880get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
11882 int len = 0;
11883 char_u *p, *s;
11884 char_u *retval;
11885 char_u *sname;
11886
11887 if (curbuf->b_ffname == NULL)
11888 {
11889 EMSG(_(e_noname));
11890 return NULL;
11891 }
11892 sname = home_replace_save(NULL, curbuf->b_ffname);
11893 if (sname == NULL)
11894 return NULL;
11895
11896 /*
11897 * We want a file name without separators, because we're not going to make
11898 * a directory.
11899 * "normal" path separator -> "=+"
11900 * "=" -> "=="
11901 * ":" path separator -> "=-"
11902 */
11903 for (p = sname; *p; ++p)
11904 if (*p == '=' || vim_ispathsep(*p))
11905 ++len;
11906 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11907 if (retval != NULL)
11908 {
11909 STRCPY(retval, p_vdir);
11910 add_pathsep(retval);
11911 s = retval + STRLEN(retval);
11912 for (p = sname; *p; ++p)
11913 {
11914 if (*p == '=')
11915 {
11916 *s++ = '=';
11917 *s++ = '=';
11918 }
11919 else if (vim_ispathsep(*p))
11920 {
11921 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011922#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 if (*p == ':')
11924 *s++ = '-';
11925 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011927 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 }
11929 else
11930 *s++ = *p;
11931 }
11932 *s++ = '=';
11933 *s++ = c;
11934 STRCPY(s, ".vim");
11935 }
11936
11937 vim_free(sname);
11938 return retval;
11939}
11940
11941#endif /* FEAT_SESSION */
11942
11943/*
11944 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11945 * Return FAIL for a write error.
11946 */
11947 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011948put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949{
11950 if (
11951#ifdef USE_CRNL
11952 (
11953# ifdef MKSESSION_NL
11954 !mksession_nl &&
11955# endif
11956 (putc('\r', fd) < 0)) ||
11957#endif
11958 (putc('\n', fd) < 0))
11959 return FAIL;
11960 return OK;
11961}
11962
11963/*
11964 * Write a line to "fd".
11965 * Return FAIL for a write error.
11966 */
11967 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011968put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11971 return FAIL;
11972 return OK;
11973}
11974
11975#ifdef FEAT_VIMINFO
11976/*
11977 * ":rviminfo" and ":wviminfo".
11978 */
11979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011980ex_viminfo(
11981 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
11983 char_u *save_viminfo;
11984
11985 save_viminfo = p_viminfo;
11986 if (*p_viminfo == NUL)
11987 p_viminfo = (char_u *)"'100";
11988 if (eap->cmdidx == CMD_rviminfo)
11989 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011990 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11991 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 EMSG(_("E195: Cannot open viminfo file for reading"));
11993 }
11994 else
11995 write_viminfo(eap->arg, eap->forceit);
11996 p_viminfo = save_viminfo;
11997}
11998#endif
11999
12000#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012001/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012002 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012003 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012006dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 if (fname == NULL)
12009 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012010 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011}
12012#endif
12013
12014/*
12015 * ":behave {mswin,xterm}"
12016 */
12017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012018ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019{
12020 if (STRCMP(eap->arg, "mswin") == 0)
12021 {
12022 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12023 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12024 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12025 set_option_value((char_u *)"keymodel", 0L,
12026 (char_u *)"startsel,stopsel", 0);
12027 }
12028 else if (STRCMP(eap->arg, "xterm") == 0)
12029 {
12030 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12031 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12032 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12033 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12034 }
12035 else
12036 EMSG2(_(e_invarg2), eap->arg);
12037}
12038
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012039#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12040/*
12041 * Function given to ExpandGeneric() to obtain the possible arguments of the
12042 * ":behave {mswin,xterm}" command.
12043 */
12044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012045get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012046{
12047 if (idx == 0)
12048 return (char_u *)"mswin";
12049 if (idx == 1)
12050 return (char_u *)"xterm";
12051 return NULL;
12052}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012053
12054/*
12055 * Function given to ExpandGeneric() to obtain the possible arguments of the
12056 * ":messages {clear}" command.
12057 */
12058 char_u *
12059get_messages_arg(expand_T *xp UNUSED, int idx)
12060{
12061 if (idx == 0)
12062 return (char_u *)"clear";
12063 return NULL;
12064}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012065#endif
12066
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012067 char_u *
12068get_mapclear_arg(expand_T *xp UNUSED, int idx)
12069{
12070 if (idx == 0)
12071 return (char_u *)"<buffer>";
12072 return NULL;
12073}
12074
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075static int filetype_detect = FALSE;
12076static int filetype_plugin = FALSE;
12077static int filetype_indent = FALSE;
12078
12079/*
12080 * ":filetype [plugin] [indent] {on,off,detect}"
12081 * on: Load the filetype.vim file to install autocommands for file types.
12082 * off: Load the ftoff.vim file to remove all autocommands for file types.
12083 * plugin on: load filetype.vim and ftplugin.vim
12084 * plugin off: load ftplugof.vim
12085 * indent on: load filetype.vim and indent.vim
12086 * indent off: load indoff.vim
12087 */
12088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012089ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090{
12091 char_u *arg = eap->arg;
12092 int plugin = FALSE;
12093 int indent = FALSE;
12094
12095 if (*eap->arg == NUL)
12096 {
12097 /* Print current status. */
12098 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12099 filetype_detect ? "ON" : "OFF",
12100 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12101 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12102 return;
12103 }
12104
12105 /* Accept "plugin" and "indent" in any order. */
12106 for (;;)
12107 {
12108 if (STRNCMP(arg, "plugin", 6) == 0)
12109 {
12110 plugin = TRUE;
12111 arg = skipwhite(arg + 6);
12112 continue;
12113 }
12114 if (STRNCMP(arg, "indent", 6) == 0)
12115 {
12116 indent = TRUE;
12117 arg = skipwhite(arg + 6);
12118 continue;
12119 }
12120 break;
12121 }
12122 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12123 {
12124 if (*arg == 'o' || !filetype_detect)
12125 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012126 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 filetype_detect = TRUE;
12128 if (plugin)
12129 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012130 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 filetype_plugin = TRUE;
12132 }
12133 if (indent)
12134 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012135 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 filetype_indent = TRUE;
12137 }
12138 }
12139 if (*arg == 'd')
12140 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012141 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012142 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 }
12144 }
12145 else if (STRCMP(arg, "off") == 0)
12146 {
12147 if (plugin || indent)
12148 {
12149 if (plugin)
12150 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012151 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 filetype_plugin = FALSE;
12153 }
12154 if (indent)
12155 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012156 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 filetype_indent = FALSE;
12158 }
12159 }
12160 else
12161 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012162 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 filetype_detect = FALSE;
12164 }
12165 }
12166 else
12167 EMSG2(_(e_invarg2), arg);
12168}
12169
12170/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012171 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 */
12173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012174ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175{
12176 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012177 {
12178 char_u *arg = eap->arg;
12179
12180 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12181 arg += 9;
12182
12183 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12184 if (arg != eap->arg)
12185 did_filetype = FALSE;
12186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012190ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
12192#ifdef FEAT_DIGRAPHS
12193 if (*eap->arg != NUL)
12194 putdigraph(eap->arg);
12195 else
12196 listdigraphs();
12197#else
12198 EMSG(_("E196: No digraphs in this version"));
12199#endif
12200}
12201
12202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012203ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
12205 int flags = 0;
12206
12207 if (eap->cmdidx == CMD_setlocal)
12208 flags = OPT_LOCAL;
12209 else if (eap->cmdidx == CMD_setglobal)
12210 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012211#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 if (cmdmod.browse && flags == 0)
12213 ex_options(eap);
12214 else
12215#endif
12216 (void)do_set(eap->arg, flags);
12217}
12218
12219#ifdef FEAT_SEARCH_EXTRA
12220/*
12221 * ":nohlsearch"
12222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012224ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012226 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012227 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228}
12229
12230/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012231 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 * Sets nextcmd to the start of the next command, if any. Also called when
12233 * skipping commands to find the next command.
12234 */
12235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012236ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237{
12238 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012239 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 char_u *end;
12241 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012242 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012243
12244 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012245 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012246 else
12247 {
12248 EMSG(e_invcmd);
12249 return;
12250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
12252 /* First clear any old pattern. */
12253 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012254 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255
12256 if (ends_excmd(*eap->arg))
12257 end = eap->arg;
12258 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012259 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 end = eap->arg + 4;
12261 else
12262 {
12263 p = skiptowhite(eap->arg);
12264 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012265 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 p = skipwhite(p);
12267 if (*p == NUL)
12268 {
12269 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012270 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 EMSG2(_(e_invarg2), eap->arg);
12272 return;
12273 }
12274 end = skip_regexp(p + 1, *p, TRUE, NULL);
12275 if (!eap->skip)
12276 {
12277 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12278 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012279 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 eap->errmsg = e_trailing;
12281 return;
12282 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012283 if (*end != *p)
12284 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012285 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012286 EMSG2(_(e_invarg2), p);
12287 return;
12288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289
12290 c = *end;
12291 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012292 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012293 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012294 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 }
12296 }
12297 eap->nextcmd = find_nextcmd(end);
12298}
12299#endif
12300
12301#ifdef FEAT_CRYPT
12302/*
12303 * ":X": Get crypt key
12304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012306ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012308 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012309 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310}
12311#endif
12312
12313#ifdef FEAT_FOLDING
12314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012315ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316{
12317 if (foldManualAllowed(TRUE))
12318 foldCreate(eap->line1, eap->line2);
12319}
12320
12321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012322ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12325 eap->forceit, FALSE);
12326}
12327
12328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012329ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331 linenr_T lnum;
12332
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012333#ifdef FEAT_CLIPBOARD
12334 start_global_changes();
12335#endif
12336
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 /* First set the marks for all lines closed/open. */
12338 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12339 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12340 ml_setmarked(lnum);
12341
12342 /* Execute the command on the marked lines. */
12343 global_exe(eap->arg);
12344 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012345#ifdef FEAT_CLIPBOARD
12346 end_global_changes();
12347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348}
12349#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012350
12351# if defined(FEAT_TIMERS) || defined(PROTO)
12352 int
12353get_pressedreturn(void)
12354{
12355 return ex_pressedreturn;
12356}
12357
12358 void
12359set_pressedreturn(int val)
12360{
12361 ex_pressedreturn = val;
12362}
12363#endif